1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2017 Adam Wulkiewicz, Lodz, Poland.
5 // Copyright (c) 2015-2019, Oracle and/or its affiliates.
7 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
8 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
10 // Licensed under the Boost Software License version 1.0.
11 // http://www.boost.org/users/license.html
14 #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_POINTLIKE_LINEAR_HPP
15 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_POINTLIKE_LINEAR_HPP
20 #include <boost/range.hpp>
22 #include <boost/geometry/core/tags.hpp>
24 #include <boost/geometry/geometries/box.hpp>
26 #include <boost/geometry/iterators/segment_iterator.hpp>
28 #include <boost/geometry/algorithms/disjoint.hpp>
29 #include <boost/geometry/algorithms/envelope.hpp>
30 #include <boost/geometry/algorithms/expand.hpp>
31 #include <boost/geometry/algorithms/not_implemented.hpp>
33 #include <boost/geometry/algorithms/detail/not.hpp>
34 #include <boost/geometry/algorithms/detail/partition.hpp>
35 #include <boost/geometry/algorithms/detail/disjoint/point_geometry.hpp>
36 #include <boost/geometry/algorithms/detail/equals/point_point.hpp>
37 #include <boost/geometry/algorithms/detail/overlay/overlay_type.hpp>
38 #include <boost/geometry/algorithms/detail/overlay/pointlike_pointlike.hpp>
41 namespace boost { namespace geometry
45 #ifndef DOXYGEN_NO_DETAIL
46 namespace detail { namespace overlay
50 // action struct for pointlike-linear difference/intersection
51 // it works the same as its pointlike-pointlike counterpart, hence the
53 template <typename PointOut, overlay_type OverlayType>
54 struct action_selector_pl_l
55 : action_selector_pl_pl<PointOut, OverlayType>
58 // difference/intersection of point-linear
64 overlay_type OverlayType,
67 struct point_linear_point
69 template <typename RobustPolicy, typename OutputIterator, typename Strategy>
70 static inline OutputIterator apply(Point const& point,
74 Strategy const& strategy)
79 >::apply(point, Policy::apply(point, linear, strategy), oit);
84 // difference/intersection of multipoint-segment
90 overlay_type OverlayType,
93 struct multipoint_segment_point
95 template <typename RobustPolicy, typename OutputIterator, typename Strategy>
96 static inline OutputIterator apply(MultiPoint const& multipoint,
97 Segment const& segment,
100 Strategy const& strategy)
102 for (typename boost::range_iterator<MultiPoint const>::type
103 it = boost::begin(multipoint);
104 it != boost::end(multipoint);
109 PointOut, OverlayType
110 >::apply(*it, Policy::apply(*it, segment, strategy), oit);
118 // difference/intersection of multipoint-linear
124 overlay_type OverlayType,
127 class multipoint_linear_point
130 // structs for partition -- start
131 template <typename ExpandPointStrategy>
132 struct expand_box_point
134 template <typename Box, typename Point>
135 static inline void apply(Box& total, Point const& point)
137 geometry::expand(total, point, ExpandPointStrategy());
141 template <typename EnvelopeStrategy>
142 struct expand_box_segment
144 explicit expand_box_segment(EnvelopeStrategy const& strategy)
145 : m_strategy(strategy)
148 template <typename Box, typename Segment>
149 inline void apply(Box& total, Segment const& segment) const
151 geometry::expand(total,
152 geometry::return_envelope<Box>(segment, m_strategy),
153 m_strategy.get_box_expand_strategy());
156 EnvelopeStrategy const& m_strategy;
159 template <typename DisjointPointBoxStrategy>
160 struct overlaps_box_point
162 template <typename Box, typename Point>
163 static inline bool apply(Box const& box, Point const& point)
165 return ! geometry::disjoint(point, box, DisjointPointBoxStrategy());
169 template <typename DisjointStrategy>
170 struct overlaps_box_segment
172 explicit overlaps_box_segment(DisjointStrategy const& strategy)
173 : m_strategy(strategy)
176 template <typename Box, typename Segment>
177 inline bool apply(Box const& box, Segment const& segment) const
179 return ! geometry::disjoint(segment, box, m_strategy);
182 DisjointStrategy const& m_strategy;
185 template <typename OutputIterator, typename Strategy>
186 class item_visitor_type
189 item_visitor_type(OutputIterator& oit, Strategy const& strategy)
191 , m_strategy(strategy)
194 template <typename Item1, typename Item2>
195 inline bool apply(Item1 const& item1, Item2 const& item2)
199 PointOut, overlay_intersection
200 >::apply(item1, Policy::apply(item1, item2, m_strategy), m_oit);
206 OutputIterator& m_oit;
207 Strategy const& m_strategy;
209 // structs for partition -- end
214 typedef geometry::segment_iterator<Linear const> const_iterator;
215 typedef const_iterator iterator;
217 segment_range(Linear const& linear)
221 const_iterator begin() const
223 return geometry::segments_begin(m_linear);
226 const_iterator end() const
228 return geometry::segments_end(m_linear);
232 Linear const& m_linear;
235 template <typename OutputIterator, typename Strategy>
236 static inline OutputIterator get_common_points(MultiPoint const& multipoint,
237 Linear const& linear,
239 Strategy const& strategy)
241 item_visitor_type<OutputIterator, Strategy> item_visitor(oit, strategy);
243 typedef typename Strategy::envelope_strategy_type envelope_strategy_type;
244 typedef typename Strategy::disjoint_strategy_type disjoint_strategy_type;
245 typedef typename Strategy::disjoint_point_box_strategy_type disjoint_point_box_strategy_type;
246 typedef typename Strategy::expand_point_strategy_type expand_point_strategy_type;
248 // TODO: disjoint Segment/Box may be called in partition multiple times
249 // possibly for non-cartesian segments which could be slow. We should consider
250 // passing a range of bounding boxes of segments after calculating them once.
251 // Alternatively instead of a range of segments a range of Segment/Envelope pairs
252 // should be passed, where envelope would be lazily calculated when needed the first time
257 typename boost::range_value<MultiPoint>::type
259 >::apply(multipoint, segment_range(linear), item_visitor,
260 expand_box_point<expand_point_strategy_type>(),
261 overlaps_box_point<disjoint_point_box_strategy_type>(),
262 expand_box_segment<envelope_strategy_type>(strategy.get_envelope_strategy()),
263 overlaps_box_segment<disjoint_strategy_type>(strategy.get_disjoint_strategy()));
269 template <typename RobustPolicy, typename OutputIterator, typename Strategy>
270 static inline OutputIterator apply(MultiPoint const& multipoint,
271 Linear const& linear,
272 RobustPolicy const& robust_policy,
274 Strategy const& strategy)
278 typename boost::range_value<MultiPoint>::type
281 point_vector_type common_points;
283 // compute the common points
284 get_common_points(multipoint, linear,
285 std::back_inserter(common_points),
288 return multipoint_multipoint_point
290 MultiPoint, point_vector_type, PointOut, OverlayType
291 >::apply(multipoint, common_points, robust_policy, oit, strategy);
296 }} // namespace detail::overlay
297 #endif // DOXYGEN_NO_DETAIL
300 #ifndef DOXYGEN_NO_DISPATCH
301 namespace detail_dispatch { namespace overlay
304 // dispatch struct for pointlike-linear difference/intersection computation
310 overlay_type OverlayType,
314 struct pointlike_linear_point
315 : not_implemented<PointLike, Linear, PointOut>
324 overlay_type OverlayType
326 struct pointlike_linear_point
328 Point, Linear, PointOut, OverlayType, point_tag, linear_tag
329 > : detail::overlay::point_linear_point
331 Point, Linear, PointOut, OverlayType,
332 detail::not_<detail::disjoint::reverse_covered_by>
342 overlay_type OverlayType
344 struct pointlike_linear_point
346 Point, Segment, PointOut, OverlayType, point_tag, segment_tag
347 > : detail::overlay::point_linear_point
349 Point, Segment, PointOut, OverlayType,
350 detail::not_<detail::disjoint::reverse_covered_by>
360 overlay_type OverlayType
362 struct pointlike_linear_point
364 MultiPoint, Linear, PointOut, OverlayType, multi_point_tag, linear_tag
365 > : detail::overlay::multipoint_linear_point
367 MultiPoint, Linear, PointOut, OverlayType,
368 detail::not_<detail::disjoint::reverse_covered_by>
378 overlay_type OverlayType
380 struct pointlike_linear_point
382 MultiPoint, Segment, PointOut, OverlayType, multi_point_tag, segment_tag
383 > : detail::overlay::multipoint_segment_point
385 MultiPoint, Segment, PointOut, OverlayType,
386 detail::not_<detail::disjoint::reverse_covered_by>
391 }} // namespace detail_dispatch::overlay
392 #endif // DOXYGEN_NO_DISPATCH
395 }} // namespace boost::geometry
398 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_POINTLIKE_LINEAR_HPP