geometry_to_segment_or_box.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2014, 2019, Oracle and/or its affiliates.
  3. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  4. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  5. // Licensed under the Boost Software License version 1.0.
  6. // http://www.boost.org/users/license.html
  7. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_DISTANCE_GEOMETRY_TO_SEGMENT_OR_BOX_HPP
  8. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_DISTANCE_GEOMETRY_TO_SEGMENT_OR_BOX_HPP
  9. #include <iterator>
  10. #include <boost/range.hpp>
  11. #include <boost/geometry/core/point_type.hpp>
  12. #include <boost/geometry/core/tag.hpp>
  13. #include <boost/geometry/core/tags.hpp>
  14. #include <boost/geometry/strategies/distance.hpp>
  15. #include <boost/geometry/strategies/tags.hpp>
  16. #include <boost/geometry/algorithms/assign.hpp>
  17. #include <boost/geometry/algorithms/intersects.hpp>
  18. #include <boost/geometry/algorithms/num_points.hpp>
  19. #include <boost/geometry/iterators/point_iterator.hpp>
  20. #include <boost/geometry/iterators/segment_iterator.hpp>
  21. #include <boost/geometry/algorithms/dispatch/distance.hpp>
  22. #include <boost/geometry/algorithms/detail/closest_feature/geometry_to_range.hpp>
  23. #include <boost/geometry/algorithms/detail/closest_feature/point_to_range.hpp>
  24. #include <boost/geometry/algorithms/detail/distance/is_comparable.hpp>
  25. #include <boost/geometry/util/condition.hpp>
  26. namespace boost { namespace geometry
  27. {
  28. #ifndef DOXYGEN_NO_DETAIL
  29. namespace detail { namespace distance
  30. {
  31. // closure of segment or box point range
  32. template
  33. <
  34. typename SegmentOrBox,
  35. typename Tag = typename tag<SegmentOrBox>::type
  36. >
  37. struct segment_or_box_point_range_closure
  38. : not_implemented<SegmentOrBox>
  39. {};
  40. template <typename Segment>
  41. struct segment_or_box_point_range_closure<Segment, segment_tag>
  42. {
  43. static const closure_selector value = closed;
  44. };
  45. template <typename Box>
  46. struct segment_or_box_point_range_closure<Box, box_tag>
  47. {
  48. static const closure_selector value = open;
  49. };
  50. template
  51. <
  52. typename Geometry,
  53. typename SegmentOrBox,
  54. typename Strategy,
  55. typename Tag = typename tag<Geometry>::type
  56. >
  57. class geometry_to_segment_or_box
  58. {
  59. private:
  60. typedef typename point_type<SegmentOrBox>::type segment_or_box_point;
  61. typedef typename strategy::distance::services::comparable_type
  62. <
  63. Strategy
  64. >::type comparable_strategy;
  65. typedef detail::closest_feature::point_to_point_range
  66. <
  67. typename point_type<Geometry>::type,
  68. std::vector<segment_or_box_point>,
  69. segment_or_box_point_range_closure<SegmentOrBox>::value,
  70. comparable_strategy
  71. > point_to_point_range;
  72. typedef detail::closest_feature::geometry_to_range geometry_to_range;
  73. typedef typename strategy::distance::services::return_type
  74. <
  75. comparable_strategy,
  76. typename point_type<Geometry>::type,
  77. segment_or_box_point
  78. >::type comparable_return_type;
  79. // assign the new minimum value for an iterator of the point range
  80. // of a segment or a box
  81. template
  82. <
  83. typename SegOrBox,
  84. typename SegOrBoxTag = typename tag<SegOrBox>::type
  85. >
  86. struct assign_new_min_iterator
  87. : not_implemented<SegOrBox>
  88. {};
  89. template <typename Segment>
  90. struct assign_new_min_iterator<Segment, segment_tag>
  91. {
  92. template <typename Iterator>
  93. static inline void apply(Iterator&, Iterator)
  94. {
  95. }
  96. };
  97. template <typename Box>
  98. struct assign_new_min_iterator<Box, box_tag>
  99. {
  100. template <typename Iterator>
  101. static inline void apply(Iterator& it_min, Iterator it)
  102. {
  103. it_min = it;
  104. }
  105. };
  106. // assign the points of a segment or a box to a range
  107. template
  108. <
  109. typename SegOrBox,
  110. typename PointRange,
  111. typename SegOrBoxTag = typename tag<SegOrBox>::type
  112. >
  113. struct assign_segment_or_box_points
  114. {};
  115. template <typename Segment, typename PointRange>
  116. struct assign_segment_or_box_points<Segment, PointRange, segment_tag>
  117. {
  118. static inline void apply(Segment const& segment, PointRange& range)
  119. {
  120. detail::assign_point_from_index<0>(segment, range[0]);
  121. detail::assign_point_from_index<1>(segment, range[1]);
  122. }
  123. };
  124. template <typename Box, typename PointRange>
  125. struct assign_segment_or_box_points<Box, PointRange, box_tag>
  126. {
  127. static inline void apply(Box const& box, PointRange& range)
  128. {
  129. detail::assign_box_corners_oriented<true>(box, range);
  130. }
  131. };
  132. template
  133. <
  134. typename SegOrBox,
  135. typename SegOrBoxTag = typename tag<SegOrBox>::type
  136. >
  137. struct intersects
  138. {
  139. static inline bool apply(Geometry const& g1, SegOrBox const& g2, Strategy const&)
  140. {
  141. return geometry::intersects(g1, g2);
  142. }
  143. };
  144. template <typename SegOrBox>
  145. struct intersects<SegOrBox, segment_tag>
  146. {
  147. static inline bool apply(Geometry const& g1, SegOrBox const& g2, Strategy const& s)
  148. {
  149. return geometry::intersects(g1, g2, s.get_relate_segment_segment_strategy());
  150. }
  151. };
  152. public:
  153. typedef typename strategy::distance::services::return_type
  154. <
  155. Strategy,
  156. typename point_type<Geometry>::type,
  157. segment_or_box_point
  158. >::type return_type;
  159. static inline return_type apply(Geometry const& geometry,
  160. SegmentOrBox const& segment_or_box,
  161. Strategy const& strategy,
  162. bool check_intersection = true)
  163. {
  164. typedef geometry::point_iterator<Geometry const> point_iterator_type;
  165. typedef geometry::segment_iterator
  166. <
  167. Geometry const
  168. > segment_iterator_type;
  169. typedef typename boost::range_const_iterator
  170. <
  171. std::vector<segment_or_box_point>
  172. >::type seg_or_box_const_iterator;
  173. typedef assign_new_min_iterator<SegmentOrBox> assign_new_value;
  174. if (check_intersection
  175. && intersects<SegmentOrBox>::apply(geometry, segment_or_box, strategy))
  176. {
  177. return 0;
  178. }
  179. comparable_strategy cstrategy =
  180. strategy::distance::services::get_comparable
  181. <
  182. Strategy
  183. >::apply(strategy);
  184. // get all points of the segment or the box
  185. std::vector<segment_or_box_point>
  186. seg_or_box_points(geometry::num_points(segment_or_box));
  187. assign_segment_or_box_points
  188. <
  189. SegmentOrBox,
  190. std::vector<segment_or_box_point>
  191. >::apply(segment_or_box, seg_or_box_points);
  192. // consider all distances of the points in the geometry to the
  193. // segment or box
  194. comparable_return_type cd_min1(0);
  195. point_iterator_type pit_min;
  196. seg_or_box_const_iterator it_min1 = boost::const_begin(seg_or_box_points);
  197. seg_or_box_const_iterator it_min2 = it_min1;
  198. ++it_min2;
  199. bool first = true;
  200. for (point_iterator_type pit = points_begin(geometry);
  201. pit != points_end(geometry); ++pit, first = false)
  202. {
  203. comparable_return_type cd;
  204. std::pair
  205. <
  206. seg_or_box_const_iterator, seg_or_box_const_iterator
  207. > it_pair
  208. = point_to_point_range::apply(*pit,
  209. boost::const_begin(seg_or_box_points),
  210. boost::const_end(seg_or_box_points),
  211. cstrategy,
  212. cd);
  213. if (first || cd < cd_min1)
  214. {
  215. cd_min1 = cd;
  216. pit_min = pit;
  217. assign_new_value::apply(it_min1, it_pair.first);
  218. assign_new_value::apply(it_min2, it_pair.second);
  219. }
  220. }
  221. // consider all distances of the points in the segment or box to the
  222. // segments of the geometry
  223. comparable_return_type cd_min2(0);
  224. segment_iterator_type sit_min;
  225. seg_or_box_const_iterator it_min;
  226. first = true;
  227. for (seg_or_box_const_iterator it = boost::const_begin(seg_or_box_points);
  228. it != boost::const_end(seg_or_box_points); ++it, first = false)
  229. {
  230. comparable_return_type cd;
  231. segment_iterator_type sit
  232. = geometry_to_range::apply(*it,
  233. segments_begin(geometry),
  234. segments_end(geometry),
  235. cstrategy,
  236. cd);
  237. if (first || cd < cd_min2)
  238. {
  239. cd_min2 = cd;
  240. it_min = it;
  241. sit_min = sit;
  242. }
  243. }
  244. if (BOOST_GEOMETRY_CONDITION(is_comparable<Strategy>::value))
  245. {
  246. return (std::min)(cd_min1, cd_min2);
  247. }
  248. if (cd_min1 < cd_min2)
  249. {
  250. return strategy.apply(*pit_min, *it_min1, *it_min2);
  251. }
  252. else
  253. {
  254. return dispatch::distance
  255. <
  256. segment_or_box_point,
  257. typename std::iterator_traits
  258. <
  259. segment_iterator_type
  260. >::value_type,
  261. Strategy
  262. >::apply(*it_min, *sit_min, strategy);
  263. }
  264. }
  265. static inline return_type
  266. apply(SegmentOrBox const& segment_or_box, Geometry const& geometry,
  267. Strategy const& strategy, bool check_intersection = true)
  268. {
  269. return apply(geometry, segment_or_box, strategy, check_intersection);
  270. }
  271. };
  272. template <typename MultiPoint, typename SegmentOrBox, typename Strategy>
  273. class geometry_to_segment_or_box
  274. <
  275. MultiPoint, SegmentOrBox, Strategy, multi_point_tag
  276. >
  277. {
  278. private:
  279. typedef detail::closest_feature::geometry_to_range base_type;
  280. typedef typename boost::range_iterator
  281. <
  282. MultiPoint const
  283. >::type iterator_type;
  284. typedef detail::closest_feature::geometry_to_range geometry_to_range;
  285. public:
  286. typedef typename strategy::distance::services::return_type
  287. <
  288. Strategy,
  289. typename point_type<SegmentOrBox>::type,
  290. typename point_type<MultiPoint>::type
  291. >::type return_type;
  292. static inline return_type apply(MultiPoint const& multipoint,
  293. SegmentOrBox const& segment_or_box,
  294. Strategy const& strategy)
  295. {
  296. namespace sds = strategy::distance::services;
  297. typename sds::return_type
  298. <
  299. typename sds::comparable_type<Strategy>::type,
  300. typename point_type<SegmentOrBox>::type,
  301. typename point_type<MultiPoint>::type
  302. >::type cd_min;
  303. iterator_type it_min
  304. = geometry_to_range::apply(segment_or_box,
  305. boost::begin(multipoint),
  306. boost::end(multipoint),
  307. sds::get_comparable
  308. <
  309. Strategy
  310. >::apply(strategy),
  311. cd_min);
  312. return
  313. is_comparable<Strategy>::value
  314. ?
  315. cd_min
  316. :
  317. dispatch::distance
  318. <
  319. typename point_type<MultiPoint>::type,
  320. SegmentOrBox,
  321. Strategy
  322. >::apply(*it_min, segment_or_box, strategy);
  323. }
  324. };
  325. }} // namespace detail::distance
  326. #endif // DOXYGEN_NO_DETAIL
  327. #ifndef DOXYGEN_NO_DISPATCH
  328. namespace dispatch
  329. {
  330. template <typename Linear, typename Segment, typename Strategy>
  331. struct distance
  332. <
  333. Linear, Segment, Strategy, linear_tag, segment_tag,
  334. strategy_tag_distance_point_segment, false
  335. > : detail::distance::geometry_to_segment_or_box<Linear, Segment, Strategy>
  336. {};
  337. template <typename Areal, typename Segment, typename Strategy>
  338. struct distance
  339. <
  340. Areal, Segment, Strategy, areal_tag, segment_tag,
  341. strategy_tag_distance_point_segment, false
  342. > : detail::distance::geometry_to_segment_or_box<Areal, Segment, Strategy>
  343. {};
  344. template <typename Segment, typename Areal, typename Strategy>
  345. struct distance
  346. <
  347. Segment, Areal, Strategy, segment_tag, areal_tag,
  348. strategy_tag_distance_point_segment, false
  349. > : detail::distance::geometry_to_segment_or_box<Areal, Segment, Strategy>
  350. {};
  351. template <typename Linear, typename Box, typename Strategy>
  352. struct distance
  353. <
  354. Linear, Box, Strategy, linear_tag, box_tag,
  355. strategy_tag_distance_point_segment, false
  356. > : detail::distance::geometry_to_segment_or_box
  357. <
  358. Linear, Box, Strategy
  359. >
  360. {};
  361. template <typename Areal, typename Box, typename Strategy>
  362. struct distance
  363. <
  364. Areal, Box, Strategy, areal_tag, box_tag,
  365. strategy_tag_distance_point_segment, false
  366. > : detail::distance::geometry_to_segment_or_box<Areal, Box, Strategy>
  367. {};
  368. template <typename MultiPoint, typename Segment, typename Strategy>
  369. struct distance
  370. <
  371. MultiPoint, Segment, Strategy,
  372. multi_point_tag, segment_tag,
  373. strategy_tag_distance_point_segment, false
  374. > : detail::distance::geometry_to_segment_or_box
  375. <
  376. MultiPoint, Segment, Strategy
  377. >
  378. {};
  379. template <typename MultiPoint, typename Box, typename Strategy>
  380. struct distance
  381. <
  382. MultiPoint, Box, Strategy,
  383. multi_point_tag, box_tag,
  384. strategy_tag_distance_point_box, false
  385. > : detail::distance::geometry_to_segment_or_box
  386. <
  387. MultiPoint, Box, Strategy
  388. >
  389. {};
  390. } // namespace dispatch
  391. #endif // DOXYGEN_NO_DISPATCH
  392. }} // namespace boost::geometry
  393. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_DISTANCE_GEOMETRY_TO_SEGMENT_OR_BOX_HPP