1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2015-2016 Barend Gehrels, Amsterdam, the Netherlands.
5 // Use, modification and distribution is subject to the Boost Software License,
6 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_TRAVERSAL_SWITCH_DETECTOR_HPP
10 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_TRAVERSAL_SWITCH_DETECTOR_HPP
14 #include <boost/range.hpp>
16 #include <boost/geometry/algorithms/detail/ring_identifier.hpp>
17 #include <boost/geometry/algorithms/detail/overlay/copy_segments.hpp>
18 #include <boost/geometry/algorithms/detail/overlay/cluster_info.hpp>
19 #include <boost/geometry/algorithms/detail/overlay/is_self_turn.hpp>
20 #include <boost/geometry/algorithms/detail/overlay/turn_info.hpp>
21 #include <boost/geometry/core/access.hpp>
22 #include <boost/geometry/core/assert.hpp>
24 namespace boost { namespace geometry
27 #ifndef DOXYGEN_NO_DETAIL
28 namespace detail { namespace overlay
31 // Generic function (is this used somewhere else too?)
32 inline ring_identifier ring_id_by_seg_id(segment_identifier const& seg_id)
34 return ring_identifier(seg_id.source_index, seg_id.multi_index, seg_id.ring_index);
41 overlay_type OverlayType,
46 typename RobustPolicy,
49 struct traversal_switch_detector
53 isolation_unknown = -1,
56 isolation_multiple = 2
59 typedef typename boost::range_value<Turns>::type turn_type;
60 typedef typename turn_type::turn_operation_type turn_operation_type;
61 typedef std::set<signed_size_type> set_type;
63 // Per ring, first turns are collected (in turn_indices), and later
64 // a region_id is assigned
65 struct merged_ring_properties
67 signed_size_type region_id;
68 set_type turn_indices;
70 merged_ring_properties()
75 struct connection_properties
78 // Contains turn-index OR, if clustered, minus-cluster_id
79 set_type unique_turn_ids;
80 connection_properties()
85 typedef std::map<signed_size_type, connection_properties> connection_map;
87 // Per region, a set of properties is maintained, including its connections
89 struct region_properties
91 signed_size_type region_id;
92 isolation_type isolated;
93 set_type unique_turn_ids;
95 // Maps from connected region_id to their properties
96 connection_map connected_region_counts;
100 , isolated(isolation_unknown)
104 // Keeps turn indices per ring
105 typedef std::map<ring_identifier, merged_ring_properties > merge_map;
106 typedef std::map<signed_size_type, region_properties> region_connection_map;
108 typedef set_type::const_iterator set_iterator;
110 inline traversal_switch_detector(Geometry1 const& geometry1, Geometry2 const& geometry2,
111 Turns& turns, Clusters& clusters,
112 RobustPolicy const& robust_policy, Visitor& visitor)
113 : m_geometry1(geometry1)
114 , m_geometry2(geometry2)
116 , m_clusters(clusters)
117 , m_robust_policy(robust_policy)
122 bool inspect_difference(set_type& turn_id_difference,
123 set_type const& turn_ids,
124 set_type const& other_turn_ids) const
126 // TODO: consider if std::set_difference can be used in the final version
127 int const turn_count = turn_ids.size();
128 int const other_turn_count = other_turn_ids.size();
130 // First quick check on size (TODO: implement multiple-multiple connections)
131 if (turn_count - other_turn_count > 1)
136 // Check if all turns are also present in the connection.
137 // The difference is returned
138 for (set_iterator it = turn_ids.begin(); it != turn_ids.end(); ++it)
140 signed_size_type const& id = *it;
141 if (other_turn_ids.count(id) == 0)
143 turn_id_difference.insert(id);
149 bool one_connection_to_another_region(region_properties const& region) const
152 // +----------------------+
158 // +----------------------+
160 if (region.connected_region_counts.size() == 1)
162 connection_properties const& cprop = region.connected_region_counts.begin()->second;
163 return cprop.count <= 1;
165 return region.connected_region_counts.empty();
168 // TODO: might be combined with previous
169 bool multiple_connections_to_one_region(region_properties const& region) const
172 // +----------------------+
181 // +----------------------+
183 if (region.connected_region_counts.size() == 1)
185 connection_properties const& cprop = region.connected_region_counts.begin()->second;
186 return cprop.count > 1;
191 bool one_connection_to_multiple_regions(region_properties const& region) const
194 // +----------------------+
200 // +----------------------+
203 signed_size_type first_turn_id = 0;
204 for (typename connection_map::const_iterator it = region.connected_region_counts.begin();
205 it != region.connected_region_counts.end(); ++it)
207 connection_properties const& cprop = it->second;
209 if (cprop.count != 1)
213 signed_size_type const unique_turn_id = *cprop.unique_turn_ids.begin();
216 first_turn_id = unique_turn_id;
219 else if (first_turn_id != unique_turn_id)
227 bool has_only_isolated_children(region_properties const& region) const
229 bool first_with_turn = true;
230 bool first_with_multiple = true;
231 signed_size_type first_turn_id = 0;
232 signed_size_type first_multiple_region_id = 0;
234 for (typename connection_map::const_iterator it = region.connected_region_counts.begin();
235 it != region.connected_region_counts.end(); ++it)
237 signed_size_type const region_id = it->first;
238 connection_properties const& cprop = it->second;
240 typename region_connection_map::const_iterator mit = m_connected_regions.find(region_id);
241 if (mit == m_connected_regions.end())
247 region_properties const& connected_region = mit->second;
249 bool const multiple = connected_region.isolated == isolation_multiple;
251 if (cprop.count != 1)
258 // It connects multiple times to an isolated region.
259 // This is allowed as long as it happens only once
260 if (first_with_multiple)
262 first_multiple_region_id = connected_region.region_id;
263 first_with_multiple = false;
265 else if (first_multiple_region_id != connected_region.region_id)
270 // Turns in region should be either present in the connection,
271 // of form part of the connection with the other region
273 if (! inspect_difference(diff, region.unique_turn_ids,
274 connected_region.unique_turn_ids))
285 if (connected_region.isolated != isolation_yes && ! multiple)
287 signed_size_type const unique_turn_id = *cprop.unique_turn_ids.begin();
290 first_turn_id = unique_turn_id;
291 first_with_turn = false;
293 else if (first_turn_id != unique_turn_id)
299 // If there is only one connection (with a 'parent'), and all other
300 // connections are itself isolated, it is isolated
304 void get_isolated_regions()
306 typedef typename region_connection_map::iterator it_type;
308 // First time: check regions isolated (one connection only),
309 // semi-isolated (multiple connections between same region),
310 // and complex isolated (connection with multiple rings but all
312 for (it_type it = m_connected_regions.begin();
313 it != m_connected_regions.end(); ++it)
315 region_properties& properties = it->second;
316 if (one_connection_to_another_region(properties))
318 properties.isolated = isolation_yes;
320 else if (multiple_connections_to_one_region(properties))
322 properties.isolated = isolation_multiple;
324 else if (one_connection_to_multiple_regions(properties))
326 properties.isolated = isolation_yes;
330 // Propagate isolation to next level
331 // TODO: should be optimized
332 std::size_t defensive_check = 0;
334 while (changed && defensive_check++ < m_connected_regions.size())
337 for (it_type it = m_connected_regions.begin();
338 it != m_connected_regions.end(); ++it)
340 region_properties& properties = it->second;
342 if (properties.isolated == isolation_unknown
343 && has_only_isolated_children(properties))
345 properties.isolated = isolation_yes;
352 void assign_isolation()
354 for (std::size_t turn_index = 0; turn_index < m_turns.size(); ++turn_index)
356 turn_type& turn = m_turns[turn_index];
358 for (int op_index = 0; op_index < 2; op_index++)
360 turn_operation_type& op = turn.operations[op_index];
361 typename region_connection_map::const_iterator mit = m_connected_regions.find(op.enriched.region_id);
362 if (mit != m_connected_regions.end())
364 region_properties const& prop = mit->second;
365 op.enriched.isolated = prop.isolated == isolation_yes;
371 void assign_region_ids()
373 for (typename merge_map::const_iterator it
374 = m_turns_per_ring.begin(); it != m_turns_per_ring.end(); ++it)
376 ring_identifier const& ring_id = it->first;
377 merged_ring_properties const& properties = it->second;
379 for (set_iterator sit = properties.turn_indices.begin();
380 sit != properties.turn_indices.end(); ++sit)
382 turn_type& turn = m_turns[*sit];
384 for (int i = 0; i < 2; i++)
386 turn_operation_type& op = turn.operations[i];
387 if (ring_id_by_seg_id(op.seg_id) == ring_id)
389 op.enriched.region_id = properties.region_id;
396 void assign_connected_regions()
398 for (std::size_t turn_index = 0; turn_index < m_turns.size(); ++turn_index)
400 turn_type const& turn = m_turns[turn_index];
402 signed_size_type const unique_turn_id
403 = turn.is_clustered() ? -turn.cluster_id : turn_index;
405 turn_operation_type op0 = turn.operations[0];
406 turn_operation_type op1 = turn.operations[1];
408 signed_size_type const& id0 = op0.enriched.region_id;
409 signed_size_type const& id1 = op1.enriched.region_id;
411 // Add region (by assigning) and add involved turns
414 m_connected_regions[id0].region_id = id0;
415 m_connected_regions[id0].unique_turn_ids.insert(unique_turn_id);
417 if (id1 != -1 && id0 != id1)
419 m_connected_regions[id1].region_id = id1;
420 m_connected_regions[id1].unique_turn_ids.insert(unique_turn_id);
423 if (id0 != id1 && id0 != -1 && id1 != -1)
425 // Assign connections
426 connection_properties& prop0 = m_connected_regions[id0].connected_region_counts[id1];
427 connection_properties& prop1 = m_connected_regions[id1].connected_region_counts[id0];
429 // Reference this turn or cluster to later check uniqueness on ring
430 if (prop0.unique_turn_ids.count(unique_turn_id) == 0)
433 prop0.unique_turn_ids.insert(unique_turn_id);
435 if (prop1.unique_turn_ids.count(unique_turn_id) == 0)
438 prop1.unique_turn_ids.insert(unique_turn_id);
444 inline bool connects_same_region(turn_type const& turn) const
448 // Discarded turns don't connect same region (otherwise discarded colocated uu turn
449 // could make a connection)
453 if (! turn.is_clustered())
455 // If it is a uu/ii-turn (non clustered), it is never same region
456 return ! (turn.both(operation_union) || turn.both(operation_intersection));
459 if (operation_from_overlay<OverlayType>::value == operation_union)
461 // It is a cluster, check zones
462 // (assigned by sort_by_side/handle colocations) of both operations
463 return turn.operations[0].enriched.zone
464 == turn.operations[1].enriched.zone;
467 // For an intersection, two regions connect if they are not ii
468 // (ii-regions are isolated) or, in some cases, not iu (for example
469 // when a multi-polygon is inside an interior ring and connecting it)
470 return ! (turn.both(operation_intersection)
471 || turn.combination(operation_intersection, operation_union));
475 inline signed_size_type get_region_id(turn_operation_type const& op) const
477 return op.enriched.region_id;
481 void create_region(signed_size_type& new_region_id, ring_identifier const& ring_id,
482 merged_ring_properties& properties, signed_size_type region_id = -1)
484 if (properties.region_id > 0)
490 // Assign new id if this is a new region
493 region_id = new_region_id++;
496 // Assign this ring to specified region
497 properties.region_id = region_id;
499 #if defined(BOOST_GEOMETRY_DEBUG_TRAVERSAL_SWITCH_DETECTOR)
500 std::cout << " ADD " << ring_id << " TO REGION " << region_id << std::endl;
503 // Find connecting rings, recursively
504 for (set_iterator sit = properties.turn_indices.begin();
505 sit != properties.turn_indices.end(); ++sit)
507 signed_size_type const turn_index = *sit;
508 turn_type const& turn = m_turns[turn_index];
509 if (! connects_same_region(turn))
511 // This is a non clustered uu/ii-turn, or a cluster connecting different 'zones'
515 // Union: This turn connects two rings (interior connected), create the region
516 // Intersection: This turn connects two rings, set same regions for these two rings
517 for (int op_index = 0; op_index < 2; op_index++)
519 turn_operation_type const& op = turn.operations[op_index];
520 ring_identifier connected_ring_id = ring_id_by_seg_id(op.seg_id);
521 if (connected_ring_id != ring_id)
523 propagate_region(new_region_id, connected_ring_id, region_id);
529 void propagate_region(signed_size_type& new_region_id,
530 ring_identifier const& ring_id, signed_size_type region_id)
532 typename merge_map::iterator it = m_turns_per_ring.find(ring_id);
533 if (it != m_turns_per_ring.end())
535 create_region(new_region_id, ring_id, it->second, region_id);
542 #if defined(BOOST_GEOMETRY_DEBUG_TRAVERSAL_SWITCH_DETECTOR)
543 std::cout << "SWITCH BEGIN ITERATION" << std::endl;
546 // Collect turns per ring
547 m_turns_per_ring.clear();
548 m_connected_regions.clear();
550 for (std::size_t turn_index = 0; turn_index < m_turns.size(); ++turn_index)
552 turn_type const& turn = m_turns[turn_index];
555 && operation_from_overlay<OverlayType>::value == operation_intersection)
557 // Discarded turn (union currently still needs it to determine regions)
561 for (int op_index = 0; op_index < 2; op_index++)
563 turn_operation_type const& op = turn.operations[op_index];
564 m_turns_per_ring[ring_id_by_seg_id(op.seg_id)].turn_indices.insert(turn_index);
568 // All rings having turns are in turns/ring map. Process them.
570 signed_size_type new_region_id = 1;
571 for (typename merge_map::iterator it
572 = m_turns_per_ring.begin(); it != m_turns_per_ring.end(); ++it)
574 create_region(new_region_id, it->first, it->second);
578 assign_connected_regions();
579 get_isolated_regions();
583 // Now that all regions are filled, assign switch_source property
584 // Iterate through all clusters
585 for (typename Clusters::iterator it = m_clusters.begin(); it != m_clusters.end(); ++it)
587 cluster_info& cinfo = it->second;
588 if (cinfo.open_count <= 1)
590 // Not a touching cluster
594 // A touching cluster, gather regions
596 set_type const& ids = cinfo.turn_indices;
598 #if defined(BOOST_GEOMETRY_DEBUG_TRAVERSAL_SWITCH_DETECTOR)
599 std::cout << "SWITCH EXAMINE CLUSTER " << it->first << std::endl;
602 for (set_iterator sit = ids.begin(); sit != ids.end(); ++sit)
604 signed_size_type turn_index = *sit;
605 turn_type const& turn = m_turns[turn_index];
606 for (int oi = 0; oi < 2; oi++)
608 signed_size_type const region_id = get_region_id(turn.operations[oi]);
609 regions.insert(region_id);
612 // Switch source if this cluster connects the same region
613 cinfo.switch_source = regions.size() <= 1;
616 // Iterate through all uu/ii turns (non-clustered)
617 for (std::size_t turn_index = 0; turn_index < m_turns.size(); ++turn_index)
619 turn_type& turn = m_turns[turn_index];
623 || turn.is_clustered()
624 || ! (turn.both(operation_union) || turn.both(operation_intersection)))
626 // Skip discarded, blocked, non-uu/ii and clustered turns
631 signed_size_type const region0 = get_region_id(turn.operations[0]);
632 signed_size_type const region1 = get_region_id(turn.operations[1]);
634 // Switch sources for same region
635 turn.switch_source = region0 == region1;
639 #if defined(BOOST_GEOMETRY_DEBUG_TRAVERSAL_SWITCH_DETECTOR)
640 std::cout << "SWITCH END ITERATION" << std::endl;
642 for (std::size_t turn_index = 0; turn_index < m_turns.size(); ++turn_index)
644 turn_type const& turn = m_turns[turn_index];
646 if ((turn.both(operation_union) || turn.both(operation_intersection))
647 && ! turn.is_clustered())
649 std::cout << "UU/II SWITCH RESULT "
650 << turn_index << " -> "
651 << turn.switch_source << std::endl;
655 for (typename Clusters::const_iterator it = m_clusters.begin(); it != m_clusters.end(); ++it)
657 cluster_info const& cinfo = it->second;
658 if (cinfo.open_count > 1)
660 std::cout << "CL SWITCH RESULT " << it->first
661 << " -> " << cinfo.switch_source << std::endl;
665 std::cout << "CL SWITCH RESULT " << it->first
666 << " is not registered as open" << std::endl;
675 Geometry1 const& m_geometry1;
676 Geometry2 const& m_geometry2;
678 Clusters& m_clusters;
679 merge_map m_turns_per_ring;
680 region_connection_map m_connected_regions;
681 RobustPolicy const& m_robust_policy;
685 }} // namespace detail::overlay
686 #endif // DOXYGEN_NO_DETAIL
688 }} // namespace boost::geometry
690 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_TRAVERSAL_SWITCH_DETECTOR_HPP