1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2014 Adam Wulkiewicz, Lodz, Poland.
6 // Use, modification and distribution is subject to the Boost Software License,
7 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
10 #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_SELECT_RINGS_HPP
11 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_SELECT_RINGS_HPP
16 #include <boost/range.hpp>
18 #include <boost/geometry/core/tags.hpp>
20 #include <boost/geometry/algorithms/area.hpp>
21 #include <boost/geometry/algorithms/within.hpp>
22 #include <boost/geometry/algorithms/detail/interior_iterator.hpp>
23 #include <boost/geometry/algorithms/detail/point_on_border.hpp>
24 #include <boost/geometry/algorithms/detail/ring_identifier.hpp>
25 #include <boost/geometry/algorithms/detail/overlay/ring_properties.hpp>
26 #include <boost/geometry/algorithms/detail/overlay/overlay_type.hpp>
29 namespace boost { namespace geometry
33 #ifndef DOXYGEN_NO_DETAIL
34 namespace detail { namespace overlay
41 template <typename Tag, typename Geometry>
45 template <typename Box>
46 struct select_rings<box_tag, Box>
48 template <typename Geometry, typename Map>
49 static inline void apply(Box const& box, Geometry const& ,
50 ring_identifier const& id, Map& map, bool midpoint)
52 map[id] = typename Map::mapped_type(box, midpoint);
55 template <typename Map>
56 static inline void apply(Box const& box,
57 ring_identifier const& id, Map& map, bool midpoint)
59 map[id] = typename Map::mapped_type(box, midpoint);
63 template <typename Ring>
64 struct select_rings<ring_tag, Ring>
66 template <typename Geometry, typename Map>
67 static inline void apply(Ring const& ring, Geometry const& ,
68 ring_identifier const& id, Map& map, bool midpoint)
70 if (boost::size(ring) > 0)
72 map[id] = typename Map::mapped_type(ring, midpoint);
76 template <typename Map>
77 static inline void apply(Ring const& ring,
78 ring_identifier const& id, Map& map, bool midpoint)
80 if (boost::size(ring) > 0)
82 map[id] = typename Map::mapped_type(ring, midpoint);
88 template <typename Polygon>
89 struct select_rings<polygon_tag, Polygon>
91 template <typename Geometry, typename Map>
92 static inline void apply(Polygon const& polygon, Geometry const& geometry,
93 ring_identifier id, Map& map, bool midpoint)
95 typedef typename geometry::ring_type<Polygon>::type ring_type;
96 typedef select_rings<ring_tag, ring_type> per_ring;
98 per_ring::apply(exterior_ring(polygon), geometry, id, map, midpoint);
100 typename interior_return_type<Polygon const>::type
101 rings = interior_rings(polygon);
102 for (typename detail::interior_iterator<Polygon const>::type
103 it = boost::begin(rings); it != boost::end(rings); ++it)
106 per_ring::apply(*it, geometry, id, map, midpoint);
110 template <typename Map>
111 static inline void apply(Polygon const& polygon,
112 ring_identifier id, Map& map, bool midpoint)
114 typedef typename geometry::ring_type<Polygon>::type ring_type;
115 typedef select_rings<ring_tag, ring_type> per_ring;
117 per_ring::apply(exterior_ring(polygon), id, map, midpoint);
119 typename interior_return_type<Polygon const>::type
120 rings = interior_rings(polygon);
121 for (typename detail::interior_iterator<Polygon const>::type
122 it = boost::begin(rings); it != boost::end(rings); ++it)
125 per_ring::apply(*it, id, map, midpoint);
130 template <typename Multi>
131 struct select_rings<multi_polygon_tag, Multi>
133 template <typename Geometry, typename Map>
134 static inline void apply(Multi const& multi, Geometry const& geometry,
135 ring_identifier id, Map& map, bool midpoint)
137 typedef typename boost::range_iterator
140 >::type iterator_type;
142 typedef select_rings<polygon_tag, typename boost::range_value<Multi>::type> per_polygon;
145 for (iterator_type it = boost::begin(multi); it != boost::end(multi); ++it)
148 per_polygon::apply(*it, geometry, id, map, midpoint);
154 } // namespace dispatch
157 template<overlay_type OverlayType>
162 struct decide<overlay_union>
164 template <typename Code>
165 static bool include(ring_identifier const& , Code const& code)
167 return code.within_code * -1 == 1;
170 template <typename Code>
171 static bool reversed(ring_identifier const& , Code const& )
178 struct decide<overlay_difference>
180 template <typename Code>
181 static bool include(ring_identifier const& id, Code const& code)
183 bool is_first = id.source_index == 0;
184 return code.within_code * -1 * (is_first ? 1 : -1) == 1;
187 template <typename Code>
188 static bool reversed(ring_identifier const& id, Code const& code)
190 return include(id, code) && id.source_index == 1;
195 struct decide<overlay_intersection>
197 template <typename Code>
198 static bool include(ring_identifier const& , Code const& code)
200 return code.within_code * 1 == 1;
203 template <typename Code>
204 static bool reversed(ring_identifier const& , Code const& )
213 overlay_type OverlayType,
214 typename Geometry1, typename Geometry2,
215 typename IntersectionMap, typename SelectionMap
217 inline void update_selection_map(Geometry1 const& geometry1,
218 Geometry2 const& geometry2,
219 IntersectionMap const& intersection_map,
220 SelectionMap const& map_with_all, SelectionMap& selection_map)
222 selection_map.clear();
224 for (typename SelectionMap::const_iterator it = boost::begin(map_with_all);
225 it != boost::end(map_with_all);
229 int union_code = it->second.within_code * -1;
230 bool is_first = it->first.source_index == 0;
231 std::cout << it->first << " " << it->second.area
232 << ": " << it->second.within_code
233 << " union: " << union_code
234 << " intersection: " << (it->second.within_code * 1)
235 << " G1-G2: " << (union_code * (is_first ? 1 : -1))
236 << " G2-G1: " << (union_code * (is_first ? -1 : 1))
237 << " -> " << (decide<OverlayType>::include(it->first, it->second) ? "INC" : "")
238 << decide<OverlayType>::reverse(it->first, it->second)
242 bool found = intersection_map.find(it->first) != intersection_map.end();
245 ring_identifier const id = it->first;
246 typename SelectionMap::mapped_type properties = it->second; // Copy by value
248 // Calculate the "within code" (previously this was done earlier but is
249 // much efficienter here - it can be even more efficient doing it all at once,
250 // using partition, TODO)
251 // So though this is less elegant than before, it avoids many unused point-in-poly calculations
252 switch(id.source_index)
255 properties.within_code
256 = geometry::within(properties.point, geometry2) ? 1 : -1;
259 properties.within_code
260 = geometry::within(properties.point, geometry1) ? 1 : -1;
264 if (decide<OverlayType>::include(id, properties))
266 properties.reversed = decide<OverlayType>::reversed(id, properties);
267 selection_map[id] = properties;
275 \brief The function select_rings select rings based on the overlay-type (union,intersection)
279 overlay_type OverlayType,
280 typename Geometry1, typename Geometry2,
281 typename IntersectionMap, typename SelectionMap
283 inline void select_rings(Geometry1 const& geometry1, Geometry2 const& geometry2,
284 IntersectionMap const& intersection_map,
285 SelectionMap& selection_map, bool midpoint)
287 typedef typename geometry::tag<Geometry1>::type tag1;
288 typedef typename geometry::tag<Geometry2>::type tag2;
290 SelectionMap map_with_all;
291 dispatch::select_rings<tag1, Geometry1>::apply(geometry1, geometry2,
292 ring_identifier(0, -1, -1), map_with_all, midpoint);
293 dispatch::select_rings<tag2, Geometry2>::apply(geometry2, geometry1,
294 ring_identifier(1, -1, -1), map_with_all, midpoint);
296 update_selection_map<OverlayType>(geometry1, geometry2, intersection_map,
297 map_with_all, selection_map);
302 overlay_type OverlayType,
304 typename IntersectionMap, typename SelectionMap
306 inline void select_rings(Geometry const& geometry,
307 IntersectionMap const& intersection_map,
308 SelectionMap& selection_map, bool midpoint)
310 typedef typename geometry::tag<Geometry>::type tag;
312 SelectionMap map_with_all;
313 dispatch::select_rings<tag, Geometry>::apply(geometry,
314 ring_identifier(0, -1, -1), map_with_all, midpoint);
316 update_selection_map<OverlayType>(geometry, geometry, intersection_map,
317 map_with_all, selection_map);
321 }} // namespace detail::overlay
322 #endif // DOXYGEN_NO_DETAIL
325 }} // namespace boost::geometry
328 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_SELECT_RINGS_HPP