1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
5 // This file was modified by Oracle on 2017.
6 // 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_TRAVERSAL_HPP
15 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_TRAVERSAL_HPP
19 #include <boost/range.hpp>
21 #include <boost/geometry/algorithms/detail/overlay/aggregate_operations.hpp>
22 #include <boost/geometry/algorithms/detail/overlay/is_self_turn.hpp>
23 #include <boost/geometry/algorithms/detail/overlay/sort_by_side.hpp>
24 #include <boost/geometry/algorithms/detail/overlay/traversal_intersection_patterns.hpp>
25 #include <boost/geometry/algorithms/detail/overlay/turn_info.hpp>
26 #include <boost/geometry/core/access.hpp>
27 #include <boost/geometry/core/assert.hpp>
29 #if defined(BOOST_GEOMETRY_DEBUG_INTERSECTION) \
30 || defined(BOOST_GEOMETRY_OVERLAY_REPORT_WKT) \
31 || defined(BOOST_GEOMETRY_DEBUG_TRAVERSE)
33 # include <boost/geometry/algorithms/detail/overlay/debug_turn_info.hpp>
34 # include <boost/geometry/io/wkt/wkt.hpp>
37 namespace boost { namespace geometry
40 #ifndef DOXYGEN_NO_DETAIL
41 namespace detail { namespace overlay
44 template <typename Turn, typename Operation>
45 #ifdef BOOST_GEOMETRY_DEBUG_TRAVERSE
46 inline void debug_traverse(Turn const& turn, Operation op,
47 std::string const& header, bool condition = true)
53 std::cout << " " << header
54 << " at " << op.seg_id
55 << " meth: " << method_char(turn.method)
56 << " op: " << operation_char(op.operation)
57 << " vis: " << visited_char(op.visited)
58 << " of: " << operation_char(turn.operations[0].operation)
59 << operation_char(turn.operations[1].operation)
60 << " " << geometry::wkt(turn.point)
63 if (boost::contains(header, "Finished"))
65 std::cout << std::endl;
69 inline void debug_traverse(Turn const& , Operation, const char*, bool = true)
75 //! Metafunction to define side_order (clockwise, ccw) by operation_type
76 template <operation_type OpType>
77 struct side_compare {};
80 struct side_compare<operation_union>
82 typedef std::greater<int> type;
86 struct side_compare<operation_intersection>
88 typedef std::less<int> type;
96 overlay_type OverlayType,
101 typename RobustPolicy,
102 typename SideStrategy,
107 static const operation_type target_operation = operation_from_overlay<OverlayType>::value;
109 typedef typename side_compare<target_operation>::type side_compare_type;
110 typedef typename boost::range_value<Turns>::type turn_type;
111 typedef typename turn_type::turn_operation_type turn_operation_type;
113 typedef typename geometry::point_type<Geometry1>::type point_type;
114 typedef sort_by_side::side_sorter
116 Reverse1, Reverse2, OverlayType,
117 point_type, SideStrategy, side_compare_type
120 inline traversal(Geometry1 const& geometry1, Geometry2 const& geometry2,
121 Turns& turns, Clusters const& clusters,
122 RobustPolicy const& robust_policy, SideStrategy const& strategy,
124 : m_geometry1(geometry1)
125 , m_geometry2(geometry2)
127 , m_clusters(clusters)
128 , m_robust_policy(robust_policy)
129 , m_strategy(strategy)
134 template <typename TurnInfoMap>
135 inline void finalize_visit_info(TurnInfoMap& turn_info_map)
137 for (typename boost::range_iterator<Turns>::type
138 it = boost::begin(m_turns);
139 it != boost::end(m_turns);
142 turn_type& turn = *it;
143 for (int i = 0; i < 2; i++)
145 turn_operation_type& op = turn.operations[i];
146 if (op.visited.visited()
147 || op.visited.started()
148 || op.visited.finished() )
150 ring_identifier const ring_id
152 op.seg_id.source_index,
153 op.seg_id.multi_index,
156 turn_info_map[ring_id].has_traversed_turn = true;
158 if (op.operation == operation_continue)
160 // Continue operations should mark the other operation
162 turn_operation_type& other_op = turn.operations[1 - i];
163 ring_identifier const other_ring_id
165 other_op.seg_id.source_index,
166 other_op.seg_id.multi_index,
167 other_op.seg_id.ring_index
169 turn_info_map[other_ring_id].has_traversed_turn = true;
172 op.visited.finalize();
177 //! Sets visited for ALL turns traveling to the same turn
178 inline void set_visited_in_cluster(signed_size_type cluster_id,
179 signed_size_type rank)
181 typename Clusters::const_iterator mit = m_clusters.find(cluster_id);
182 BOOST_ASSERT(mit != m_clusters.end());
184 cluster_info const& cinfo = mit->second;
185 std::set<signed_size_type> const& ids = cinfo.turn_indices;
187 for (typename std::set<signed_size_type>::const_iterator it = ids.begin();
188 it != ids.end(); ++it)
190 signed_size_type const turn_index = *it;
191 turn_type& turn = m_turns[turn_index];
193 for (int i = 0; i < 2; i++)
195 turn_operation_type& op = turn.operations[i];
196 if (op.visited.none()
197 && op.enriched.rank == rank)
199 op.visited.set_visited();
204 inline void set_visited(turn_type& turn, turn_operation_type& op)
206 if (op.operation == detail::overlay::operation_continue)
208 // On "continue", all go in same direction so set "visited" for ALL
209 for (int i = 0; i < 2; i++)
211 turn_operation_type& turn_op = turn.operations[i];
212 if (turn_op.visited.none())
214 turn_op.visited.set_visited();
220 op.visited.set_visited();
222 if (turn.is_clustered())
224 set_visited_in_cluster(turn.cluster_id, op.enriched.rank);
228 inline bool is_visited(turn_type const& , turn_operation_type const& op,
229 signed_size_type , int) const
231 return op.visited.visited();
234 template <signed_size_type segment_identifier::*Member>
235 inline bool select_source_generic(bool switch_source,
236 segment_identifier const& current,
237 segment_identifier const& previous) const
240 ? current.*Member != previous.*Member
241 : current.*Member == previous.*Member;
244 inline bool select_source(signed_size_type turn_index,
245 segment_identifier const& candidate_seg_id,
246 segment_identifier const& previous_seg_id) const
248 // For uu/ii, only switch sources if indicated
249 turn_type const& turn = m_turns[turn_index];
251 #if defined(BOOST_GEOMETRY_DEBUG_TRAVERSAL_SWITCH_DETECTOR)
252 if (turn.switch_source)
254 std::cout << "Switch source at " << turn_index << std::endl;
258 std::cout << "DON'T SWITCH SOURCES at " << turn_index << std::endl;
261 if (OverlayType == overlay_buffer
262 || OverlayType == overlay_dissolve_union)
264 // Buffer does not use source_index (always 0).
265 return select_source_generic<&segment_identifier::multi_index>(
266 turn.switch_source, candidate_seg_id, previous_seg_id);
269 if (is_self_turn<OverlayType>(turn))
271 // Also, if it is a self-turn, stay on same ring (multi/ring)
272 return select_source_generic<&segment_identifier::multi_index>(
273 turn.switch_source, candidate_seg_id, previous_seg_id);
277 return select_source_generic<&segment_identifier::source_index>(
278 turn.switch_source, candidate_seg_id, previous_seg_id);
281 inline bool traverse_possible(signed_size_type turn_index) const
283 if (turn_index == -1)
288 turn_type const& turn = m_turns[turn_index];
290 // It is not a dead end if there is an operation to continue, or of
291 // there is a cluster (assuming for now we can get out of the cluster)
292 return turn.is_clustered()
293 || turn.has(target_operation)
294 || turn.has(operation_continue);
298 bool select_cc_operation(turn_type const& turn,
299 signed_size_type start_turn_index,
300 int& selected_op_index) const
302 // For "cc", take either one, but if there is a starting one,
303 // take that one. If next is dead end, skip that one.
304 // If both are valid candidates, take the one with minimal remaining
305 // distance (important for #mysql_23023665 in buffer).
307 // Initialize with 0, automatically assigned on first result
308 typename turn_operation_type::comparable_distance_type
309 min_remaining_distance = 0;
313 for (int i = 0; i < 2; i++)
315 turn_operation_type const& op = turn.operations[i];
317 signed_size_type const next_turn_index = op.enriched.get_next_turn_index();
319 if (! traverse_possible(next_turn_index))
325 || next_turn_index == start_turn_index
326 || op.remaining_distance < min_remaining_distance)
328 debug_traverse(turn, op, "First candidate cc", ! result);
329 debug_traverse(turn, op, "Candidate cc override (start)",
330 result && next_turn_index == start_turn_index);
331 debug_traverse(turn, op, "Candidate cc override (remaining)",
332 result && op.remaining_distance < min_remaining_distance);
334 selected_op_index = i;
335 min_remaining_distance = op.remaining_distance;
344 bool select_noncc_operation(turn_type const& turn,
345 signed_size_type turn_index,
346 segment_identifier const& previous_seg_id,
347 int& selected_op_index) const
351 for (int i = 0; i < 2; i++)
353 turn_operation_type const& op = turn.operations[i];
355 if (op.operation == target_operation
356 && ! op.visited.finished()
357 && ! op.visited.visited()
358 && (! result || select_source(turn_index, op.seg_id, previous_seg_id)))
360 selected_op_index = i;
361 debug_traverse(turn, op, "Candidate");
370 bool select_operation(const turn_type& turn,
371 signed_size_type turn_index,
372 signed_size_type start_turn_index,
373 segment_identifier const& previous_seg_id,
374 int& selected_op_index) const
377 selected_op_index = -1;
378 if (turn.both(operation_continue))
380 result = select_cc_operation(turn, start_turn_index,
385 result = select_noncc_operation(turn, turn_index,
386 previous_seg_id, selected_op_index);
390 debug_traverse(turn, turn.operations[selected_op_index], "Accepted");
396 inline int starting_operation_index(const turn_type& turn) const
398 for (int i = 0; i < 2; i++)
400 if (turn.operations[i].visited.started())
408 inline bool both_finished(const turn_type& turn) const
410 for (int i = 0; i < 2; i++)
412 if (! turn.operations[i].visited.finished())
420 inline int select_turn_in_cluster_union(std::size_t selected_rank,
421 typename sbs_type::rp const& ranked_point,
422 signed_size_type start_turn_index, int start_op_index) const
424 // Returns 0 if it not OK
425 // Returns 1 if it OK
426 // Returns 2 if it OK and start turn matches
427 // Returns 3 if it OK and start turn and start op both match
428 if (ranked_point.rank != selected_rank
429 || ranked_point.direction != sort_by_side::dir_to)
434 turn_type const& turn = m_turns[ranked_point.turn_index];
435 turn_operation_type const& op = turn.operations[ranked_point.operation_index];
437 // Check finalized: TODO: this should be finetuned, it is not necessary
438 if (op.visited.finalized())
443 if (OverlayType != overlay_dissolve_union
444 && (op.enriched.count_left != 0 || op.enriched.count_right == 0))
446 // Check counts: in some cases interior rings might be generated with
447 // polygons on both sides. For dissolve it can be anything.
451 return ranked_point.turn_index == start_turn_index
452 && ranked_point.operation_index == start_op_index ? 3
453 : ranked_point.turn_index == start_turn_index ? 2
458 inline bool select_from_cluster_union(signed_size_type& turn_index,
459 int& op_index, sbs_type& sbs,
460 signed_size_type start_turn_index, int start_op_index) const
462 std::vector<sort_by_side::rank_with_rings> aggregation;
463 sort_by_side::aggregate_operations(sbs, aggregation, m_turns, operation_union);
465 sort_by_side::rank_with_rings const& incoming = aggregation.front();
467 // Take the first one outgoing for the incoming region
468 std::size_t selected_rank = 0;
469 for (std::size_t i = 1; i < aggregation.size(); i++)
471 sort_by_side::rank_with_rings const& rwr = aggregation[i];
473 && rwr.region_id() == incoming.region_id())
475 selected_rank = rwr.rank;
482 for (std::size_t i = 1; i < sbs.m_ranked_points.size(); i++)
484 typename sbs_type::rp const& ranked_point = sbs.m_ranked_points[i];
486 if (ranked_point.rank > selected_rank)
488 // Sorted on rank, so it makes no sense to continue
493 = select_turn_in_cluster_union(selected_rank, ranked_point,
494 start_turn_index, start_op_index);
496 if (code > best_code)
498 // It is 1 or higher and matching better than previous
500 turn_index = ranked_point.turn_index;
501 op_index = ranked_point.operation_index;
508 inline bool analyze_cluster_intersection(signed_size_type& turn_index,
509 int& op_index, sbs_type const& sbs) const
511 std::vector<sort_by_side::rank_with_rings> aggregation;
512 sort_by_side::aggregate_operations(sbs, aggregation, m_turns, operation_intersection);
514 std::size_t selected_rank = 0;
517 // Detect specific pattern(s)
519 = intersection_pattern_common_interior1(selected_rank, aggregation)
520 || intersection_pattern_common_interior2(selected_rank, aggregation)
521 || intersection_pattern_common_interior3(selected_rank, aggregation)
522 || intersection_pattern_common_interior4(selected_rank, aggregation)
523 || intersection_pattern_common_interior5(selected_rank, aggregation)
524 || intersection_pattern_common_interior6(selected_rank, aggregation)
529 signed_size_type incoming_region_id = 0;
530 std::set<signed_size_type> outgoing_region_ids;
532 for (std::size_t i = 0; i < aggregation.size(); i++)
534 sort_by_side::rank_with_rings const& rwr = aggregation[i];
537 && rwr.traversable(m_turns)
538 && selected_rank == 0)
540 // Take the first (= right) where segments leave,
541 // having the polygon on the right side
542 selected_rank = rwr.rank;
547 && outgoing_region_ids.empty())
553 if (incoming_region_id == 0)
555 sort_by_side::ring_with_direction const& rwd = *rwr.rings.begin();
556 turn_type const& turn = m_turns[rwd.turn_index];
557 incoming_region_id = turn.operations[rwd.operation_index].enriched.region_id;
561 if (rwr.rings.size() == 1)
563 sort_by_side::ring_with_direction const& rwd = *rwr.rings.begin();
564 turn_type const& turn = m_turns[rwd.turn_index];
565 if (rwd.direction == sort_by_side::dir_to
566 && turn.both(operation_intersection))
569 turn_operation_type const& op = turn.operations[rwd.operation_index];
570 if (op.enriched.region_id != incoming_region_id
571 && op.enriched.isolated)
573 outgoing_region_ids.insert(op.enriched.region_id);
576 else if (! outgoing_region_ids.empty())
578 for (int i = 0; i < 2; i++)
580 signed_size_type const region_id = turn.operations[i].enriched.region_id;
581 if (outgoing_region_ids.count(region_id) == 1)
584 outgoing_region_ids.erase(region_id);
593 if (selected_rank > 0)
595 typename turn_operation_type::comparable_distance_type
596 min_remaining_distance = 0;
598 std::size_t selected_index = sbs.m_ranked_points.size();
599 for (std::size_t i = 0; i < sbs.m_ranked_points.size(); i++)
601 typename sbs_type::rp const& ranked_point = sbs.m_ranked_points[i];
603 if (ranked_point.rank == selected_rank)
605 turn_type const& ranked_turn = m_turns[ranked_point.turn_index];
606 turn_operation_type const& ranked_op = ranked_turn.operations[ranked_point.operation_index];
608 if (ranked_op.visited.finalized())
610 // This direction is already traveled before, the same
611 // cannot be traveled again
615 // Take turn with the smallest remaining distance
616 if (selected_index == sbs.m_ranked_points.size()
617 || ranked_op.remaining_distance < min_remaining_distance)
620 min_remaining_distance = ranked_op.remaining_distance;
625 if (selected_index < sbs.m_ranked_points.size())
627 typename sbs_type::rp const& ranked_point = sbs.m_ranked_points[selected_index];
628 turn_index = ranked_point.turn_index;
629 op_index = ranked_point.operation_index;
637 inline bool select_turn_from_cluster(signed_size_type& turn_index,
639 signed_size_type start_turn_index, int start_op_index,
640 segment_identifier const& previous_seg_id) const
642 bool const is_union = target_operation == operation_union;
644 turn_type const& turn = m_turns[turn_index];
645 BOOST_ASSERT(turn.is_clustered());
647 typename Clusters::const_iterator mit = m_clusters.find(turn.cluster_id);
648 BOOST_ASSERT(mit != m_clusters.end());
650 cluster_info const& cinfo = mit->second;
651 std::set<signed_size_type> const& ids = cinfo.turn_indices;
653 sbs_type sbs(m_strategy);
655 for (typename std::set<signed_size_type>::const_iterator sit = ids.begin();
656 sit != ids.end(); ++sit)
658 signed_size_type cluster_turn_index = *sit;
659 turn_type const& cluster_turn = m_turns[cluster_turn_index];
660 bool const departure_turn = cluster_turn_index == turn_index;
661 if (cluster_turn.discarded)
663 // Defensive check, discarded turns should not be in cluster
667 for (int i = 0; i < 2; i++)
669 sbs.add(cluster_turn.operations[i],
670 cluster_turn_index, i, previous_seg_id,
671 m_geometry1, m_geometry2,
676 if (! sbs.has_origin())
680 sbs.apply(turn.point);
686 result = select_from_cluster_union(turn_index, op_index, sbs,
687 start_turn_index, start_op_index);
691 result = analyze_cluster_intersection(turn_index, op_index, sbs);
696 inline bool analyze_ii_intersection(signed_size_type& turn_index, int& op_index,
697 turn_type const& current_turn,
698 segment_identifier const& previous_seg_id)
700 sbs_type sbs(m_strategy);
702 // Add this turn to the sort-by-side sorter
703 for (int i = 0; i < 2; i++)
705 sbs.add(current_turn.operations[i],
706 turn_index, i, previous_seg_id,
707 m_geometry1, m_geometry2,
711 if (! sbs.has_origin())
716 sbs.apply(current_turn.point);
718 bool result = analyze_cluster_intersection(turn_index, op_index, sbs);
723 inline void change_index_for_self_turn(signed_size_type& to_vertex_index,
724 turn_type const& start_turn,
725 turn_operation_type const& start_op,
726 int start_op_index) const
728 if (OverlayType != overlay_buffer
729 && OverlayType != overlay_dissolve_union
730 && OverlayType != overlay_dissolve_intersection)
735 const bool allow_uu = OverlayType != overlay_buffer;
737 // It travels to itself, can happen. If this is a buffer, it can
738 // sometimes travel to itself in the following configuration:
742 // | +---*----+ *: one turn, with segment index 2/7
744 // | +---C | C: closing point (start/end)
748 // If it starts on segment 2 and travels to itself on segment 2, that
749 // should be corrected to 7 because that is the shortest path
751 // Also a uu turn (touching with another buffered ring) might have this
752 // apparent configuration, but there it should
753 // always travel the whole ring
755 turn_operation_type const& other_op
756 = start_turn.operations[1 - start_op_index];
759 = (allow_uu || ! start_turn.both(operation_union))
760 && start_op.seg_id.source_index == other_op.seg_id.source_index
761 && start_op.seg_id.multi_index == other_op.seg_id.multi_index
762 && start_op.seg_id.ring_index == other_op.seg_id.ring_index
763 && start_op.seg_id.segment_index == to_vertex_index;
765 #if defined(BOOST_GEOMETRY_DEBUG_TRAVERSE)
766 std::cout << " WARNING: self-buffer "
767 << " correct=" << correct
768 << " turn=" << operation_char(start_turn.operations[0].operation)
769 << operation_char(start_turn.operations[1].operation)
770 << " start=" << start_op.seg_id.segment_index
771 << " from=" << to_vertex_index
772 << " to=" << other_op.enriched.travels_to_vertex_index
778 to_vertex_index = other_op.enriched.travels_to_vertex_index;
782 bool select_turn_from_enriched(signed_size_type& turn_index,
783 segment_identifier& previous_seg_id,
784 signed_size_type& to_vertex_index,
785 signed_size_type start_turn_index,
787 turn_type const& previous_turn,
788 turn_operation_type const& previous_op,
791 to_vertex_index = -1;
793 if (previous_op.enriched.next_ip_index < 0)
795 // There is no next IP on this segment
796 if (previous_op.enriched.travels_to_vertex_index < 0
797 || previous_op.enriched.travels_to_ip_index < 0)
802 to_vertex_index = previous_op.enriched.travels_to_vertex_index;
805 previous_op.enriched.travels_to_ip_index == start_turn_index)
807 change_index_for_self_turn(to_vertex_index, previous_turn,
808 previous_op, start_op_index);
811 turn_index = previous_op.enriched.travels_to_ip_index;
812 previous_seg_id = previous_op.seg_id;
816 // Take the next IP on this segment
817 turn_index = previous_op.enriched.next_ip_index;
818 previous_seg_id = previous_op.seg_id;
823 bool select_turn(signed_size_type start_turn_index, int start_op_index,
824 signed_size_type& turn_index,
826 int previous_op_index,
827 signed_size_type previous_turn_index,
828 segment_identifier const& previous_seg_id,
831 turn_type const& current_turn = m_turns[turn_index];
833 if (target_operation == operation_intersection)
835 bool const back_at_start_cluster
836 = current_turn.is_clustered()
837 && m_turns[start_turn_index].cluster_id == current_turn.cluster_id;
839 if (turn_index == start_turn_index || back_at_start_cluster)
841 // Intersection can always be finished if returning
842 turn_index = start_turn_index;
843 op_index = start_op_index;
847 if (! current_turn.is_clustered()
848 && current_turn.both(operation_intersection))
850 if (analyze_ii_intersection(turn_index, op_index,
851 current_turn, previous_seg_id))
858 if (current_turn.is_clustered())
860 if (! select_turn_from_cluster(turn_index, op_index,
861 start_turn_index, start_op_index, previous_seg_id))
866 if (is_start && turn_index == previous_turn_index)
868 op_index = previous_op_index;
873 op_index = starting_operation_index(current_turn);
876 if (both_finished(current_turn))
881 if (! select_operation(current_turn, turn_index,
894 Geometry1 const& m_geometry1;
895 Geometry2 const& m_geometry2;
897 Clusters const& m_clusters;
898 RobustPolicy const& m_robust_policy;
899 SideStrategy m_strategy;
905 }} // namespace detail::overlay
906 #endif // DOXYGEN_NO_DETAIL
908 }} // namespace boost::geometry
910 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_TRAVERSAL_HPP