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