1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2017 Adam Wulkiewicz, Lodz, Poland.
6 // This file was modified by Oracle on 2017-2020.
7 // Modifications copyright (c) 2017-2020 Oracle and/or its affiliates.
9 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
11 // Use, modification and distribution is subject to the Boost Software License,
12 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
13 // http://www.boost.org/LICENSE_1_0.txt)
15 #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_ENRICH_HPP
16 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_ENRICH_HPP
24 #ifdef BOOST_GEOMETRY_DEBUG_ENRICH
26 # include <boost/geometry/algorithms/detail/overlay/debug_turn_info.hpp>
27 # include <boost/geometry/io/wkt/wkt.hpp>
28 # if ! defined(BOOST_GEOMETRY_DEBUG_IDENTIFIER)
29 # define BOOST_GEOMETRY_DEBUG_IDENTIFIER
33 #include <boost/range/begin.hpp>
34 #include <boost/range/end.hpp>
35 #include <boost/range/value_type.hpp>
37 #include <boost/geometry/algorithms/detail/ring_identifier.hpp>
38 #include <boost/geometry/algorithms/detail/overlay/handle_colocations.hpp>
39 #include <boost/geometry/algorithms/detail/overlay/handle_self_turns.hpp>
40 #include <boost/geometry/algorithms/detail/overlay/is_self_turn.hpp>
41 #include <boost/geometry/algorithms/detail/overlay/less_by_segment_ratio.hpp>
42 #include <boost/geometry/algorithms/detail/overlay/overlay_type.hpp>
43 #include <boost/geometry/policies/robustness/robust_type.hpp>
45 #ifdef BOOST_GEOMETRY_DEBUG_ENRICH
46 # include <boost/geometry/algorithms/detail/overlay/check_enrich.hpp>
50 namespace boost { namespace geometry
53 #ifndef DOXYGEN_NO_DETAIL
54 namespace detail { namespace overlay
57 template <typename Turns>
58 struct discarded_indexed_turn
60 discarded_indexed_turn(Turns const& turns)
64 template <typename IndexedTurn>
65 inline bool operator()(IndexedTurn const& indexed) const
67 return m_turns[indexed.turn_index].discarded;
73 // Sorts IP-s of this ring on segment-identifier, and if on same segment,
75 // Then assigns for each IP which is the next IP on this segment,
76 // plus the vertex-index to travel to, plus the next IP
77 // (might be on another segment)
80 bool Reverse1, bool Reverse2,
83 typename Geometry1, typename Geometry2,
84 typename RobustPolicy,
87 inline void enrich_sort(Operations& operations,
89 Geometry1 const& geometry1,
90 Geometry2 const& geometry2,
91 RobustPolicy const& robust_policy,
92 SideStrategy const& strategy)
94 std::sort(boost::begin(operations),
95 boost::end(operations),
99 typename boost::range_value<Operations>::type,
100 Geometry1, Geometry2,
104 >(turns, geometry1, geometry2, robust_policy, strategy));
108 template <typename Operations, typename Turns>
109 inline void enrich_assign(Operations& operations, Turns& turns,
112 typedef typename boost::range_value<Turns>::type turn_type;
113 typedef typename turn_type::turn_operation_type op_type;
114 typedef typename boost::range_iterator<Operations>::type iterator_type;
117 if (operations.size() > 0)
119 // Assign travel-to-vertex/ip index for each turning point.
120 // Iterator "next" is circular
122 geometry::ever_circling_range_iterator<Operations const> next(operations);
125 for (iterator_type it = boost::begin(operations);
126 it != boost::end(operations); ++it)
128 turn_type& turn = turns[it->turn_index];
129 op_type& op = turn.operations[it->operation_index];
131 if (check_turns && it->turn_index == next->turn_index)
133 // Normal behaviour: next points at next turn, increase next.
134 // For dissolve this should not be done, turn_index is often
135 // the same for two consecutive operations
139 // Cluster behaviour: next should point after cluster, unless
140 // their seg_ids are not the same
141 // (For dissolve, this is still to be examined - TODO)
142 while (turn.is_clustered()
143 && it->turn_index != next->turn_index
144 && turn.cluster_id == turns[next->turn_index].cluster_id
145 && op.seg_id == turns[next->turn_index].operations[next->operation_index].seg_id)
150 turn_type const& next_turn = turns[next->turn_index];
151 op_type const& next_op = next_turn.operations[next->operation_index];
153 op.enriched.travels_to_ip_index
154 = static_cast<signed_size_type>(next->turn_index);
155 op.enriched.travels_to_vertex_index
156 = next->subject->seg_id.segment_index;
158 if (op.seg_id.segment_index == next_op.seg_id.segment_index
159 && op.fraction < next_op.fraction)
161 // Next turn is located further on same segment
162 // assign next_ip_index
163 // (this is one not circular therefore fraction is considered)
164 op.enriched.next_ip_index = static_cast<signed_size_type>(next->turn_index);
175 #ifdef BOOST_GEOMETRY_DEBUG_ENRICH
177 for (iterator_type it = boost::begin(operations);
178 it != boost::end(operations);
181 op_type const& op = turns[it->turn_index]
182 .operations[it->operation_index];
184 std::cout << it->turn_index
185 << " cl=" << turns[it->turn_index].cluster_id
186 << " meth=" << method_char(turns[it->turn_index].method)
187 << " seg=" << op.seg_id
188 << " dst=" << op.fraction // needs define
189 << " op=" << operation_char(turns[it->turn_index].operations[0].operation)
190 << operation_char(turns[it->turn_index].operations[1].operation)
191 << " (" << operation_char(op.operation) << ")"
192 << " nxt=" << op.enriched.next_ip_index
193 << " / " << op.enriched.travels_to_ip_index
194 << " [vx " << op.enriched.travels_to_vertex_index << "]"
195 << std::boolalpha << turns[it->turn_index].discarded
205 template <typename Operations, typename Turns>
206 inline void enrich_adapt(Operations& operations, Turns& turns)
208 typedef typename boost::range_value<Turns>::type turn_type;
209 typedef typename turn_type::turn_operation_type op_type;
210 typedef typename boost::range_value<Operations>::type indexed_turn_type;
212 if (operations.size() < 3)
214 // If it is empty, or contains one or two turns, it makes no sense
218 // Operations is a vector of indexed_turn_operation<>
221 std::size_t const x = operations.size() - 1;
222 bool next_phase = false;
224 for (std::size_t i = 0; i < operations.size(); i++)
226 indexed_turn_type const& indexed = operations[i];
228 turn_type& turn = turns[indexed.turn_index];
229 op_type& op = turn.operations[indexed.operation_index];
231 // Previous/next index
232 std::size_t const p = i > 0 ? i - 1 : x;
233 std::size_t const n = i < x ? i + 1 : 0;
235 turn_type const& next_turn = turns[operations[n].turn_index];
236 op_type const& next_op = next_turn.operations[operations[n].operation_index];
238 if (op.seg_id.segment_index == next_op.seg_id.segment_index)
240 turn_type const& prev_turn = turns[operations[p].turn_index];
241 op_type const& prev_op = prev_turn.operations[operations[p].operation_index];
242 if (op.seg_id.segment_index == prev_op.seg_id.segment_index)
244 op.enriched.startable = false;
255 // Discard turns which are both non-startable
257 for (typename boost::range_iterator<Turns>::type
258 it = boost::begin(turns);
259 it != boost::end(turns);
262 turn_type& turn = *it;
263 if (! turn.operations[0].enriched.startable
264 && ! turn.operations[1].enriched.startable)
266 turn.discarded = true;
276 // Remove discarded turns from operations to avoid having them as next turn
277 discarded_indexed_turn<Turns> const predicate(turns);
278 operations.erase(std::remove_if(boost::begin(operations),
279 boost::end(operations), predicate), boost::end(operations));
282 struct enriched_map_default_include_policy
284 template <typename Operation>
285 static inline bool include(Operation const& )
287 // By default include all operations
292 template <typename Turns, typename MappedVector, typename IncludePolicy>
293 inline void create_map(Turns const& turns, MappedVector& mapped_vector,
294 IncludePolicy const& include_policy)
296 typedef typename boost::range_value<Turns>::type turn_type;
297 typedef typename turn_type::container_type container_type;
298 typedef typename MappedVector::mapped_type mapped_type;
299 typedef typename boost::range_value<mapped_type>::type indexed_type;
301 std::size_t index = 0;
302 for (typename boost::range_iterator<Turns const>::type
303 it = boost::begin(turns);
304 it != boost::end(turns);
307 // Add all (non discarded) operations on this ring
308 // Blocked operations or uu on clusters (for intersection)
309 // should be included, to block potential paths in clusters
310 turn_type const& turn = *it;
316 std::size_t op_index = 0;
317 for (typename boost::range_iterator<container_type const>::type
318 op_it = boost::begin(turn.operations);
319 op_it != boost::end(turn.operations);
322 if (include_policy.include(op_it->operation))
324 ring_identifier const ring_id
326 op_it->seg_id.source_index,
327 op_it->seg_id.multi_index,
328 op_it->seg_id.ring_index
330 mapped_vector[ring_id].push_back
332 indexed_type(index, op_index, *op_it,
333 it->operations[1 - op_index].seg_id)
340 template <typename Point1, typename Point2>
341 inline typename geometry::coordinate_type<Point1>::type
342 distance_measure(Point1 const& a, Point2 const& b)
344 // TODO: use comparable distance for point-point instead - but that
345 // causes currently cycling include problems
346 typedef typename geometry::coordinate_type<Point1>::type ctype;
347 ctype const dx = get<0>(a) - get<0>(b);
348 ctype const dy = get<1>(a) - get<1>(b);
349 return dx * dx + dy * dy;
352 template <typename Turns>
353 inline void calculate_remaining_distance(Turns& turns)
355 typedef typename boost::range_value<Turns>::type turn_type;
356 typedef typename turn_type::turn_operation_type op_type;
358 for (typename boost::range_iterator<Turns>::type
359 it = boost::begin(turns);
360 it != boost::end(turns);
363 turn_type& turn = *it;
365 op_type& op0 = turn.operations[0];
366 op_type& op1 = turn.operations[1];
368 if (op0.remaining_distance != 0
369 || op1.remaining_distance != 0)
374 signed_size_type const to_index0 = op0.enriched.get_next_turn_index();
375 signed_size_type const to_index1 = op1.enriched.get_next_turn_index();
378 && to_index0 != to_index1)
380 op0.remaining_distance = distance_measure(turn.point, turns[to_index0].point);
381 op1.remaining_distance = distance_measure(turn.point, turns[to_index1].point);
387 }} // namespace detail::overlay
388 #endif //DOXYGEN_NO_DETAIL
393 \brief All intersection points are enriched with successor information
395 \tparam Turns type of intersection container
396 (e.g. vector of "intersection/turn point"'s)
397 \tparam Clusters type of cluster container
398 \tparam Geometry1 \tparam_geometry
399 \tparam Geometry2 \tparam_geometry
400 \tparam PointInGeometryStrategy point in geometry strategy type
401 \param turns container containing intersection points
402 \param clusters container containing clusters
403 \param geometry1 \param_geometry
404 \param geometry2 \param_geometry
405 \param robust_policy policy to handle robustness issues
406 \param strategy point in geometry strategy
410 bool Reverse1, bool Reverse2,
411 overlay_type OverlayType,
414 typename Geometry1, typename Geometry2,
415 typename RobustPolicy,
416 typename IntersectionStrategy
418 inline void enrich_intersection_points(Turns& turns,
420 Geometry1 const& geometry1, Geometry2 const& geometry2,
421 RobustPolicy const& robust_policy,
422 IntersectionStrategy const& strategy)
424 static const detail::overlay::operation_type target_operation
425 = detail::overlay::operation_from_overlay<OverlayType>::value;
426 static const detail::overlay::operation_type opposite_operation
427 = target_operation == detail::overlay::operation_union
428 ? detail::overlay::operation_intersection
429 : detail::overlay::operation_union;
430 static const bool is_dissolve = OverlayType == overlay_dissolve;
432 typedef typename boost::range_value<Turns>::type turn_type;
433 typedef typename turn_type::turn_operation_type op_type;
434 typedef detail::overlay::indexed_turn_operation
437 > indexed_turn_operation;
442 std::vector<indexed_turn_operation>
443 > mapped_vector_type;
445 // From here on, turn indexes are used (in clusters, next_index, etc)
446 // and may only be flagged as discarded
449 bool const has_colocations
450 = detail::overlay::handle_colocations<Reverse1, Reverse2, OverlayType>(turns,
451 clusters, geometry1, geometry2);
453 // Discard turns not part of target overlay
454 for (typename boost::range_iterator<Turns>::type
455 it = boost::begin(turns);
456 it != boost::end(turns);
459 turn_type& turn = *it;
461 if (turn.both(detail::overlay::operation_none)
462 || turn.both(opposite_operation)
463 || turn.both(detail::overlay::operation_blocked)
464 || (detail::overlay::is_self_turn<OverlayType>(turn)
465 && ! turn.is_clustered()
466 && ! turn.both(target_operation)))
468 // For all operations, discard xx and none/none
469 // For intersections, remove uu to avoid the need to travel
470 // a union (during intersection) in uu/cc clusters (e.g. #31,#32,#33)
471 // The ux is necessary to indicate impossible paths
472 // (especially if rescaling is removed)
474 // Similarly, for union, discard ii and ix
476 // For self-turns, only keep uu / ii
478 turn.discarded = true;
479 turn.cluster_id = -1;
484 && turn.both(detail::overlay::operation_continue))
492 detail::overlay::discard_closed_turns
496 >::apply(turns, clusters, geometry1, geometry2,
498 detail::overlay::discard_open_turns
502 >::apply(turns, clusters, geometry1, geometry2,
506 // Create a map of vectors of indexed operation-types to be able
507 // to sort intersection points PER RING
508 mapped_vector_type mapped_vector;
510 detail::overlay::create_map(turns, mapped_vector,
511 detail::overlay::enriched_map_default_include_policy());
513 // No const-iterator; contents of mapped copy is temporary,
514 // and changed by enrich
515 for (typename mapped_vector_type::iterator mit
516 = mapped_vector.begin();
517 mit != mapped_vector.end();
520 #ifdef BOOST_GEOMETRY_DEBUG_ENRICH
521 std::cout << "ENRICH-sort Ring "
522 << mit->first << std::endl;
524 detail::overlay::enrich_sort<Reverse1, Reverse2>(
526 geometry1, geometry2,
527 robust_policy, strategy.get_side_strategy());
530 for (typename mapped_vector_type::iterator mit
531 = mapped_vector.begin();
532 mit != mapped_vector.end();
535 #ifdef BOOST_GEOMETRY_DEBUG_ENRICH
536 std::cout << "ENRICH-assign Ring "
537 << mit->first << std::endl;
541 detail::overlay::enrich_adapt(mit->second, turns);
544 detail::overlay::enrich_assign(mit->second, turns, ! is_dissolve);
549 // First gather cluster properties (using even clusters with
550 // discarded turns - for open turns), then clean up clusters
551 detail::overlay::gather_cluster_properties
556 >(clusters, turns, target_operation,
557 geometry1, geometry2, strategy.get_side_strategy());
559 detail::overlay::cleanup_clusters(turns, clusters);
564 detail::overlay::calculate_remaining_distance(turns);
567 #ifdef BOOST_GEOMETRY_DEBUG_ENRICH
568 //detail::overlay::check_graph(turns, for_operation);
573 }} // namespace boost::geometry
575 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_ENRICH_HPP