spherical.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // Boost.Geometry
  2. // Copyright (c) 2021, Oracle and/or its affiliates.
  3. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  4. // Licensed under the Boost Software License version 1.0.
  5. // http://www.boost.org/users/license.html
  6. #ifndef BOOST_GEOMETRY_STRATEGIES_DISTANCE_SPHERICAL_HPP
  7. #define BOOST_GEOMETRY_STRATEGIES_DISTANCE_SPHERICAL_HPP
  8. #include <boost/geometry/strategies/distance/comparable.hpp>
  9. #include <boost/geometry/strategies/distance/detail.hpp>
  10. #include <boost/geometry/strategies/distance/services.hpp>
  11. #include <boost/geometry/strategies/detail.hpp>
  12. #include <boost/geometry/strategies/normalize.hpp>
  13. #include <boost/geometry/strategies/relate/spherical.hpp>
  14. #include <boost/geometry/strategies/spherical/azimuth.hpp>
  15. #include <boost/geometry/strategies/spherical/distance_cross_track.hpp>
  16. #include <boost/geometry/strategies/spherical/distance_cross_track_box_box.hpp>
  17. #include <boost/geometry/strategies/spherical/distance_cross_track_point_box.hpp>
  18. #include <boost/geometry/strategies/spherical/distance_haversine.hpp>
  19. #include <boost/geometry/strategies/spherical/distance_segment_box.hpp>
  20. namespace boost { namespace geometry
  21. {
  22. namespace strategies { namespace distance
  23. {
  24. #ifndef DOXYGEN_NO_DETAIL
  25. namespace detail
  26. {
  27. // TODO: azimuth and normalize getters would not be needed if distance_segment_box was implemented differently
  28. // right now it calls disjoint algorithm details.
  29. template <typename RadiusTypeOrSphere, typename CalculationType>
  30. class spherical
  31. : public strategies::relate::detail::spherical<RadiusTypeOrSphere, CalculationType>
  32. {
  33. using base_t = strategies::relate::detail::spherical<RadiusTypeOrSphere, CalculationType>;
  34. public:
  35. spherical() = default;
  36. template <typename RadiusOrSphere>
  37. explicit spherical(RadiusOrSphere const& radius_or_sphere)
  38. : base_t(radius_or_sphere)
  39. {}
  40. // azimuth
  41. static auto azimuth()
  42. {
  43. return strategy::azimuth::spherical<CalculationType>();
  44. }
  45. // distance
  46. template <typename Geometry1, typename Geometry2>
  47. auto distance(Geometry1 const&, Geometry2 const&,
  48. detail::enable_if_pp_t<Geometry1, Geometry2> * = nullptr) const
  49. {
  50. return strategy::distance::haversine
  51. <
  52. typename base_t::radius_type, CalculationType
  53. >(base_t::radius());
  54. }
  55. template <typename Geometry1, typename Geometry2>
  56. auto distance(Geometry1 const&, Geometry2 const&,
  57. detail::enable_if_ps_t<Geometry1, Geometry2> * = nullptr) const
  58. {
  59. return strategy::distance::cross_track
  60. <
  61. CalculationType,
  62. strategy::distance::haversine<typename base_t::radius_type, CalculationType>
  63. >(base_t::radius());
  64. }
  65. template <typename Geometry1, typename Geometry2>
  66. auto distance(Geometry1 const&, Geometry2 const&,
  67. detail::enable_if_pb_t<Geometry1, Geometry2> * = nullptr) const
  68. {
  69. return strategy::distance::cross_track_point_box
  70. <
  71. CalculationType,
  72. strategy::distance::haversine<typename base_t::radius_type, CalculationType>
  73. >(base_t::radius());
  74. }
  75. template <typename Geometry1, typename Geometry2>
  76. auto distance(Geometry1 const&, Geometry2 const&,
  77. detail::enable_if_sb_t<Geometry1, Geometry2> * = nullptr) const
  78. {
  79. return strategy::distance::spherical_segment_box
  80. <
  81. CalculationType,
  82. strategy::distance::haversine<typename base_t::radius_type, CalculationType>
  83. >(base_t::radius());
  84. }
  85. template <typename Geometry1, typename Geometry2>
  86. auto distance(Geometry1 const&, Geometry2 const&,
  87. detail::enable_if_bb_t<Geometry1, Geometry2> * = nullptr) const
  88. {
  89. return strategy::distance::cross_track_box_box
  90. <
  91. CalculationType,
  92. strategy::distance::haversine<typename base_t::radius_type, CalculationType>
  93. >(base_t::radius());
  94. }
  95. // normalize
  96. template <typename Geometry>
  97. static auto normalize(Geometry const&,
  98. std::enable_if_t
  99. <
  100. util::is_point<Geometry>::value
  101. > * = nullptr)
  102. {
  103. return strategy::normalize::spherical_point();
  104. }
  105. };
  106. } // namespace detail
  107. #endif // DOXYGEN_NO_DETAIL
  108. template
  109. <
  110. typename RadiusTypeOrSphere = double,
  111. typename CalculationType = void
  112. >
  113. class spherical
  114. : public strategies::distance::detail::spherical<RadiusTypeOrSphere, CalculationType>
  115. {
  116. using base_t = strategies::distance::detail::spherical<RadiusTypeOrSphere, CalculationType>;
  117. public:
  118. spherical() = default;
  119. template <typename RadiusOrSphere>
  120. explicit spherical(RadiusOrSphere const& radius_or_sphere)
  121. : base_t(radius_or_sphere)
  122. {}
  123. };
  124. namespace services
  125. {
  126. template <typename Geometry1, typename Geometry2>
  127. struct default_strategy
  128. <
  129. Geometry1, Geometry2,
  130. spherical_equatorial_tag, spherical_equatorial_tag
  131. >
  132. {
  133. using type = strategies::distance::spherical<>;
  134. };
  135. template <typename R, typename CT>
  136. struct strategy_converter<strategy::distance::haversine<R, CT> >
  137. {
  138. template <typename S>
  139. static auto get(S const& s)
  140. {
  141. return strategies::distance::spherical<R, CT>(s.radius());
  142. }
  143. };
  144. template <typename CT, typename PPS>
  145. struct strategy_converter<strategy::distance::cross_track<CT, PPS> >
  146. : strategy_converter<PPS>
  147. {};
  148. template <typename CT, typename PPS>
  149. struct strategy_converter<strategy::distance::cross_track_point_box<CT, PPS> >
  150. : strategy_converter<PPS>
  151. {};
  152. template <typename CT, typename PPS>
  153. struct strategy_converter<strategy::distance::spherical_segment_box<CT, PPS> >
  154. : strategy_converter<PPS>
  155. {};
  156. template <typename CT, typename PPS>
  157. struct strategy_converter<strategy::distance::cross_track_box_box<CT, PPS> >
  158. : strategy_converter<PPS>
  159. {};
  160. template <typename R, typename CT>
  161. struct strategy_converter<strategy::distance::comparable::haversine<R, CT> >
  162. {
  163. template <typename S>
  164. static auto get(S const& s)
  165. {
  166. return strategies::distance::detail::make_comparable(
  167. strategies::distance::spherical<R, CT>(s.radius()));
  168. }
  169. };
  170. template <typename CT, typename PPS>
  171. struct strategy_converter<strategy::distance::comparable::cross_track<CT, PPS> >
  172. : strategy_converter<PPS>
  173. {};
  174. } // namespace services
  175. }} // namespace strategies::distance
  176. }} // namespace boost::geometry
  177. #endif // BOOST_GEOMETRY_STRATEGIES_DISTANCE_SPHERICAL_HPP