1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 
3 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2017 Adam Wulkiewicz, Lodz, Poland.
5 
6 // This file was modified by Oracle on 2017-2021.
7 // Modifications copyright (c) 2017-2020 Oracle and/or its affiliates.
8 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
9 
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)
13 
14 #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_ENRICH_HPP
15 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_ENRICH_HPP
16 
17 #include <cstddef>
18 #include <algorithm>
19 #include <map>
20 #include <set>
21 #include <vector>
22 
23 #ifdef BOOST_GEOMETRY_DEBUG_ENRICH
24 #  include <iostream>
25 #  include <boost/geometry/algorithms/detail/overlay/debug_turn_info.hpp>
26 #  include <boost/geometry/io/wkt/wkt.hpp>
27 #  if ! defined(BOOST_GEOMETRY_DEBUG_IDENTIFIER)
28 #    define BOOST_GEOMETRY_DEBUG_IDENTIFIER
29   #endif
30 #endif
31 
32 #include <boost/range/begin.hpp>
33 #include <boost/range/end.hpp>
34 #include <boost/range/value_type.hpp>
35 
36 #include <boost/geometry/algorithms/detail/ring_identifier.hpp>
37 #include <boost/geometry/algorithms/detail/overlay/discard_duplicate_turns.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>
44 
45 #ifdef BOOST_GEOMETRY_DEBUG_ENRICH
46 #  include <boost/geometry/algorithms/detail/overlay/check_enrich.hpp>
47 #endif
48 
49 
50 namespace boost { namespace geometry
51 {
52 
53 #ifndef DOXYGEN_NO_DETAIL
54 namespace detail { namespace overlay
55 {
56 
57 template <typename Turns>
58 struct discarded_indexed_turn
59 {
discarded_indexed_turnboost::geometry::detail::overlay::discarded_indexed_turn60     discarded_indexed_turn(Turns const& turns)
61         : m_turns(turns)
62     {}
63 
64     template <typename IndexedTurn>
operator ()boost::geometry::detail::overlay::discarded_indexed_turn65     inline bool operator()(IndexedTurn const& indexed) const
66     {
67         return m_turns[indexed.turn_index].discarded;
68     }
69 
70     Turns const& m_turns;
71 };
72 
73 // Sorts IP-s of this ring on segment-identifier, and if on same segment,
74 //  on distance.
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)
78 template
79 <
80     bool Reverse1, bool Reverse2,
81     typename Operations,
82     typename Turns,
83     typename Geometry1, typename Geometry2,
84     typename RobustPolicy,
85     typename SideStrategy
86 >
enrich_sort(Operations & operations,Turns const & turns,Geometry1 const & geometry1,Geometry2 const & geometry2,RobustPolicy const & robust_policy,SideStrategy const & strategy)87 inline void enrich_sort(Operations& operations,
88             Turns const& turns,
89             Geometry1 const& geometry1,
90             Geometry2 const& geometry2,
91             RobustPolicy const& robust_policy,
92             SideStrategy const& strategy)
93 {
94     std::sort(boost::begin(operations),
95             boost::end(operations),
96             less_by_segment_ratio
97                 <
98                     Turns,
99                     typename boost::range_value<Operations>::type,
100                     Geometry1, Geometry2,
101                     RobustPolicy,
102                     SideStrategy,
103                     Reverse1, Reverse2
104                 >(turns, geometry1, geometry2, robust_policy, strategy));
105 }
106 
107 
108 template <typename Operations, typename Turns>
enrich_assign(Operations & operations,Turns & turns,bool check_turns)109 inline void enrich_assign(Operations& operations, Turns& turns,
110                           bool check_turns)
111 {
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;
115 
116 
117     if (operations.size() > 0)
118     {
119         // Assign travel-to-vertex/ip index for each turning point.
120         // Iterator "next" is circular
121 
122         geometry::ever_circling_range_iterator<Operations const> next(operations);
123         ++next;
124 
125         for (iterator_type it = boost::begin(operations);
126              it != boost::end(operations); ++it)
127         {
128             turn_type& turn = turns[it->turn_index];
129             op_type& op = turn.operations[it->operation_index];
130 
131             if (check_turns && it->turn_index == next->turn_index)
132             {
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
136                 ++next;
137             }
138 
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)
146             {
147                 ++next;
148             }
149 
150             turn_type const& next_turn = turns[next->turn_index];
151             op_type const& next_op = next_turn.operations[next->operation_index];
152 
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;
157 
158             if (op.seg_id.segment_index == next_op.seg_id.segment_index
159                     && op.fraction < next_op.fraction)
160             {
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);
165             }
166 
167             if (! check_turns)
168             {
169                 ++next;
170             }
171         }
172     }
173 
174     // DEBUG
175 #ifdef BOOST_GEOMETRY_DEBUG_ENRICH
176     {
177         for (iterator_type it = boost::begin(operations);
178              it != boost::end(operations);
179              ++it)
180         {
181             op_type const& op = turns[it->turn_index]
182                 .operations[it->operation_index];
183 
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                 << (turns[it->turn_index].discarded ? " discarded" : "")
196                 << std::endl;
197                 ;
198         }
199     }
200 #endif
201     // END DEBUG
202 
203 }
204 
205 template <typename Operations, typename Turns>
enrich_adapt(Operations & operations,Turns & turns)206 inline void enrich_adapt(Operations& operations, Turns& turns)
207 {
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;
211 
212     if (operations.size() < 3)
213     {
214         // If it is empty, or contains one or two turns, it makes no sense
215         return;
216     }
217 
218     // Operations is a vector of indexed_turn_operation<>
219 
220     // Last index:
221     std::size_t const x = operations.size() - 1;
222     bool next_phase = false;
223 
224     for (std::size_t i = 0; i < operations.size(); i++)
225     {
226         indexed_turn_type const& indexed = operations[i];
227 
228         turn_type& turn = turns[indexed.turn_index];
229         op_type& op = turn.operations[indexed.operation_index];
230 
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;
234 
235         turn_type const& next_turn = turns[operations[n].turn_index];
236         op_type const& next_op = next_turn.operations[operations[n].operation_index];
237 
238         if (op.seg_id.segment_index == next_op.seg_id.segment_index)
239         {
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)
243             {
244                 op.enriched.startable = false;
245                 next_phase = true;
246             }
247         }
248     }
249 
250     if (! next_phase)
251     {
252         return;
253     }
254 
255     // Discard turns which are both non-startable
256     next_phase = false;
257     for (typename boost::range_iterator<Turns>::type
258             it = boost::begin(turns);
259          it != boost::end(turns);
260          ++it)
261     {
262         turn_type& turn = *it;
263         if (! turn.operations[0].enriched.startable
264             && ! turn.operations[1].enriched.startable)
265         {
266             turn.discarded = true;
267             next_phase = true;
268         }
269     }
270 
271     if (! next_phase)
272     {
273         return;
274     }
275 
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));
280 }
281 
282 struct enriched_map_default_include_policy
283 {
284     template <typename Operation>
includeboost::geometry::detail::overlay::enriched_map_default_include_policy285     static inline bool include(Operation const& )
286     {
287         // By default include all operations
288         return true;
289     }
290 };
291 
292 template <typename Turns, typename MappedVector, typename IncludePolicy>
create_map(Turns const & turns,MappedVector & mapped_vector,IncludePolicy const & include_policy)293 inline void create_map(Turns const& turns, MappedVector& mapped_vector,
294                        IncludePolicy const& include_policy)
295 {
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;
300 
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);
305          ++it, ++index)
306     {
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;
311         if (turn.discarded)
312         {
313             continue;
314         }
315 
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);
320             ++op_it, ++op_index)
321         {
322             if (include_policy.include(op_it->operation))
323             {
324                 ring_identifier const ring_id
325                     (
326                         op_it->seg_id.source_index,
327                         op_it->seg_id.multi_index,
328                         op_it->seg_id.ring_index
329                     );
330                 mapped_vector[ring_id].push_back
331                     (
332                         indexed_type(index, op_index, *op_it,
333                             it->operations[1 - op_index].seg_id)
334                     );
335             }
336         }
337     }
338 }
339 
340 template <typename Point1, typename Point2>
341 inline typename geometry::coordinate_type<Point1>::type
distance_measure(Point1 const & a,Point2 const & b)342         distance_measure(Point1 const& a, Point2 const& b)
343 {
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;
350 }
351 
352 template <typename Turns>
calculate_remaining_distance(Turns & turns)353 inline void calculate_remaining_distance(Turns& turns)
354 {
355     using turn_type = typename boost::range_value<Turns>::type;
356     using op_type = typename turn_type::turn_operation_type;
357 
358     typename op_type::comparable_distance_type const zero_distance = 0;
359 
360     for (auto it = boost::begin(turns); it != boost::end(turns); ++it)
361     {
362         turn_type& turn = *it;
363 
364         op_type& op0 = turn.operations[0];
365         op_type& op1 = turn.operations[1];
366 
367         if (op0.remaining_distance != zero_distance
368          || op1.remaining_distance != zero_distance)
369         {
370             continue;
371         }
372 
373         signed_size_type const to_index0 = op0.enriched.get_next_turn_index();
374         signed_size_type const to_index1 = op1.enriched.get_next_turn_index();
375         if (to_index0 >= 0
376                 && to_index1 >= 0
377                 && to_index0 != to_index1)
378         {
379             op0.remaining_distance = distance_measure(turn.point, turns[to_index0].point);
380             op1.remaining_distance = distance_measure(turn.point, turns[to_index1].point);
381         }
382     }
383 }
384 
385 
386 }} // namespace detail::overlay
387 #endif //DOXYGEN_NO_DETAIL
388 
389 
390 
391 /*!
392 \brief All intersection points are enriched with successor information
393 \ingroup overlay
394 \tparam Turns type of intersection container
395             (e.g. vector of "intersection/turn point"'s)
396 \tparam Clusters type of cluster container
397 \tparam Geometry1 \tparam_geometry
398 \tparam Geometry2 \tparam_geometry
399 \tparam PointInGeometryStrategy point in geometry strategy type
400 \param turns container containing intersection points
401 \param clusters container containing clusters
402 \param geometry1 \param_geometry
403 \param geometry2 \param_geometry
404 \param robust_policy policy to handle robustness issues
405 \param strategy point in geometry strategy
406  */
407 template
408 <
409     bool Reverse1, bool Reverse2,
410     overlay_type OverlayType,
411     typename Turns,
412     typename Clusters,
413     typename Geometry1, typename Geometry2,
414     typename RobustPolicy,
415     typename IntersectionStrategy
416 >
enrich_intersection_points(Turns & turns,Clusters & clusters,Geometry1 const & geometry1,Geometry2 const & geometry2,RobustPolicy const & robust_policy,IntersectionStrategy const & strategy)417 inline void enrich_intersection_points(Turns& turns,
418     Clusters& clusters,
419     Geometry1 const& geometry1, Geometry2 const& geometry2,
420     RobustPolicy const& robust_policy,
421     IntersectionStrategy const& strategy)
422 {
423     static const detail::overlay::operation_type target_operation
424             = detail::overlay::operation_from_overlay<OverlayType>::value;
425     static const detail::overlay::operation_type opposite_operation
426             = target_operation == detail::overlay::operation_union
427             ? detail::overlay::operation_intersection
428             : detail::overlay::operation_union;
429     static const bool is_dissolve = OverlayType == overlay_dissolve;
430 
431     typedef typename boost::range_value<Turns>::type turn_type;
432     typedef typename turn_type::turn_operation_type op_type;
433     typedef detail::overlay::indexed_turn_operation
434         <
435             op_type
436         > indexed_turn_operation;
437 
438     typedef std::map
439         <
440             ring_identifier,
441             std::vector<indexed_turn_operation>
442         > mapped_vector_type;
443 
444     // From here on, turn indexes are used (in clusters, next_index, etc)
445     // and may not be DELETED - they may only be flagged as discarded
446     discard_duplicate_start_turns(turns, geometry1, geometry2);
447 
448     bool has_cc = false;
449     bool const has_colocations
450         = detail::overlay::handle_colocations
451             <
452                 Reverse1, Reverse2, OverlayType, Geometry1, Geometry2
453             >(turns, clusters, robust_policy);
454 
455     // Discard turns not part of target overlay
456     for (typename boost::range_iterator<Turns>::type
457             it = boost::begin(turns);
458          it != boost::end(turns);
459          ++it)
460     {
461         turn_type& turn = *it;
462 
463         if (turn.both(detail::overlay::operation_none)
464             || turn.both(opposite_operation)
465             || turn.both(detail::overlay::operation_blocked)
466             || (detail::overlay::is_self_turn<OverlayType>(turn)
467                 && ! turn.is_clustered()
468                 && ! turn.both(target_operation)))
469         {
470             // For all operations, discard xx and none/none
471             // For intersections, remove uu to avoid the need to travel
472             // a union (during intersection) in uu/cc clusters (e.g. #31,#32,#33)
473             // The ux is necessary to indicate impossible paths
474             // (especially if rescaling is removed)
475 
476             // Similarly, for union, discard ii and ix
477 
478             // For self-turns, only keep uu / ii
479 
480             turn.discarded = true;
481             turn.cluster_id = -1;
482             continue;
483         }
484 
485         if (! turn.discarded
486             && turn.both(detail::overlay::operation_continue))
487         {
488             has_cc = true;
489         }
490     }
491 
492     if (! is_dissolve)
493     {
494         detail::overlay::discard_closed_turns
495             <
496                 OverlayType,
497                 target_operation
498             >::apply(turns, clusters, geometry1, geometry2,
499                      strategy);
500         detail::overlay::discard_open_turns
501             <
502                 OverlayType,
503                 target_operation
504             >::apply(turns, clusters, geometry1, geometry2,
505                      strategy);
506     }
507 
508     // Create a map of vectors of indexed operation-types to be able
509     // to sort intersection points PER RING
510     mapped_vector_type mapped_vector;
511 
512     detail::overlay::create_map(turns, mapped_vector,
513                                 detail::overlay::enriched_map_default_include_policy());
514 
515     // No const-iterator; contents of mapped copy is temporary,
516     // and changed by enrich
517     for (typename mapped_vector_type::iterator mit
518         = mapped_vector.begin();
519         mit != mapped_vector.end();
520         ++mit)
521     {
522         detail::overlay::enrich_sort<Reverse1, Reverse2>(
523                     mit->second, turns,
524                     geometry1, geometry2,
525                     robust_policy, strategy.side()); // TODO: pass strategy
526 #ifdef BOOST_GEOMETRY_DEBUG_ENRICH
527         std::cout << "ENRICH-sort Ring " << mit->first << std::endl;
528         for (auto const& op : mit->second)
529         {
530             std::cout << op.turn_index << " " << op.operation_index << std::endl;
531         }
532 #endif
533     }
534 
535     if (has_colocations)
536     {
537         // First gather cluster properties (using even clusters with
538         // discarded turns - for open turns), then clean up clusters
539         detail::overlay::gather_cluster_properties
540             <
541                 Reverse1,
542                 Reverse2,
543                 OverlayType
544             >(clusters, turns, target_operation,
545               geometry1, geometry2, strategy.side()); // TODO: pass strategy
546 
547         detail::overlay::cleanup_clusters(turns, clusters);
548     }
549 
550     // After cleaning up clusters assign the next turns
551 
552     for (typename mapped_vector_type::iterator mit
553         = mapped_vector.begin();
554         mit != mapped_vector.end();
555         ++mit)
556     {
557 #ifdef BOOST_GEOMETRY_DEBUG_ENRICH
558     std::cout << "ENRICH-assign Ring " << mit->first << std::endl;
559 #endif
560         if (is_dissolve)
561         {
562             detail::overlay::enrich_adapt(mit->second, turns);
563         }
564 
565         detail::overlay::enrich_assign(mit->second, turns, ! is_dissolve);
566     }
567 
568     if (has_cc)
569     {
570         detail::overlay::calculate_remaining_distance(turns);
571     }
572 
573 #ifdef BOOST_GEOMETRY_DEBUG_ENRICH
574     //detail::overlay::check_graph(turns, for_operation);
575 #endif
576 
577 }
578 
579 }} // namespace boost::geometry
580 
581 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_ENRICH_HPP
582