distance_cross_track_box_box.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2016-2020 Oracle and/or its affiliates.
  3. // Contributed and/or modified by Vissarion Fisikopoulos, on behalf of Oracle
  4. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  5. // Use, modification and distribution is subject to the Boost Software License,
  6. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. #ifndef BOOST_GEOMETRY_STRATEGIES_SPHERICAL_DISTANCE_CROSS_TRACK_BOX_BOX_HPP
  9. #define BOOST_GEOMETRY_STRATEGIES_SPHERICAL_DISTANCE_CROSS_TRACK_BOX_BOX_HPP
  10. #include <type_traits>
  11. #include <boost/config.hpp>
  12. #include <boost/concept_check.hpp>
  13. #include <boost/geometry/core/access.hpp>
  14. #include <boost/geometry/core/assert.hpp>
  15. #include <boost/geometry/core/point_type.hpp>
  16. #include <boost/geometry/core/radian_access.hpp>
  17. #include <boost/geometry/core/tags.hpp>
  18. #include <boost/geometry/strategies/distance.hpp>
  19. #include <boost/geometry/strategies/concepts/distance_concept.hpp>
  20. #include <boost/geometry/strategies/spherical/distance_cross_track.hpp>
  21. #include <boost/geometry/util/math.hpp>
  22. #include <boost/geometry/algorithms/detail/assign_box_corners.hpp>
  23. namespace boost { namespace geometry
  24. {
  25. namespace strategy { namespace distance
  26. {
  27. namespace details
  28. {
  29. template <typename ReturnType>
  30. class cross_track_box_box_generic
  31. {
  32. public :
  33. template <typename Point, typename PPStrategy, typename PSStrategy>
  34. ReturnType static inline diagonal_case(Point topA,
  35. Point topB,
  36. Point bottomA,
  37. Point bottomB,
  38. bool north_shortest,
  39. bool non_overlap,
  40. PPStrategy pp_strategy,
  41. PSStrategy ps_strategy)
  42. {
  43. if (north_shortest && non_overlap)
  44. {
  45. return pp_strategy.apply(topA, bottomB);
  46. }
  47. if (north_shortest && !non_overlap)
  48. {
  49. return ps_strategy.apply(topA, topB, bottomB);
  50. }
  51. if (!north_shortest && non_overlap)
  52. {
  53. return pp_strategy.apply(bottomA, topB);
  54. }
  55. return ps_strategy.apply(bottomA, topB, bottomB);
  56. }
  57. template
  58. <
  59. typename Box1,
  60. typename Box2,
  61. typename PPStrategy,
  62. typename PSStrategy
  63. >
  64. ReturnType static inline apply (Box1 const& box1,
  65. Box2 const& box2,
  66. PPStrategy pp_strategy,
  67. PSStrategy ps_strategy)
  68. {
  69. // this method assumes that the coordinates of the point and
  70. // the box are normalized
  71. typedef typename point_type<Box1>::type box_point_type1;
  72. typedef typename point_type<Box2>::type box_point_type2;
  73. box_point_type1 bottom_left1, bottom_right1, top_left1, top_right1;
  74. geometry::detail::assign_box_corners(box1,
  75. bottom_left1, bottom_right1,
  76. top_left1, top_right1);
  77. box_point_type2 bottom_left2, bottom_right2, top_left2, top_right2;
  78. geometry::detail::assign_box_corners(box2,
  79. bottom_left2, bottom_right2,
  80. top_left2, top_right2);
  81. ReturnType lon_min1 = geometry::get_as_radian<0>(bottom_left1);
  82. ReturnType const lat_min1 = geometry::get_as_radian<1>(bottom_left1);
  83. ReturnType lon_max1 = geometry::get_as_radian<0>(top_right1);
  84. ReturnType const lat_max1 = geometry::get_as_radian<1>(top_right1);
  85. ReturnType lon_min2 = geometry::get_as_radian<0>(bottom_left2);
  86. ReturnType const lat_min2 = geometry::get_as_radian<1>(bottom_left2);
  87. ReturnType lon_max2 = geometry::get_as_radian<0>(top_right2);
  88. ReturnType const lat_max2 = geometry::get_as_radian<1>(top_right2);
  89. ReturnType const two_pi = math::two_pi<ReturnType>();
  90. // Test which sides of the boxes are closer and if boxes cross
  91. // antimeridian
  92. bool right_wrap;
  93. if (lon_min2 > 0 && lon_max2 < 0) // box2 crosses antimeridian
  94. {
  95. #ifdef BOOST_GEOMETRY_DEBUG_CROSS_TRACK_BOX_BOX
  96. std::cout << "(box2 crosses antimeridian)";
  97. #endif
  98. right_wrap = lon_min2 - lon_max1 < lon_min1 - lon_max2;
  99. lon_max2 += two_pi;
  100. if (lon_min1 > 0 && lon_max1 < 0) // both boxes crosses antimeridian
  101. {
  102. lon_max1 += two_pi;
  103. }
  104. }
  105. else if (lon_min1 > 0 && lon_max1 < 0) // only box1 crosses antimeridian
  106. {
  107. #ifdef BOOST_GEOMETRY_DEBUG_CROSS_TRACK_BOX_BOX
  108. std::cout << "(box1 crosses antimeridian)";
  109. #endif
  110. return apply(box2, box1, pp_strategy, ps_strategy);
  111. }
  112. else
  113. {
  114. right_wrap = lon_max1 <= lon_min2
  115. ? lon_min2 - lon_max1 < two_pi - (lon_max2 - lon_min1)
  116. : lon_min1 - lon_max2 > two_pi - (lon_max1 - lon_min2);
  117. }
  118. // Check1: if box2 crosses the band defined by the
  119. // minimum and maximum longitude of box1; if yes, determine
  120. // if the box2 is above, below or intersects/is inside box1 and compute
  121. // the distance (easy in this case)
  122. bool lon_min12 = lon_min1 <= lon_min2;
  123. bool right = lon_max1 <= lon_min2;
  124. bool left = lon_min1 >= lon_max2;
  125. bool lon_max12 = lon_max1 <= lon_max2;
  126. if ((lon_min12 && !right)
  127. || (!left && !lon_max12)
  128. || (!lon_min12 && lon_max12))
  129. {
  130. #ifdef BOOST_GEOMETRY_DEBUG_CROSS_TRACK_BOX_BOX
  131. std::cout << "(up-down)\n";
  132. #endif
  133. if (lat_min1 > lat_max2)
  134. {
  135. return geometry::strategy::distance::services::result_from_distance
  136. <
  137. PSStrategy, box_point_type1, box_point_type2
  138. >::apply(ps_strategy, ps_strategy
  139. .vertical_or_meridian(lat_min1, lat_max2));
  140. }
  141. else if (lat_max1 < lat_min2)
  142. {
  143. return geometry::strategy::distance::services::result_from_distance
  144. <
  145. PSStrategy, box_point_type1, box_point_type2
  146. >::apply(ps_strategy, ps_strategy
  147. .vertical_or_meridian(lat_min2, lat_max1));
  148. }
  149. else
  150. {
  151. //BOOST_GEOMETRY_ASSERT(plat >= lat_min && plat <= lat_max);
  152. return ReturnType(0);
  153. }
  154. }
  155. // Check2: if box2 is right/left of box1
  156. // the max lat of box2 should be less than the max lat of box1
  157. bool bottom_max;
  158. ReturnType top_common = (std::min)(lat_max1, lat_max2);
  159. ReturnType bottom_common = (std::max)(lat_min1, lat_min2);
  160. // true if the closest points are on northern hemisphere
  161. bool north_shortest = top_common + bottom_common > 0;
  162. // true if box bands do not overlap
  163. bool non_overlap = top_common < bottom_common;
  164. if (north_shortest)
  165. {
  166. bottom_max = lat_max1 >= lat_max2;
  167. }
  168. else
  169. {
  170. bottom_max = lat_min1 <= lat_min2;
  171. }
  172. #ifdef BOOST_GEOMETRY_DEBUG_CROSS_TRACK_BOX_BOX
  173. std::cout << "(diagonal)";
  174. #endif
  175. if (bottom_max && !right_wrap)
  176. {
  177. #ifdef BOOST_GEOMETRY_DEBUG_CROSS_TRACK_BOX_BOX
  178. std::cout << "(bottom left)";
  179. #endif
  180. return diagonal_case(top_right2, top_left1,
  181. bottom_right2, bottom_left1,
  182. north_shortest, non_overlap,
  183. pp_strategy, ps_strategy);
  184. }
  185. if (bottom_max && right_wrap)
  186. {
  187. #ifdef BOOST_GEOMETRY_DEBUG_CROSS_TRACK_BOX_BOX
  188. std::cout << "(bottom right)";
  189. #endif
  190. return diagonal_case(top_left2, top_right1,
  191. bottom_left2, bottom_right1,
  192. north_shortest, non_overlap,
  193. pp_strategy, ps_strategy);
  194. }
  195. if (!bottom_max && !right_wrap)
  196. {
  197. #ifdef BOOST_GEOMETRY_DEBUG_CROSS_TRACK_BOX_BOX
  198. std::cout << "(top left)";
  199. #endif
  200. return diagonal_case(top_left1, top_right2,
  201. bottom_left1, bottom_right2,
  202. north_shortest, non_overlap,
  203. pp_strategy, ps_strategy);
  204. }
  205. if (!bottom_max && right_wrap)
  206. {
  207. #ifdef BOOST_GEOMETRY_DEBUG_CROSS_TRACK_BOX_BOX
  208. std::cout << "(top right)";
  209. #endif
  210. return diagonal_case(top_right1, top_left2,
  211. bottom_right1, bottom_left2,
  212. north_shortest, non_overlap,
  213. pp_strategy, ps_strategy);
  214. }
  215. return ReturnType(0);
  216. }
  217. };
  218. } //namespace details
  219. /*!
  220. \brief Strategy functor for distance box to box calculation
  221. \ingroup strategies
  222. \details Class which calculates the distance of a box to a box, for
  223. boxes on a sphere or globe
  224. \tparam CalculationType \tparam_calculation
  225. \tparam Strategy underlying point-segment distance strategy, defaults
  226. to cross track
  227. \qbk{
  228. [heading See also]
  229. [link geometry.reference.algorithms.distance.distance_3_with_strategy distance (with strategy)]
  230. }
  231. */
  232. template
  233. <
  234. typename CalculationType = void,
  235. typename Strategy = haversine<double, CalculationType>
  236. >
  237. class cross_track_box_box
  238. {
  239. public:
  240. template <typename Box1, typename Box2>
  241. struct return_type
  242. : services::return_type<Strategy,
  243. typename point_type<Box1>::type,
  244. typename point_type<Box2>::type>
  245. {};
  246. typedef typename Strategy::radius_type radius_type;
  247. // strategy getters
  248. // point-segment strategy getters
  249. struct distance_ps_strategy
  250. {
  251. typedef cross_track<CalculationType, Strategy> type;
  252. };
  253. typedef typename strategy::distance::services::comparable_type
  254. <
  255. Strategy
  256. >::type pp_comparable_strategy;
  257. typedef std::conditional_t
  258. <
  259. std::is_same
  260. <
  261. pp_comparable_strategy,
  262. Strategy
  263. >::value,
  264. typename strategy::distance::services::comparable_type
  265. <
  266. typename distance_ps_strategy::type
  267. >::type,
  268. typename distance_ps_strategy::type
  269. > ps_strategy_type;
  270. // constructors
  271. inline cross_track_box_box()
  272. {}
  273. explicit inline cross_track_box_box(typename Strategy::radius_type const& r)
  274. : m_strategy(r)
  275. {}
  276. inline cross_track_box_box(Strategy const& s)
  277. : m_strategy(s)
  278. {}
  279. // It might be useful in the future
  280. // to overload constructor with strategy info.
  281. // crosstrack(...) {}
  282. template <typename Box1, typename Box2>
  283. inline typename return_type<Box1, Box2>::type
  284. apply(Box1 const& box1, Box2 const& box2) const
  285. {
  286. #if !defined(BOOST_MSVC)
  287. BOOST_CONCEPT_ASSERT
  288. (
  289. (concepts::PointDistanceStrategy
  290. <
  291. Strategy,
  292. typename point_type<Box1>::type,
  293. typename point_type<Box2>::type
  294. >)
  295. );
  296. #endif
  297. typedef typename return_type<Box1, Box2>::type return_type;
  298. return details::cross_track_box_box_generic
  299. <return_type>::apply(box1, box2,
  300. m_strategy,
  301. ps_strategy_type(m_strategy));
  302. }
  303. inline typename Strategy::radius_type radius() const
  304. {
  305. return m_strategy.radius();
  306. }
  307. private:
  308. Strategy m_strategy;
  309. };
  310. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  311. namespace services
  312. {
  313. template <typename CalculationType, typename Strategy>
  314. struct tag<cross_track_box_box<CalculationType, Strategy> >
  315. {
  316. typedef strategy_tag_distance_box_box type;
  317. };
  318. template <typename CalculationType, typename Strategy, typename Box1, typename Box2>
  319. struct return_type<cross_track_box_box<CalculationType, Strategy>, Box1, Box2>
  320. : cross_track_box_box
  321. <
  322. CalculationType, Strategy
  323. >::template return_type<Box1, Box2>
  324. {};
  325. template <typename CalculationType, typename Strategy>
  326. struct comparable_type<cross_track_box_box<CalculationType, Strategy> >
  327. {
  328. typedef cross_track_box_box
  329. <
  330. CalculationType, typename comparable_type<Strategy>::type
  331. > type;
  332. };
  333. template <typename CalculationType, typename Strategy>
  334. struct get_comparable<cross_track_box_box<CalculationType, Strategy> >
  335. {
  336. typedef cross_track_box_box<CalculationType, Strategy> this_strategy;
  337. typedef typename comparable_type<this_strategy>::type comparable_type;
  338. public:
  339. static inline comparable_type apply(this_strategy const& strategy)
  340. {
  341. return comparable_type(strategy.radius());
  342. }
  343. };
  344. template <typename CalculationType, typename Strategy, typename Box1, typename Box2>
  345. struct result_from_distance
  346. <
  347. cross_track_box_box<CalculationType, Strategy>, Box1, Box2
  348. >
  349. {
  350. private:
  351. typedef cross_track_box_box<CalculationType, Strategy> this_strategy;
  352. typedef typename this_strategy::template return_type
  353. <
  354. Box1, Box2
  355. >::type return_type;
  356. public:
  357. template <typename T>
  358. static inline return_type apply(this_strategy const& strategy,
  359. T const& distance)
  360. {
  361. Strategy s(strategy.radius());
  362. return result_from_distance
  363. <
  364. Strategy,
  365. typename point_type<Box1>::type,
  366. typename point_type<Box2>::type
  367. >::apply(s, distance);
  368. }
  369. };
  370. // define cross_track_box_box<default_point_segment_strategy> as
  371. // default box-box strategy for the spherical equatorial coordinate system
  372. template <typename Box1, typename Box2, typename Strategy>
  373. struct default_strategy
  374. <
  375. box_tag, box_tag, Box1, Box2,
  376. spherical_equatorial_tag, spherical_equatorial_tag,
  377. Strategy
  378. >
  379. {
  380. typedef cross_track_box_box
  381. <
  382. void,
  383. std::conditional_t
  384. <
  385. std::is_void<Strategy>::value,
  386. typename default_strategy
  387. <
  388. point_tag, point_tag,
  389. typename point_type<Box1>::type, typename point_type<Box2>::type,
  390. spherical_equatorial_tag, spherical_equatorial_tag
  391. >::type,
  392. Strategy
  393. >
  394. > type;
  395. };
  396. } // namespace services
  397. #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  398. }} // namespace strategy::distance
  399. }} // namespace boost::geometry
  400. #endif // BOOST_GEOMETRY_STRATEGIES_SPHERICAL_DISTANCE_CROSS_TRACK_BOX_BOX_HPP