1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2014-2017, Oracle and/or its affiliates.
5 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
6 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
8 // Licensed under the Boost Software License version 1.0.
9 // http://www.boost.org/users/license.html
12 #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_POINTLIKE_POINTLIKE_HPP
13 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_POINTLIKE_POINTLIKE_HPP
18 #include <boost/range.hpp>
20 #include <boost/geometry/core/assert.hpp>
21 #include <boost/geometry/core/point_type.hpp>
22 #include <boost/geometry/core/tag.hpp>
23 #include <boost/geometry/core/tags.hpp>
25 #include <boost/geometry/algorithms/convert.hpp>
26 #include <boost/geometry/algorithms/not_implemented.hpp>
28 #include <boost/geometry/algorithms/detail/equals/point_point.hpp>
29 #include <boost/geometry/algorithms/detail/overlay/overlay_type.hpp>
31 #include <boost/geometry/policies/compare.hpp>
34 namespace boost { namespace geometry
38 #ifndef DOXYGEN_NO_DETAIL
39 namespace detail { namespace overlay
43 // struct for copying points of the pointlike geometries to output
48 typename TagIn = typename tag<GeometryIn>::type
51 : not_implemented<PointOut, GeometryIn>
54 template <typename PointOut, typename PointIn>
55 struct copy_points<PointOut, PointIn, point_tag>
57 template <typename OutputIterator>
58 static inline void apply(PointIn const& point_in,
62 geometry::convert(point_in, point_out);
68 template <typename PointOut, typename MultiPointIn>
69 struct copy_points<PointOut, MultiPointIn, multi_point_tag>
71 template <typename OutputIterator>
72 static inline void apply(MultiPointIn const& multi_point_in,
75 for (typename boost::range_iterator<MultiPointIn const>::type
76 it = boost::begin(multi_point_in);
77 it != boost::end(multi_point_in); ++it)
80 geometry::convert(*it, point_out);
88 // action struct for difference/intersection
89 template <typename PointOut, overlay_type OverlayType>
90 struct action_selector_pl_pl
93 template <typename PointOut>
94 struct action_selector_pl_pl<PointOut, overlay_intersection>
99 typename OutputIterator
101 static inline void apply(Point const& point,
107 copy_points<PointOut, Point>::apply(point, oit);
114 template <typename PointOut>
115 struct action_selector_pl_pl<PointOut, overlay_difference>
120 typename OutputIterator
122 static inline void apply(Point const& point,
128 copy_points<PointOut, Point>::apply(point, oit);
134 //===========================================================================
136 // difference/intersection of point-point
142 overlay_type OverlayType
144 struct point_point_point
146 template <typename RobustPolicy, typename OutputIterator, typename Strategy>
147 static inline OutputIterator apply(Point1 const& point1,
148 Point2 const& point2,
149 RobustPolicy const& ,
153 action_selector_pl_pl
155 PointOut, OverlayType
157 detail::equals::equals_point_point(point1, point2),
166 // difference of multipoint-point
168 // the apply method in the following struct is called only for
169 // difference; for intersection the reversal will
170 // always call the point-multipoint version
176 overlay_type OverlayType
178 struct multipoint_point_point
180 template <typename RobustPolicy, typename OutputIterator, typename Strategy>
181 static inline OutputIterator apply(MultiPoint const& multipoint,
183 RobustPolicy const& ,
187 BOOST_GEOMETRY_ASSERT( OverlayType == overlay_difference );
189 for (typename boost::range_iterator<MultiPoint const>::type
190 it = boost::begin(multipoint);
191 it != boost::end(multipoint); ++it)
193 action_selector_pl_pl
195 PointOut, OverlayType
197 detail::equals::equals_point_point(*it, point),
206 // difference/intersection of point-multipoint
212 overlay_type OverlayType
214 struct point_multipoint_point
216 template <typename RobustPolicy, typename OutputIterator, typename Strategy>
217 static inline OutputIterator apply(Point const& point,
218 MultiPoint const& multipoint,
219 RobustPolicy const& ,
223 typedef action_selector_pl_pl<PointOut, OverlayType> action;
225 for (typename boost::range_iterator<MultiPoint const>::type
226 it = boost::begin(multipoint);
227 it != boost::end(multipoint); ++it)
229 if ( detail::equals::equals_point_point(*it, point) )
231 action::apply(point, true, oit);
236 action::apply(point, false, oit);
243 // difference/intersection of multipoint-multipoint
246 typename MultiPoint1,
247 typename MultiPoint2,
249 overlay_type OverlayType
251 struct multipoint_multipoint_point
253 template <typename RobustPolicy, typename OutputIterator, typename Strategy>
254 static inline OutputIterator apply(MultiPoint1 const& multipoint1,
255 MultiPoint2 const& multipoint2,
256 RobustPolicy const& robust_policy,
258 Strategy const& strategy)
260 if ( OverlayType != overlay_difference
261 && boost::size(multipoint1) > boost::size(multipoint2) )
263 return multipoint_multipoint_point
265 MultiPoint2, MultiPoint1, PointOut, OverlayType
266 >::apply(multipoint2, multipoint1, robust_policy, oit, strategy);
269 typedef typename boost::range_value<MultiPoint2>::type point2_type;
271 std::vector<point2_type> points2(boost::begin(multipoint2),
272 boost::end(multipoint2));
274 geometry::less<> const less = geometry::less<>();
275 std::sort(points2.begin(), points2.end(), less);
277 for (typename boost::range_iterator<MultiPoint1 const>::type
278 it1 = boost::begin(multipoint1);
279 it1 != boost::end(multipoint1); ++it1)
281 bool found = std::binary_search(points2.begin(), points2.end(),
284 action_selector_pl_pl
286 PointOut, OverlayType
287 >::apply(*it1, found, oit);
293 }} // namespace detail::overlay
294 #endif // DOXYGEN_NO_DETAIL
297 //===========================================================================
300 #ifndef DOXYGEN_NO_DISPATCH
301 namespace detail_dispatch { namespace overlay
304 // dispatch struct for pointlike-pointlike difference/intersection
311 overlay_type OverlayType,
315 struct pointlike_pointlike_point
316 : not_implemented<PointLike1, PointLike2, PointOut>
325 overlay_type OverlayType
327 struct pointlike_pointlike_point
329 Point1, Point2, PointOut, OverlayType,
331 > : detail::overlay::point_point_point
333 Point1, Point2, PointOut, OverlayType
343 overlay_type OverlayType
345 struct pointlike_pointlike_point
347 Point, MultiPoint, PointOut, OverlayType,
348 point_tag, multi_point_tag
349 > : detail::overlay::point_multipoint_point
351 Point, MultiPoint, PointOut, OverlayType
361 overlay_type OverlayType
363 struct pointlike_pointlike_point
365 MultiPoint, Point, PointOut, OverlayType,
366 multi_point_tag, point_tag
367 > : detail::overlay::multipoint_point_point
369 MultiPoint, Point, PointOut, OverlayType
376 typename MultiPoint1,
377 typename MultiPoint2,
379 overlay_type OverlayType
381 struct pointlike_pointlike_point
383 MultiPoint1, MultiPoint2, PointOut, OverlayType,
384 multi_point_tag, multi_point_tag
385 > : detail::overlay::multipoint_multipoint_point
387 MultiPoint1, MultiPoint2, PointOut, OverlayType
392 }} // namespace detail_dispatch::overlay
393 #endif // DOXYGEN_NO_DISPATCH
396 //===========================================================================
399 #ifndef DOXYGEN_NO_DETAIL
400 namespace detail { namespace overlay
404 // generic pointlike-pointlike union implementation
411 struct union_pointlike_pointlike_point
413 template <typename RobustPolicy, typename OutputIterator, typename Strategy>
414 static inline OutputIterator apply(PointLike1 const& pointlike1,
415 PointLike2 const& pointlike2,
416 RobustPolicy const& robust_policy,
418 Strategy const& strategy)
420 copy_points<PointOut, PointLike1>::apply(pointlike1, oit);
422 return detail_dispatch::overlay::pointlike_pointlike_point
424 PointLike2, PointLike1, PointOut, overlay_difference,
425 typename tag<PointLike2>::type,
426 typename tag<PointLike1>::type
427 >::apply(pointlike2, pointlike1, robust_policy, oit, strategy);
433 }} // namespace detail::overlay
434 #endif // DOXYGEN_NO_DETAIL
437 }} // namespace boost::geometry
440 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_POINTLIKE_POINTLIKE_HPP