azimuth.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // This file was modified by Oracle on 2014, 2016, 2017.
  4. // Modifications copyright (c) 2014-2017, Oracle and/or its affiliates.
  5. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  6. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_AZIMUTH_HPP
  11. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_AZIMUTH_HPP
  12. #include <boost/geometry/algorithms/not_implemented.hpp>
  13. #include <boost/geometry/core/cs.hpp>
  14. #include <boost/geometry/core/access.hpp>
  15. #include <boost/geometry/core/radian_access.hpp>
  16. #include <boost/geometry/core/tags.hpp>
  17. #include <boost/geometry/formulas/spherical.hpp>
  18. #include <boost/geometry/formulas/vincenty_inverse.hpp>
  19. #include <boost/geometry/srs/spheroid.hpp>
  20. #include <boost/geometry/util/math.hpp>
  21. namespace boost { namespace geometry
  22. {
  23. // An azimuth is an angle between a vector/segment from origin to a point of
  24. // interest and a reference vector. Typically north-based azimuth is used.
  25. // North direction is used as a reference, angle is measured clockwise
  26. // (North - 0deg, East - 90deg). For consistency in 2d cartesian CS
  27. // the reference vector is Y axis, angle is measured clockwise.
  28. // http://en.wikipedia.org/wiki/Azimuth
  29. #ifndef DOXYGEN_NO_DISPATCH
  30. namespace detail_dispatch
  31. {
  32. template <typename ReturnType, typename Tag>
  33. struct azimuth
  34. : not_implemented<Tag>
  35. {};
  36. template <typename ReturnType>
  37. struct azimuth<ReturnType, geographic_tag>
  38. {
  39. template <typename P1, typename P2, typename Spheroid>
  40. static inline ReturnType apply(P1 const& p1, P2 const& p2, Spheroid const& spheroid)
  41. {
  42. return geometry::formula::vincenty_inverse<ReturnType, false, true>().apply
  43. ( get_as_radian<0>(p1), get_as_radian<1>(p1),
  44. get_as_radian<0>(p2), get_as_radian<1>(p2),
  45. spheroid ).azimuth;
  46. }
  47. template <typename P1, typename P2>
  48. static inline ReturnType apply(P1 const& p1, P2 const& p2)
  49. {
  50. return apply(p1, p2, srs::spheroid<ReturnType>());
  51. }
  52. };
  53. template <typename ReturnType>
  54. struct azimuth<ReturnType, spherical_equatorial_tag>
  55. {
  56. template <typename P1, typename P2, typename Sphere>
  57. static inline ReturnType apply(P1 const& p1, P2 const& p2, Sphere const& /*unused*/)
  58. {
  59. return geometry::formula::spherical_azimuth<ReturnType, false>
  60. ( get_as_radian<0>(p1), get_as_radian<1>(p1),
  61. get_as_radian<0>(p2), get_as_radian<1>(p2)).azimuth;
  62. }
  63. template <typename P1, typename P2>
  64. static inline ReturnType apply(P1 const& p1, P2 const& p2)
  65. {
  66. return apply(p1, p2, 0); // dummy model
  67. }
  68. };
  69. template <typename ReturnType>
  70. struct azimuth<ReturnType, spherical_polar_tag>
  71. : azimuth<ReturnType, spherical_equatorial_tag>
  72. {};
  73. template <typename ReturnType>
  74. struct azimuth<ReturnType, cartesian_tag>
  75. {
  76. template <typename P1, typename P2, typename Plane>
  77. static inline ReturnType apply(P1 const& p1, P2 const& p2, Plane const& /*unused*/)
  78. {
  79. ReturnType x = get<0>(p2) - get<0>(p1);
  80. ReturnType y = get<1>(p2) - get<1>(p1);
  81. // NOTE: azimuth 0 is at Y axis, increasing right
  82. // as in spherical/geographic where 0 is at North axis
  83. return atan2(x, y);
  84. }
  85. template <typename P1, typename P2>
  86. static inline ReturnType apply(P1 const& p1, P2 const& p2)
  87. {
  88. return apply(p1, p2, 0); // dummy model
  89. }
  90. };
  91. } // detail_dispatch
  92. #endif // DOXYGEN_NO_DISPATCH
  93. #ifndef DOXYGEN_NO_DETAIL
  94. namespace detail
  95. {
  96. /// Calculate azimuth between two points.
  97. /// The result is in radians.
  98. template <typename ReturnType, typename Point1, typename Point2>
  99. inline ReturnType azimuth(Point1 const& p1, Point2 const& p2)
  100. {
  101. return detail_dispatch::azimuth
  102. <
  103. ReturnType,
  104. typename geometry::cs_tag<Point1>::type
  105. >::apply(p1, p2);
  106. }
  107. /// Calculate azimuth between two points.
  108. /// The result is in radians.
  109. template <typename ReturnType, typename Point1, typename Point2, typename Model>
  110. inline ReturnType azimuth(Point1 const& p1, Point2 const& p2, Model const& model)
  111. {
  112. return detail_dispatch::azimuth
  113. <
  114. ReturnType,
  115. typename geometry::cs_tag<Point1>::type
  116. >::apply(p1, p2, model);
  117. }
  118. } // namespace detail
  119. #endif // DOXYGEN_NO_DETAIL
  120. }} // namespace boost::geometry
  121. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_AZIMUTH_HPP