/Src/Dependencies/Boost/libs/geometry/example/c10_custom_cs_example.cpp

http://hadesmem.googlecode.com/ · C++ · 106 lines · 46 code · 23 blank · 37 comment · 0 complexity · 751308390637cb2cd258cce7cbd84d2f MD5 · raw file

  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2011 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2011 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2011 Mateusz Loskot, London, UK.
  5. // Use, modification and distribution is subject to the Boost Software License,
  6. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // Example: Custom coordinate system example
  10. #include <iostream>
  11. #include <boost/geometry/geometry.hpp>
  12. #ifdef OPTIONALLY_ELLIPSOIDAL // see below
  13. #include <boost/geometry/extensions/gis/geographic/strategies/andoyer.hpp>
  14. #endif
  15. // 1: declare a coordinate system. For example for Mars
  16. // Like for the Earth, we let the use choose between degrees or radians
  17. // (Unfortunately, in real life Mars has two coordinate systems:
  18. // http://planetarynames.wr.usgs.gov/Page/MARS/system)
  19. template<typename DegreeOrRadian>
  20. struct martian
  21. {
  22. typedef DegreeOrRadian units;
  23. };
  24. // 2: give it also a family
  25. struct martian_tag;
  26. // 3: register to which coordinate system family it belongs to
  27. // this must be done in namespace boost::geometry::traits
  28. namespace boost { namespace geometry { namespace traits
  29. {
  30. template <typename DegreeOrRadian>
  31. struct cs_tag<martian<DegreeOrRadian> >
  32. {
  33. typedef martian_tag type;
  34. };
  35. }}} // namespaces
  36. // NOTE: if the next steps would not be here,
  37. // compiling a distance function call with martian coordinates
  38. // would result in a MPL assertion
  39. // 4: so register a distance strategy as its default strategy
  40. namespace boost { namespace geometry { namespace strategy { namespace distance { namespace services
  41. {
  42. template <typename Point1, typename Point2>
  43. struct default_strategy<point_tag, Point1, Point2, martian_tag, martian_tag>
  44. {
  45. typedef haversine<Point1, Point2> type;
  46. };
  47. }}}}} // namespaces
  48. // 5: not worked out. To implement a specific distance strategy for Mars,
  49. // e.g. with the Mars radius given by default,
  50. // you will have to implement (/register) several other metafunctions:
  51. // tag, return_type, similar_type, comparable_type,
  52. // and structs:
  53. // get_similar, get_comparable, result_from_distance
  54. // See e.g. .../boost/geometry/extensions/gis/geographic/strategies/andoyer.hpp
  55. int main()
  56. {
  57. typedef boost::geometry::model::point
  58. <
  59. double, 2, martian<boost::geometry::degree>
  60. > mars_point;
  61. // Declare two points
  62. // (Source: http://nssdc.gsfc.nasa.gov/planetary/mars_mileage_guide.html)
  63. // (Other sources: Wiki and Google give slightly different coordinates, resulting
  64. // in other distance, 20 km off)
  65. mars_point viking1(-48.23, 22.54); // Viking 1 landing site in Chryse Planitia
  66. mars_point pathfinder(-33.55, 19.33); // Pathfinder landing site in Ares Vallis
  67. double d = boost::geometry::distance(viking1, pathfinder); // Distance in radians on unit-sphere
  68. // Using the Mars mean radius
  69. // (Source: http://nssdc.gsfc.nasa.gov/planetary/factsheet/marsfact.html)
  70. std::cout << "Distance between Viking1 and Pathfinder landing sites: "
  71. << d * 3389.5 << " km" << std::endl;
  72. // We would get 832.616 here, same order as the 835 (rounded on 5 km) listed
  73. // on the mentioned site
  74. #ifdef OPTIONALLY_ELLIPSOIDAL
  75. // Optionally the distance can be calculated more accurate by an Ellipsoidal approach,
  76. // giving 834.444 km
  77. d = boost::geometry::distance(viking1, pathfinder,
  78. boost::geometry::strategy::distance::andoyer<mars_point>
  79. (boost::geometry::detail::ellipsoid<double>(3396.2, 3376.2)));
  80. std::cout << "Ellipsoidal distance: " << d << " km" << std::endl;
  81. #endif
  82. return 0;
  83. }