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.
6 // Modifications copyright (c) 2013-2014 Oracle and/or its affiliates.
8 // Use, modification and distribution is subject to the Boost Software License,
9 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
10 // http://www.boost.org/LICENSE_1_0.txt)
12 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
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/geometry/core/assert.hpp>
18 #include <boost/geometry/algorithms/detail/overlay/get_turn_info.hpp>
19 #include <boost/geometry/policies/robustness/no_rescale_policy.hpp>
21 namespace boost { namespace geometry {
23 #ifndef DOXYGEN_NO_DETAIL
24 namespace detail { namespace overlay {
26 // SEGMENT_INTERSECTION RESULT
28 // C H0 H1 A0 A1 O IP1 IP2
32 // |--------> 2 0 0 0 0 F i/i x/x
35 // |--------> 2 0 0 0 0 T i/x x/i
38 // |-----> 1 0 0 0 0 T x/x
42 // |---------> 2 0 0 0 1 T i/x x/i
45 // |---------> 2 0 0 0 0 F i/i x/x
48 // |---------> 2 0 0 -1 1 F i/i u/x
51 // |---------> 2 0 0 -1 0 T i/x u/i
54 // |-------> 2 0 0 1 -1 F and i/i x/u
55 // |-------> 2 0 0 -1 1 F symetric i/i u/x
58 // |-------> 2 0 0 -1 -1 T i/u u/i
61 // |-------> 2 0 0 1 1 T i/x x/i
64 // |--------> 2 0 0 -1 1 F i/i u/x
67 // |--------> 2 0 0 -1 1 T i/x u/i
70 // |-----> 1 -1 -1 -1 -1 T u/u
73 // |-----> 1 -1 0 -1 0 F and u/x
74 // |-----> 1 0 -1 0 -1 F symetric x/u
81 // + 1 -1 1 -1 1 F and u/x (P is vertical)
82 // |--------> 1 1 -1 1 -1 F symetric x/u (P is horizontal)
90 // |--------> 1 1 1 1 1 F x/x (P is vertical)
95 // |--------> 1 -1 -1 -1 -1 F u/u (P is vertical)
100 // |--------> 1 0 -1 0 -1 F u/u (P is vertical)
105 // |--------> 1 0 1 0 1 F u/x (P is vertical)
108 class linear_intersections
111 template <typename Point1, typename Point2, typename IntersectionResult>
112 linear_intersections(Point1 const& pi,
114 IntersectionResult const& result,
115 bool is_p_last, bool is_q_last)
117 int arrival_a = result.template get<1>().arrival[0];
118 int arrival_b = result.template get<1>().arrival[1];
119 bool same_dirs = result.template get<1>().dir_a == 0
120 && result.template get<1>().dir_b == 0;
124 if ( result.template get<0>().count == 2 )
126 if ( ! result.template get<1>().opposite )
128 ips[0].p_operation = operation_intersection;
129 ips[0].q_operation = operation_intersection;
130 ips[1].p_operation = union_or_blocked_same_dirs(arrival_a, is_p_last);
131 ips[1].q_operation = union_or_blocked_same_dirs(arrival_b, is_q_last);
134 = equals::equals_point_point(
135 pi, result.template get<0>().intersections[0]);
137 = equals::equals_point_point(
138 qi, result.template get<0>().intersections[0]);
139 ips[1].is_pj = arrival_a != -1;
140 ips[1].is_qj = arrival_b != -1;
144 ips[0].p_operation = operation_intersection;
145 ips[0].q_operation = union_or_blocked_same_dirs(arrival_b, is_q_last);
146 ips[1].p_operation = union_or_blocked_same_dirs(arrival_a, is_p_last);
147 ips[1].q_operation = operation_intersection;
149 ips[0].is_pi = arrival_b != 1;
150 ips[0].is_qj = arrival_b != -1;
151 ips[1].is_pj = arrival_a != -1;
152 ips[1].is_qi = arrival_a != 1;
157 BOOST_GEOMETRY_ASSERT(result.template get<0>().count == 1);
158 ips[0].p_operation = union_or_blocked_same_dirs(arrival_a, is_p_last);
159 ips[0].q_operation = union_or_blocked_same_dirs(arrival_b, is_q_last);
161 ips[0].is_pi = arrival_a == -1;
162 ips[0].is_qi = arrival_b == -1;
163 ips[0].is_pj = arrival_a == 0;
164 ips[0].is_qj = arrival_b == 0;
169 ips[0].p_operation = union_or_blocked_different_dirs(arrival_a, is_p_last);
170 ips[0].q_operation = union_or_blocked_different_dirs(arrival_b, is_q_last);
172 ips[0].is_pi = arrival_a == -1;
173 ips[0].is_qi = arrival_b == -1;
174 ips[0].is_pj = arrival_a == 1;
175 ips[0].is_qj = arrival_b == 1;
182 : p_operation(operation_none), q_operation(operation_none)
183 , is_pi(false), is_pj(false), is_qi(false), is_qj(false)
186 operation_type p_operation, q_operation;
187 bool is_pi, is_pj, is_qi, is_qj;
190 template <std::size_t I>
191 ip_info const& get() const
193 BOOST_STATIC_ASSERT(I < 2);
199 // only if collinear (same_dirs)
200 static inline operation_type union_or_blocked_same_dirs(int arrival, bool is_last)
203 return operation_blocked;
204 else if ( arrival == -1 )
205 return operation_union;
207 return is_last ? operation_blocked : operation_union;
208 //return operation_blocked;
211 // only if not collinear (!same_dirs)
212 static inline operation_type union_or_blocked_different_dirs(int arrival, bool is_last)
215 //return operation_blocked;
216 return is_last ? operation_blocked : operation_union;
218 return operation_union;
224 template <typename AssignPolicy, bool EnableFirst, bool EnableLast>
225 struct get_turn_info_for_endpoint
227 BOOST_STATIC_ASSERT(EnableFirst || EnableLast);
229 template<typename Point1,
232 typename IntersectionInfo,
233 typename OutputIterator
235 static inline bool apply(Point1 const& pi, Point1 const& pj, Point1 const& pk,
236 Point2 const& qi, Point2 const& qj, Point2 const& qk,
237 bool is_p_first, bool is_p_last,
238 bool is_q_first, bool is_q_last,
239 TurnInfo const& tp_model,
240 IntersectionInfo const& inters,
241 method_type /*method*/,
244 std::size_t ip_count = inters.i_info().count;
245 // no intersection points
249 if ( !is_p_first && !is_p_last && !is_q_first && !is_q_last )
252 linear_intersections intersections(pi, qi, inters.result(), is_p_last, is_q_last);
255 = analyse_segment_and_assign_ip(pi, pj, pk, qi, qj, qk,
256 is_p_first, is_p_last, is_q_first, is_q_last,
257 intersections.template get<0>(),
258 tp_model, inters, 0, out);
260 // NOTE: opposite && ip_count == 1 may be true!
261 bool opposite = inters.d_info().opposite;
263 // don't ignore only for collinear opposite
264 bool result_ignore_ip0 = append0_last && ( ip_count == 1 || !opposite );
266 if ( intersections.template get<1>().p_operation == operation_none )
267 return result_ignore_ip0;
270 = analyse_segment_and_assign_ip(pi, pj, pk, qi, qj, qk,
271 is_p_first, is_p_last, is_q_first, is_q_last,
272 intersections.template get<1>(),
273 tp_model, inters, 1, out);
275 // don't ignore only for collinear opposite
276 bool result_ignore_ip1 = append1_last && !opposite /*&& ip_count == 2*/;
278 return result_ignore_ip0 || result_ignore_ip1;
281 template <typename Point1,
284 typename IntersectionInfo,
285 typename OutputIterator>
287 bool analyse_segment_and_assign_ip(Point1 const& pi, Point1 const& pj, Point1 const& pk,
288 Point2 const& qi, Point2 const& qj, Point2 const& qk,
289 bool is_p_first, bool is_p_last,
290 bool is_q_first, bool is_q_last,
291 linear_intersections::ip_info const& ip_info,
292 TurnInfo const& tp_model,
293 IntersectionInfo const& inters,
294 unsigned int ip_index,
297 #ifdef BOOST_GEOMETRY_DEBUG_GET_TURNS_LINEAR_LINEAR
298 // may this give false positives for INTs?
299 typename IntersectionResult::point_type const&
300 inters_pt = result.template get<0>().intersections[ip_index];
301 BOOST_GEOMETRY_ASSERT(ip_info.is_pi == equals::equals_point_point(pi, inters_pt));
302 BOOST_GEOMETRY_ASSERT(ip_info.is_qi == equals::equals_point_point(qi, inters_pt));
303 BOOST_GEOMETRY_ASSERT(ip_info.is_pj == equals::equals_point_point(pj, inters_pt));
304 BOOST_GEOMETRY_ASSERT(ip_info.is_qj == equals::equals_point_point(qj, inters_pt));
307 // TODO - calculate first/last only if needed
308 bool is_p_first_ip = is_p_first && ip_info.is_pi;
309 bool is_p_last_ip = is_p_last && ip_info.is_pj;
310 bool is_q_first_ip = is_q_first && ip_info.is_qi;
311 bool is_q_last_ip = is_q_last && ip_info.is_qj;
312 bool append_first = EnableFirst && (is_p_first_ip || is_q_first_ip);
313 bool append_last = EnableLast && (is_p_last_ip || is_q_last_ip);
315 operation_type p_operation = ip_info.p_operation;
316 operation_type q_operation = ip_info.q_operation;
318 if ( append_first || append_last )
320 bool handled = handle_internal<0>(pi, pj, pk, qi, qj, qk,
321 inters.rpi(), inters.rpj(), inters.rpk(),
322 inters.rqi(), inters.rqj(), inters.rqk(),
323 is_p_first_ip, is_p_last_ip,
324 is_q_first_ip, is_q_last_ip,
325 ip_info.is_qi, ip_info.is_qj,
326 tp_model, inters, ip_index,
327 p_operation, q_operation);
330 handle_internal<1>(qi, qj, qk, pi, pj, pk,
331 inters.rqi(), inters.rqj(), inters.rqk(),
332 inters.rpi(), inters.rpj(), inters.rpk(),
333 is_q_first_ip, is_q_last_ip,
334 is_p_first_ip, is_p_last_ip,
335 ip_info.is_pi, ip_info.is_pj,
336 tp_model, inters, ip_index,
337 q_operation, p_operation);
340 if ( p_operation != operation_none )
342 method_type method = endpoint_ip_method(ip_info.is_pi, ip_info.is_pj,
343 ip_info.is_qi, ip_info.is_qj);
344 turn_position p_pos = ip_position(is_p_first_ip, is_p_last_ip);
345 turn_position q_pos = ip_position(is_q_first_ip, is_q_last_ip);
349 // P is spike and should be handled
351 && ip_info.is_pj // this check is redundant (also in is_spike_p) but faster
352 && inters.i_info().count == 2
353 && inters.is_spike_p() )
355 assign(pi, qi, inters.result(), ip_index, method, operation_blocked, q_operation,
356 p_pos, q_pos, is_p_first_ip, is_q_first_ip, true, false, tp_model, out);
357 assign(pi, qi, inters.result(), ip_index, method, operation_intersection, q_operation,
358 p_pos, q_pos, is_p_first_ip, is_q_first_ip, true, false, tp_model, out);
360 // Q is spike and should be handled
362 && 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(pi, qi, 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(pi, qi, 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(pi, qi, 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,
389 typename RobustPoint1,
390 typename RobustPoint2,
392 typename IntersectionInfo
394 static inline bool handle_internal(Point1 const& /*i1*/, Point1 const& /*j1*/, Point1 const& /*k1*/,
395 Point2 const& i2, Point2 const& j2, Point2 const& /*k2*/,
396 RobustPoint1 const& ri1, RobustPoint1 const& rj1, RobustPoint1 const& /*rk1*/,
397 RobustPoint2 const& ri2, RobustPoint2 const& rj2, RobustPoint2 const& rk2,
398 bool first1, bool last1, bool first2, bool last2,
399 bool ip_i2, bool ip_j2, TurnInfo const& tp_model,
400 IntersectionInfo const& inters, unsigned int ip_index,
401 operation_type & op1, operation_type & op2)
403 typedef typename cs_tag<typename TurnInfo::point_type>::type cs_tag;
405 boost::ignore_unused_variable_warning(i2);
406 boost::ignore_unused_variable_warning(j2);
407 boost::ignore_unused_variable_warning(ip_index);
408 boost::ignore_unused_variable_warning(tp_model);
410 if ( !first2 && !last2 )
414 #ifdef BOOST_GEOMETRY_DEBUG_GET_TURNS_LINEAR_LINEAR
415 // may this give false positives for INTs?
416 typename IntersectionResult::point_type const&
417 inters_pt = inters.i_info().intersections[ip_index];
418 BOOST_GEOMETRY_ASSERT(ip_i2 == equals::equals_point_point(i2, inters_pt));
419 BOOST_GEOMETRY_ASSERT(ip_j2 == equals::equals_point_point(j2, inters_pt));
423 // don't output this IP - for the first point of other geometry segment
424 op1 = operation_none;
425 op2 = operation_none;
430 side_calculator<cs_tag, RobustPoint1, RobustPoint2, RobustPoint2>
431 side_calc(ri2, ri1, rj1, ri2, rj2, rk2);
433 std::pair<operation_type, operation_type>
434 operations = operations_of_equal(side_calc);
436 // TODO: must the above be calculated?
437 // wouldn't it be enough to check if segments are collinear?
439 if ( operations_both(operations, operation_continue) )
441 if ( op1 != operation_union
442 || op2 != operation_union
443 || ! ( G1Index == 0 ? inters.is_spike_q() : inters.is_spike_p() ) )
445 // THIS IS WRT THE ORIGINAL SEGMENTS! NOT THE ONES ABOVE!
446 bool opposite = inters.d_info().opposite;
448 op1 = operation_intersection;
449 op2 = opposite ? operation_union : operation_intersection;
454 BOOST_GEOMETRY_ASSERT(operations_combination(operations, operation_intersection, operation_union));
455 //op1 = operation_union;
456 //op2 = operation_union;
461 // else do nothing - shouldn't be handled this way
465 #ifdef BOOST_GEOMETRY_DEBUG_GET_TURNS_LINEAR_LINEAR
466 // may this give false positives for INTs?
467 typename IntersectionResult::point_type const&
468 inters_pt = inters.i_info().intersections[ip_index];
469 BOOST_GEOMETRY_ASSERT(ip_i2 == equals::equals_point_point(i2, inters_pt));
470 BOOST_GEOMETRY_ASSERT(ip_j2 == equals::equals_point_point(j2, inters_pt));
474 // don't output this IP - for the first point of other geometry segment
475 op1 = operation_none;
476 op2 = operation_none;
481 side_calculator<cs_tag, RobustPoint1, RobustPoint2, RobustPoint2>
482 side_calc(ri2, rj1, ri1, ri2, rj2, rk2);
484 std::pair<operation_type, operation_type>
485 operations = operations_of_equal(side_calc);
487 // TODO: must the above be calculated?
488 // wouldn't it be enough to check if segments are collinear?
490 if ( operations_both(operations, operation_continue) )
492 if ( op1 != operation_blocked
493 || op2 != operation_union
494 || ! ( G1Index == 0 ? inters.is_spike_q() : inters.is_spike_p() ) )
496 // THIS IS WRT THE ORIGINAL SEGMENTS! NOT THE ONES ABOVE!
497 bool second_going_out = inters.i_info().count > 1;
499 op1 = operation_blocked;
500 op2 = second_going_out ? operation_union : operation_intersection;
505 BOOST_GEOMETRY_ASSERT(operations_combination(operations, operation_intersection, operation_union));
506 //op1 = operation_blocked;
507 //op2 = operation_union;
512 // else do nothing - shouldn't be handled this way
514 // else do nothing - shouldn't be handled this way
520 static inline method_type endpoint_ip_method(bool ip_pi, bool ip_pj, bool ip_qi, bool ip_qj)
522 if ( (ip_pi || ip_pj) && (ip_qi || ip_qj) )
525 return method_touch_interior;
528 static inline turn_position ip_position(bool is_ip_first_i, bool is_ip_last_j)
530 return is_ip_first_i ? position_front :
531 ( is_ip_last_j ? position_back : position_middle );
534 template <typename Point1,
536 typename IntersectionResult,
538 typename OutputIterator>
539 static inline void assign(Point1 const& pi, Point2 const& qi,
540 IntersectionResult const& result,
541 unsigned int ip_index,
543 operation_type op0, operation_type op1,
544 turn_position pos0, turn_position pos1,
545 bool is_p_first_ip, bool is_q_first_ip,
546 bool is_p_spike, bool is_q_spike,
547 TurnInfo const& tp_model,
550 TurnInfo tp = tp_model;
552 //geometry::convert(ip, tp.point);
553 //tp.method = method;
554 base_turn_handler::assign_point(tp, method, result.template get<0>(), ip_index);
556 tp.operations[0].operation = op0;
557 tp.operations[1].operation = op1;
558 tp.operations[0].position = pos0;
559 tp.operations[1].position = pos1;
561 if ( result.template get<0>().count > 1 )
563 // NOTE: is_collinear is NOT set for the first endpoint
564 // for which there is no preceding segment
566 //BOOST_GEOMETRY_ASSERT( result.template get<1>().dir_a == 0 && result.template get<1>().dir_b == 0 );
567 if ( ! is_p_first_ip )
569 tp.operations[0].is_collinear = op0 != operation_intersection
573 if ( ! is_q_first_ip )
575 tp.operations[1].is_collinear = op1 != operation_intersection
579 else //if ( result.template get<0>().count == 1 )
581 if ( op0 == operation_blocked && op1 == operation_intersection )
583 tp.operations[0].is_collinear = true;
585 else if ( op0 == operation_intersection && op1 == operation_blocked )
587 tp.operations[1].is_collinear = true;
591 // TODO: this should get an intersection_info, which is unavailable here
592 // Because the assign_null policy accepts any structure, we pass the result instead for now
593 AssignPolicy::apply(tp, pi, qi, result);
597 template <typename SidePolicy>
598 static inline std::pair<operation_type, operation_type> operations_of_equal(SidePolicy const& side)
600 int const side_pk_q2 = side.pk_wrt_q2();
601 int const side_pk_p = side.pk_wrt_p1();
602 int const side_qk_p = side.qk_wrt_p1();
604 // If pk is collinear with qj-qk, they continue collinearly.
605 // This can be on either side of p1 (== q1), or collinear
606 // The second condition checks if they do not continue
608 if ( side_pk_q2 == 0 && side_pk_p == side_qk_p )
610 return std::make_pair(operation_continue, operation_continue);
613 // If they turn to same side (not opposite sides)
614 if ( ! base_turn_handler::opposite(side_pk_p, side_qk_p) )
616 // If pk is left of q2 or collinear: p: union, q: intersection
617 if ( side_pk_q2 != -1 )
619 return std::make_pair(operation_union, operation_intersection);
623 return std::make_pair(operation_intersection, operation_union);
628 // They turn opposite sides. If p turns left (or collinear),
629 // p: union, q: intersection
630 if ( side_pk_p != -1 )
632 return std::make_pair(operation_union, operation_intersection);
636 return std::make_pair(operation_intersection, operation_union);
641 static inline bool operations_both(
642 std::pair<operation_type, operation_type> const& operations,
643 operation_type const op)
645 return operations.first == op && operations.second == op;
648 static inline bool operations_combination(
649 std::pair<operation_type, operation_type> const& operations,
650 operation_type const op1, operation_type const op2)
652 return ( operations.first == op1 && operations.second == op2 )
653 || ( operations.first == op2 && operations.second == op1 );
657 }} // namespace detail::overlay
658 #endif // DOXYGEN_NO_DETAIL
660 }} // namespace boost::geometry
662 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_GET_TURN_INFO_FOR_ENDPOINT_HPP