3 // Copyright (c) 2017-2020 Oracle and/or its affiliates.
5 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
7 // Use, modification and distribution is subject to the Boost Software License,
8 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
11 #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_RELATE_MULTI_POINT_GEOMETRY_HPP
12 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_RELATE_MULTI_POINT_GEOMETRY_HPP
15 #include <boost/range/begin.hpp>
16 #include <boost/range/end.hpp>
17 #include <boost/range/size.hpp>
18 #include <boost/range/value_type.hpp>
20 #include <boost/geometry/algorithms/detail/disjoint/box_box.hpp>
21 #include <boost/geometry/algorithms/detail/disjoint/point_box.hpp>
22 #include <boost/geometry/algorithms/detail/expand_by_epsilon.hpp>
23 #include <boost/geometry/algorithms/detail/partition.hpp>
24 #include <boost/geometry/algorithms/detail/relate/result.hpp>
25 #include <boost/geometry/algorithms/detail/relate/topology_check.hpp>
26 #include <boost/geometry/algorithms/detail/within/point_in_geometry.hpp>
27 #include <boost/geometry/algorithms/envelope.hpp>
29 #include <boost/geometry/core/point_type.hpp>
31 #include <boost/geometry/geometries/box.hpp>
33 #include <boost/geometry/index/rtree.hpp>
36 #include <boost/geometry/strategies/envelope/cartesian.hpp>
37 #include <boost/geometry/strategies/envelope/geographic.hpp>
38 #include <boost/geometry/strategies/envelope/spherical.hpp>
40 #include <boost/geometry/util/type_traits.hpp>
43 namespace boost { namespace geometry
46 #ifndef DOXYGEN_NO_DETAIL
47 namespace detail { namespace relate
53 typename Tag = typename tag<Geometry>::type
55 struct multi_point_geometry_eb
57 template <typename MultiPoint, typename Strategy>
58 static inline bool apply(MultiPoint const& ,
59 detail::relate::topology_check<Geometry, Strategy> const& )
65 template <typename Geometry>
66 struct multi_point_geometry_eb<Geometry, linestring_tag>
68 template <typename Points>
69 struct boundary_visitor
71 boundary_visitor(Points const& points)
73 , m_boundary_found(false)
76 template <typename Point, typename Strategy>
79 find_pred(Point const& point, Strategy const& strategy)
81 , m_strategy(strategy)
84 template <typename Pt>
85 bool operator()(Pt const& pt) const
87 return detail::equals::equals_point_point(pt, m_point, m_strategy);
91 Strategy const& m_strategy;
94 template <typename Point, typename Strategy>
95 bool apply(Point const& boundary_point, Strategy const& strategy)
97 if ( std::none_of(m_points.begin(), m_points.end(),
98 find_pred<Point, Strategy>(boundary_point, strategy)))
100 m_boundary_found = true;
106 bool result() const { return m_boundary_found; }
109 Points const& m_points;
110 bool m_boundary_found;
113 template <typename MultiPoint, typename Strategy>
114 static inline bool apply(MultiPoint const& multi_point,
115 detail::relate::topology_check<Geometry, Strategy> const& tc)
117 boundary_visitor<MultiPoint> visitor(multi_point);
118 tc.for_each_boundary_point(visitor);
119 return visitor.result();
123 template <typename Geometry>
124 struct multi_point_geometry_eb<Geometry, multi_linestring_tag>
126 template <typename Points>
127 struct boundary_visitor
129 boundary_visitor(Points const& points)
131 , m_boundary_found(false)
134 template <typename Point, typename Strategy>
135 bool apply(Point const& boundary_point, Strategy const&)
137 typedef geometry::less<void, -1, typename Strategy::cs_tag> less_type;
139 if (! std::binary_search(m_points.begin(), m_points.end(),
140 boundary_point, less_type()) )
142 m_boundary_found = true;
148 bool result() const { return m_boundary_found; }
151 Points const& m_points;
152 bool m_boundary_found;
155 template <typename MultiPoint, typename Strategy>
156 static inline bool apply(MultiPoint const& multi_point,
157 detail::relate::topology_check<Geometry, Strategy> const& tc)
159 typedef typename boost::range_value<MultiPoint>::type point_type;
160 typedef std::vector<point_type> points_type;
161 typedef geometry::less<void, -1, typename Strategy::cs_tag> less_type;
163 points_type points(boost::begin(multi_point), boost::end(multi_point));
164 std::sort(points.begin(), points.end(), less_type());
166 boundary_visitor<points_type> visitor(points);
167 tc.for_each_boundary_point(visitor);
168 return visitor.result();
172 // SingleGeometry - Linear or Areal
173 template <typename MultiPoint, typename SingleGeometry, bool Transpose = false>
174 struct multi_point_single_geometry
176 static const bool interruption_enabled = true;
178 template <typename Result, typename Strategy>
179 static inline void apply(MultiPoint const& multi_point,
180 SingleGeometry const& single_geometry,
182 Strategy const& strategy)
184 typedef typename point_type<SingleGeometry>::type point2_type;
185 typedef model::box<point2_type> box2_type;
188 geometry::envelope(single_geometry, box2, strategy);
189 geometry::detail::expand_by_epsilon(box2);
191 typedef typename boost::range_const_iterator<MultiPoint>::type iterator;
192 for ( iterator it = boost::begin(multi_point) ; it != boost::end(multi_point) ; ++it )
194 if (! (relate::may_update<interior, interior, '0', Transpose>(result)
195 || relate::may_update<interior, boundary, '0', Transpose>(result)
196 || relate::may_update<interior, exterior, '0', Transpose>(result) ) )
201 // The default strategy is enough for Point/Box
202 if (detail::disjoint::disjoint_point_box(*it, box2, strategy))
204 relate::set<interior, exterior, '0', Transpose>(result);
208 int in_val = detail::within::point_in_geometry(*it, single_geometry, strategy);
210 if (in_val > 0) // within
212 relate::set<interior, interior, '0', Transpose>(result);
214 else if (in_val == 0)
216 relate::set<interior, boundary, '0', Transpose>(result);
218 else // in_val < 0 - not within
220 relate::set<interior, exterior, '0', Transpose>(result);
224 if ( BOOST_GEOMETRY_CONDITION(result.interrupt) )
230 typedef detail::relate::topology_check<SingleGeometry, Strategy> tc_t;
232 if ( relate::may_update<exterior, interior, tc_t::interior, Transpose>(result)
233 || relate::may_update<exterior, boundary, tc_t::boundary, Transpose>(result) )
235 tc_t tc(single_geometry, strategy);
237 if ( relate::may_update<exterior, interior, tc_t::interior, Transpose>(result)
238 && tc.has_interior() )
240 // TODO: this is not true if a linestring is degenerated to a point
241 // then the interior has topological dimension = 0, not 1
242 relate::set<exterior, interior, tc_t::interior, Transpose>(result);
245 if ( relate::may_update<exterior, boundary, tc_t::boundary, Transpose>(result)
246 && tc.has_boundary() )
248 if (multi_point_geometry_eb<SingleGeometry>::apply(multi_point, tc))
250 relate::set<exterior, boundary, tc_t::boundary, Transpose>(result);
255 relate::set<exterior, exterior, result_dimension<MultiPoint>::value, Transpose>(result);
260 // MultiGeometry - Linear or Areal
261 // part of the algorithm calculating II and IB when no IE has to be calculated
263 template <typename MultiPoint, typename MultiGeometry, bool Transpose>
264 class multi_point_multi_geometry_ii_ib
266 template <typename Strategy>
267 struct expand_box_point
269 expand_box_point(Strategy const& strategy)
270 : m_strategy(strategy)
273 template <typename Box, typename Point>
274 inline void apply(Box& total, Point const& point) const
276 geometry::expand(total, point, m_strategy);
280 Strategy const& m_strategy;
283 template <typename Strategy>
284 struct expand_box_box_pair
286 expand_box_box_pair(Strategy const& strategy)
287 : m_strategy(strategy)
290 template <typename Box, typename BoxPair>
291 inline void apply(Box& total, BoxPair const& box_pair) const
293 geometry::expand(total, box_pair.first, m_strategy);
297 Strategy const& m_strategy;
300 template <typename Strategy>
301 struct overlaps_box_point
303 overlaps_box_point(Strategy const& strategy)
304 : m_strategy(strategy)
307 template <typename Box, typename Point>
308 inline bool apply(Box const& box, Point const& point) const
310 // The default strategy is enough for Point/Box
311 return ! detail::disjoint::disjoint_point_box(point, box,
316 Strategy const& m_strategy;
319 template <typename Strategy>
320 struct overlaps_box_box_pair
322 overlaps_box_box_pair(Strategy const& strategy)
323 : m_strategy(strategy)
326 template <typename Box, typename BoxPair>
327 inline bool apply(Box const& box, BoxPair const& box_pair) const
329 // The default strategy is enough for Box/Box
330 return ! detail::disjoint::disjoint_box_box(box_pair.first, box,
335 Strategy const& m_strategy;
338 template <typename Result, typename Strategy>
339 class item_visitor_type
341 typedef detail::relate::topology_check<MultiGeometry, Strategy> topology_check_type;
344 item_visitor_type(MultiGeometry const& multi_geometry,
345 topology_check_type const& tc,
347 Strategy const& strategy)
348 : m_multi_geometry(multi_geometry)
351 , m_strategy(strategy)
354 template <typename Point, typename BoxPair>
355 inline bool apply(Point const& point, BoxPair const& box_pair)
357 // The default strategy is enough for Point/Box
358 if (! detail::disjoint::disjoint_point_box(point, box_pair.first, m_strategy) )
360 typename boost::range_value<MultiGeometry>::type const&
361 single = range::at(m_multi_geometry, box_pair.second);
363 int in_val = detail::within::point_in_geometry(point, single, m_strategy);
365 if (in_val > 0) // within
367 relate::set<interior, interior, '0', Transpose>(m_result);
369 else if (in_val == 0)
371 if (m_tc.check_boundary_point(point))
372 relate::set<interior, boundary, '0', Transpose>(m_result);
374 relate::set<interior, interior, '0', Transpose>(m_result);
378 if ( BOOST_GEOMETRY_CONDITION(m_result.interrupt) )
383 if (! (relate::may_update<interior, interior, '0', Transpose>(m_result)
384 || relate::may_update<interior, boundary, '0', Transpose>(m_result) ) )
394 MultiGeometry const& m_multi_geometry;
395 topology_check_type const& m_tc;
397 Strategy const& m_strategy;
401 typedef typename point_type<MultiPoint>::type point1_type;
402 typedef typename point_type<MultiGeometry>::type point2_type;
403 typedef model::box<point1_type> box1_type;
404 typedef model::box<point2_type> box2_type;
405 typedef std::pair<box2_type, std::size_t> box_pair_type;
407 template <typename Result, typename Strategy>
408 static inline void apply(MultiPoint const& multi_point,
409 MultiGeometry const& multi_geometry,
410 std::vector<box_pair_type> const& boxes,
411 detail::relate::topology_check
413 MultiGeometry, Strategy
416 Strategy const& strategy)
418 item_visitor_type<Result, Strategy> visitor(multi_geometry, tc, result, strategy);
423 >::apply(multi_point, boxes, visitor,
424 expand_box_point<Strategy>(strategy),
425 overlaps_box_point<Strategy>(strategy),
426 expand_box_box_pair<Strategy>(strategy),
427 overlaps_box_box_pair<Strategy>(strategy));
432 // MultiGeometry - Linear or Areal
433 // part of the algorithm calculating II, IB and IE
435 template <typename MultiPoint, typename MultiGeometry, bool Transpose>
436 struct multi_point_multi_geometry_ii_ib_ie
438 typedef typename point_type<MultiPoint>::type point1_type;
439 typedef typename point_type<MultiGeometry>::type point2_type;
440 typedef model::box<point1_type> box1_type;
441 typedef model::box<point2_type> box2_type;
442 typedef std::pair<box2_type, std::size_t> box_pair_type;
443 typedef std::vector<box_pair_type> boxes_type;
444 typedef typename boxes_type::const_iterator boxes_iterator;
446 template <typename Result, typename Strategy>
447 static inline void apply(MultiPoint const& multi_point,
448 MultiGeometry const& multi_geometry,
449 std::vector<box_pair_type> const& boxes,
450 detail::relate::topology_check
452 MultiGeometry, Strategy
455 Strategy const& strategy)
457 typedef index::parameters
459 index::rstar<4>, Strategy
460 > index_parameters_type;
461 index::rtree<box_pair_type, index_parameters_type>
462 rtree(boxes.begin(), boxes.end(),
463 index_parameters_type(index::rstar<4>(), strategy));
465 typedef typename boost::range_const_iterator<MultiPoint>::type iterator;
466 for ( iterator it = boost::begin(multi_point) ; it != boost::end(multi_point) ; ++it )
468 if (! (relate::may_update<interior, interior, '0', Transpose>(result)
469 || relate::may_update<interior, boundary, '0', Transpose>(result)
470 || relate::may_update<interior, exterior, '0', Transpose>(result) ) )
475 typename boost::range_value<MultiPoint>::type const& point = *it;
477 boxes_type boxes_found;
478 rtree.query(index::intersects(point), std::back_inserter(boxes_found));
480 bool found_ii_or_ib = false;
481 for (boxes_iterator bi = boxes_found.begin() ; bi != boxes_found.end() ; ++bi)
483 typename boost::range_value<MultiGeometry>::type const&
484 single = range::at(multi_geometry, bi->second);
486 int in_val = detail::within::point_in_geometry(point, single, strategy);
488 if (in_val > 0) // within
490 relate::set<interior, interior, '0', Transpose>(result);
491 found_ii_or_ib = true;
493 else if (in_val == 0) // on boundary of single
495 if (tc.check_boundary_point(point))
496 relate::set<interior, boundary, '0', Transpose>(result);
498 relate::set<interior, interior, '0', Transpose>(result);
499 found_ii_or_ib = true;
503 // neither interior nor boundary found -> exterior
504 if (found_ii_or_ib == false)
506 relate::set<interior, exterior, '0', Transpose>(result);
509 if ( BOOST_GEOMETRY_CONDITION(result.interrupt) )
517 // MultiGeometry - Linear or Areal
518 template <typename MultiPoint, typename MultiGeometry, bool Transpose = false>
519 struct multi_point_multi_geometry
521 static const bool interruption_enabled = true;
523 template <typename Result, typename Strategy>
524 static inline void apply(MultiPoint const& multi_point,
525 MultiGeometry const& multi_geometry,
527 Strategy const& strategy)
529 typedef typename point_type<MultiGeometry>::type point2_type;
530 typedef model::box<point2_type> box2_type;
531 typedef std::pair<box2_type, std::size_t> box_pair_type;
533 std::size_t count2 = boost::size(multi_geometry);
534 std::vector<box_pair_type> boxes(count2);
535 for (std::size_t i = 0 ; i < count2 ; ++i)
537 geometry::envelope(range::at(multi_geometry, i), boxes[i].first, strategy);
538 geometry::detail::expand_by_epsilon(boxes[i].first);
542 typedef detail::relate::topology_check<MultiGeometry, Strategy> tc_t;
543 tc_t tc(multi_geometry, strategy);
545 if ( relate::may_update<interior, interior, '0', Transpose>(result)
546 || relate::may_update<interior, boundary, '0', Transpose>(result)
547 || relate::may_update<interior, exterior, '0', Transpose>(result) )
549 // If there is no need to calculate IE, use partition
550 if (! relate::may_update<interior, exterior, '0', Transpose>(result) )
552 multi_point_multi_geometry_ii_ib<MultiPoint, MultiGeometry, Transpose>
553 ::apply(multi_point, multi_geometry, boxes, tc, result, strategy);
555 else // otherwise use rtree
557 multi_point_multi_geometry_ii_ib_ie<MultiPoint, MultiGeometry, Transpose>
558 ::apply(multi_point, multi_geometry, boxes, tc, result, strategy);
562 if ( BOOST_GEOMETRY_CONDITION(result.interrupt) )
567 if ( relate::may_update<exterior, interior, tc_t::interior, Transpose>(result)
568 || relate::may_update<exterior, boundary, tc_t::boundary, Transpose>(result) )
570 if ( relate::may_update<exterior, interior, tc_t::interior, Transpose>(result)
571 && tc.has_interior() )
573 // TODO: this is not true if a linestring is degenerated to a point
574 // then the interior has topological dimension = 0, not 1
575 relate::set<exterior, interior, tc_t::interior, Transpose>(result);
578 if ( relate::may_update<exterior, boundary, tc_t::boundary, Transpose>(result)
579 && tc.has_boundary() )
581 if (multi_point_geometry_eb<MultiGeometry>::apply(multi_point, tc))
583 relate::set<exterior, boundary, tc_t::boundary, Transpose>(result);
588 relate::set<exterior, exterior, result_dimension<MultiPoint>::value, Transpose>(result);
596 typename MultiPoint, typename Geometry,
597 bool Transpose = false,
598 bool isMulti = util::is_multi<Geometry>::value
600 struct multi_point_geometry
601 : multi_point_single_geometry<MultiPoint, Geometry, Transpose>
604 template <typename MultiPoint, typename Geometry, bool Transpose>
605 struct multi_point_geometry<MultiPoint, Geometry, Transpose, true>
606 : multi_point_multi_geometry<MultiPoint, Geometry, Transpose>
610 // transposed result of multi_point_geometry
611 template <typename Geometry, typename MultiPoint>
612 struct geometry_multi_point
614 static const bool interruption_enabled = true;
616 template <typename Result, typename Strategy>
617 static inline void apply(Geometry const& geometry, MultiPoint const& multi_point,
618 Result & result, Strategy const& strategy)
620 multi_point_geometry<MultiPoint, Geometry, true>::apply(multi_point, geometry, result, strategy);
624 }} // namespace detail::relate
625 #endif // DOXYGEN_NO_DETAIL
627 }} // namespace boost::geometry
629 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_RELATE_MULTI_POINT_GEOMETRY_HPP