compare.hpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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.
  4. // Modifications copyright (c) 2017, 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/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, boost::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, boost::false_type /*different units*/)
  39. {
  40. return geometry::get_as_radian<I>(p);
  41. }
  42. template
  43. <
  44. typename ComparePolicy,
  45. typename Point1,
  46. typename Point2,
  47. std::size_t DimensionCount
  48. >
  49. struct spherical_latitude
  50. {
  51. typedef typename geometry::coordinate_type<Point1>::type coordinate1_type;
  52. typedef typename geometry::coordinate_system<Point1>::type::units units1_type;
  53. typedef typename geometry::coordinate_type<Point2>::type coordinate2_type;
  54. typedef typename geometry::coordinate_system<Point2>::type::units units2_type;
  55. typedef typename boost::is_same<units1_type, units2_type>::type same_units_type;
  56. template <typename T1, typename T2>
  57. static inline bool apply(Point1 const& left, Point2 const& right,
  58. T1 const& l1, T2 const& r1)
  59. {
  60. // latitudes equal
  61. if (math::equals(l1, r1))
  62. {
  63. return compare::detail::compare_loop
  64. <
  65. ComparePolicy, 2, DimensionCount
  66. >::apply(left, right);
  67. }
  68. else
  69. {
  70. return ComparePolicy::apply(l1, r1);
  71. }
  72. }
  73. static inline bool apply(Point1 const& left, Point2 const& right)
  74. {
  75. coordinate1_type const& l1 = compare::detail::get<1>(left, same_units_type());
  76. coordinate2_type const& r1 = compare::detail::get<1>(right, same_units_type());
  77. return apply(left, right, l1, r1);
  78. }
  79. };
  80. template
  81. <
  82. typename ComparePolicy,
  83. typename Point1,
  84. typename Point2
  85. >
  86. struct spherical_latitude<ComparePolicy, Point1, Point2, 1>
  87. {
  88. template <typename T1, typename T2>
  89. static inline bool apply(Point1 const& left, Point2 const& right,
  90. T1 const& , T2 const& )
  91. {
  92. return apply(left, right);
  93. }
  94. static inline bool apply(Point1 const& left, Point2 const& right)
  95. {
  96. return compare::detail::compare_loop
  97. <
  98. ComparePolicy, 1, 1
  99. >::apply(left, right);
  100. }
  101. };
  102. template
  103. <
  104. typename ComparePolicy,
  105. typename Point1,
  106. typename Point2,
  107. std::size_t DimensionCount
  108. >
  109. struct spherical_longitude
  110. {
  111. typedef typename geometry::coordinate_type<Point1>::type coordinate1_type;
  112. typedef typename geometry::coordinate_system<Point1>::type::units units1_type;
  113. typedef typename geometry::coordinate_type<Point2>::type coordinate2_type;
  114. typedef typename geometry::coordinate_system<Point2>::type::units units2_type;
  115. typedef typename boost::is_same<units1_type, units2_type>::type same_units_type;
  116. typedef typename boost::mpl::if_<same_units_type, units1_type, geometry::radian>::type units_type;
  117. static const bool is_equatorial = ! boost::is_same
  118. <
  119. typename geometry::cs_tag<Point1>::type,
  120. geometry::spherical_polar_tag
  121. >::value;
  122. static inline bool are_both_at_antimeridian(coordinate1_type const& l0,
  123. coordinate2_type const& r0,
  124. bool & is_left_at,
  125. bool & is_right_at)
  126. {
  127. is_left_at = math::is_longitude_antimeridian<units_type>(l0);
  128. is_right_at = math::is_longitude_antimeridian<units_type>(r0);
  129. return is_left_at && is_right_at;
  130. }
  131. static inline bool apply(Point1 const& left, Point2 const& right)
  132. {
  133. // if units are different the coordinates are in radians
  134. coordinate1_type const& l0 = compare::detail::get<0>(left, same_units_type());
  135. coordinate2_type const& r0 = compare::detail::get<0>(right, same_units_type());
  136. coordinate1_type const& l1 = compare::detail::get<1>(left, same_units_type());
  137. coordinate2_type const& r1 = compare::detail::get<1>(right, same_units_type());
  138. bool is_left_at_antimeridian = false;
  139. bool is_right_at_antimeridian = false;
  140. // longitudes equal
  141. if (math::equals(l0, r0)
  142. // both at antimeridian
  143. || are_both_at_antimeridian(l0, r0, is_left_at_antimeridian, is_right_at_antimeridian)
  144. // both at pole
  145. || (math::equals(l1, r1)
  146. && math::is_latitude_pole<units_type, is_equatorial>(l1)))
  147. {
  148. return spherical_latitude
  149. <
  150. ComparePolicy, Point1, Point2, DimensionCount
  151. >::apply(left, right, l1, r1);
  152. }
  153. // if left is at antimeridian and right is not at antimeridian
  154. // then left is greater than right
  155. else if (is_left_at_antimeridian)
  156. {
  157. // less/equal_to -> false, greater -> true
  158. return ComparePolicy::apply(1, 0);
  159. }
  160. // if right is at antimeridian and left is not at antimeridian
  161. // then left is lesser than right
  162. else if (is_right_at_antimeridian)
  163. {
  164. // less -> true, equal_to/greater -> false
  165. return ComparePolicy::apply(0, 1);
  166. }
  167. else
  168. {
  169. return ComparePolicy::apply(l0, r0);
  170. }
  171. }
  172. };
  173. } // namespace detail
  174. #endif // DOXYGEN_NO_DETAIL
  175. /*!
  176. \brief Compare strategy for spherical coordinates
  177. \ingroup strategies
  178. \tparam Point point-type
  179. \tparam Dimension dimension
  180. */
  181. template
  182. <
  183. typename ComparePolicy,
  184. int Dimension = -1
  185. >
  186. struct spherical
  187. : cartesian<ComparePolicy, Dimension>
  188. {};
  189. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  190. // all dimensions starting from longitude
  191. template <typename ComparePolicy>
  192. struct spherical<ComparePolicy, -1>
  193. {
  194. template <typename Point1, typename Point2>
  195. static inline bool apply(Point1 const& left, Point2 const& right)
  196. {
  197. return compare::detail::spherical_longitude
  198. <
  199. ComparePolicy,
  200. Point1,
  201. Point2,
  202. boost::mpl::min
  203. <
  204. geometry::dimension<Point1>,
  205. geometry::dimension<Point2>
  206. >::type::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_polar_tag, spherical_polar_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_equatorial_tag, spherical_equatorial_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. geographic_tag, geographic_tag
  262. >
  263. {
  264. typedef compare::spherical<ComparePolicy, Dimension> type;
  265. };
  266. } // namespace services
  267. }} // namespace strategy::compare
  268. }} // namespace boost::geometry
  269. #endif // BOOST_GEOMETRY_STRATEGIES_SPHERICAL_COMPARE_HPP