distance_cross_track_point_box.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2008-2015 Bruno Lalande, Paris, France.
  3. // Copyright (c) 2008-2015 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Copyright (c) 2009-2015 Mateusz Loskot, London, UK.
  5. // This file was modified by Oracle on 2014-2020.
  6. // Modifications copyright (c) 2014-2020, Oracle and/or its affiliates.
  7. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  8. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  9. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  10. // Use, modification and distribution is subject to the Boost Software License,
  11. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. #ifndef BOOST_GEOMETRY_STRATEGIES_SPHERICAL_DISTANCE_CROSS_TRACK_POINT_BOX_HPP
  14. #define BOOST_GEOMETRY_STRATEGIES_SPHERICAL_DISTANCE_CROSS_TRACK_POINT_BOX_HPP
  15. #include <type_traits>
  16. #include <boost/config.hpp>
  17. #include <boost/concept_check.hpp>
  18. #include <boost/geometry/core/access.hpp>
  19. #include <boost/geometry/core/assert.hpp>
  20. #include <boost/geometry/core/point_type.hpp>
  21. #include <boost/geometry/core/radian_access.hpp>
  22. #include <boost/geometry/core/tags.hpp>
  23. #include <boost/geometry/strategies/distance.hpp>
  24. #include <boost/geometry/strategies/concepts/distance_concept.hpp>
  25. #include <boost/geometry/strategies/spherical/distance_cross_track.hpp>
  26. #include <boost/geometry/util/math.hpp>
  27. #include <boost/geometry/algorithms/detail/assign_box_corners.hpp>
  28. namespace boost { namespace geometry
  29. {
  30. namespace strategy { namespace distance
  31. {
  32. namespace details
  33. {
  34. template <typename ReturnType>
  35. class cross_track_point_box_generic
  36. {
  37. public :
  38. template
  39. <
  40. typename Point,
  41. typename Box,
  42. typename Strategy
  43. >
  44. ReturnType static inline apply (Point const& point,
  45. Box const& box,
  46. Strategy ps_strategy)
  47. {
  48. // this method assumes that the coordinates of the point and
  49. // the box are normalized
  50. typedef typename point_type<Box>::type box_point_type;
  51. box_point_type bottom_left, bottom_right, top_left, top_right;
  52. geometry::detail::assign_box_corners(box,
  53. bottom_left, bottom_right,
  54. top_left, top_right);
  55. ReturnType const plon = geometry::get_as_radian<0>(point);
  56. ReturnType const plat = geometry::get_as_radian<1>(point);
  57. ReturnType const lon_min = geometry::get_as_radian<0>(bottom_left);
  58. ReturnType const lat_min = geometry::get_as_radian<1>(bottom_left);
  59. ReturnType const lon_max = geometry::get_as_radian<0>(top_right);
  60. ReturnType const lat_max = geometry::get_as_radian<1>(top_right);
  61. ReturnType const pi = math::pi<ReturnType>();
  62. ReturnType const two_pi = math::two_pi<ReturnType>();
  63. typedef typename point_type<Box>::type box_point_type;
  64. // First check if the point is within the band defined by the
  65. // minimum and maximum longitude of the box; if yes, determine
  66. // if the point is above, below or inside the box and compute
  67. // the distance (easy in this case)
  68. //
  69. // Notice that the point may not be inside the longitude range
  70. // of the box, but the shifted point may be inside the
  71. // longitude range of the box; in this case the point is still
  72. // considered as inside the longitude range band of the box
  73. if ((plon >= lon_min && plon <= lon_max) || plon + two_pi <= lon_max)
  74. {
  75. if (plat > lat_max)
  76. {
  77. return geometry::strategy::distance::services::result_from_distance
  78. <
  79. Strategy, Point, box_point_type
  80. >::apply(ps_strategy, ps_strategy
  81. .vertical_or_meridian(plat, lat_max));
  82. }
  83. else if (plat < lat_min)
  84. {
  85. return geometry::strategy::distance::services::result_from_distance
  86. <
  87. Strategy, Point, box_point_type
  88. >::apply(ps_strategy, ps_strategy
  89. .vertical_or_meridian(lat_min, plat));
  90. }
  91. else
  92. {
  93. BOOST_GEOMETRY_ASSERT(plat >= lat_min && plat <= lat_max);
  94. return ReturnType(0);
  95. }
  96. }
  97. // Otherwise determine which among the two medirian segments of the
  98. // box the point is closest to, and compute the distance of
  99. // the point to this closest segment
  100. // Below lon_midway is the longitude of the meridian that:
  101. // (1) is midway between the meridians of the left and right
  102. // meridians of the box, and
  103. // (2) does not intersect the box
  104. ReturnType const two = 2.0;
  105. bool use_left_segment;
  106. if (lon_max > pi)
  107. {
  108. // the box crosses the antimeridian
  109. // midway longitude = lon_min - (lon_min + (lon_max - 2 * pi)) / 2;
  110. ReturnType const lon_midway = (lon_min - lon_max) / two + pi;
  111. BOOST_GEOMETRY_ASSERT(lon_midway >= -pi && lon_midway <= pi);
  112. use_left_segment = plon > lon_midway;
  113. }
  114. else
  115. {
  116. // the box does not cross the antimeridian
  117. ReturnType const lon_sum = lon_min + lon_max;
  118. if (math::equals(lon_sum, ReturnType(0)))
  119. {
  120. // special case: the box is symmetric with respect to
  121. // the prime meridian; the midway meridian is the antimeridian
  122. use_left_segment = plon < lon_min;
  123. }
  124. else
  125. {
  126. // midway long. = lon_min - (2 * pi - (lon_max - lon_min)) / 2;
  127. ReturnType lon_midway = (lon_min + lon_max) / two - pi;
  128. // normalize the midway longitude
  129. if (lon_midway > pi)
  130. {
  131. lon_midway -= two_pi;
  132. }
  133. else if (lon_midway < -pi)
  134. {
  135. lon_midway += two_pi;
  136. }
  137. BOOST_GEOMETRY_ASSERT(lon_midway >= -pi && lon_midway <= pi);
  138. // if lon_sum is positive the midway meridian is left
  139. // of the box, or right of the box otherwise
  140. use_left_segment = lon_sum > 0
  141. ? (plon < lon_min && plon >= lon_midway)
  142. : (plon <= lon_max || plon > lon_midway);
  143. }
  144. }
  145. return use_left_segment
  146. ? ps_strategy.apply(point, bottom_left, top_left)
  147. : ps_strategy.apply(point, bottom_right, top_right);
  148. }
  149. };
  150. } //namespace details
  151. /*!
  152. \brief Strategy functor for distance point to box calculation
  153. \ingroup strategies
  154. \details Class which calculates the distance of a point to a box, for
  155. points and boxes on a sphere or globe
  156. \tparam CalculationType \tparam_calculation
  157. \tparam Strategy underlying point-point distance strategy
  158. \qbk{
  159. [heading See also]
  160. [link geometry.reference.algorithms.distance.distance_3_with_strategy distance (with strategy)]
  161. }
  162. */
  163. template
  164. <
  165. typename CalculationType = void,
  166. typename Strategy = haversine<double, CalculationType>
  167. >
  168. class cross_track_point_box
  169. {
  170. public:
  171. template <typename Point, typename Box>
  172. struct return_type
  173. : services::return_type<Strategy, Point, typename point_type<Box>::type>
  174. {};
  175. typedef typename Strategy::radius_type radius_type;
  176. // strategy getters
  177. // point-segment strategy getters
  178. struct distance_ps_strategy
  179. {
  180. typedef cross_track<CalculationType, Strategy> type;
  181. };
  182. typedef typename strategy::distance::services::comparable_type
  183. <
  184. Strategy
  185. >::type pp_comparable_strategy;
  186. typedef std::conditional_t
  187. <
  188. std::is_same
  189. <
  190. pp_comparable_strategy,
  191. Strategy
  192. >::value,
  193. typename strategy::distance::services::comparable_type
  194. <
  195. typename distance_ps_strategy::type
  196. >::type,
  197. typename distance_ps_strategy::type
  198. > ps_strategy_type;
  199. // constructors
  200. inline cross_track_point_box()
  201. {}
  202. explicit inline cross_track_point_box(typename Strategy::radius_type const& r)
  203. : m_strategy(r)
  204. {}
  205. inline cross_track_point_box(Strategy const& s)
  206. : m_strategy(s)
  207. {}
  208. // methods
  209. // It might be useful in the future
  210. // to overload constructor with strategy info.
  211. // crosstrack(...) {}
  212. template <typename Point, typename Box>
  213. inline typename return_type<Point, Box>::type
  214. apply(Point const& point, Box const& box) const
  215. {
  216. #if !defined(BOOST_MSVC)
  217. BOOST_CONCEPT_ASSERT
  218. (
  219. (concepts::PointDistanceStrategy
  220. <
  221. Strategy, Point, typename point_type<Box>::type
  222. >)
  223. );
  224. #endif
  225. typedef typename return_type<Point, Box>::type return_type;
  226. return details::cross_track_point_box_generic
  227. <return_type>::apply(point, box,
  228. ps_strategy_type(m_strategy));
  229. }
  230. inline typename Strategy::radius_type radius() const
  231. {
  232. return m_strategy.radius();
  233. }
  234. private:
  235. Strategy m_strategy;
  236. };
  237. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  238. namespace services
  239. {
  240. template <typename CalculationType, typename Strategy>
  241. struct tag<cross_track_point_box<CalculationType, Strategy> >
  242. {
  243. typedef strategy_tag_distance_point_box type;
  244. };
  245. template <typename CalculationType, typename Strategy, typename P, typename Box>
  246. struct return_type<cross_track_point_box<CalculationType, Strategy>, P, Box>
  247. : cross_track_point_box
  248. <
  249. CalculationType, Strategy
  250. >::template return_type<P, Box>
  251. {};
  252. template <typename CalculationType, typename Strategy>
  253. struct comparable_type<cross_track_point_box<CalculationType, Strategy> >
  254. {
  255. typedef cross_track_point_box
  256. <
  257. CalculationType, typename comparable_type<Strategy>::type
  258. > type;
  259. };
  260. template <typename CalculationType, typename Strategy>
  261. struct get_comparable<cross_track_point_box<CalculationType, Strategy> >
  262. {
  263. typedef cross_track_point_box<CalculationType, Strategy> this_strategy;
  264. typedef typename comparable_type<this_strategy>::type comparable_type;
  265. public:
  266. static inline comparable_type apply(this_strategy const& strategy)
  267. {
  268. return comparable_type(strategy.radius());
  269. }
  270. };
  271. template <typename CalculationType, typename Strategy, typename P, typename Box>
  272. struct result_from_distance
  273. <
  274. cross_track_point_box<CalculationType, Strategy>, P, Box
  275. >
  276. {
  277. private:
  278. typedef cross_track_point_box<CalculationType, Strategy> this_strategy;
  279. typedef typename this_strategy::template return_type
  280. <
  281. P, Box
  282. >::type return_type;
  283. public:
  284. template <typename T>
  285. static inline return_type apply(this_strategy const& strategy,
  286. T const& distance)
  287. {
  288. Strategy s(strategy.radius());
  289. return result_from_distance
  290. <
  291. Strategy, P, typename point_type<Box>::type
  292. >::apply(s, distance);
  293. }
  294. };
  295. // define cross_track_point_box<default_point_segment_strategy> as
  296. // default point-box strategy for the spherical equatorial coordinate system
  297. template <typename Point, typename Box, typename Strategy>
  298. struct default_strategy
  299. <
  300. point_tag, box_tag, Point, Box,
  301. spherical_equatorial_tag, spherical_equatorial_tag,
  302. Strategy
  303. >
  304. {
  305. typedef cross_track_point_box
  306. <
  307. void,
  308. std::conditional_t
  309. <
  310. std::is_void<Strategy>::value,
  311. typename default_strategy
  312. <
  313. point_tag, point_tag,
  314. Point, typename point_type<Box>::type,
  315. spherical_equatorial_tag, spherical_equatorial_tag
  316. >::type,
  317. Strategy
  318. >
  319. > type;
  320. };
  321. template <typename Box, typename Point, typename Strategy>
  322. struct default_strategy
  323. <
  324. box_tag, point_tag, Box, Point,
  325. spherical_equatorial_tag, spherical_equatorial_tag,
  326. Strategy
  327. >
  328. {
  329. typedef typename default_strategy
  330. <
  331. point_tag, box_tag, Point, Box,
  332. spherical_equatorial_tag, spherical_equatorial_tag,
  333. Strategy
  334. >::type type;
  335. };
  336. } // namespace services
  337. #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  338. }} // namespace strategy::distance
  339. }} // namespace boost::geometry
  340. #endif // BOOST_GEOMETRY_STRATEGIES_SPHERICAL_DISTANCE_CROSS_TRACK_POINT_BOX_HPP