compare.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 2017-2023.
  4. // Modifications copyright (c) 2017-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_STRATEGIES_SPHERICAL_COMPARE_HPP
  11. #define BOOST_GEOMETRY_STRATEGIES_SPHERICAL_COMPARE_HPP
  12. #include <type_traits>
  13. #include <boost/geometry/core/access.hpp>
  14. #include <boost/geometry/core/coordinate_dimension.hpp>
  15. #include <boost/geometry/core/coordinate_system.hpp>
  16. #include <boost/geometry/core/coordinate_type.hpp>
  17. #include <boost/geometry/core/cs.hpp>
  18. #include <boost/geometry/core/radian_access.hpp>
  19. #include <boost/geometry/core/tags.hpp>
  20. #include <boost/geometry/strategies/compare.hpp>
  21. #include <boost/geometry/util/math.hpp>
  22. #include <boost/geometry/util/normalize_spheroidal_coordinates.hpp>
  23. namespace boost { namespace geometry
  24. {
  25. namespace strategy { namespace compare
  26. {
  27. #ifndef DOXYGEN_NO_DETAIL
  28. namespace detail
  29. {
  30. template <std::size_t I, typename P>
  31. static inline typename geometry::coordinate_type<P>::type
  32. get(P const& p, std::true_type /*same units*/)
  33. {
  34. return geometry::get<I>(p);
  35. }
  36. template <std::size_t I, typename P>
  37. static inline typename geometry::coordinate_type<P>::type
  38. get(P const& p, std::false_type /*different units*/)
  39. {
  40. return geometry::get_as_radian<I>(p);
  41. }
  42. template
  43. <
  44. typename ComparePolicy,
  45. typename EqualsPolicy,
  46. typename Point1,
  47. typename Point2,
  48. std::size_t DimensionCount
  49. >
  50. struct spherical_latitude
  51. {
  52. typedef typename geometry::coordinate_type<Point1>::type coordinate1_type;
  53. typedef typename geometry::detail::cs_angular_units<Point1>::type units1_type;
  54. typedef typename geometry::coordinate_type<Point2>::type coordinate2_type;
  55. typedef typename geometry::detail::cs_angular_units<Point2>::type units2_type;
  56. typedef std::is_same<units1_type, units2_type> same_units_type;
  57. template <typename T1, typename T2>
  58. static inline bool apply(Point1 const& left, Point2 const& right,
  59. T1 const& l1, T2 const& r1)
  60. {
  61. // latitudes equal
  62. if (EqualsPolicy::apply(l1, r1))
  63. {
  64. return compare::detail::compare_loop
  65. <
  66. ComparePolicy, EqualsPolicy, 2, DimensionCount
  67. >::apply(left, right);
  68. }
  69. else
  70. {
  71. return ComparePolicy::apply(l1, r1);
  72. }
  73. }
  74. static inline bool apply(Point1 const& left, Point2 const& right)
  75. {
  76. coordinate1_type const& l1 = compare::detail::get<1>(left, same_units_type());
  77. coordinate2_type const& r1 = compare::detail::get<1>(right, same_units_type());
  78. return apply(left, right, l1, r1);
  79. }
  80. };
  81. template
  82. <
  83. typename ComparePolicy,
  84. typename EqualsPolicy,
  85. typename Point1,
  86. typename Point2
  87. >
  88. struct spherical_latitude<ComparePolicy, EqualsPolicy, Point1, Point2, 1>
  89. {
  90. template <typename T1, typename T2>
  91. static inline bool apply(Point1 const& left, Point2 const& right,
  92. T1 const& , T2 const& )
  93. {
  94. return apply(left, right);
  95. }
  96. static inline bool apply(Point1 const& left, Point2 const& right)
  97. {
  98. return compare::detail::compare_loop
  99. <
  100. ComparePolicy, EqualsPolicy, 1, 1
  101. >::apply(left, right);
  102. }
  103. };
  104. template
  105. <
  106. typename ComparePolicy,
  107. typename EqualsPolicy,
  108. typename Point1,
  109. typename Point2,
  110. std::size_t DimensionCount
  111. >
  112. struct spherical_longitude
  113. {
  114. typedef typename geometry::coordinate_type<Point1>::type coordinate1_type;
  115. typedef typename geometry::detail::cs_angular_units<Point1>::type units1_type;
  116. typedef typename geometry::coordinate_type<Point2>::type coordinate2_type;
  117. typedef typename geometry::detail::cs_angular_units<Point2>::type units2_type;
  118. typedef std::is_same<units1_type, units2_type> same_units_type;
  119. typedef std::conditional_t<same_units_type::value, units1_type, geometry::radian> units_type;
  120. static const bool is_equatorial = ! std::is_same
  121. <
  122. typename geometry::cs_tag<Point1>::type,
  123. geometry::spherical_polar_tag
  124. >::value;
  125. static inline bool are_both_at_antimeridian(coordinate1_type const& l0,
  126. coordinate2_type const& r0,
  127. bool & is_left_at,
  128. bool & is_right_at)
  129. {
  130. is_left_at = math::is_longitude_antimeridian<units_type>(l0);
  131. is_right_at = math::is_longitude_antimeridian<units_type>(r0);
  132. return is_left_at && is_right_at;
  133. }
  134. static inline bool apply(Point1 const& left, Point2 const& right)
  135. {
  136. // if units are different the coordinates are in radians
  137. coordinate1_type const& l0 = compare::detail::get<0>(left, same_units_type());
  138. coordinate2_type const& r0 = compare::detail::get<0>(right, same_units_type());
  139. coordinate1_type const& l1 = compare::detail::get<1>(left, same_units_type());
  140. coordinate2_type const& r1 = compare::detail::get<1>(right, same_units_type());
  141. bool is_left_at_antimeridian = false;
  142. bool is_right_at_antimeridian = false;
  143. // longitudes equal
  144. if (EqualsPolicy::apply(l0, r0)
  145. // both at antimeridian
  146. || are_both_at_antimeridian(l0, r0, is_left_at_antimeridian, is_right_at_antimeridian)
  147. // both at pole
  148. || (EqualsPolicy::apply(l1, r1)
  149. && math::is_latitude_pole<units_type, is_equatorial>(l1)))
  150. {
  151. return spherical_latitude
  152. <
  153. ComparePolicy, EqualsPolicy, Point1, Point2, DimensionCount
  154. >::apply(left, right, l1, r1);
  155. }
  156. // if left is at antimeridian and right is not at antimeridian
  157. // then left is greater than right
  158. else if (is_left_at_antimeridian)
  159. {
  160. // less/equal_to -> false, greater -> true
  161. return ComparePolicy::apply(1, 0);
  162. }
  163. // if right is at antimeridian and left is not at antimeridian
  164. // then left is lesser than right
  165. else if (is_right_at_antimeridian)
  166. {
  167. // less -> true, equal_to/greater -> false
  168. return ComparePolicy::apply(0, 1);
  169. }
  170. else
  171. {
  172. return ComparePolicy::apply(l0, r0);
  173. }
  174. }
  175. };
  176. } // namespace detail
  177. #endif // DOXYGEN_NO_DETAIL
  178. /*!
  179. \brief Compare strategy for spherical coordinates
  180. \ingroup strategies
  181. \tparam Point point-type
  182. \tparam Dimension dimension
  183. */
  184. template
  185. <
  186. typename ComparePolicy,
  187. typename EqualsPolicy,
  188. int Dimension = -1
  189. >
  190. struct spherical
  191. : cartesian<ComparePolicy, EqualsPolicy, Dimension>
  192. {};
  193. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  194. // all dimensions starting from longitude
  195. template <typename ComparePolicy, typename EqualsPolicy>
  196. struct spherical<ComparePolicy, EqualsPolicy, -1>
  197. {
  198. template <typename Point1, typename Point2>
  199. static inline bool apply(Point1 const& left, Point2 const& right)
  200. {
  201. return compare::detail::spherical_longitude
  202. <
  203. ComparePolicy,
  204. EqualsPolicy,
  205. Point1,
  206. Point2,
  207. std::conditional_t
  208. <
  209. (dimension<Point1>::value < dimension<Point2>::value),
  210. std::integral_constant<std::size_t, dimension<Point1>::value>,
  211. std::integral_constant<std::size_t, dimension<Point2>::value>
  212. >::value
  213. >::apply(left, right);
  214. }
  215. };
  216. // only longitudes (and latitudes to check poles)
  217. template <typename ComparePolicy, typename EqualsPolicy>
  218. struct spherical<ComparePolicy, EqualsPolicy, 0>
  219. {
  220. template <typename Point1, typename Point2>
  221. static inline bool apply(Point1 const& left, Point2 const& right)
  222. {
  223. return compare::detail::spherical_longitude
  224. <
  225. ComparePolicy, EqualsPolicy, Point1, Point2, 1
  226. >::apply(left, right);
  227. }
  228. };
  229. // only latitudes
  230. template <typename ComparePolicy, typename EqualsPolicy>
  231. struct spherical<ComparePolicy, EqualsPolicy, 1>
  232. {
  233. template <typename Point1, typename Point2>
  234. static inline bool apply(Point1 const& left, Point2 const& right)
  235. {
  236. return compare::detail::spherical_latitude
  237. <
  238. ComparePolicy, EqualsPolicy, Point1, Point2, 2
  239. >::apply(left, right);
  240. }
  241. };
  242. #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  243. namespace services
  244. {
  245. template <typename ComparePolicy, typename EqualsPolicy, typename Point1, typename Point2, int Dimension>
  246. struct default_strategy
  247. <
  248. ComparePolicy, EqualsPolicy,
  249. Point1, Point2, Dimension,
  250. spherical_tag, spherical_tag
  251. >
  252. {
  253. typedef compare::spherical<ComparePolicy, EqualsPolicy, Dimension> type;
  254. };
  255. template <typename ComparePolicy, typename EqualsPolicy, typename Point1, typename Point2, int Dimension>
  256. struct default_strategy
  257. <
  258. ComparePolicy, EqualsPolicy,
  259. Point1, Point2, Dimension,
  260. spherical_polar_tag, spherical_polar_tag
  261. >
  262. {
  263. typedef compare::spherical<ComparePolicy, EqualsPolicy, Dimension> type;
  264. };
  265. template <typename ComparePolicy, typename EqualsPolicy, typename Point1, typename Point2, int Dimension>
  266. struct default_strategy
  267. <
  268. ComparePolicy, EqualsPolicy,
  269. Point1, Point2, Dimension,
  270. spherical_equatorial_tag, spherical_equatorial_tag
  271. >
  272. {
  273. typedef compare::spherical<ComparePolicy, EqualsPolicy, Dimension> type;
  274. };
  275. template <typename ComparePolicy, typename EqualsPolicy, typename Point1, typename Point2, int Dimension>
  276. struct default_strategy
  277. <
  278. ComparePolicy, EqualsPolicy,
  279. Point1, Point2, Dimension,
  280. geographic_tag, geographic_tag
  281. >
  282. {
  283. typedef compare::spherical<ComparePolicy, EqualsPolicy, Dimension> type;
  284. };
  285. } // namespace services
  286. }} // namespace strategy::compare
  287. }} // namespace boost::geometry
  288. #endif // BOOST_GEOMETRY_STRATEGIES_SPHERICAL_COMPARE_HPP