azimuth.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Boost.Geometry
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // This file was modified by Oracle on 2014-2023.
  4. // Modifications copyright (c) 2014-2023, 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_AZIMUTH_HPP
  11. #define BOOST_GEOMETRY_ALGORITHMS_AZIMUTH_HPP
  12. #include <boost/geometry/algorithms/not_implemented.hpp>
  13. #include <boost/geometry/core/radian_access.hpp>
  14. #include <boost/geometry/core/tag.hpp>
  15. #include <boost/geometry/geometries/concepts/check.hpp>
  16. #include <boost/geometry/strategies/default_strategy.hpp>
  17. #include <boost/geometry/strategies/azimuth/cartesian.hpp>
  18. #include <boost/geometry/strategies/azimuth/geographic.hpp>
  19. #include <boost/geometry/strategies/azimuth/spherical.hpp>
  20. namespace boost { namespace geometry
  21. {
  22. #ifndef DOXYGEN_NO_DETAIL
  23. namespace detail
  24. {
  25. } // namespace detail
  26. #endif // DOXYGEN_NO_DETAIL
  27. #ifndef DOXYGEN_NO_DISPATCH
  28. namespace dispatch
  29. {
  30. template
  31. <
  32. typename Geometry1, typename Geometry2,
  33. typename Tag1 = typename tag<Geometry1>::type,
  34. typename Tag2 = typename tag<Geometry2>::type
  35. >
  36. struct azimuth : not_implemented<Tag1, Tag2>
  37. {};
  38. template <typename Point1, typename Point2>
  39. struct azimuth<Point1, Point2, point_tag, point_tag>
  40. {
  41. template <typename Strategy>
  42. static auto apply(Point1 const& p1, Point2 const& p2, Strategy const& strategy)
  43. {
  44. typedef typename decltype(strategy.azimuth())::template result_type
  45. <
  46. typename coordinate_type<Point1>::type,
  47. typename coordinate_type<Point2>::type
  48. >::type calc_t;
  49. calc_t result = 0;
  50. calc_t const x1 = geometry::get_as_radian<0>(p1);
  51. calc_t const y1 = geometry::get_as_radian<1>(p1);
  52. calc_t const x2 = geometry::get_as_radian<0>(p2);
  53. calc_t const y2 = geometry::get_as_radian<1>(p2);
  54. strategy.azimuth().apply(x1, y1, x2, y2, result);
  55. // NOTE: It is not clear which units we should use for the result.
  56. // For now radians are always returned but a user could expect
  57. // e.g. something like this:
  58. /*
  59. bool const both_degree = std::is_same
  60. <
  61. typename detail::cs_angular_units<Point1>::type,
  62. geometry::degree
  63. >::value
  64. && std::is_same
  65. <
  66. typename detail::cs_angular_units<Point2>::type,
  67. geometry::degree
  68. >::value;
  69. if (both_degree)
  70. {
  71. result *= math::r2d<calc_t>();
  72. }
  73. */
  74. return result;
  75. }
  76. };
  77. } // namespace dispatch
  78. #endif // DOXYGEN_NO_DISPATCH
  79. namespace resolve_strategy
  80. {
  81. template
  82. <
  83. typename Strategy,
  84. bool IsUmbrella = strategies::detail::is_umbrella_strategy<Strategy>::value
  85. >
  86. struct azimuth
  87. {
  88. template <typename P1, typename P2>
  89. static auto apply(P1 const& p1, P2 const& p2, Strategy const& strategy)
  90. {
  91. return dispatch::azimuth<P1, P2>::apply(p1, p2, strategy);
  92. }
  93. };
  94. template <typename Strategy>
  95. struct azimuth<Strategy, false>
  96. {
  97. template <typename P1, typename P2>
  98. static auto apply(P1 const& p1, P2 const& p2, Strategy const& strategy)
  99. {
  100. using strategies::azimuth::services::strategy_converter;
  101. return dispatch::azimuth
  102. <
  103. P1, P2
  104. >::apply(p1, p2, strategy_converter<Strategy>::get(strategy));
  105. }
  106. };
  107. template <>
  108. struct azimuth<default_strategy, false>
  109. {
  110. template <typename P1, typename P2>
  111. static auto apply(P1 const& p1, P2 const& p2, default_strategy)
  112. {
  113. typedef typename strategies::azimuth::services::default_strategy
  114. <
  115. P1, P2
  116. >::type strategy_type;
  117. return dispatch::azimuth<P1, P2>::apply(p1, p2, strategy_type());
  118. }
  119. };
  120. } // namespace resolve_strategy
  121. namespace resolve_variant
  122. {
  123. } // namespace resolve_variant
  124. /*!
  125. \brief Calculate azimuth of a segment defined by a pair of points.
  126. \ingroup azimuth
  127. \tparam Point1 Type of the first point of a segment.
  128. \tparam Point2 Type of the second point of a segment.
  129. \param point1 First point of a segment.
  130. \param point2 Second point of a segment.
  131. \return Azimuth in radians.
  132. \qbk{[include reference/algorithms/azimuth.qbk]}
  133. \qbk{
  134. [heading Example]
  135. [azimuth]
  136. [azimuth_output]
  137. }
  138. */
  139. template <typename Point1, typename Point2>
  140. inline auto azimuth(Point1 const& point1, Point2 const& point2)
  141. {
  142. concepts::check<Point1 const>();
  143. concepts::check<Point2 const>();
  144. return resolve_strategy::azimuth
  145. <
  146. default_strategy
  147. >::apply(point1, point2, default_strategy());
  148. }
  149. /*!
  150. \brief Calculate azimuth of a segment defined by a pair of points.
  151. \ingroup azimuth
  152. \tparam Point1 Type of the first point of a segment.
  153. \tparam Point2 Type of the second point of a segment.
  154. \tparam Strategy Type of an umbrella strategy defining azimuth strategy.
  155. \param point1 First point of a segment.
  156. \param point2 Second point of a segment.
  157. \param strategy Umbrella strategy defining azimuth strategy.
  158. \return Azimuth in radians.
  159. \qbk{distinguish,with strategy}
  160. \qbk{[include reference/algorithms/azimuth.qbk]}
  161. \qbk{
  162. [heading Example]
  163. [azimuth_strategy]
  164. [azimuth_strategy_output]
  165. }
  166. */
  167. template <typename Point1, typename Point2, typename Strategy>
  168. inline auto azimuth(Point1 const& point1, Point2 const& point2, Strategy const& strategy)
  169. {
  170. concepts::check<Point1 const>();
  171. concepts::check<Point2 const>();
  172. return resolve_strategy::azimuth<Strategy>::apply(point1, point2, strategy);
  173. }
  174. }} // namespace boost::geometry
  175. #endif // BOOST_GEOMETRY_ALGORITHMS_AZIMUTH_HPP