diff --git a/example/Jamfile.v2 b/example/Jamfile.v2 index 27861bd08..27db0d251 100644 --- a/example/Jamfile.v2 +++ b/example/Jamfile.v2 @@ -28,4 +28,9 @@ exe c04_a_custom_triangle_example : c04_a_custom_triangle_example.cpp ; exe c04_b_custom_triangle_example : c04_b_custom_triangle_example.cpp ; exe c06_custom_polygon_example : c06_custom_polygon_example.cpp ; exe c07_custom_ring_pointer_example : c07_custom_ring_pointer_example.cpp ; + # exe c08_custom_non_std_example : c08_custom_non_std_example.cpp ; +# c09 is not yet finished + +exe c10_custom_cs_example : c10_custom_cs_example.cpp ; +exe c11_custom_cs_transform_example : c11_custom_cs_transform_example.cpp ; diff --git a/example/c09_custom_fusion_example.cpp b/example/c09_custom_fusion_example.cpp index 4380923c5..256a5bf0f 100644 --- a/example/c09_custom_fusion_example.cpp +++ b/example/c09_custom_fusion_example.cpp @@ -6,6 +6,7 @@ // http://www.boost.org/LICENSE_1_0.txt) // // Custom point / fusion Example +// NOT FINISHED #include @@ -47,7 +48,7 @@ int main() std::cout << boost::fusion::at_c<1>(p1) << std::endl; - std::cout << boost::geometry::get<1>(p1) << std::endl; + //std::cout << boost::geometry::get<1>(p1) << std::endl; return 0; } diff --git a/example/c10_custom_cs_example.cpp b/example/c10_custom_cs_example.cpp new file mode 100644 index 000000000..3fb1a36ab --- /dev/null +++ b/example/c10_custom_cs_example.cpp @@ -0,0 +1,103 @@ +// Boost.Geometry (aka GGL, Generic Geometry Library) +// +// Copyright Barend Gehrels 2010, Geodan, Amsterdam, the Netherlands +// Use, modification and distribution is subject to the Boost Software License, +// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// Example: Custom coordinate system example + +#include + +#include + +#ifdef OPTIONALLY_ELLIPSOIDAL // see below +#include +#endif + +// 1: declare a coordinate system. For example for Mars +// Like for the Earth, we let the use choose between degrees or radians +// (Unfortunately, in real life Mars has two coordinate systems: +// http://planetarynames.wr.usgs.gov/Page/MARS/system) +template +struct martian +{ + typedef DegreeOrRadian units; +}; + +// 2: give it also a family +struct martian_tag; + +// 3: register to which coordinate system family it belongs to +// this must be done in namespace boost::geometry::traits +namespace boost { namespace geometry { namespace traits +{ + +template +struct cs_tag > +{ + typedef martian_tag type; +}; + +}}} // namespaces + + +// NOTE: if the next steps would not be here, +// compiling a distance function call with martian coordinates +// would result in a MPL assertion + +// 4: so register a distance strategy as its default strategy +namespace boost { namespace geometry { namespace strategy { namespace distance { namespace services +{ + +template +struct default_strategy +{ + typedef haversine type; +}; + +}}}}} // namespaces + +// 5: not worked out. To implement a specific distance strategy for Mars, +// e.g. with the Mars radius given by default, +// you will have to implement (/register) several other metafunctions: +// tag, return_type, similar_type, comparable_type, +// and structs: +// get_similar, get_comparable, result_from_distance +// See e.g. .../boost/geometry/extensions/gis/geographic/strategies/andoyer.hpp + +int main() +{ + typedef boost::geometry::point + < + double, 2, martian + > mars_point; + + // Declare two points + // (Source: http://nssdc.gsfc.nasa.gov/planetary/mars_mileage_guide.html) + // (Other sources: Wiki and Google give slightly different coordinates, resulting + // in other distance, 20 km off) + mars_point viking1(-48.23, 22.54); // Viking 1 landing site in Chryse Planitia + mars_point pathfinder(-33.55, 19.33); // Pathfinder landing site in Ares Vallis + + double d = boost::geometry::distance(viking1, pathfinder); // Distance in radians on unit-sphere + + // Using the Mars mean radius + // (Source: http://nssdc.gsfc.nasa.gov/planetary/factsheet/marsfact.html) + std::cout << "Distance between Viking1 and Pathfinder landing sites: " + << d * 3389.5 << " km" << std::endl; + + // We would get 832.616 here, same order as the 835 (rounded on 5 km) listed + // on the mentioned site + +#ifdef OPTIONALLY_ELLIPSOIDAL + // Optionally the distance can be calculated more accurate by an Ellipsoidal approach, + // giving 834.444 km + d = boost::geometry::distance(viking1, pathfinder, + boost::geometry::strategy::distance::andoyer + (boost::geometry::detail::ellipsoid(3396.2, 3376.2))); + std::cout << "Ellipsoidal distance: " << d << " km" << std::endl; +#endif + + return 0; +} diff --git a/example/c10_custom_cs_example.vcproj b/example/c10_custom_cs_example.vcproj new file mode 100644 index 000000000..8d23b1f4e --- /dev/null +++ b/example/c10_custom_cs_example.vcproj @@ -0,0 +1,171 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/example/c11_custom_cs_transform_example.cpp b/example/c11_custom_cs_transform_example.cpp new file mode 100644 index 000000000..45ec13a48 --- /dev/null +++ b/example/c11_custom_cs_transform_example.cpp @@ -0,0 +1,128 @@ +// Boost.Geometry (aka GGL, Generic Geometry Library) +// +// Copyright Barend Gehrels 2010, Geodan, Amsterdam, the Netherlands +// Use, modification and distribution is subject to the Boost Software License, +// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// Example: Custom coordinate system example, using transform + +#include + +#include + +// See also c10_custom_cs_example + +// 1: declare, for example two cartesian coordinate systems +struct cart {}; +struct cart_shifted5 {}; + +// 2: register to which coordinate system family they belong +namespace boost { namespace geometry { namespace traits +{ + +template<> struct cs_tag { typedef cartesian_tag type; }; +template<> struct cs_tag { typedef cartesian_tag type; }; + +}}} // namespaces + + +// 3: sample implementation of a shift +// to convert coordinate system "cart" to "cart_shirted5" +template +struct shift +{ + inline bool apply(P1 const& p1, P2& p2) const + { + namespace bg = boost::geometry; + bg::set<0>(p2, bg::get<0>(p1) + 5); + bg::set<1>(p2, bg::get<1>(p1)); + return true; + } +}; + + +// 4: register the default strategy to transform any cart point to any cart_shifted5 point +// (note: this will be renamed to "default_strategy" in the near future) +namespace boost { namespace geometry +{ + +template +struct strategy_transform +{ + typedef shift type; +}; + +}} // namespaces + + +// 5: implement a distance strategy between the two different ones +template +struct shift_and_calc_distance +{ + inline double apply(P1 const& p1, P2 const& p2) const + { + P2 p1_shifted; + boost::geometry::transform(p1, p1_shifted); + return boost::geometry::distance(p1_shifted, p2); + } +}; + +// 6: Define point types using this explicitly +typedef boost::geometry::point point1; +typedef boost::geometry::point point2; + +// 7: register the distance strategy +namespace boost { namespace geometry { namespace strategy { namespace distance { namespace services +{ + template + struct tag > + { + typedef strategy_tag_distance_point_point type; + }; + + template + struct return_type > + { + typedef double type; + }; + + template <> + struct default_strategy + { + typedef shift_and_calc_distance type; + }; + + +}}}}} + + + +int main() +{ + point1 p1_a(0, 0), p1_b(5, 5); + point2 p2_a(2, 2), p2_b(6, 6); + + // Distances run for points on the same coordinate system. + // This is possible by default because they are cartesian coordinate systems. + double d1 = boost::geometry::distance(p1_a, p1_b); + double d2 = boost::geometry::distance(p2_a, p2_b); + + // Transform from a to b: + boost::geometry::point p1_shifted; + boost::geometry::transform(p1_a, p1_shifted); + + + // Of course this can be calculated now, same CS + double d3 = boost::geometry::distance(p1_shifted, p2_a); + + + // Calculate distance between them. Note that inside distance the + // transformation is called. + double d4 = boost::geometry::distance(p1_a, p2_a); + + // The result should be the same. + std::cout << d3 << " " << d4 << std::endl; + + return 0; +} diff --git a/example/c11_custom_cs_transform_example.vcproj b/example/c11_custom_cs_transform_example.vcproj new file mode 100644 index 000000000..c4764b483 --- /dev/null +++ b/example/c11_custom_cs_transform_example.vcproj @@ -0,0 +1,171 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/example/custom_examples.sln b/example/custom_examples.sln index 20678f139..85b56ea74 100644 --- a/example/custom_examples.sln +++ b/example/custom_examples.sln @@ -20,6 +20,10 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "c08_custom_non_std_example" EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "c09_custom_fusion_example", "c09_custom_fusion_example.vcproj", "{DA36AD55-E448-43DE-A974-EA765AE3967A}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "c10_custom_cs_example", "c10_custom_cs_example.vcproj", "{E16737C9-E1F7-450E-9251-B35840FE69B5}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "c11_custom_cs_transform_example", "c11_custom_cs_transform_example.vcproj", "{E73E52EC-9BDC-4777-ABB2-DB14FAF27AA5}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32 @@ -66,6 +70,14 @@ Global {DA36AD55-E448-43DE-A974-EA765AE3967A}.Debug|Win32.Build.0 = Debug|Win32 {DA36AD55-E448-43DE-A974-EA765AE3967A}.Release|Win32.ActiveCfg = Release|Win32 {DA36AD55-E448-43DE-A974-EA765AE3967A}.Release|Win32.Build.0 = Release|Win32 + {E16737C9-E1F7-450E-9251-B35840FE69B5}.Debug|Win32.ActiveCfg = Debug|Win32 + {E16737C9-E1F7-450E-9251-B35840FE69B5}.Debug|Win32.Build.0 = Debug|Win32 + {E16737C9-E1F7-450E-9251-B35840FE69B5}.Release|Win32.ActiveCfg = Release|Win32 + {E16737C9-E1F7-450E-9251-B35840FE69B5}.Release|Win32.Build.0 = Release|Win32 + {E73E52EC-9BDC-4777-ABB2-DB14FAF27AA5}.Debug|Win32.ActiveCfg = Debug|Win32 + {E73E52EC-9BDC-4777-ABB2-DB14FAF27AA5}.Debug|Win32.Build.0 = Debug|Win32 + {E73E52EC-9BDC-4777-ABB2-DB14FAF27AA5}.Release|Win32.ActiveCfg = Release|Win32 + {E73E52EC-9BDC-4777-ABB2-DB14FAF27AA5}.Release|Win32.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE