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.
7 // Modifications copyright (c) 2017 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.hpp>
35 #include <boost/geometry/algorithms/detail/ring_identifier.hpp>
36 #include <boost/geometry/algorithms/detail/overlay/handle_colocations.hpp>
37 #include <boost/geometry/algorithms/detail/overlay/handle_self_turns.hpp>
38 #include <boost/geometry/algorithms/detail/overlay/is_self_turn.hpp>
39 #include <boost/geometry/algorithms/detail/overlay/less_by_segment_ratio.hpp>
40 #include <boost/geometry/algorithms/detail/overlay/overlay_type.hpp>
41 #include <boost/geometry/policies/robustness/robust_type.hpp>
43 #ifdef BOOST_GEOMETRY_DEBUG_ENRICH
44 # include <boost/geometry/algorithms/detail/overlay/check_enrich.hpp>
48 namespace boost { namespace geometry
51 #ifndef DOXYGEN_NO_DETAIL
52 namespace detail { namespace overlay
55 template <typename Turns>
58 discarded_turn(Turns const& turns)
62 template <typename IndexedTurn>
63 inline bool operator()(IndexedTurn const& indexed) const
65 return m_turns[indexed.turn_index].discarded;
71 // Sorts IP-s of this ring on segment-identifier, and if on same segment,
73 // Then assigns for each IP which is the next IP on this segment,
74 // plus the vertex-index to travel to, plus the next IP
75 // (might be on another segment)
78 bool Reverse1, bool Reverse2,
81 typename Geometry1, typename Geometry2,
82 typename RobustPolicy,
85 inline void enrich_sort(Operations& operations,
87 Geometry1 const& geometry1,
88 Geometry2 const& geometry2,
89 RobustPolicy const& robust_policy,
90 SideStrategy const& strategy)
92 std::sort(boost::begin(operations),
93 boost::end(operations),
97 typename boost::range_value<Operations>::type,
102 >(turns, geometry1, geometry2, robust_policy, strategy));
106 template <typename Operations, typename Turns>
107 inline void enrich_assign(Operations& operations, Turns& turns,
110 typedef typename boost::range_value<Turns>::type turn_type;
111 typedef typename turn_type::turn_operation_type op_type;
112 typedef typename boost::range_iterator<Operations>::type iterator_type;
115 if (operations.size() > 0)
117 // Assign travel-to-vertex/ip index for each turning point.
118 // Iterator "next" is circular
120 geometry::ever_circling_range_iterator<Operations const> next(operations);
123 for (iterator_type it = boost::begin(operations);
124 it != boost::end(operations); ++it)
126 turn_type& turn = turns[it->turn_index];
127 op_type& op = turn.operations[it->operation_index];
129 if (check_turns && it->turn_index == next->turn_index)
131 // Normal behaviour: next points at next turn, increase next.
132 // For dissolve this should not be done, turn_index is often
133 // the same for two consecutive operations
137 // Cluster behaviour: next should point after cluster, unless
138 // their seg_ids are not the same
139 // (For dissolve, this is still to be examined - TODO)
140 while (turn.is_clustered()
141 && it->turn_index != next->turn_index
142 && turn.cluster_id == turns[next->turn_index].cluster_id
143 && op.seg_id == turns[next->turn_index].operations[next->operation_index].seg_id)
148 turn_type const& next_turn = turns[next->turn_index];
149 op_type const& next_op = next_turn.operations[next->operation_index];
151 op.enriched.travels_to_ip_index
152 = static_cast<signed_size_type>(next->turn_index);
153 op.enriched.travels_to_vertex_index
154 = next->subject->seg_id.segment_index;
156 if (op.seg_id.segment_index == next_op.seg_id.segment_index
157 && op.fraction < next_op.fraction)
159 // Next turn is located further on same segment
160 // assign next_ip_index
161 // (this is one not circular therefore fraction is considered)
162 op.enriched.next_ip_index = static_cast<signed_size_type>(next->turn_index);
173 #ifdef BOOST_GEOMETRY_DEBUG_ENRICH
175 for (iterator_type it = boost::begin(operations);
176 it != boost::end(operations);
179 op_type const& op = turns[it->turn_index]
180 .operations[it->operation_index];
182 std::cout << it->turn_index
183 << " cl=" << turns[it->turn_index].cluster_id
184 << " meth=" << method_char(turns[it->turn_index].method)
185 << " seg=" << op.seg_id
186 << " dst=" << op.fraction // needs define
187 << " op=" << operation_char(turns[it->turn_index].operations[0].operation)
188 << operation_char(turns[it->turn_index].operations[1].operation)
189 << " (" << operation_char(op.operation) << ")"
190 << " nxt=" << op.enriched.next_ip_index
191 << " / " << op.enriched.travels_to_ip_index
192 << " [vx " << op.enriched.travels_to_vertex_index << "]"
193 << std::boolalpha << turns[it->turn_index].discarded
203 template <typename Operations, typename Turns>
204 inline void enrich_adapt(Operations& operations, Turns& turns)
206 typedef typename boost::range_value<Turns>::type turn_type;
207 typedef typename turn_type::turn_operation_type op_type;
208 typedef typename boost::range_value<Operations>::type indexed_turn_type;
210 if (operations.size() < 3)
212 // If it is empty, or contains one or two turns, it makes no sense
216 // Operations is a vector of indexed_turn_operation<>
219 std::size_t const x = operations.size() - 1;
220 bool next_phase = false;
222 for (std::size_t i = 0; i < operations.size(); i++)
224 indexed_turn_type const& indexed = operations[i];
226 turn_type& turn = turns[indexed.turn_index];
227 op_type& op = turn.operations[indexed.operation_index];
229 // Previous/next index
230 std::size_t const p = i > 0 ? i - 1 : x;
231 std::size_t const n = i < x ? i + 1 : 0;
233 turn_type const& next_turn = turns[operations[n].turn_index];
234 op_type const& next_op = next_turn.operations[operations[n].operation_index];
236 if (op.seg_id.segment_index == next_op.seg_id.segment_index)
238 turn_type const& prev_turn = turns[operations[p].turn_index];
239 op_type const& prev_op = prev_turn.operations[operations[p].operation_index];
240 if (op.seg_id.segment_index == prev_op.seg_id.segment_index)
242 op.enriched.startable = false;
253 // Discard turns which are both non-startable
255 for (typename boost::range_iterator<Turns>::type
256 it = boost::begin(turns);
257 it != boost::end(turns);
260 turn_type& turn = *it;
261 if (! turn.operations[0].enriched.startable
262 && ! turn.operations[1].enriched.startable)
264 turn.discarded = true;
274 // Remove discarded turns from operations to avoid having them as next turn
275 discarded_turn<Turns> const predicate(turns);
276 operations.erase(std::remove_if(boost::begin(operations),
277 boost::end(operations), predicate), boost::end(operations));
280 template <typename Turns, typename MappedVector>
281 inline void create_map(Turns const& turns, MappedVector& mapped_vector)
283 typedef typename boost::range_value<Turns>::type turn_type;
284 typedef typename turn_type::container_type container_type;
285 typedef typename MappedVector::mapped_type mapped_type;
286 typedef typename boost::range_value<mapped_type>::type indexed_type;
288 std::size_t index = 0;
289 for (typename boost::range_iterator<Turns const>::type
290 it = boost::begin(turns);
291 it != boost::end(turns);
294 // Add all (non discarded) operations on this ring
295 // Blocked operations or uu on clusters (for intersection)
296 // should be included, to block potential paths in clusters
297 turn_type const& turn = *it;
303 std::size_t op_index = 0;
304 for (typename boost::range_iterator<container_type const>::type
305 op_it = boost::begin(turn.operations);
306 op_it != boost::end(turn.operations);
309 ring_identifier const ring_id
311 op_it->seg_id.source_index,
312 op_it->seg_id.multi_index,
313 op_it->seg_id.ring_index
315 mapped_vector[ring_id].push_back
317 indexed_type(index, op_index, *op_it,
318 it->operations[1 - op_index].seg_id)
324 template <typename Point1, typename Point2>
325 inline typename geometry::coordinate_type<Point1>::type
326 distance_measure(Point1 const& a, Point2 const& b)
328 // TODO: use comparable distance for point-point instead - but that
329 // causes currently cycling include problems
330 typedef typename geometry::coordinate_type<Point1>::type ctype;
331 ctype const dx = get<0>(a) - get<0>(b);
332 ctype const dy = get<1>(a) - get<1>(b);
333 return dx * dx + dy * dy;
336 template <typename Turns>
337 inline void calculate_remaining_distance(Turns& turns)
339 typedef typename boost::range_value<Turns>::type turn_type;
340 typedef typename turn_type::turn_operation_type op_type;
342 for (typename boost::range_iterator<Turns>::type
343 it = boost::begin(turns);
344 it != boost::end(turns);
347 turn_type& turn = *it;
349 op_type& op0 = turn.operations[0];
350 op_type& op1 = turn.operations[1];
352 if (op0.remaining_distance != 0
353 || op1.remaining_distance != 0)
358 signed_size_type const to_index0 = op0.enriched.get_next_turn_index();
359 signed_size_type const to_index1 = op1.enriched.get_next_turn_index();
362 && to_index0 != to_index1)
364 op0.remaining_distance = distance_measure(turn.point, turns[to_index0].point);
365 op1.remaining_distance = distance_measure(turn.point, turns[to_index1].point);
371 }} // namespace detail::overlay
372 #endif //DOXYGEN_NO_DETAIL
377 \brief All intersection points are enriched with successor information
379 \tparam Turns type of intersection container
380 (e.g. vector of "intersection/turn point"'s)
381 \tparam Clusters type of cluster container
382 \tparam Geometry1 \tparam_geometry
383 \tparam Geometry2 \tparam_geometry
384 \tparam SideStrategy side strategy type
385 \param turns container containing intersection points
386 \param clusters container containing clusters
387 \param geometry1 \param_geometry
388 \param geometry2 \param_geometry
389 \param robust_policy policy to handle robustness issues
390 \param strategy strategy
394 bool Reverse1, bool Reverse2,
395 overlay_type OverlayType,
398 typename Geometry1, typename Geometry2,
399 typename RobustPolicy,
400 typename SideStrategy
402 inline void enrich_intersection_points(Turns& turns,
404 Geometry1 const& geometry1, Geometry2 const& geometry2,
405 RobustPolicy const& robust_policy,
406 SideStrategy const& strategy)
408 static const detail::overlay::operation_type target_operation
409 = detail::overlay::operation_from_overlay<OverlayType>::value;
410 static const detail::overlay::operation_type opposite_operation
411 = target_operation == detail::overlay::operation_union
412 ? detail::overlay::operation_intersection
413 : detail::overlay::operation_union;
414 static const bool is_dissolve = OverlayType == overlay_dissolve;
416 typedef typename boost::range_value<Turns>::type turn_type;
417 typedef typename turn_type::turn_operation_type op_type;
418 typedef detail::overlay::indexed_turn_operation
421 > indexed_turn_operation;
426 std::vector<indexed_turn_operation>
427 > mapped_vector_type;
430 bool const has_colocations
431 = detail::overlay::handle_colocations<Reverse1, Reverse2, OverlayType>(turns,
432 clusters, geometry1, geometry2);
434 // Discard turns not part of target overlay
435 for (typename boost::range_iterator<Turns>::type
436 it = boost::begin(turns);
437 it != boost::end(turns);
440 turn_type& turn = *it;
442 if (turn.both(detail::overlay::operation_none)
443 || turn.both(opposite_operation)
444 || turn.both(detail::overlay::operation_blocked)
445 || (detail::overlay::is_self_turn<OverlayType>(turn)
446 && ! turn.is_clustered()
447 && ! turn.both(target_operation)))
449 // For all operations, discard xx and none/none
450 // For intersections, remove uu to avoid the need to travel
451 // a union (during intersection) in uu/cc clusters (e.g. #31,#32,#33)
452 // The ux is necessary to indicate impossible paths
453 // (especially if rescaling is removed)
455 // Similarly, for union, discard ii and ix
457 // For self-turns, only keep uu / ii
459 turn.discarded = true;
460 turn.cluster_id = -1;
465 && turn.both(detail::overlay::operation_continue))
473 detail::overlay::discard_closed_turns
477 >::apply(turns, clusters, geometry1, geometry2);
478 detail::overlay::discard_open_turns
482 >::apply(turns, clusters, geometry1, geometry2);
485 // Create a map of vectors of indexed operation-types to be able
486 // to sort intersection points PER RING
487 mapped_vector_type mapped_vector;
489 detail::overlay::create_map(turns, mapped_vector);
491 // No const-iterator; contents of mapped copy is temporary,
492 // and changed by enrich
493 for (typename mapped_vector_type::iterator mit
494 = mapped_vector.begin();
495 mit != mapped_vector.end();
498 #ifdef BOOST_GEOMETRY_DEBUG_ENRICH
499 std::cout << "ENRICH-sort Ring "
500 << mit->first << std::endl;
502 detail::overlay::enrich_sort<Reverse1, Reverse2>(
504 geometry1, geometry2,
505 robust_policy, strategy);
508 for (typename mapped_vector_type::iterator mit
509 = mapped_vector.begin();
510 mit != mapped_vector.end();
513 #ifdef BOOST_GEOMETRY_DEBUG_ENRICH
514 std::cout << "ENRICH-assign Ring "
515 << mit->first << std::endl;
519 detail::overlay::enrich_adapt(mit->second, turns);
522 detail::overlay::enrich_assign(mit->second, turns, ! is_dissolve);
527 // First gather cluster properties (using even clusters with
528 // discarded turns - for open turns), then clean up clusters
529 detail::overlay::gather_cluster_properties
534 >(clusters, turns, target_operation,
535 geometry1, geometry2, strategy);
537 detail::overlay::cleanup_clusters(turns, clusters);
542 detail::overlay::calculate_remaining_distance(turns);
545 #ifdef BOOST_GEOMETRY_DEBUG_ENRICH
546 //detail::overlay::check_graph(turns, for_operation);
551 }} // namespace boost::geometry
553 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_ENRICH_HPP