multi_point_geometry.hpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. // Boost.Geometry
  2. // Copyright (c) 2017-2019 Oracle and/or its affiliates.
  3. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  4. // Use, modification and distribution is subject to the Boost Software License,
  5. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_RELATE_MULTI_POINT_GEOMETRY_HPP
  8. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_RELATE_MULTI_POINT_GEOMETRY_HPP
  9. #include <boost/range.hpp>
  10. #include <boost/geometry/algorithms/detail/disjoint/box_box.hpp>
  11. #include <boost/geometry/algorithms/detail/disjoint/point_box.hpp>
  12. #include <boost/geometry/algorithms/detail/expand_by_epsilon.hpp>
  13. #include <boost/geometry/algorithms/detail/partition.hpp>
  14. #include <boost/geometry/algorithms/detail/relate/result.hpp>
  15. #include <boost/geometry/algorithms/detail/relate/topology_check.hpp>
  16. #include <boost/geometry/algorithms/detail/within/point_in_geometry.hpp>
  17. #include <boost/geometry/algorithms/envelope.hpp>
  18. #include <boost/geometry/core/is_areal.hpp>
  19. #include <boost/geometry/core/point_type.hpp>
  20. #include <boost/geometry/geometries/box.hpp>
  21. #include <boost/geometry/index/rtree.hpp>
  22. namespace boost { namespace geometry
  23. {
  24. #ifndef DOXYGEN_NO_DETAIL
  25. namespace detail { namespace relate
  26. {
  27. template
  28. <
  29. typename Geometry,
  30. typename EqPPStrategy,
  31. typename Tag = typename tag<Geometry>::type
  32. >
  33. struct multi_point_geometry_eb
  34. {
  35. template <typename MultiPoint>
  36. static inline bool apply(MultiPoint const& ,
  37. detail::relate::topology_check<Geometry, EqPPStrategy> const& )
  38. {
  39. return true;
  40. }
  41. };
  42. template <typename Geometry, typename EqPPStrategy>
  43. struct multi_point_geometry_eb<Geometry, EqPPStrategy, linestring_tag>
  44. {
  45. template <typename Points>
  46. struct boundary_visitor
  47. {
  48. boundary_visitor(Points const& points)
  49. : m_points(points)
  50. , m_boundary_found(false)
  51. {}
  52. template <typename Point>
  53. struct find_pred
  54. {
  55. find_pred(Point const& point)
  56. : m_point(point)
  57. {}
  58. template <typename Pt>
  59. bool operator()(Pt const& pt) const
  60. {
  61. return detail::equals::equals_point_point(pt, m_point, EqPPStrategy());
  62. }
  63. Point const& m_point;
  64. };
  65. template <typename Point>
  66. bool apply(Point const& boundary_point)
  67. {
  68. if (std::find_if(m_points.begin(), m_points.end(), find_pred<Point>(boundary_point)) == m_points.end())
  69. {
  70. m_boundary_found = true;
  71. return false;
  72. }
  73. return true;
  74. }
  75. bool result() const { return m_boundary_found; }
  76. private:
  77. Points const& m_points;
  78. bool m_boundary_found;
  79. };
  80. template <typename MultiPoint>
  81. static inline bool apply(MultiPoint const& multi_point,
  82. detail::relate::topology_check<Geometry, EqPPStrategy> const& tc)
  83. {
  84. boundary_visitor<MultiPoint> visitor(multi_point);
  85. tc.for_each_boundary_point(visitor);
  86. return visitor.result();
  87. }
  88. };
  89. template <typename Geometry, typename EqPPStrategy>
  90. struct multi_point_geometry_eb<Geometry, EqPPStrategy, multi_linestring_tag>
  91. {
  92. // TODO: CS-specific less compare strategy derived from EqPPStrategy
  93. typedef geometry::less<void, -1, typename EqPPStrategy::cs_tag> less_type;
  94. template <typename Points>
  95. struct boundary_visitor
  96. {
  97. boundary_visitor(Points const& points)
  98. : m_points(points)
  99. , m_boundary_found(false)
  100. {}
  101. template <typename Point>
  102. bool apply(Point const& boundary_point)
  103. {
  104. if (! std::binary_search(m_points.begin(), m_points.end(), boundary_point, less_type()))
  105. {
  106. m_boundary_found = true;
  107. return false;
  108. }
  109. return true;
  110. }
  111. bool result() const { return m_boundary_found; }
  112. private:
  113. Points const& m_points;
  114. bool m_boundary_found;
  115. };
  116. template <typename MultiPoint>
  117. static inline bool apply(MultiPoint const& multi_point,
  118. detail::relate::topology_check<Geometry, EqPPStrategy> const& tc)
  119. {
  120. typedef typename boost::range_value<MultiPoint>::type point_type;
  121. typedef std::vector<point_type> points_type;
  122. points_type points(boost::begin(multi_point), boost::end(multi_point));
  123. std::sort(points.begin(), points.end(), less_type());
  124. boundary_visitor<points_type> visitor(points);
  125. tc.for_each_boundary_point(visitor);
  126. return visitor.result();
  127. }
  128. };
  129. // SingleGeometry - Linear or Areal
  130. template <typename MultiPoint, typename SingleGeometry, bool Transpose = false>
  131. struct multi_point_single_geometry
  132. {
  133. static const bool interruption_enabled = true;
  134. template <typename Result, typename Strategy>
  135. static inline void apply(MultiPoint const& multi_point,
  136. SingleGeometry const& single_geometry,
  137. Result & result,
  138. Strategy const& strategy)
  139. {
  140. typedef typename point_type<SingleGeometry>::type point2_type;
  141. typedef model::box<point2_type> box2_type;
  142. typedef typename Strategy::equals_point_point_strategy_type eq_pp_strategy_type;
  143. typedef typename Strategy::disjoint_point_box_strategy_type d_pb_strategy_type;
  144. box2_type box2;
  145. geometry::envelope(single_geometry, box2, strategy.get_envelope_strategy());
  146. geometry::detail::expand_by_epsilon(box2);
  147. typedef typename boost::range_const_iterator<MultiPoint>::type iterator;
  148. for ( iterator it = boost::begin(multi_point) ; it != boost::end(multi_point) ; ++it )
  149. {
  150. if (! (relate::may_update<interior, interior, '0', Transpose>(result)
  151. || relate::may_update<interior, boundary, '0', Transpose>(result)
  152. || relate::may_update<interior, exterior, '0', Transpose>(result) ) )
  153. {
  154. break;
  155. }
  156. // The default strategy is enough for Point/Box
  157. if (detail::disjoint::disjoint_point_box(*it, box2, d_pb_strategy_type()))
  158. {
  159. relate::set<interior, exterior, '0', Transpose>(result);
  160. }
  161. else
  162. {
  163. int in_val = detail::within::point_in_geometry(*it, single_geometry, strategy);
  164. if (in_val > 0) // within
  165. {
  166. relate::set<interior, interior, '0', Transpose>(result);
  167. }
  168. else if (in_val == 0)
  169. {
  170. relate::set<interior, boundary, '0', Transpose>(result);
  171. }
  172. else // in_val < 0 - not within
  173. {
  174. relate::set<interior, exterior, '0', Transpose>(result);
  175. }
  176. }
  177. if ( BOOST_GEOMETRY_CONDITION(result.interrupt) )
  178. {
  179. return;
  180. }
  181. }
  182. typedef detail::relate::topology_check
  183. <
  184. SingleGeometry, eq_pp_strategy_type
  185. > tc_t;
  186. if ( relate::may_update<exterior, interior, tc_t::interior, Transpose>(result)
  187. || relate::may_update<exterior, boundary, tc_t::boundary, Transpose>(result) )
  188. {
  189. tc_t tc(single_geometry);
  190. if ( relate::may_update<exterior, interior, tc_t::interior, Transpose>(result)
  191. && tc.has_interior() )
  192. {
  193. // TODO: this is not true if a linestring is degenerated to a point
  194. // then the interior has topological dimension = 0, not 1
  195. relate::set<exterior, interior, tc_t::interior, Transpose>(result);
  196. }
  197. if ( relate::may_update<exterior, boundary, tc_t::boundary, Transpose>(result)
  198. && tc.has_boundary() )
  199. {
  200. if (multi_point_geometry_eb
  201. <
  202. SingleGeometry, eq_pp_strategy_type
  203. >::apply(multi_point, tc))
  204. {
  205. relate::set<exterior, boundary, tc_t::boundary, Transpose>(result);
  206. }
  207. }
  208. }
  209. relate::set<exterior, exterior, result_dimension<MultiPoint>::value, Transpose>(result);
  210. }
  211. };
  212. // MultiGeometry - Linear or Areal
  213. // part of the algorithm calculating II and IB when no IE has to be calculated
  214. // using partition()
  215. template <typename MultiPoint, typename MultiGeometry, bool Transpose>
  216. class multi_point_multi_geometry_ii_ib
  217. {
  218. template <typename ExpandPointStrategy>
  219. struct expand_box_point
  220. {
  221. template <typename Box, typename Point>
  222. static inline void apply(Box& total, Point const& point)
  223. {
  224. geometry::expand(total, point, ExpandPointStrategy());
  225. }
  226. };
  227. template <typename ExpandBoxStrategy>
  228. struct expand_box_box_pair
  229. {
  230. template <typename Box, typename BoxPair>
  231. static inline void apply(Box& total, BoxPair const& box_pair)
  232. {
  233. geometry::expand(total, box_pair.first, ExpandBoxStrategy());
  234. }
  235. };
  236. template <typename DisjointPointBoxStrategy>
  237. struct overlaps_box_point
  238. {
  239. template <typename Box, typename Point>
  240. static inline bool apply(Box const& box, Point const& point)
  241. {
  242. // The default strategy is enough for Point/Box
  243. return ! detail::disjoint::disjoint_point_box(point, box,
  244. DisjointPointBoxStrategy());
  245. }
  246. };
  247. template <typename DisjointBoxBoxStrategy>
  248. struct overlaps_box_box_pair
  249. {
  250. template <typename Box, typename BoxPair>
  251. static inline bool apply(Box const& box, BoxPair const& box_pair)
  252. {
  253. // The default strategy is enough for Box/Box
  254. return ! detail::disjoint::disjoint_box_box(box_pair.first, box,
  255. DisjointBoxBoxStrategy());
  256. }
  257. };
  258. template <typename Result, typename PtSegStrategy>
  259. class item_visitor_type
  260. {
  261. typedef typename PtSegStrategy::equals_point_point_strategy_type pp_strategy_type;
  262. typedef typename PtSegStrategy::disjoint_point_box_strategy_type d_pp_strategy_type;
  263. typedef detail::relate::topology_check<MultiGeometry, pp_strategy_type> topology_check_type;
  264. public:
  265. item_visitor_type(MultiGeometry const& multi_geometry,
  266. topology_check_type const& tc,
  267. Result & result,
  268. PtSegStrategy const& strategy)
  269. : m_multi_geometry(multi_geometry)
  270. , m_tc(tc)
  271. , m_result(result)
  272. , m_strategy(strategy)
  273. {}
  274. template <typename Point, typename BoxPair>
  275. inline bool apply(Point const& point, BoxPair const& box_pair)
  276. {
  277. // The default strategy is enough for Point/Box
  278. if (! detail::disjoint::disjoint_point_box(point, box_pair.first, d_pp_strategy_type()))
  279. {
  280. typename boost::range_value<MultiGeometry>::type const&
  281. single = range::at(m_multi_geometry, box_pair.second);
  282. int in_val = detail::within::point_in_geometry(point, single, m_strategy);
  283. if (in_val > 0) // within
  284. {
  285. relate::set<interior, interior, '0', Transpose>(m_result);
  286. }
  287. else if (in_val == 0)
  288. {
  289. if (m_tc.check_boundary_point(point))
  290. relate::set<interior, boundary, '0', Transpose>(m_result);
  291. else
  292. relate::set<interior, interior, '0', Transpose>(m_result);
  293. }
  294. }
  295. if ( BOOST_GEOMETRY_CONDITION(m_result.interrupt) )
  296. {
  297. return false;
  298. }
  299. if (! (relate::may_update<interior, interior, '0', Transpose>(m_result)
  300. || relate::may_update<interior, boundary, '0', Transpose>(m_result) ) )
  301. {
  302. return false;
  303. }
  304. return true;
  305. }
  306. private:
  307. MultiGeometry const& m_multi_geometry;
  308. topology_check_type const& m_tc;
  309. Result & m_result;
  310. PtSegStrategy const& m_strategy;
  311. };
  312. public:
  313. typedef typename point_type<MultiPoint>::type point1_type;
  314. typedef typename point_type<MultiGeometry>::type point2_type;
  315. typedef model::box<point1_type> box1_type;
  316. typedef model::box<point2_type> box2_type;
  317. typedef std::pair<box2_type, std::size_t> box_pair_type;
  318. template <typename Result, typename Strategy>
  319. static inline void apply(MultiPoint const& multi_point,
  320. MultiGeometry const& multi_geometry,
  321. std::vector<box_pair_type> const& boxes,
  322. detail::relate::topology_check
  323. <
  324. MultiGeometry,
  325. typename Strategy::equals_point_point_strategy_type
  326. > const& tc,
  327. Result & result,
  328. Strategy const& strategy)
  329. {
  330. item_visitor_type<Result, Strategy> visitor(multi_geometry, tc, result, strategy);
  331. typedef expand_box_point
  332. <
  333. typename Strategy::expand_point_strategy_type
  334. > expand_box_point_type;
  335. typedef overlaps_box_point
  336. <
  337. typename Strategy::disjoint_point_box_strategy_type
  338. > overlaps_box_point_type;
  339. typedef expand_box_box_pair
  340. <
  341. typename Strategy::envelope_strategy_type::box_expand_strategy_type
  342. > expand_box_box_pair_type;
  343. typedef overlaps_box_box_pair
  344. <
  345. typename Strategy::disjoint_box_box_strategy_type
  346. > overlaps_box_box_pair_type;
  347. geometry::partition
  348. <
  349. box1_type
  350. >::apply(multi_point, boxes, visitor,
  351. expand_box_point_type(),
  352. overlaps_box_point_type(),
  353. expand_box_box_pair_type(),
  354. overlaps_box_box_pair_type());
  355. }
  356. };
  357. // MultiGeometry - Linear or Areal
  358. // part of the algorithm calculating II, IB and IE
  359. // using rtree
  360. template <typename MultiPoint, typename MultiGeometry, bool Transpose>
  361. struct multi_point_multi_geometry_ii_ib_ie
  362. {
  363. typedef typename point_type<MultiPoint>::type point1_type;
  364. typedef typename point_type<MultiGeometry>::type point2_type;
  365. typedef model::box<point1_type> box1_type;
  366. typedef model::box<point2_type> box2_type;
  367. typedef std::pair<box2_type, std::size_t> box_pair_type;
  368. typedef std::vector<box_pair_type> boxes_type;
  369. typedef typename boxes_type::const_iterator boxes_iterator;
  370. template <typename Result, typename Strategy>
  371. static inline void apply(MultiPoint const& multi_point,
  372. MultiGeometry const& multi_geometry,
  373. std::vector<box_pair_type> const& boxes,
  374. detail::relate::topology_check
  375. <
  376. MultiGeometry,
  377. typename Strategy::equals_point_point_strategy_type
  378. > const& tc,
  379. Result & result,
  380. Strategy const& strategy)
  381. {
  382. typedef strategy::index::services::from_strategy
  383. <
  384. Strategy
  385. > index_strategy_from;
  386. typedef index::parameters
  387. <
  388. index::rstar<4>, typename index_strategy_from::type
  389. > index_parameters_type;
  390. index::rtree<box_pair_type, index_parameters_type>
  391. rtree(boxes.begin(), boxes.end(),
  392. index_parameters_type(index::rstar<4>(), index_strategy_from::get(strategy)));
  393. typedef typename boost::range_const_iterator<MultiPoint>::type iterator;
  394. for ( iterator it = boost::begin(multi_point) ; it != boost::end(multi_point) ; ++it )
  395. {
  396. if (! (relate::may_update<interior, interior, '0', Transpose>(result)
  397. || relate::may_update<interior, boundary, '0', Transpose>(result)
  398. || relate::may_update<interior, exterior, '0', Transpose>(result) ) )
  399. {
  400. return;
  401. }
  402. typename boost::range_value<MultiPoint>::type const& point = *it;
  403. boxes_type boxes_found;
  404. rtree.query(index::intersects(point), std::back_inserter(boxes_found));
  405. bool found_ii_or_ib = false;
  406. for (boxes_iterator bi = boxes_found.begin() ; bi != boxes_found.end() ; ++bi)
  407. {
  408. typename boost::range_value<MultiGeometry>::type const&
  409. single = range::at(multi_geometry, bi->second);
  410. int in_val = detail::within::point_in_geometry(point, single, strategy);
  411. if (in_val > 0) // within
  412. {
  413. relate::set<interior, interior, '0', Transpose>(result);
  414. found_ii_or_ib = true;
  415. }
  416. else if (in_val == 0) // on boundary of single
  417. {
  418. if (tc.check_boundary_point(point))
  419. relate::set<interior, boundary, '0', Transpose>(result);
  420. else
  421. relate::set<interior, interior, '0', Transpose>(result);
  422. found_ii_or_ib = true;
  423. }
  424. }
  425. // neither interior nor boundary found -> exterior
  426. if (found_ii_or_ib == false)
  427. {
  428. relate::set<interior, exterior, '0', Transpose>(result);
  429. }
  430. if ( BOOST_GEOMETRY_CONDITION(result.interrupt) )
  431. {
  432. return;
  433. }
  434. }
  435. }
  436. };
  437. // MultiGeometry - Linear or Areal
  438. template <typename MultiPoint, typename MultiGeometry, bool Transpose = false>
  439. struct multi_point_multi_geometry
  440. {
  441. static const bool interruption_enabled = true;
  442. template <typename Result, typename Strategy>
  443. static inline void apply(MultiPoint const& multi_point,
  444. MultiGeometry const& multi_geometry,
  445. Result & result,
  446. Strategy const& strategy)
  447. {
  448. typedef typename point_type<MultiGeometry>::type point2_type;
  449. typedef model::box<point2_type> box2_type;
  450. typedef std::pair<box2_type, std::size_t> box_pair_type;
  451. typedef typename Strategy::equals_point_point_strategy_type eq_pp_strategy_type;
  452. typename Strategy::envelope_strategy_type const
  453. envelope_strategy = strategy.get_envelope_strategy();
  454. std::size_t count2 = boost::size(multi_geometry);
  455. std::vector<box_pair_type> boxes(count2);
  456. for (std::size_t i = 0 ; i < count2 ; ++i)
  457. {
  458. geometry::envelope(range::at(multi_geometry, i), boxes[i].first, envelope_strategy);
  459. geometry::detail::expand_by_epsilon(boxes[i].first);
  460. boxes[i].second = i;
  461. }
  462. typedef detail::relate::topology_check<MultiGeometry, eq_pp_strategy_type> tc_t;
  463. tc_t tc(multi_geometry);
  464. if ( relate::may_update<interior, interior, '0', Transpose>(result)
  465. || relate::may_update<interior, boundary, '0', Transpose>(result)
  466. || relate::may_update<interior, exterior, '0', Transpose>(result) )
  467. {
  468. // If there is no need to calculate IE, use partition
  469. if (! relate::may_update<interior, exterior, '0', Transpose>(result) )
  470. {
  471. multi_point_multi_geometry_ii_ib<MultiPoint, MultiGeometry, Transpose>
  472. ::apply(multi_point, multi_geometry, boxes, tc, result, strategy);
  473. }
  474. else // otherwise use rtree
  475. {
  476. multi_point_multi_geometry_ii_ib_ie<MultiPoint, MultiGeometry, Transpose>
  477. ::apply(multi_point, multi_geometry, boxes, tc, result, strategy);
  478. }
  479. }
  480. if ( BOOST_GEOMETRY_CONDITION(result.interrupt) )
  481. {
  482. return;
  483. }
  484. if ( relate::may_update<exterior, interior, tc_t::interior, Transpose>(result)
  485. || relate::may_update<exterior, boundary, tc_t::boundary, Transpose>(result) )
  486. {
  487. if ( relate::may_update<exterior, interior, tc_t::interior, Transpose>(result)
  488. && tc.has_interior() )
  489. {
  490. // TODO: this is not true if a linestring is degenerated to a point
  491. // then the interior has topological dimension = 0, not 1
  492. relate::set<exterior, interior, tc_t::interior, Transpose>(result);
  493. }
  494. if ( relate::may_update<exterior, boundary, tc_t::boundary, Transpose>(result)
  495. && tc.has_boundary() )
  496. {
  497. if (multi_point_geometry_eb
  498. <
  499. MultiGeometry, eq_pp_strategy_type
  500. >::apply(multi_point, tc))
  501. {
  502. relate::set<exterior, boundary, tc_t::boundary, Transpose>(result);
  503. }
  504. }
  505. }
  506. relate::set<exterior, exterior, result_dimension<MultiPoint>::value, Transpose>(result);
  507. }
  508. };
  509. template
  510. <
  511. typename MultiPoint, typename Geometry,
  512. bool Transpose = false,
  513. bool isMulti = boost::is_same
  514. <
  515. typename tag_cast
  516. <
  517. typename tag<Geometry>::type, multi_tag
  518. >::type,
  519. multi_tag
  520. >::value
  521. >
  522. struct multi_point_geometry
  523. : multi_point_single_geometry<MultiPoint, Geometry, Transpose>
  524. {};
  525. template <typename MultiPoint, typename Geometry, bool Transpose>
  526. struct multi_point_geometry<MultiPoint, Geometry, Transpose, true>
  527. : multi_point_multi_geometry<MultiPoint, Geometry, Transpose>
  528. {};
  529. // transposed result of multi_point_geometry
  530. template <typename Geometry, typename MultiPoint>
  531. struct geometry_multi_point
  532. {
  533. static const bool interruption_enabled = true;
  534. template <typename Result, typename Strategy>
  535. static inline void apply(Geometry const& geometry, MultiPoint const& multi_point,
  536. Result & result, Strategy const& strategy)
  537. {
  538. multi_point_geometry<MultiPoint, Geometry, true>::apply(multi_point, geometry, result, strategy);
  539. }
  540. };
  541. }} // namespace detail::relate
  542. #endif // DOXYGEN_NO_DETAIL
  543. }} // namespace boost::geometry
  544. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_RELATE_MULTI_POINT_GEOMETRY_HPP