1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2014-2017, Oracle and/or its affiliates.
4 // Copyright (c) 2017 Adam Wulkiewicz, Lodz, Poland.
6 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
7 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
9 // Licensed under the Boost Software License version 1.0.
10 // http://www.boost.org/users/license.html
12 #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_DISJOINT_MULTIPOINT_GEOMETRY_HPP
13 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_DISJOINT_MULTIPOINT_GEOMETRY_HPP
18 #include <boost/range.hpp>
19 #include <boost/mpl/assert.hpp>
21 #include <boost/geometry/core/assert.hpp>
22 #include <boost/geometry/core/tag.hpp>
23 #include <boost/geometry/core/tags.hpp>
25 #include <boost/geometry/geometries/box.hpp>
27 #include <boost/geometry/iterators/segment_iterator.hpp>
29 #include <boost/geometry/algorithms/envelope.hpp>
30 #include <boost/geometry/algorithms/expand.hpp>
32 #include <boost/geometry/algorithms/detail/check_iterator_range.hpp>
33 #include <boost/geometry/algorithms/detail/partition.hpp>
34 #include <boost/geometry/algorithms/detail/disjoint/box_box.hpp>
35 #include <boost/geometry/algorithms/detail/disjoint/multirange_geometry.hpp>
36 #include <boost/geometry/algorithms/detail/disjoint/point_box.hpp>
37 #include <boost/geometry/algorithms/detail/disjoint/point_point.hpp>
38 #include <boost/geometry/algorithms/detail/disjoint/point_geometry.hpp>
40 #include <boost/geometry/algorithms/dispatch/disjoint.hpp>
42 #include <boost/geometry/policies/compare.hpp>
45 namespace boost { namespace geometry
49 #ifndef DOXYGEN_NO_DETAIL
50 namespace detail { namespace disjoint
54 template <typename MultiPoint1, typename MultiPoint2>
55 class multipoint_multipoint
58 template <typename Iterator>
59 class unary_disjoint_predicate
63 typedef geometry::less<> base_type;
66 unary_disjoint_predicate(Iterator first, Iterator last)
67 : base_type(), m_first(first), m_last(last)
70 template <typename Point>
71 inline bool apply(Point const& point) const
73 return !std::binary_search(m_first,
76 static_cast<base_type const&>(*this));
80 Iterator m_first, m_last;
84 static inline bool apply(MultiPoint1 const& multipoint1,
85 MultiPoint2 const& multipoint2)
87 BOOST_GEOMETRY_ASSERT( boost::size(multipoint1) <= boost::size(multipoint2) );
89 typedef typename boost::range_value<MultiPoint1>::type point1_type;
91 std::vector<point1_type> points1(boost::begin(multipoint1),
92 boost::end(multipoint1));
94 std::sort(points1.begin(), points1.end(), geometry::less<>());
96 typedef unary_disjoint_predicate
98 typename std::vector<point1_type>::const_iterator
101 return check_iterator_range
104 >::apply(boost::begin(multipoint2),
105 boost::end(multipoint2),
106 predicate_type(points1.begin(), points1.end()));
111 template <typename MultiPoint, typename Linear>
112 class multipoint_linear
115 struct expand_box_point
117 template <typename Box, typename Point>
118 static inline void apply(Box& total, Point const& point)
120 geometry::expand(total, point);
124 template <typename EnvelopeStrategy>
125 struct expand_box_segment
127 explicit expand_box_segment(EnvelopeStrategy const& strategy)
128 : m_strategy(strategy)
131 template <typename Box, typename Segment>
132 inline void apply(Box& total, Segment const& segment) const
134 geometry::expand(total,
135 geometry::return_envelope<Box>(segment, m_strategy));
138 EnvelopeStrategy const& m_strategy;
141 struct overlaps_box_point
143 template <typename Box, typename Point>
144 static inline bool apply(Box const& box, Point const& point)
146 // The default strategy is enough in this case
147 return ! detail::disjoint::disjoint_point_box(point, box);
151 template <typename DisjointStrategy>
152 struct overlaps_box_segment
154 explicit overlaps_box_segment(DisjointStrategy const& strategy)
155 : m_strategy(strategy)
158 template <typename Box, typename Segment>
159 inline bool apply(Box const& box, Segment const& segment) const
161 return ! dispatch::disjoint<Segment, Box>::apply(segment, box, m_strategy);
164 DisjointStrategy const& m_strategy;
167 template <typename PtSegStrategy>
168 class item_visitor_type
171 item_visitor_type(PtSegStrategy const& strategy)
172 : m_intersection_found(false)
173 , m_strategy(strategy)
176 template <typename Item1, typename Item2>
177 inline bool apply(Item1 const& item1, Item2 const& item2)
179 if (! m_intersection_found
180 && ! dispatch::disjoint<Item1, Item2>::apply(item1, item2, m_strategy))
182 m_intersection_found = true;
188 inline bool intersection_found() const { return m_intersection_found; }
191 bool m_intersection_found;
192 PtSegStrategy const& m_strategy;
194 // structs for partition -- end
199 typedef geometry::segment_iterator<Linear const> const_iterator;
200 typedef const_iterator iterator;
202 segment_range(Linear const& linear)
206 const_iterator begin() const
208 return geometry::segments_begin(m_linear);
211 const_iterator end() const
213 return geometry::segments_end(m_linear);
217 Linear const& m_linear;
221 template <typename Strategy>
222 static inline bool apply(MultiPoint const& multipoint, Linear const& linear, Strategy const& strategy)
224 item_visitor_type<Strategy> visitor(strategy);
226 typedef typename Strategy::envelope_strategy_type envelope_strategy_type;
227 typedef typename Strategy::disjoint_strategy_type disjoint_strategy_type;
229 // TODO: disjoint Segment/Box may be called in partition multiple times
230 // possibly for non-cartesian segments which could be slow. We should consider
231 // passing a range of bounding boxes of segments after calculating them once.
232 // Alternatively instead of a range of segments a range of Segment/Envelope pairs
233 // should be passed, where envelope would be lazily calculated when needed the first time
236 geometry::model::box<typename point_type<MultiPoint>::type>
237 >::apply(multipoint, segment_range(linear), visitor,
239 overlaps_box_point(),
240 expand_box_segment<envelope_strategy_type>(strategy.get_envelope_strategy()),
241 overlaps_box_segment<disjoint_strategy_type>(strategy.get_disjoint_strategy()));
243 return ! visitor.intersection_found();
246 template <typename Strategy>
247 static inline bool apply(Linear const& linear, MultiPoint const& multipoint, Strategy const& strategy)
249 return apply(multipoint, linear, strategy);
254 template <typename MultiPoint, typename SingleGeometry>
255 class multi_point_single_geometry
258 template <typename Strategy>
259 static inline bool apply(MultiPoint const& multi_point, SingleGeometry const& single_geometry, Strategy const& strategy)
261 typedef typename point_type<MultiPoint>::type point1_type;
262 typedef typename point_type<SingleGeometry>::type point2_type;
263 typedef model::box<point2_type> box2_type;
266 geometry::envelope(single_geometry, box2, strategy.get_envelope_strategy());
267 geometry::detail::expand_by_epsilon(box2);
269 typedef typename boost::range_const_iterator<MultiPoint>::type iterator;
270 for ( iterator it = boost::begin(multi_point) ; it != boost::end(multi_point) ; ++it )
272 // The default strategy is enough for Point/Box
273 if (! detail::disjoint::disjoint_point_box(*it, box2)
274 && ! dispatch::disjoint<point1_type, SingleGeometry>::apply(*it, single_geometry, strategy))
283 template <typename Strategy>
284 static inline bool apply(SingleGeometry const& single_geometry, MultiPoint const& multi_point, Strategy const& strategy)
286 return apply(multi_point, single_geometry, strategy);
291 template <typename MultiPoint, typename MultiGeometry>
292 class multi_point_multi_geometry
295 struct expand_box_point
297 template <typename Box, typename Point>
298 static inline void apply(Box& total, Point const& point)
300 geometry::expand(total, point);
304 struct expand_box_box_pair
306 template <typename Box, typename BoxPair>
307 inline void apply(Box& total, BoxPair const& box_pair) const
309 geometry::expand(total, box_pair.first);
313 struct overlaps_box_point
315 template <typename Box, typename Point>
316 static inline bool apply(Box const& box, Point const& point)
318 // The default strategy is enough for Point/Box
319 return ! detail::disjoint::disjoint_point_box(point, box);
323 struct overlaps_box_box_pair
325 template <typename Box, typename BoxPair>
326 inline bool apply(Box const& box, BoxPair const& box_pair) const
328 // The default strategy is enough for Box/Box
329 return ! detail::disjoint::disjoint_box_box(box_pair.first, box);
333 template <typename PtSegStrategy>
334 class item_visitor_type
337 item_visitor_type(MultiGeometry const& multi_geometry,
338 PtSegStrategy const& strategy)
339 : m_intersection_found(false)
340 , m_multi_geometry(multi_geometry)
341 , m_strategy(strategy)
344 template <typename Point, typename BoxPair>
345 inline bool apply(Point const& point, BoxPair const& box_pair)
347 typedef typename boost::range_value<MultiGeometry>::type single_type;
349 // The default strategy is enough for Point/Box
350 if (! m_intersection_found
351 && ! detail::disjoint::disjoint_point_box(point, box_pair.first)
352 && ! dispatch::disjoint<Point, single_type>::apply(point, range::at(m_multi_geometry, box_pair.second), m_strategy))
354 m_intersection_found = true;
360 inline bool intersection_found() const { return m_intersection_found; }
363 bool m_intersection_found;
364 MultiGeometry const& m_multi_geometry;
365 PtSegStrategy const& m_strategy;
367 // structs for partition -- end
370 template <typename Strategy>
371 static inline bool apply(MultiPoint const& multi_point, MultiGeometry const& multi_geometry, Strategy const& strategy)
373 typedef typename point_type<MultiPoint>::type point1_type;
374 typedef typename point_type<MultiGeometry>::type point2_type;
375 typedef model::box<point1_type> box1_type;
376 typedef model::box<point2_type> box2_type;
377 typedef std::pair<box2_type, std::size_t> box_pair_type;
379 typename Strategy::envelope_strategy_type const
380 envelope_strategy = strategy.get_envelope_strategy();
382 std::size_t count2 = boost::size(multi_geometry);
383 std::vector<box_pair_type> boxes(count2);
384 for (std::size_t i = 0 ; i < count2 ; ++i)
386 geometry::envelope(range::at(multi_geometry, i), boxes[i].first, envelope_strategy);
387 geometry::detail::expand_by_epsilon(boxes[i].first);
391 item_visitor_type<Strategy> visitor(multi_geometry, strategy);
396 >::apply(multi_point, boxes, visitor,
398 overlaps_box_point(),
399 expand_box_box_pair(),
400 overlaps_box_box_pair());
402 return ! visitor.intersection_found();
405 template <typename Strategy>
406 static inline bool apply(MultiGeometry const& multi_geometry, MultiPoint const& multi_point, Strategy const& strategy)
408 return apply(multi_point, multi_geometry, strategy);
413 template <typename MultiPoint, typename Areal, typename Tag = typename tag<Areal>::type>
414 struct multipoint_areal
415 : multi_point_single_geometry<MultiPoint, Areal>
418 template <typename MultiPoint, typename Areal>
419 struct multipoint_areal<MultiPoint, Areal, multi_polygon_tag>
420 : multi_point_multi_geometry<MultiPoint, Areal>
424 }} // namespace detail::disjoint
425 #endif // DOXYGEN_NO_DETAIL
430 #ifndef DOXYGEN_NO_DISPATCH
435 template <typename Point, typename MultiPoint, std::size_t DimensionCount>
438 Point, MultiPoint, DimensionCount, point_tag, multi_point_tag, false
439 > : detail::disjoint::multirange_constant_size_geometry<MultiPoint, Point>
443 template <typename MultiPoint, typename Segment, std::size_t DimensionCount>
446 MultiPoint, Segment, DimensionCount, multi_point_tag, segment_tag, false
447 > : detail::disjoint::multirange_constant_size_geometry<MultiPoint, Segment>
451 template <typename MultiPoint, typename Box, std::size_t DimensionCount>
454 MultiPoint, Box, DimensionCount, multi_point_tag, box_tag, false
455 > : detail::disjoint::multirange_constant_size_geometry<MultiPoint, Box>
461 typename MultiPoint1,
462 typename MultiPoint2,
463 std::size_t DimensionCount
467 MultiPoint1, MultiPoint2, DimensionCount,
468 multi_point_tag, multi_point_tag, false
471 template <typename Strategy>
472 static inline bool apply(MultiPoint1 const& multipoint1,
473 MultiPoint2 const& multipoint2,
476 if ( boost::size(multipoint2) < boost::size(multipoint1) )
478 return detail::disjoint::multipoint_multipoint
480 MultiPoint2, MultiPoint1
481 >::apply(multipoint2, multipoint1);
484 return detail::disjoint::multipoint_multipoint
486 MultiPoint1, MultiPoint2
487 >::apply(multipoint1, multipoint2);
492 template <typename Linear, typename MultiPoint, std::size_t DimensionCount>
495 Linear, MultiPoint, DimensionCount, linear_tag, multi_point_tag, false
496 > : detail::disjoint::multipoint_linear<MultiPoint, Linear>
500 template <typename MultiPoint, typename Linear, std::size_t DimensionCount>
503 MultiPoint, Linear, DimensionCount, multi_point_tag, linear_tag, false
504 > : detail::disjoint::multipoint_linear<MultiPoint, Linear>
508 template <typename Areal, typename MultiPoint, std::size_t DimensionCount>
511 Areal, MultiPoint, DimensionCount, areal_tag, multi_point_tag, false
512 > : detail::disjoint::multipoint_areal<MultiPoint, Areal>
516 template <typename MultiPoint, typename Areal, std::size_t DimensionCount>
519 MultiPoint, Areal, DimensionCount, multi_point_tag, areal_tag, false
520 > : detail::disjoint::multipoint_areal<MultiPoint, Areal>
524 } // namespace dispatch
525 #endif // DOXYGEN_NO_DISPATCH
528 }} // namespace boost::geometry
532 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_DISJOINT_MULTIPOINT_GEOMETRY_HPP