point_to_geometry.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2014 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2014 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2014 Mateusz Loskot, London, UK.
  5. // Copyright (c) 2013-2014 Adam Wulkiewicz, Lodz, Poland.
  6. // This file was modified by Oracle on 2014, 2019.
  7. // Modifications copyright (c) 2014-2019, Oracle and/or its affiliates.
  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. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  11. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  12. // Use, modification and distribution is subject to the Boost Software License,
  13. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  14. // http://www.boost.org/LICENSE_1_0.txt)
  15. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_DISTANCE_POINT_TO_GEOMETRY_HPP
  16. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_DISTANCE_POINT_TO_GEOMETRY_HPP
  17. #include <iterator>
  18. #include <boost/core/ignore_unused.hpp>
  19. #include <boost/range.hpp>
  20. #include <boost/type_traits/is_same.hpp>
  21. #include <boost/geometry/core/closure.hpp>
  22. #include <boost/geometry/core/point_type.hpp>
  23. #include <boost/geometry/core/exterior_ring.hpp>
  24. #include <boost/geometry/core/interior_rings.hpp>
  25. #include <boost/geometry/core/tag.hpp>
  26. #include <boost/geometry/core/tags.hpp>
  27. #include <boost/geometry/util/math.hpp>
  28. #include <boost/geometry/strategies/distance.hpp>
  29. #include <boost/geometry/strategies/tags.hpp>
  30. #include <boost/geometry/algorithms/assign.hpp>
  31. #include <boost/geometry/algorithms/detail/closest_feature/geometry_to_range.hpp>
  32. #include <boost/geometry/algorithms/detail/closest_feature/point_to_range.hpp>
  33. #include <boost/geometry/algorithms/detail/distance/is_comparable.hpp>
  34. #include <boost/geometry/algorithms/detail/distance/iterator_selector.hpp>
  35. #include <boost/geometry/algorithms/detail/within/point_in_geometry.hpp>
  36. #include <boost/geometry/algorithms/dispatch/distance.hpp>
  37. namespace boost { namespace geometry
  38. {
  39. #ifndef DOXYGEN_NO_DETAIL
  40. namespace detail { namespace distance
  41. {
  42. template <typename P1, typename P2, typename Strategy>
  43. struct point_to_point
  44. {
  45. static inline
  46. typename strategy::distance::services::return_type<Strategy, P1, P2>::type
  47. apply(P1 const& p1, P2 const& p2, Strategy const& strategy)
  48. {
  49. boost::ignore_unused(strategy);
  50. return strategy.apply(p1, p2);
  51. }
  52. };
  53. template
  54. <
  55. typename Point,
  56. typename Range,
  57. closure_selector Closure,
  58. typename Strategy
  59. >
  60. class point_to_range
  61. {
  62. private:
  63. typedef typename strategy::distance::services::comparable_type
  64. <
  65. Strategy
  66. >::type comparable_strategy;
  67. typedef detail::closest_feature::point_to_point_range
  68. <
  69. Point, Range, Closure, comparable_strategy
  70. > point_to_point_range;
  71. public:
  72. typedef typename strategy::distance::services::return_type
  73. <
  74. Strategy,
  75. Point,
  76. typename boost::range_value<Range>::type
  77. >::type return_type;
  78. static inline return_type apply(Point const& point, Range const& range,
  79. Strategy const& strategy)
  80. {
  81. return_type const zero = return_type(0);
  82. if (boost::size(range) == 0)
  83. {
  84. return zero;
  85. }
  86. namespace sds = strategy::distance::services;
  87. typename sds::return_type
  88. <
  89. comparable_strategy,
  90. Point,
  91. typename point_type<Range>::type
  92. >::type cd_min;
  93. std::pair
  94. <
  95. typename boost::range_iterator<Range const>::type,
  96. typename boost::range_iterator<Range const>::type
  97. > it_pair
  98. = point_to_point_range::apply(point,
  99. boost::begin(range),
  100. boost::end(range),
  101. sds::get_comparable
  102. <
  103. Strategy
  104. >::apply(strategy),
  105. cd_min);
  106. return
  107. is_comparable<Strategy>::value
  108. ?
  109. cd_min
  110. :
  111. strategy.apply(point, *it_pair.first, *it_pair.second);
  112. }
  113. };
  114. template
  115. <
  116. typename Point,
  117. typename Ring,
  118. closure_selector Closure,
  119. typename Strategy
  120. >
  121. struct point_to_ring
  122. {
  123. typedef typename strategy::distance::services::return_type
  124. <
  125. Strategy, Point, typename point_type<Ring>::type
  126. >::type return_type;
  127. static inline return_type apply(Point const& point,
  128. Ring const& ring,
  129. Strategy const& strategy)
  130. {
  131. // TODO: pass strategy
  132. if (within::within_point_geometry(point, ring,
  133. strategy.get_point_in_geometry_strategy()))
  134. {
  135. return return_type(0);
  136. }
  137. return point_to_range
  138. <
  139. Point, Ring, closure<Ring>::value, Strategy
  140. >::apply(point, ring, strategy);
  141. }
  142. };
  143. template
  144. <
  145. typename Point,
  146. typename Polygon,
  147. closure_selector Closure,
  148. typename Strategy
  149. >
  150. class point_to_polygon
  151. {
  152. public:
  153. typedef typename strategy::distance::services::return_type
  154. <
  155. Strategy, Point, typename point_type<Polygon>::type
  156. >::type return_type;
  157. private:
  158. typedef point_to_range
  159. <
  160. Point, typename ring_type<Polygon>::type, Closure, Strategy
  161. > per_ring;
  162. struct distance_to_interior_rings
  163. {
  164. template <typename InteriorRingIterator>
  165. static inline return_type apply(Point const& point,
  166. InteriorRingIterator first,
  167. InteriorRingIterator last,
  168. Strategy const& strategy)
  169. {
  170. for (InteriorRingIterator it = first; it != last; ++it)
  171. {
  172. // TODO: pass strategy
  173. if (within::within_point_geometry(point, *it,
  174. strategy.get_point_in_geometry_strategy()))
  175. {
  176. // the point is inside a polygon hole, so its distance
  177. // to the polygon its distance to the polygon's
  178. // hole boundary
  179. return per_ring::apply(point, *it, strategy);
  180. }
  181. }
  182. return 0;
  183. }
  184. template <typename InteriorRings>
  185. static inline return_type apply(Point const& point,
  186. InteriorRings const& interior_rings,
  187. Strategy const& strategy)
  188. {
  189. return apply(point,
  190. boost::begin(interior_rings),
  191. boost::end(interior_rings),
  192. strategy);
  193. }
  194. };
  195. public:
  196. static inline return_type apply(Point const& point,
  197. Polygon const& polygon,
  198. Strategy const& strategy)
  199. {
  200. // TODO: pass strategy
  201. if (! within::covered_by_point_geometry(point, exterior_ring(polygon),
  202. strategy.get_point_in_geometry_strategy()))
  203. {
  204. // the point is outside the exterior ring, so its distance
  205. // to the polygon is its distance to the polygon's exterior ring
  206. return per_ring::apply(point, exterior_ring(polygon), strategy);
  207. }
  208. // Check interior rings
  209. return distance_to_interior_rings::apply(point,
  210. interior_rings(polygon),
  211. strategy);
  212. }
  213. };
  214. template
  215. <
  216. typename Point,
  217. typename MultiGeometry,
  218. typename Strategy,
  219. bool CheckCoveredBy = boost::is_same
  220. <
  221. typename tag<MultiGeometry>::type, multi_polygon_tag
  222. >::value
  223. >
  224. class point_to_multigeometry
  225. {
  226. private:
  227. typedef detail::closest_feature::geometry_to_range geometry_to_range;
  228. public:
  229. typedef typename strategy::distance::services::return_type
  230. <
  231. Strategy,
  232. Point,
  233. typename point_type<MultiGeometry>::type
  234. >::type return_type;
  235. static inline return_type apply(Point const& point,
  236. MultiGeometry const& multigeometry,
  237. Strategy const& strategy)
  238. {
  239. typedef iterator_selector<MultiGeometry const> selector_type;
  240. namespace sds = strategy::distance::services;
  241. typename sds::return_type
  242. <
  243. typename sds::comparable_type<Strategy>::type,
  244. Point,
  245. typename point_type<MultiGeometry>::type
  246. >::type cd;
  247. typename selector_type::iterator_type it_min
  248. = geometry_to_range::apply(point,
  249. selector_type::begin(multigeometry),
  250. selector_type::end(multigeometry),
  251. sds::get_comparable
  252. <
  253. Strategy
  254. >::apply(strategy),
  255. cd);
  256. return
  257. is_comparable<Strategy>::value
  258. ?
  259. cd
  260. :
  261. dispatch::distance
  262. <
  263. Point,
  264. typename std::iterator_traits
  265. <
  266. typename selector_type::iterator_type
  267. >::value_type,
  268. Strategy
  269. >::apply(point, *it_min, strategy);
  270. }
  271. };
  272. // this is called only for multipolygons, hence the change in the
  273. // template parameter name MultiGeometry to MultiPolygon
  274. template <typename Point, typename MultiPolygon, typename Strategy>
  275. struct point_to_multigeometry<Point, MultiPolygon, Strategy, true>
  276. {
  277. typedef typename strategy::distance::services::return_type
  278. <
  279. Strategy,
  280. Point,
  281. typename point_type<MultiPolygon>::type
  282. >::type return_type;
  283. static inline return_type apply(Point const& point,
  284. MultiPolygon const& multipolygon,
  285. Strategy const& strategy)
  286. {
  287. // TODO: pass strategy
  288. if (within::covered_by_point_geometry(point, multipolygon,
  289. strategy.get_point_in_geometry_strategy()))
  290. {
  291. return 0;
  292. }
  293. return point_to_multigeometry
  294. <
  295. Point, MultiPolygon, Strategy, false
  296. >::apply(point, multipolygon, strategy);
  297. }
  298. };
  299. }} // namespace detail::distance
  300. #endif // DOXYGEN_NO_DETAIL
  301. #ifndef DOXYGEN_NO_DISPATCH
  302. namespace dispatch
  303. {
  304. // Point-point
  305. template <typename P1, typename P2, typename Strategy>
  306. struct distance
  307. <
  308. P1, P2, Strategy, point_tag, point_tag,
  309. strategy_tag_distance_point_point, false
  310. > : detail::distance::point_to_point<P1, P2, Strategy>
  311. {};
  312. // Point-line version 2, where point-segment strategy is specified
  313. template <typename Point, typename Linestring, typename Strategy>
  314. struct distance
  315. <
  316. Point, Linestring, Strategy, point_tag, linestring_tag,
  317. strategy_tag_distance_point_segment, false
  318. > : detail::distance::point_to_range<Point, Linestring, closed, Strategy>
  319. {};
  320. // Point-ring , where point-segment strategy is specified
  321. template <typename Point, typename Ring, typename Strategy>
  322. struct distance
  323. <
  324. Point, Ring, Strategy, point_tag, ring_tag,
  325. strategy_tag_distance_point_segment, false
  326. > : detail::distance::point_to_ring
  327. <
  328. Point, Ring, closure<Ring>::value, Strategy
  329. >
  330. {};
  331. // Point-polygon , where point-segment strategy is specified
  332. template <typename Point, typename Polygon, typename Strategy>
  333. struct distance
  334. <
  335. Point, Polygon, Strategy, point_tag, polygon_tag,
  336. strategy_tag_distance_point_segment, false
  337. > : detail::distance::point_to_polygon
  338. <
  339. Point, Polygon, closure<Polygon>::value, Strategy
  340. >
  341. {};
  342. // Point-segment version 2, with point-segment strategy
  343. template <typename Point, typename Segment, typename Strategy>
  344. struct distance
  345. <
  346. Point, Segment, Strategy, point_tag, segment_tag,
  347. strategy_tag_distance_point_segment, false
  348. >
  349. {
  350. static inline typename strategy::distance::services::return_type
  351. <
  352. Strategy, Point, typename point_type<Segment>::type
  353. >::type apply(Point const& point,
  354. Segment const& segment,
  355. Strategy const& strategy)
  356. {
  357. typename point_type<Segment>::type p[2];
  358. geometry::detail::assign_point_from_index<0>(segment, p[0]);
  359. geometry::detail::assign_point_from_index<1>(segment, p[1]);
  360. boost::ignore_unused(strategy);
  361. return strategy.apply(point, p[0], p[1]);
  362. }
  363. };
  364. template <typename Point, typename Box, typename Strategy>
  365. struct distance
  366. <
  367. Point, Box, Strategy, point_tag, box_tag,
  368. strategy_tag_distance_point_box, false
  369. >
  370. {
  371. static inline typename strategy::distance::services::return_type
  372. <
  373. Strategy, Point, typename point_type<Box>::type
  374. >::type
  375. apply(Point const& point, Box const& box, Strategy const& strategy)
  376. {
  377. boost::ignore_unused(strategy);
  378. return strategy.apply(point, box);
  379. }
  380. };
  381. template<typename Point, typename MultiPoint, typename Strategy>
  382. struct distance
  383. <
  384. Point, MultiPoint, Strategy, point_tag, multi_point_tag,
  385. strategy_tag_distance_point_point, false
  386. > : detail::distance::point_to_multigeometry
  387. <
  388. Point, MultiPoint, Strategy
  389. >
  390. {};
  391. template<typename Point, typename MultiLinestring, typename Strategy>
  392. struct distance
  393. <
  394. Point, MultiLinestring, Strategy, point_tag, multi_linestring_tag,
  395. strategy_tag_distance_point_segment, false
  396. > : detail::distance::point_to_multigeometry
  397. <
  398. Point, MultiLinestring, Strategy
  399. >
  400. {};
  401. template<typename Point, typename MultiPolygon, typename Strategy>
  402. struct distance
  403. <
  404. Point, MultiPolygon, Strategy, point_tag, multi_polygon_tag,
  405. strategy_tag_distance_point_segment, false
  406. > : detail::distance::point_to_multigeometry
  407. <
  408. Point, MultiPolygon, Strategy
  409. >
  410. {};
  411. template <typename Point, typename Linear, typename Strategy>
  412. struct distance
  413. <
  414. Point, Linear, Strategy, point_tag, linear_tag,
  415. strategy_tag_distance_point_segment, false
  416. > : distance
  417. <
  418. Point, Linear, Strategy,
  419. point_tag, typename tag<Linear>::type,
  420. strategy_tag_distance_point_segment, false
  421. >
  422. {};
  423. template <typename Point, typename Areal, typename Strategy>
  424. struct distance
  425. <
  426. Point, Areal, Strategy, point_tag, areal_tag,
  427. strategy_tag_distance_point_segment, false
  428. > : distance
  429. <
  430. Point, Areal, Strategy,
  431. point_tag, typename tag<Areal>::type,
  432. strategy_tag_distance_point_segment, false
  433. >
  434. {};
  435. } // namespace dispatch
  436. #endif // DOXYGEN_NO_DISPATCH
  437. }} // namespace boost::geometry
  438. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_DISTANCE_POINT_TO_GEOMETRY_HPP