compare.hpp 9.8 KB

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