1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2007-2014 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2014 Adam Wulkiewicz, Lodz, Poland.
6 // This file was modified by Oracle on 2017-2021.
7 // Modifications copyright (c) 2017-2021 Oracle and/or its affiliates.
8 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
10 // Use, modification and distribution is subject to the Boost Software License,
11 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
12 // http://www.boost.org/LICENSE_1_0.txt)
14 #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_SELECT_RINGS_HPP
15 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_SELECT_RINGS_HPP
20 #include <boost/range/begin.hpp>
21 #include <boost/range/end.hpp>
22 #include <boost/range/size.hpp>
24 #include <boost/geometry/core/tags.hpp>
26 #include <boost/geometry/algorithms/covered_by.hpp>
27 #include <boost/geometry/algorithms/detail/interior_iterator.hpp>
28 #include <boost/geometry/algorithms/detail/ring_identifier.hpp>
29 #include <boost/geometry/algorithms/detail/overlay/range_in_geometry.hpp>
30 #include <boost/geometry/algorithms/detail/overlay/ring_properties.hpp>
31 #include <boost/geometry/algorithms/detail/overlay/overlay_type.hpp>
34 namespace boost { namespace geometry
38 #ifndef DOXYGEN_NO_DETAIL
39 namespace detail { namespace overlay
44 bool has_traversed_turn;
45 bool has_blocked_turn;
49 : has_traversed_turn(false)
50 , has_blocked_turn(false)
58 template <typename Tag, typename Geometry>
62 template <typename Box>
63 struct select_rings<box_tag, Box>
65 template <typename Geometry, typename RingPropertyMap, typename Strategy>
66 static inline void apply(Box const& box, Geometry const& ,
67 ring_identifier const& id, RingPropertyMap& ring_properties,
68 Strategy const& strategy)
70 ring_properties[id] = typename RingPropertyMap::mapped_type(box, strategy);
73 template <typename RingPropertyMap, typename Strategy>
74 static inline void apply(Box const& box,
75 ring_identifier const& id, RingPropertyMap& ring_properties,
76 Strategy const& strategy)
78 ring_properties[id] = typename RingPropertyMap::mapped_type(box, strategy);
82 template <typename Ring>
83 struct select_rings<ring_tag, Ring>
85 template <typename Geometry, typename RingPropertyMap, typename Strategy>
86 static inline void apply(Ring const& ring, Geometry const& ,
87 ring_identifier const& id, RingPropertyMap& ring_properties,
88 Strategy const& strategy)
90 if (boost::size(ring) > 0)
92 ring_properties[id] = typename RingPropertyMap::mapped_type(ring, strategy);
96 template <typename RingPropertyMap, typename Strategy>
97 static inline void apply(Ring const& ring,
98 ring_identifier const& id, RingPropertyMap& ring_properties,
99 Strategy const& strategy)
101 if (boost::size(ring) > 0)
103 ring_properties[id] = typename RingPropertyMap::mapped_type(ring, strategy);
109 template <typename Polygon>
110 struct select_rings<polygon_tag, Polygon>
112 template <typename Geometry, typename RingPropertyMap, typename Strategy>
113 static inline void apply(Polygon const& polygon, Geometry const& geometry,
114 ring_identifier id, RingPropertyMap& ring_properties,
115 Strategy const& strategy)
117 typedef typename geometry::ring_type<Polygon>::type ring_type;
118 typedef select_rings<ring_tag, ring_type> per_ring;
120 per_ring::apply(exterior_ring(polygon), geometry, id, ring_properties, strategy);
122 typename interior_return_type<Polygon const>::type
123 rings = interior_rings(polygon);
124 for (typename detail::interior_iterator<Polygon const>::type
125 it = boost::begin(rings); it != boost::end(rings); ++it)
128 per_ring::apply(*it, geometry, id, ring_properties, strategy);
132 template <typename RingPropertyMap, typename Strategy>
133 static inline void apply(Polygon const& polygon,
134 ring_identifier id, RingPropertyMap& ring_properties,
135 Strategy const& strategy)
137 typedef typename geometry::ring_type<Polygon>::type ring_type;
138 typedef select_rings<ring_tag, ring_type> per_ring;
140 per_ring::apply(exterior_ring(polygon), id, ring_properties, strategy);
142 typename interior_return_type<Polygon const>::type
143 rings = interior_rings(polygon);
144 for (typename detail::interior_iterator<Polygon const>::type
145 it = boost::begin(rings); it != boost::end(rings); ++it)
148 per_ring::apply(*it, id, ring_properties, strategy);
153 template <typename Multi>
154 struct select_rings<multi_polygon_tag, Multi>
156 template <typename Geometry, typename RingPropertyMap, typename Strategy>
157 static inline void apply(Multi const& multi, Geometry const& geometry,
158 ring_identifier id, RingPropertyMap& ring_properties,
159 Strategy const& strategy)
161 typedef typename boost::range_iterator
164 >::type iterator_type;
166 typedef select_rings<polygon_tag, typename boost::range_value<Multi>::type> per_polygon;
169 for (iterator_type it = boost::begin(multi); it != boost::end(multi); ++it)
172 per_polygon::apply(*it, geometry, id, ring_properties, strategy);
178 } // namespace dispatch
181 template<overlay_type OverlayType>
184 // Default implementation (union, inflate, deflate, dissolve)
185 static bool include(ring_identifier const& , ring_turn_info const& info)
187 return ! info.within_other;
190 static bool reversed(ring_identifier const& , ring_turn_info const& )
198 struct decide<overlay_difference>
200 static bool include(ring_identifier const& id, ring_turn_info const& info)
204 // If this is A (source_index=0), then the ring is inside B
205 // If this is B (source_index=1), then the ring is NOT inside A
207 // If this is A and the ring is within the other geometry,
208 // then we should NOT include it.
209 // If this is B then we SHOULD include it.
211 return id.source_index == 0
212 ? ! info.within_other
216 static bool reversed(ring_identifier const& id, ring_turn_info const& info)
219 // If this is B, and the ring is included, it should be reversed afterwards
221 return id.source_index == 1 && include(id, info);
226 struct decide<overlay_intersection>
228 static bool include(ring_identifier const& , ring_turn_info const& info)
230 return info.within_other;
233 static bool reversed(ring_identifier const& , ring_turn_info const& )
241 overlay_type OverlayType,
244 typename TurnInfoMap,
245 typename RingPropertyMap,
248 inline void update_ring_selection(Geometry1 const& geometry1,
249 Geometry2 const& geometry2,
250 TurnInfoMap const& turn_info_map,
251 RingPropertyMap const& all_ring_properties,
252 RingPropertyMap& selected_ring_properties,
253 Strategy const& strategy)
255 selected_ring_properties.clear();
257 for (typename RingPropertyMap::const_iterator it = boost::begin(all_ring_properties);
258 it != boost::end(all_ring_properties);
261 ring_identifier const& id = it->first;
265 typename TurnInfoMap::const_iterator tcit = turn_info_map.find(id);
266 if (tcit != turn_info_map.end())
268 info = tcit->second; // Copy by value
271 if (info.has_traversed_turn || info.has_blocked_turn)
273 // This turn is traversed or blocked,
274 // don't include the original ring
278 // Check if the ring is within the other geometry, by taking
279 // a point lying on the ring
280 switch(id.source_index)
284 info.within_other = range_in_geometry(it->second.point,
285 geometry1, geometry2,
289 info.within_other = range_in_geometry(it->second.point,
290 geometry2, geometry1,
295 if (decide<OverlayType>::include(id, info))
297 typename RingPropertyMap::mapped_type properties = it->second; // Copy by value
298 properties.reversed = decide<OverlayType>::reversed(id, info);
299 selected_ring_properties[id] = properties;
306 \brief The function select_rings select rings based on the overlay-type (union,intersection)
310 overlay_type OverlayType,
313 typename RingTurnInfoMap,
314 typename RingPropertyMap,
317 inline void select_rings(Geometry1 const& geometry1, Geometry2 const& geometry2,
318 RingTurnInfoMap const& turn_info_per_ring,
319 RingPropertyMap& selected_ring_properties,
320 Strategy const& strategy)
322 typedef typename geometry::tag<Geometry1>::type tag1;
323 typedef typename geometry::tag<Geometry2>::type tag2;
325 RingPropertyMap all_ring_properties;
326 dispatch::select_rings<tag1, Geometry1>::apply(geometry1, geometry2,
327 ring_identifier(0, -1, -1), all_ring_properties,
329 dispatch::select_rings<tag2, Geometry2>::apply(geometry2, geometry1,
330 ring_identifier(1, -1, -1), all_ring_properties,
333 update_ring_selection<OverlayType>(geometry1, geometry2, turn_info_per_ring,
334 all_ring_properties, selected_ring_properties,
340 overlay_type OverlayType,
342 typename RingTurnInfoMap,
343 typename RingPropertyMap,
346 inline void select_rings(Geometry const& geometry,
347 RingTurnInfoMap const& turn_info_per_ring,
348 RingPropertyMap& selected_ring_properties,
349 Strategy const& strategy)
351 typedef typename geometry::tag<Geometry>::type tag;
353 RingPropertyMap all_ring_properties;
354 dispatch::select_rings<tag, Geometry>::apply(geometry,
355 ring_identifier(0, -1, -1), all_ring_properties,
358 update_ring_selection<OverlayType>(geometry, geometry, turn_info_per_ring,
359 all_ring_properties, selected_ring_properties,
364 }} // namespace detail::overlay
365 #endif // DOXYGEN_NO_DETAIL
368 }} // namespace boost::geometry
371 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_SELECT_RINGS_HPP