compare.hpp 9.8 KB

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