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 2013, 2014, 2017, 2018.
6 // Modifications copyright (c) 2013-2018 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_GET_TURN_INFO_FOR_ENDPOINT_HPP
15 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_GET_TURN_INFO_FOR_ENDPOINT_HPP
17 #include <boost/core/ignore_unused.hpp>
19 #include <boost/geometry/algorithms/detail/equals/point_point.hpp>
20 #include <boost/geometry/algorithms/detail/overlay/get_turn_info.hpp>
21 #include <boost/geometry/core/assert.hpp>
22 #include <boost/geometry/policies/robustness/no_rescale_policy.hpp>
24 namespace boost { namespace geometry {
26 #ifndef DOXYGEN_NO_DETAIL
27 namespace detail { namespace overlay {
29 // SEGMENT_INTERSECTION RESULT
31 // C H0 H1 A0 A1 O IP1 IP2
35 // |--------> 2 0 0 0 0 F i/i x/x
38 // |--------> 2 0 0 0 0 T i/x x/i
41 // |-----> 1 0 0 0 0 T x/x
45 // |---------> 2 0 0 0 1 T i/x x/i
48 // |---------> 2 0 0 0 0 F i/i x/x
51 // |---------> 2 0 0 -1 1 F i/i u/x
54 // |---------> 2 0 0 -1 0 T i/x u/i
57 // |-------> 2 0 0 1 -1 F and i/i x/u
58 // |-------> 2 0 0 -1 1 F symetric i/i u/x
61 // |-------> 2 0 0 -1 -1 T i/u u/i
64 // |-------> 2 0 0 1 1 T i/x x/i
67 // |--------> 2 0 0 -1 1 F i/i u/x
70 // |--------> 2 0 0 -1 1 T i/x u/i
73 // |-----> 1 -1 -1 -1 -1 T u/u
76 // |-----> 1 -1 0 -1 0 F and u/x
77 // |-----> 1 0 -1 0 -1 F symetric x/u
84 // + 1 -1 1 -1 1 F and u/x (P is vertical)
85 // |--------> 1 1 -1 1 -1 F symetric x/u (P is horizontal)
93 // |--------> 1 1 1 1 1 F x/x (P is vertical)
98 // |--------> 1 -1 -1 -1 -1 F u/u (P is vertical)
103 // |--------> 1 0 -1 0 -1 F u/u (P is vertical)
108 // |--------> 1 0 1 0 1 F u/x (P is vertical)
111 class linear_intersections
114 template <typename Point1, typename Point2, typename IntersectionResult, typename EqPPStrategy>
115 linear_intersections(Point1 const& pi,
117 IntersectionResult const& result,
118 bool is_p_last, bool is_q_last,
119 EqPPStrategy const& strategy)
121 int arrival_a = result.template get<1>().arrival[0];
122 int arrival_b = result.template get<1>().arrival[1];
123 bool same_dirs = result.template get<1>().dir_a == 0
124 && result.template get<1>().dir_b == 0;
128 if ( result.template get<0>().count == 2 )
130 if ( ! result.template get<1>().opposite )
132 ips[0].p_operation = operation_intersection;
133 ips[0].q_operation = operation_intersection;
134 ips[1].p_operation = union_or_blocked_same_dirs(arrival_a, is_p_last);
135 ips[1].q_operation = union_or_blocked_same_dirs(arrival_b, is_q_last);
138 = equals::equals_point_point(
139 pi, result.template get<0>().intersections[0], strategy);
141 = equals::equals_point_point(
142 qi, result.template get<0>().intersections[0], strategy);
143 ips[1].is_pj = arrival_a != -1;
144 ips[1].is_qj = arrival_b != -1;
148 ips[0].p_operation = operation_intersection;
149 ips[0].q_operation = union_or_blocked_same_dirs(arrival_b, is_q_last);
150 ips[1].p_operation = union_or_blocked_same_dirs(arrival_a, is_p_last);
151 ips[1].q_operation = operation_intersection;
153 ips[0].is_pi = arrival_b != 1;
154 ips[0].is_qj = arrival_b != -1;
155 ips[1].is_pj = arrival_a != -1;
156 ips[1].is_qi = arrival_a != 1;
161 BOOST_GEOMETRY_ASSERT(result.template get<0>().count == 1);
162 ips[0].p_operation = union_or_blocked_same_dirs(arrival_a, is_p_last);
163 ips[0].q_operation = union_or_blocked_same_dirs(arrival_b, is_q_last);
165 ips[0].is_pi = arrival_a == -1;
166 ips[0].is_qi = arrival_b == -1;
167 ips[0].is_pj = arrival_a == 0;
168 ips[0].is_qj = arrival_b == 0;
173 ips[0].p_operation = union_or_blocked_different_dirs(arrival_a, is_p_last);
174 ips[0].q_operation = union_or_blocked_different_dirs(arrival_b, is_q_last);
176 ips[0].is_pi = arrival_a == -1;
177 ips[0].is_qi = arrival_b == -1;
178 ips[0].is_pj = arrival_a == 1;
179 ips[0].is_qj = arrival_b == 1;
186 : p_operation(operation_none), q_operation(operation_none)
187 , is_pi(false), is_pj(false), is_qi(false), is_qj(false)
190 operation_type p_operation, q_operation;
191 bool is_pi, is_pj, is_qi, is_qj;
194 template <std::size_t I>
195 ip_info const& get() const
197 BOOST_STATIC_ASSERT(I < 2);
203 // only if collinear (same_dirs)
204 static inline operation_type union_or_blocked_same_dirs(int arrival, bool is_last)
207 return operation_blocked;
208 else if ( arrival == -1 )
209 return operation_union;
211 return is_last ? operation_blocked : operation_union;
212 //return operation_blocked;
215 // only if not collinear (!same_dirs)
216 static inline operation_type union_or_blocked_different_dirs(int arrival, bool is_last)
219 //return operation_blocked;
220 return is_last ? operation_blocked : operation_union;
222 return operation_union;
228 template <bool EnableFirst, bool EnableLast>
229 struct get_turn_info_for_endpoint
231 typedef std::pair<operation_type, operation_type> operations_pair;
233 BOOST_STATIC_ASSERT(EnableFirst || EnableLast);
235 template<typename UniqueSubRange1,
236 typename UniqueSubRange2,
238 typename IntersectionInfo,
239 typename OutputIterator,
240 typename EqPPStrategy
242 static inline bool apply(UniqueSubRange1 const& range_p,
243 UniqueSubRange2 const& range_q,
244 TurnInfo const& tp_model,
245 IntersectionInfo const& inters,
246 method_type /*method*/,
248 EqPPStrategy const& strategy)
250 std::size_t ip_count = inters.i_info().count;
251 // no intersection points
257 if (! range_p.is_first_segment()
258 && ! range_q.is_first_segment()
259 && ! range_p.is_last_segment()
260 && ! range_q.is_last_segment())
262 // Not an end-point from segment p or q
266 linear_intersections intersections(range_p.at(0),
269 range_p.is_last_segment(),
270 range_q.is_last_segment(),
274 = analyse_segment_and_assign_ip(range_p, range_q,
275 intersections.template get<0>(),
276 tp_model, inters, 0, out);
278 // NOTE: opposite && ip_count == 1 may be true!
279 bool opposite = inters.d_info().opposite;
281 // don't ignore only for collinear opposite
282 bool result_ignore_ip0 = append0_last && ( ip_count == 1 || !opposite );
284 if ( intersections.template get<1>().p_operation == operation_none )
285 return result_ignore_ip0;
288 = analyse_segment_and_assign_ip(range_p, range_q,
289 intersections.template get<1>(),
290 tp_model, inters, 1, out);
292 // don't ignore only for collinear opposite
293 bool result_ignore_ip1 = append1_last && !opposite /*&& ip_count == 2*/;
295 return result_ignore_ip0 || result_ignore_ip1;
298 template <typename UniqueSubRange1,
299 typename UniqueSubRange2,
301 typename IntersectionInfo,
302 typename OutputIterator>
304 bool analyse_segment_and_assign_ip(UniqueSubRange1 const& range_p,
305 UniqueSubRange2 const& range_q,
306 linear_intersections::ip_info const& ip_info,
307 TurnInfo const& tp_model,
308 IntersectionInfo const& inters,
309 unsigned int ip_index,
312 // TODO - calculate first/last only if needed
313 bool is_p_first_ip = range_p.is_first_segment() && ip_info.is_pi;
314 bool is_p_last_ip = range_p.is_last_segment() && ip_info.is_pj;
315 bool is_q_first_ip = range_q.is_first_segment() && ip_info.is_qi;
316 bool is_q_last_ip = range_q.is_last_segment() && ip_info.is_qj;
317 bool append_first = EnableFirst && (is_p_first_ip || is_q_first_ip);
318 bool append_last = EnableLast && (is_p_last_ip || is_q_last_ip);
320 operation_type p_operation = ip_info.p_operation;
321 operation_type q_operation = ip_info.q_operation;
323 if ( append_first || append_last )
325 bool handled = handle_internal<0>(range_p, range_q,
326 is_p_first_ip, is_p_last_ip,
327 is_q_first_ip, is_q_last_ip,
328 ip_info.is_qi, ip_info.is_qj,
329 tp_model, inters, ip_index,
330 p_operation, q_operation);
334 handle_internal<1>(range_q, range_p,
335 is_q_first_ip, is_q_last_ip,
336 is_p_first_ip, is_p_last_ip,
337 ip_info.is_pi, ip_info.is_pj,
338 tp_model, inters, ip_index,
339 q_operation, p_operation);
342 if ( p_operation != operation_none )
344 method_type method = endpoint_ip_method(ip_info.is_pi, ip_info.is_pj,
345 ip_info.is_qi, ip_info.is_qj);
346 turn_position p_pos = ip_position(is_p_first_ip, is_p_last_ip);
347 turn_position q_pos = ip_position(is_q_first_ip, is_q_last_ip);
351 // P is spike and should be handled
352 if (ip_info.is_pj // this check is redundant (also in is_spike_p) but faster
353 && inters.i_info().count == 2
354 && inters.is_spike_p() )
356 assign(inters.result(), ip_index, method, operation_blocked, q_operation,
357 p_pos, q_pos, is_p_first_ip, is_q_first_ip, true, false, tp_model, out);
358 assign(inters.result(), ip_index, method, operation_intersection, q_operation,
359 p_pos, q_pos, is_p_first_ip, is_q_first_ip, true, false, tp_model, out);
361 // Q is spike and should be handled
362 else if (ip_info.is_qj // this check is redundant (also in is_spike_q) but faster
363 && inters.i_info().count == 2
364 && inters.is_spike_q() )
366 assign(inters.result(), ip_index, method, p_operation, operation_blocked,
367 p_pos, q_pos, is_p_first_ip, is_q_first_ip, false, true, tp_model, out);
368 assign(inters.result(), ip_index, method, p_operation, operation_intersection,
369 p_pos, q_pos, is_p_first_ip, is_q_first_ip, false, true, tp_model, out);
374 assign(inters.result(), ip_index, method, p_operation, q_operation,
375 p_pos, q_pos, is_p_first_ip, is_q_first_ip, false, false, tp_model, out);
383 // TODO: IT'S ALSO PROBABLE THAT ALL THIS FUNCTION COULD BE INTEGRATED WITH handle_segment
384 // however now it's lazily calculated and then it would be always calculated
386 template<std::size_t G1Index,
387 typename UniqueRange1,
388 typename UniqueRange2,
390 typename IntersectionInfo
392 static inline bool handle_internal(UniqueRange1 const& range1,
393 UniqueRange2 const& range2,
394 bool first1, bool last1, bool first2, bool last2,
395 bool ip_i2, bool ip_j2, TurnInfo const& tp_model,
396 IntersectionInfo const& inters, unsigned int ip_index,
397 operation_type & op1, operation_type & op2)
399 boost::ignore_unused(ip_index, tp_model);
401 typename IntersectionInfo::side_strategy_type const& sides
402 = inters.get_side_strategy();
404 if ( !first2 && !last2 )
408 #ifdef BOOST_GEOMETRY_DEBUG_GET_TURNS_LINEAR_LINEAR
409 // may this give false positives for INTs?
410 typename IntersectionResult::point_type const&
411 inters_pt = inters.i_info().intersections[ip_index];
412 BOOST_GEOMETRY_ASSERT(ip_i2 == equals::equals_point_point(i2, inters_pt));
413 BOOST_GEOMETRY_ASSERT(ip_j2 == equals::equals_point_point(j2, inters_pt));
417 // don't output this IP - for the first point of other geometry segment
418 op1 = operation_none;
419 op2 = operation_none;
424 int const side_pj_q2 = sides.apply(range2.at(1), range2.at(2), range1.at(1));
425 int const side_pj_q1 = sides.apply(range2.at(0), range2.at(1), range1.at(1));
426 int const side_qk_q1 = sides.apply(range2.at(0), range2.at(1), range2.at(2));
428 operations_pair operations = operations_of_equal(side_pj_q2, side_pj_q1, side_qk_q1);
430 // TODO: must the above be calculated?
431 // wouldn't it be enough to check if segments are collinear?
433 if ( operations_both(operations, operation_continue) )
435 if ( op1 != operation_union
436 || op2 != operation_union
437 || ! ( G1Index == 0 ? inters.is_spike_q() : inters.is_spike_p() ) )
439 // THIS IS WRT THE ORIGINAL SEGMENTS! NOT THE ONES ABOVE!
440 bool opposite = inters.d_info().opposite;
442 op1 = operation_intersection;
443 op2 = opposite ? operation_union : operation_intersection;
448 BOOST_GEOMETRY_ASSERT(operations_combination(operations, operation_intersection, operation_union));
449 //op1 = operation_union;
450 //op2 = operation_union;
455 // else do nothing - shouldn't be handled this way
459 #ifdef BOOST_GEOMETRY_DEBUG_GET_TURNS_LINEAR_LINEAR
460 // may this give false positives for INTs?
461 typename IntersectionResult::point_type const&
462 inters_pt = inters.i_info().intersections[ip_index];
463 BOOST_GEOMETRY_ASSERT(ip_i2 == equals::equals_point_point(i2, inters_pt));
464 BOOST_GEOMETRY_ASSERT(ip_j2 == equals::equals_point_point(j2, inters_pt));
468 // don't output this IP - for the first point of other geometry segment
469 op1 = operation_none;
470 op2 = operation_none;
475 int const side_pi_q2 = sides.apply(range2.at(1), range2.at(2), range1.at(0));
476 int const side_pi_q1 = sides.apply(range2.at(0), range2.at(1), range1.at(0));
477 int const side_qk_q1 = sides.apply(range2.at(0), range2.at(1), range2.at(2));
479 operations_pair operations = operations_of_equal(side_pi_q2, side_pi_q1, side_qk_q1);
481 // TODO: must the above be calculated?
482 // wouldn't it be enough to check if segments are collinear?
484 if ( operations_both(operations, operation_continue) )
486 if ( op1 != operation_blocked
487 || op2 != operation_union
488 || ! ( G1Index == 0 ? inters.is_spike_q() : inters.is_spike_p() ) )
490 // THIS IS WRT THE ORIGINAL SEGMENTS! NOT THE ONES ABOVE!
491 bool second_going_out = inters.i_info().count > 1;
493 op1 = operation_blocked;
494 op2 = second_going_out ? operation_union : operation_intersection;
499 BOOST_GEOMETRY_ASSERT(operations_combination(operations, operation_intersection, operation_union));
500 //op1 = operation_blocked;
501 //op2 = operation_union;
506 // else do nothing - shouldn't be handled this way
508 // else do nothing - shouldn't be handled this way
514 static inline method_type endpoint_ip_method(bool ip_pi, bool ip_pj, bool ip_qi, bool ip_qj)
516 if ( (ip_pi || ip_pj) && (ip_qi || ip_qj) )
519 return method_touch_interior;
522 static inline turn_position ip_position(bool is_ip_first_i, bool is_ip_last_j)
524 return is_ip_first_i ? position_front :
525 ( is_ip_last_j ? position_back : position_middle );
528 template <typename IntersectionResult,
530 typename OutputIterator>
531 static inline void assign(IntersectionResult const& result,
532 unsigned int ip_index,
534 operation_type op0, operation_type op1,
535 turn_position pos0, turn_position pos1,
536 bool is_p_first_ip, bool is_q_first_ip,
537 bool is_p_spike, bool is_q_spike,
538 TurnInfo const& tp_model,
541 TurnInfo tp = tp_model;
543 //geometry::convert(ip, tp.point);
544 //tp.method = method;
545 base_turn_handler::assign_point(tp, method, result.template get<0>(), ip_index);
547 tp.operations[0].operation = op0;
548 tp.operations[1].operation = op1;
549 tp.operations[0].position = pos0;
550 tp.operations[1].position = pos1;
552 if ( result.template get<0>().count > 1 )
554 // NOTE: is_collinear is NOT set for the first endpoint
555 // for which there is no preceding segment
557 //BOOST_GEOMETRY_ASSERT( result.template get<1>().dir_a == 0 && result.template get<1>().dir_b == 0 );
558 if ( ! is_p_first_ip )
560 tp.operations[0].is_collinear = op0 != operation_intersection
564 if ( ! is_q_first_ip )
566 tp.operations[1].is_collinear = op1 != operation_intersection
570 else //if ( result.template get<0>().count == 1 )
572 if ( op0 == operation_blocked && op1 == operation_intersection )
574 tp.operations[0].is_collinear = true;
576 else if ( op0 == operation_intersection && op1 == operation_blocked )
578 tp.operations[1].is_collinear = true;
585 static inline operations_pair operations_of_equal(int side_px_q2,
589 // If px (pi or pj) is collinear with qj-qk (q2), they continue collinearly.
590 // This can be on either side of q1, or collinear
591 // The second condition checks if they do not continue
593 if (side_px_q2 == 0 && side_px_q1 == side_qk_q1)
595 return std::make_pair(operation_continue, operation_continue);
598 // If they turn to same side (not opposite sides)
599 if ( ! base_turn_handler::opposite(side_px_q1, side_qk_q1) )
601 // If px is left of q2 or collinear: p: union, q: intersection
602 if (side_px_q2 != -1 )
604 return std::make_pair(operation_union, operation_intersection);
608 return std::make_pair(operation_intersection, operation_union);
613 // They turn opposite sides. If p turns left (or collinear),
614 // p: union, q: intersection
615 if (side_px_q1 != -1 )
617 return std::make_pair(operation_union, operation_intersection);
621 return std::make_pair(operation_intersection, operation_union);
626 static inline bool operations_both(operations_pair const& operations,
627 operation_type const op)
629 return operations.first == op && operations.second == op;
632 static inline bool operations_combination(operations_pair const& operations,
633 operation_type const op1,
634 operation_type const op2)
636 return ( operations.first == op1 && operations.second == op2 )
637 || ( operations.first == op2 && operations.second == op1 );
641 }} // namespace detail::overlay
642 #endif // DOXYGEN_NO_DETAIL
644 }} // namespace boost::geometry
646 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_GET_TURN_INFO_FOR_ENDPOINT_HPP