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.
7 // Modifications copyright (c) 2017 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.hpp>
22 #include <boost/geometry/core/tags.hpp>
24 #include <boost/geometry/algorithms/area.hpp>
25 #include <boost/geometry/algorithms/covered_by.hpp>
26 #include <boost/geometry/algorithms/detail/interior_iterator.hpp>
27 #include <boost/geometry/algorithms/detail/ring_identifier.hpp>
28 #include <boost/geometry/algorithms/detail/overlay/range_in_geometry.hpp>
29 #include <boost/geometry/algorithms/detail/overlay/ring_properties.hpp>
30 #include <boost/geometry/algorithms/detail/overlay/overlay_type.hpp>
33 namespace boost { namespace geometry
37 #ifndef DOXYGEN_NO_DETAIL
38 namespace detail { namespace overlay
43 bool has_traversed_turn;
44 bool has_blocked_turn;
48 : has_traversed_turn(false)
49 , has_blocked_turn(false)
57 template <typename Tag, typename Geometry>
61 template <typename Box>
62 struct select_rings<box_tag, Box>
64 template <typename Geometry, typename RingPropertyMap, typename AreaStrategy>
65 static inline void apply(Box const& box, Geometry const& ,
66 ring_identifier const& id, RingPropertyMap& ring_properties,
67 AreaStrategy const& strategy)
69 ring_properties[id] = typename RingPropertyMap::mapped_type(box, strategy);
72 template <typename RingPropertyMap, typename AreaStrategy>
73 static inline void apply(Box const& box,
74 ring_identifier const& id, RingPropertyMap& ring_properties,
75 AreaStrategy const& strategy)
77 ring_properties[id] = typename RingPropertyMap::mapped_type(box, strategy);
81 template <typename Ring>
82 struct select_rings<ring_tag, Ring>
84 template <typename Geometry, typename RingPropertyMap, typename AreaStrategy>
85 static inline void apply(Ring const& ring, Geometry const& ,
86 ring_identifier const& id, RingPropertyMap& ring_properties,
87 AreaStrategy const& strategy)
89 if (boost::size(ring) > 0)
91 ring_properties[id] = typename RingPropertyMap::mapped_type(ring, strategy);
95 template <typename RingPropertyMap, typename AreaStrategy>
96 static inline void apply(Ring const& ring,
97 ring_identifier const& id, RingPropertyMap& ring_properties,
98 AreaStrategy const& strategy)
100 if (boost::size(ring) > 0)
102 ring_properties[id] = typename RingPropertyMap::mapped_type(ring, strategy);
108 template <typename Polygon>
109 struct select_rings<polygon_tag, Polygon>
111 template <typename Geometry, typename RingPropertyMap, typename AreaStrategy>
112 static inline void apply(Polygon const& polygon, Geometry const& geometry,
113 ring_identifier id, RingPropertyMap& ring_properties,
114 AreaStrategy const& strategy)
116 typedef typename geometry::ring_type<Polygon>::type ring_type;
117 typedef select_rings<ring_tag, ring_type> per_ring;
119 per_ring::apply(exterior_ring(polygon), geometry, id, ring_properties, strategy);
121 typename interior_return_type<Polygon const>::type
122 rings = interior_rings(polygon);
123 for (typename detail::interior_iterator<Polygon const>::type
124 it = boost::begin(rings); it != boost::end(rings); ++it)
127 per_ring::apply(*it, geometry, id, ring_properties, strategy);
131 template <typename RingPropertyMap, typename AreaStrategy>
132 static inline void apply(Polygon const& polygon,
133 ring_identifier id, RingPropertyMap& ring_properties,
134 AreaStrategy const& strategy)
136 typedef typename geometry::ring_type<Polygon>::type ring_type;
137 typedef select_rings<ring_tag, ring_type> per_ring;
139 per_ring::apply(exterior_ring(polygon), id, ring_properties, strategy);
141 typename interior_return_type<Polygon const>::type
142 rings = interior_rings(polygon);
143 for (typename detail::interior_iterator<Polygon const>::type
144 it = boost::begin(rings); it != boost::end(rings); ++it)
147 per_ring::apply(*it, id, ring_properties, strategy);
152 template <typename Multi>
153 struct select_rings<multi_polygon_tag, Multi>
155 template <typename Geometry, typename RingPropertyMap, typename AreaStrategy>
156 static inline void apply(Multi const& multi, Geometry const& geometry,
157 ring_identifier id, RingPropertyMap& ring_properties,
158 AreaStrategy const& strategy)
160 typedef typename boost::range_iterator
163 >::type iterator_type;
165 typedef select_rings<polygon_tag, typename boost::range_value<Multi>::type> per_polygon;
168 for (iterator_type it = boost::begin(multi); it != boost::end(multi); ++it)
171 per_polygon::apply(*it, geometry, id, ring_properties, strategy);
177 } // namespace dispatch
180 template<overlay_type OverlayType>
183 // Default implementation (union, inflate, deflate, dissolve)
184 static bool include(ring_identifier const& , ring_turn_info const& info)
186 return ! info.within_other;
189 static bool reversed(ring_identifier const& , ring_turn_info const& )
197 struct decide<overlay_difference>
199 static bool include(ring_identifier const& id, ring_turn_info const& info)
203 // If this is A (source_index=0), then the ring is inside B
204 // If this is B (source_index=1), then the ring is NOT inside A
206 // If this is A and the ring is within the other geometry,
207 // then we should NOT include it.
208 // If this is B then we SHOULD include it.
210 return id.source_index == 0
211 ? ! info.within_other
215 static bool reversed(ring_identifier const& id, ring_turn_info const& info)
218 // If this is B, and the ring is included, it should be reversed afterwards
220 return id.source_index == 1 && include(id, info);
225 struct decide<overlay_intersection>
227 static bool include(ring_identifier const& , ring_turn_info const& info)
229 return info.within_other;
232 static bool reversed(ring_identifier const& , ring_turn_info const& )
240 overlay_type OverlayType,
243 typename TurnInfoMap,
244 typename RingPropertyMap,
247 inline void update_ring_selection(Geometry1 const& geometry1,
248 Geometry2 const& geometry2,
249 TurnInfoMap const& turn_info_map,
250 RingPropertyMap const& all_ring_properties,
251 RingPropertyMap& selected_ring_properties,
252 Strategy const& strategy)
254 selected_ring_properties.clear();
256 for (typename RingPropertyMap::const_iterator it = boost::begin(all_ring_properties);
257 it != boost::end(all_ring_properties);
260 ring_identifier const& id = it->first;
264 typename TurnInfoMap::const_iterator tcit = turn_info_map.find(id);
265 if (tcit != turn_info_map.end())
267 info = tcit->second; // Copy by value
270 if (info.has_traversed_turn || info.has_blocked_turn)
272 // This turn is traversed or blocked,
273 // don't include the original ring
277 // Check if the ring is within the other geometry, by taking
278 // a point lying on the ring
279 switch(id.source_index)
283 info.within_other = range_in_geometry(it->second.point,
284 geometry1, geometry2,
288 info.within_other = range_in_geometry(it->second.point,
289 geometry2, geometry1,
294 if (decide<OverlayType>::include(id, info))
296 typename RingPropertyMap::mapped_type properties = it->second; // Copy by value
297 properties.reversed = decide<OverlayType>::reversed(id, info);
298 selected_ring_properties[id] = properties;
305 \brief The function select_rings select rings based on the overlay-type (union,intersection)
309 overlay_type OverlayType,
312 typename RingTurnInfoMap,
313 typename RingPropertyMap,
316 inline void select_rings(Geometry1 const& geometry1, Geometry2 const& geometry2,
317 RingTurnInfoMap const& turn_info_per_ring,
318 RingPropertyMap& selected_ring_properties,
319 Strategy const& strategy)
321 typedef typename geometry::tag<Geometry1>::type tag1;
322 typedef typename geometry::tag<Geometry2>::type tag2;
323 typedef typename geometry::point_type<Geometry1>::type point1_type;
324 typedef typename geometry::point_type<Geometry2>::type point2_type;
326 RingPropertyMap all_ring_properties;
327 dispatch::select_rings<tag1, Geometry1>::apply(geometry1, geometry2,
328 ring_identifier(0, -1, -1), all_ring_properties,
329 strategy.template get_area_strategy<point1_type>());
330 dispatch::select_rings<tag2, Geometry2>::apply(geometry2, geometry1,
331 ring_identifier(1, -1, -1), all_ring_properties,
332 strategy.template get_area_strategy<point2_type>());
334 update_ring_selection<OverlayType>(geometry1, geometry2, turn_info_per_ring,
335 all_ring_properties, selected_ring_properties,
341 overlay_type OverlayType,
343 typename RingTurnInfoMap,
344 typename RingPropertyMap,
347 inline void select_rings(Geometry const& geometry,
348 RingTurnInfoMap const& turn_info_per_ring,
349 RingPropertyMap& selected_ring_properties,
350 Strategy const& strategy)
352 typedef typename geometry::tag<Geometry>::type tag;
353 typedef typename geometry::point_type<Geometry>::type point_type;
355 RingPropertyMap all_ring_properties;
356 dispatch::select_rings<tag, Geometry>::apply(geometry,
357 ring_identifier(0, -1, -1), all_ring_properties,
358 strategy.template get_area_strategy<point_type>());
360 update_ring_selection<OverlayType>(geometry, geometry, turn_info_per_ring,
361 all_ring_properties, selected_ring_properties,
366 }} // namespace detail::overlay
367 #endif // DOXYGEN_NO_DETAIL
370 }} // namespace boost::geometry
373 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_SELECT_RINGS_HPP