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_ENRICH_HPP
15 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_ENRICH_HPP
23 #ifdef BOOST_GEOMETRY_DEBUG_ENRICH
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
32 #include <boost/range.hpp>
34 #include <boost/geometry/algorithms/detail/ring_identifier.hpp>
35 #include <boost/geometry/algorithms/detail/overlay/handle_colocations.hpp>
36 #include <boost/geometry/algorithms/detail/overlay/handle_self_turns.hpp>
37 #include <boost/geometry/algorithms/detail/overlay/is_self_turn.hpp>
38 #include <boost/geometry/algorithms/detail/overlay/less_by_segment_ratio.hpp>
39 #include <boost/geometry/algorithms/detail/overlay/overlay_type.hpp>
40 #include <boost/geometry/policies/robustness/robust_type.hpp>
42 #ifdef BOOST_GEOMETRY_DEBUG_ENRICH
43 # include <boost/geometry/algorithms/detail/overlay/check_enrich.hpp>
47 namespace boost { namespace geometry
50 #ifndef DOXYGEN_NO_DETAIL
51 namespace detail { namespace overlay
55 // Sorts IP-s of this ring on segment-identifier, and if on same segment,
57 // Then assigns for each IP which is the next IP on this segment,
58 // plus the vertex-index to travel to, plus the next IP
59 // (might be on another segment)
62 bool Reverse1, bool Reverse2,
65 typename Geometry1, typename Geometry2,
66 typename RobustPolicy,
69 inline void enrich_sort(Operations& operations,
71 operation_type for_operation,
72 Geometry1 const& geometry1,
73 Geometry2 const& geometry2,
74 RobustPolicy const& robust_policy,
75 SideStrategy const& strategy)
77 std::sort(boost::begin(operations),
78 boost::end(operations),
82 typename boost::range_value<Operations>::type,
87 >(turns, for_operation, geometry1, geometry2, robust_policy, strategy));
91 template <typename Operations, typename Turns>
92 inline void enrich_assign(Operations& operations, Turns& turns)
94 typedef typename boost::range_value<Turns>::type turn_type;
95 typedef typename turn_type::turn_operation_type op_type;
96 typedef typename boost::range_iterator<Operations>::type iterator_type;
99 if (operations.size() > 0)
101 // Assign travel-to-vertex/ip index for each turning point.
102 // Iterator "next" is circular
104 geometry::ever_circling_range_iterator<Operations const> next(operations);
107 for (iterator_type it = boost::begin(operations);
108 it != boost::end(operations); ++it)
110 turn_type& turn = turns[it->turn_index];
111 op_type& op = turn.operations[it->operation_index];
113 // Normal behaviour: next should point at next turn:
114 if (it->turn_index == next->turn_index)
119 // Cluster behaviour: next should point after cluster, unless
120 // their seg_ids are not the same
121 while (turn.is_clustered()
122 && it->turn_index != next->turn_index
123 && turn.cluster_id == turns[next->turn_index].cluster_id
124 && op.seg_id == turns[next->turn_index].operations[next->operation_index].seg_id)
129 turn_type const& next_turn = turns[next->turn_index];
130 op_type const& next_op = next_turn.operations[next->operation_index];
132 op.enriched.travels_to_ip_index
133 = static_cast<signed_size_type>(next->turn_index);
134 op.enriched.travels_to_vertex_index
135 = next->subject->seg_id.segment_index;
137 if (op.seg_id.segment_index == next_op.seg_id.segment_index
138 && op.fraction < next_op.fraction)
140 // Next turn is located further on same segment
141 // assign next_ip_index
142 // (this is one not circular therefore fraction is considered)
143 op.enriched.next_ip_index = static_cast<signed_size_type>(next->turn_index);
149 #ifdef BOOST_GEOMETRY_DEBUG_ENRICH
151 for (iterator_type it = boost::begin(operations);
152 it != boost::end(operations);
155 op_type const& op = turns[it->turn_index]
156 .operations[it->operation_index];
158 std::cout << it->turn_index
159 << " cl=" << turns[it->turn_index].cluster_id
160 << " meth=" << method_char(turns[it->turn_index].method)
161 << " seg=" << op.seg_id
162 << " dst=" << op.fraction // needs define
163 << " op=" << operation_char(turns[it->turn_index].operations[0].operation)
164 << operation_char(turns[it->turn_index].operations[1].operation)
165 << " (" << operation_char(op.operation) << ")"
166 << " nxt=" << op.enriched.next_ip_index
167 << " / " << op.enriched.travels_to_ip_index
168 << " [vx " << op.enriched.travels_to_vertex_index << "]"
169 << std::boolalpha << turns[it->turn_index].discarded
180 template <typename Turns, typename MappedVector>
181 inline void create_map(Turns const& turns, MappedVector& mapped_vector)
183 typedef typename boost::range_value<Turns>::type turn_type;
184 typedef typename turn_type::container_type container_type;
185 typedef typename MappedVector::mapped_type mapped_type;
186 typedef typename boost::range_value<mapped_type>::type indexed_type;
188 std::size_t index = 0;
189 for (typename boost::range_iterator<Turns const>::type
190 it = boost::begin(turns);
191 it != boost::end(turns);
194 // Add all (non discarded) operations on this ring
195 // Blocked operations or uu on clusters (for intersection)
196 // should be included, to block potential paths in clusters
197 turn_type const& turn = *it;
203 std::size_t op_index = 0;
204 for (typename boost::range_iterator<container_type const>::type
205 op_it = boost::begin(turn.operations);
206 op_it != boost::end(turn.operations);
209 ring_identifier const ring_id
211 op_it->seg_id.source_index,
212 op_it->seg_id.multi_index,
213 op_it->seg_id.ring_index
215 mapped_vector[ring_id].push_back
217 indexed_type(index, op_index, *op_it,
218 it->operations[1 - op_index].seg_id)
224 template <typename Point1, typename Point2>
225 inline typename geometry::coordinate_type<Point1>::type
226 distance_measure(Point1 const& a, Point2 const& b)
228 // TODO: use comparable distance for point-point instead - but that
229 // causes currently cycling include problems
230 typedef typename geometry::coordinate_type<Point1>::type ctype;
231 ctype const dx = get<0>(a) - get<0>(b);
232 ctype const dy = get<1>(a) - get<1>(b);
233 return dx * dx + dy * dy;
236 template <typename Turns>
237 inline void calculate_remaining_distance(Turns& turns)
239 typedef typename boost::range_value<Turns>::type turn_type;
240 typedef typename turn_type::turn_operation_type op_type;
242 for (typename boost::range_iterator<Turns>::type
243 it = boost::begin(turns);
244 it != boost::end(turns);
247 turn_type& turn = *it;
249 op_type& op0 = turn.operations[0];
250 op_type& op1 = turn.operations[1];
252 if (op0.remaining_distance != 0
253 || op1.remaining_distance != 0)
258 int const to_index0 = op0.enriched.get_next_turn_index();
259 int const to_index1 = op1.enriched.get_next_turn_index();
262 && to_index0 != to_index1)
264 op0.remaining_distance = distance_measure(turn.point, turns[to_index0].point);
265 op1.remaining_distance = distance_measure(turn.point, turns[to_index1].point);
271 }} // namespace detail::overlay
272 #endif //DOXYGEN_NO_DETAIL
277 \brief All intersection points are enriched with successor information
279 \tparam Turns type of intersection container
280 (e.g. vector of "intersection/turn point"'s)
281 \tparam Clusters type of cluster container
282 \tparam Geometry1 \tparam_geometry
283 \tparam Geometry2 \tparam_geometry
284 \tparam SideStrategy side strategy type
285 \param turns container containing intersection points
286 \param clusters container containing clusters
287 \param geometry1 \param_geometry
288 \param geometry2 \param_geometry
289 \param robust_policy policy to handle robustness issues
290 \param strategy strategy
294 bool Reverse1, bool Reverse2,
295 overlay_type OverlayType,
298 typename Geometry1, typename Geometry2,
299 typename RobustPolicy,
300 typename SideStrategy
302 inline void enrich_intersection_points(Turns& turns,
304 Geometry1 const& geometry1, Geometry2 const& geometry2,
305 RobustPolicy const& robust_policy,
306 SideStrategy const& strategy)
308 static const detail::overlay::operation_type target_operation
309 = detail::overlay::operation_from_overlay<OverlayType>::value;
310 static const detail::overlay::operation_type opposite_operation
311 = target_operation == detail::overlay::operation_union
312 ? detail::overlay::operation_intersection
313 : detail::overlay::operation_union;
315 typedef typename boost::range_value<Turns>::type turn_type;
316 typedef typename turn_type::turn_operation_type op_type;
317 typedef detail::overlay::indexed_turn_operation
320 > indexed_turn_operation;
325 std::vector<indexed_turn_operation>
326 > mapped_vector_type;
329 bool const has_colocations
330 = detail::overlay::handle_colocations<Reverse1, Reverse2, OverlayType>(turns,
331 clusters, geometry1, geometry2);
333 // Discard turns not part of target overlay
334 for (typename boost::range_iterator<Turns>::type
335 it = boost::begin(turns);
336 it != boost::end(turns);
339 turn_type& turn = *it;
341 if (turn.both(detail::overlay::operation_none))
343 turn.discarded = true;
347 if (turn.both(opposite_operation))
349 // For intersections, remove uu to avoid the need to travel
350 // a union (during intersection) in uu/cc clusters (e.g. #31,#32,#33)
351 // Also, for union, discard ii
352 turn.discarded = true;
353 turn.cluster_id = -1;
357 if (detail::overlay::is_self_turn<OverlayType>(turn)
358 && ! turn.is_clustered()
359 && ! turn.both(target_operation))
361 // Only keep self-uu-turns or self-ii-turns
362 turn.discarded = true;
367 && turn.both(detail::overlay::operation_continue))
373 detail::overlay::discard_closed_turns
377 >::apply(turns, clusters, geometry1, geometry2);
378 detail::overlay::discard_open_turns
382 >::apply(turns, clusters, geometry1, geometry2);
384 // Create a map of vectors of indexed operation-types to be able
385 // to sort intersection points PER RING
386 mapped_vector_type mapped_vector;
388 detail::overlay::create_map(turns, mapped_vector);
390 // No const-iterator; contents of mapped copy is temporary,
391 // and changed by enrich
392 for (typename mapped_vector_type::iterator mit
393 = mapped_vector.begin();
394 mit != mapped_vector.end();
397 #ifdef BOOST_GEOMETRY_DEBUG_ENRICH
398 std::cout << "ENRICH-sort Ring "
399 << mit->first << std::endl;
401 detail::overlay::enrich_sort<Reverse1, Reverse2>(
402 mit->second, turns, target_operation,
403 geometry1, geometry2,
404 robust_policy, strategy);
407 for (typename mapped_vector_type::iterator mit
408 = mapped_vector.begin();
409 mit != mapped_vector.end();
412 #ifdef BOOST_GEOMETRY_DEBUG_ENRICH
413 std::cout << "ENRICH-assign Ring "
414 << mit->first << std::endl;
416 detail::overlay::enrich_assign(mit->second, turns);
419 // Check some specific type of self-turns (after getting enriched info)
420 detail::overlay::discard_self_turns_which_loop<OverlayType>(turns);
424 // First gather cluster properties (using even clusters with
425 // discarded turns - for open turns), then clean up clusters
426 detail::overlay::gather_cluster_properties
431 >(clusters, turns, target_operation,
432 geometry1, geometry2, strategy);
434 detail::overlay::cleanup_clusters(turns, clusters);
439 detail::overlay::calculate_remaining_distance(turns);
442 #ifdef BOOST_GEOMETRY_DEBUG_ENRICH
443 //detail::overlay::check_graph(turns, for_operation);
448 }} // namespace boost::geometry
450 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_ENRICH_HPP