1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2007-2014 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2013-2014 Adam Wulkiewicz, Lodz, Poland.
6 // This file was modified by Oracle on 2014, 2016.
7 // Modifications copyright (c) 2014-2016, Oracle and/or its affiliates.
9 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
10 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
12 // Use, modification and distribution is subject to the Boost Software License,
13 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
14 // http://www.boost.org/LICENSE_1_0.txt)
16 #ifndef BOOST_GEOMETRY_STRATEGIES_CARTESIAN_INTERSECTION_HPP
17 #define BOOST_GEOMETRY_STRATEGIES_CARTESIAN_INTERSECTION_HPP
21 #include <boost/geometry/core/exception.hpp>
23 #include <boost/geometry/geometries/concepts/point_concept.hpp>
24 #include <boost/geometry/geometries/concepts/segment_concept.hpp>
26 #include <boost/geometry/arithmetic/determinant.hpp>
27 #include <boost/geometry/algorithms/detail/assign_values.hpp>
28 #include <boost/geometry/algorithms/detail/assign_indexed_point.hpp>
29 #include <boost/geometry/algorithms/detail/equals/point_point.hpp>
30 #include <boost/geometry/algorithms/detail/recalculate.hpp>
32 #include <boost/geometry/util/math.hpp>
33 #include <boost/geometry/util/promote_integral.hpp>
34 #include <boost/geometry/util/select_calculation_type.hpp>
36 // Temporary / will be Strategy as template parameter
37 #include <boost/geometry/strategies/side.hpp>
38 #include <boost/geometry/strategies/cartesian/side_by_triangle.hpp>
40 #include <boost/geometry/strategies/side_info.hpp>
41 #include <boost/geometry/strategies/intersection.hpp>
42 #include <boost/geometry/strategies/intersection_result.hpp>
44 #include <boost/geometry/policies/robustness/robust_point_type.hpp>
45 #include <boost/geometry/policies/robustness/segment_ratio_type.hpp>
48 #if defined(BOOST_GEOMETRY_DEBUG_ROBUSTNESS)
49 # include <boost/geometry/io/wkt/write.hpp>
53 namespace boost { namespace geometry
57 namespace strategy { namespace intersection
62 \see http://mathworld.wolfram.com/Line-LineIntersection.html
64 template <typename Policy, typename CalculationType = void>
65 struct relate_cartesian_segments
67 typedef typename Policy::return_type return_type;
69 template <typename CoordinateType, typename SegmentRatio>
70 struct segment_intersection_info
72 typedef typename select_most_precise
74 CoordinateType, double
75 >::type promoted_type;
77 promoted_type comparable_length_a() const
79 return dx_a * dx_a + dy_a * dy_a;
82 promoted_type comparable_length_b() const
84 return dx_b * dx_b + dy_b * dy_b;
87 template <typename Point, typename Segment1, typename Segment2>
88 void assign_a(Point& point, Segment1 const& a, Segment2 const& ) const
90 assign(point, a, dx_a, dy_a, robust_ra);
92 template <typename Point, typename Segment1, typename Segment2>
93 void assign_b(Point& point, Segment1 const& , Segment2 const& b) const
95 assign(point, b, dx_b, dy_b, robust_rb);
98 template <typename Point, typename Segment>
99 void assign(Point& point, Segment const& segment, CoordinateType const& dx, CoordinateType const& dy, SegmentRatio const& ratio) const
101 // Calculate the intersection point based on segment_ratio
102 // Up to now, division was postponed. Here we divide using numerator/
103 // denominator. In case of integer this results in an integer
105 BOOST_GEOMETRY_ASSERT(ratio.denominator() != 0);
107 typedef typename promote_integral<CoordinateType>::type promoted_type;
109 promoted_type const numerator
110 = boost::numeric_cast<promoted_type>(ratio.numerator());
111 promoted_type const denominator
112 = boost::numeric_cast<promoted_type>(ratio.denominator());
113 promoted_type const dx_promoted = boost::numeric_cast<promoted_type>(dx);
114 promoted_type const dy_promoted = boost::numeric_cast<promoted_type>(dy);
116 set<0>(point, get<0, 0>(segment) + boost::numeric_cast
119 >(numerator * dx_promoted / denominator));
120 set<1>(point, get<0, 1>(segment) + boost::numeric_cast
123 >(numerator * dy_promoted / denominator));
126 CoordinateType dx_a, dy_a;
127 CoordinateType dx_b, dy_b;
128 SegmentRatio robust_ra;
129 SegmentRatio robust_rb;
132 template <typename D, typename W, typename ResultType>
133 static inline void cramers_rule(D const& dx_a, D const& dy_a,
134 D const& dx_b, D const& dy_b, W const& wx, W const& wy,
136 ResultType& d, ResultType& da)
139 d = geometry::detail::determinant<ResultType>(dx_a, dy_a, dx_b, dy_b);
140 da = geometry::detail::determinant<ResultType>(dx_b, dy_b, wx, wy);
141 // Ratio is da/d , collinear if d == 0, intersecting if 0 <= r <= 1
142 // IntersectionPoint = (x1 + r * dx_a, y1 + r * dy_a)
146 // Relate segments a and b
147 template <typename Segment1, typename Segment2, typename RobustPolicy>
148 static inline return_type apply(Segment1 const& a, Segment2 const& b,
149 RobustPolicy const& robust_policy)
151 // type them all as in Segment1 - TODO reconsider this, most precise?
152 typedef typename geometry::point_type<Segment1>::type point_type;
154 typedef typename geometry::robust_point_type
156 point_type, RobustPolicy
157 >::type robust_point_type;
159 point_type a0, a1, b0, b1;
160 robust_point_type a0_rob, a1_rob, b0_rob, b1_rob;
162 detail::assign_point_from_index<0>(a, a0);
163 detail::assign_point_from_index<1>(a, a1);
164 detail::assign_point_from_index<0>(b, b0);
165 detail::assign_point_from_index<1>(b, b1);
167 geometry::recalculate(a0_rob, a0, robust_policy);
168 geometry::recalculate(a1_rob, a1, robust_policy);
169 geometry::recalculate(b0_rob, b0, robust_policy);
170 geometry::recalculate(b1_rob, b1, robust_policy);
172 return apply(a, b, robust_policy, a0_rob, a1_rob, b0_rob, b1_rob);
175 // The main entry-routine, calculating intersections of segments a / b
176 // NOTE: Robust* types may be the same as Segments' point types
177 template <typename Segment1, typename Segment2,
178 typename RobustPolicy,
179 typename RobustPoint1, typename RobustPoint2>
180 static inline return_type apply(Segment1 const& a, Segment2 const& b,
181 RobustPolicy const& /*robust_policy*/,
182 RobustPoint1 const& robust_a1, RobustPoint1 const& robust_a2,
183 RobustPoint2 const& robust_b1, RobustPoint2 const& robust_b2)
185 BOOST_CONCEPT_ASSERT( (concepts::ConstSegment<Segment1>) );
186 BOOST_CONCEPT_ASSERT( (concepts::ConstSegment<Segment2>) );
188 using geometry::detail::equals::equals_point_point;
189 bool const a_is_point = equals_point_point(robust_a1, robust_a2);
190 bool const b_is_point = equals_point_point(robust_b1, robust_b2);
192 if(a_is_point && b_is_point)
194 return equals_point_point(robust_a1, robust_b2)
195 ? Policy::degenerate(a, true)
200 typedef typename select_calculation_type
201 <Segment1, Segment2, CalculationType>::type coordinate_type;
203 typedef side::side_by_triangle<coordinate_type> side;
206 sides.set<0>(side::apply(robust_b1, robust_b2, robust_a1),
207 side::apply(robust_b1, robust_b2, robust_a2));
211 // Both points are at same side of other segment, we can leave
212 return Policy::disjoint();
215 sides.set<1>(side::apply(robust_a1, robust_a2, robust_b1),
216 side::apply(robust_a1, robust_a2, robust_b2));
220 // Both points are at same side of other segment, we can leave
221 return Policy::disjoint();
224 bool collinear = sides.collinear();
226 typedef typename select_most_precise
228 typename geometry::coordinate_type<RobustPoint1>::type,
229 typename geometry::coordinate_type<RobustPoint2>::type
230 >::type robust_coordinate_type;
232 typedef typename segment_ratio_type
234 typename geometry::point_type<Segment1>::type, // TODO: most precise point?
238 segment_intersection_info
244 sinfo.dx_a = get<1, 0>(a) - get<0, 0>(a); // distance in x-dir
245 sinfo.dx_b = get<1, 0>(b) - get<0, 0>(b);
246 sinfo.dy_a = get<1, 1>(a) - get<0, 1>(a); // distance in y-dir
247 sinfo.dy_b = get<1, 1>(b) - get<0, 1>(b);
249 robust_coordinate_type const robust_dx_a = get<0>(robust_a2) - get<0>(robust_a1);
250 robust_coordinate_type const robust_dx_b = get<0>(robust_b2) - get<0>(robust_b1);
251 robust_coordinate_type const robust_dy_a = get<1>(robust_a2) - get<1>(robust_a1);
252 robust_coordinate_type const robust_dy_b = get<1>(robust_b2) - get<1>(robust_b1);
254 // r: ratio 0-1 where intersection divides A/B
255 // (only calculated for non-collinear segments)
258 robust_coordinate_type robust_da0, robust_da;
259 robust_coordinate_type robust_db0, robust_db;
261 cramers_rule(robust_dx_a, robust_dy_a, robust_dx_b, robust_dy_b,
262 get<0>(robust_a1) - get<0>(robust_b1),
263 get<1>(robust_a1) - get<1>(robust_b1),
264 robust_da0, robust_da);
266 cramers_rule(robust_dx_b, robust_dy_b, robust_dx_a, robust_dy_a,
267 get<0>(robust_b1) - get<0>(robust_a1),
268 get<1>(robust_b1) - get<1>(robust_a1),
269 robust_db0, robust_db);
271 math::detail::equals_factor_policy<robust_coordinate_type>
272 policy(robust_dx_a, robust_dy_a, robust_dx_b, robust_dy_b);
273 robust_coordinate_type const zero = 0;
274 if (math::detail::equals_by_policy(robust_da0, zero, policy)
275 || math::detail::equals_by_policy(robust_db0, zero, policy))
277 // If this is the case, no rescaling is done for FP precision.
278 // We set it to collinear, but it indicates a robustness issue.
285 sinfo.robust_ra.assign(robust_da, robust_da0);
286 sinfo.robust_rb.assign(robust_db, robust_db0);
292 std::pair<bool, bool> const collinear_use_first
293 = is_x_more_significant(geometry::math::abs(robust_dx_a),
294 geometry::math::abs(robust_dy_a),
295 geometry::math::abs(robust_dx_b),
296 geometry::math::abs(robust_dy_b),
297 a_is_point, b_is_point);
299 if (collinear_use_first.second)
301 // Degenerate cases: segments of single point, lying on other segment, are not disjoint
302 // This situation is collinear too
304 if (collinear_use_first.first)
306 return relate_collinear<0, ratio_type>(a, b,
307 robust_a1, robust_a2, robust_b1, robust_b2,
308 a_is_point, b_is_point);
312 // Y direction contains larger segments (maybe dx is zero)
313 return relate_collinear<1, ratio_type>(a, b,
314 robust_a1, robust_a2, robust_b1, robust_b2,
315 a_is_point, b_is_point);
320 return Policy::segments_crosses(sides, sinfo, a, b);
324 // first is true if x is more significant
325 // second is true if the more significant difference is not 0
326 template <typename RobustCoordinateType>
327 static inline std::pair<bool, bool>
328 is_x_more_significant(RobustCoordinateType const& abs_robust_dx_a,
329 RobustCoordinateType const& abs_robust_dy_a,
330 RobustCoordinateType const& abs_robust_dx_b,
331 RobustCoordinateType const& abs_robust_dy_b,
332 bool const a_is_point,
333 bool const b_is_point)
335 //BOOST_GEOMETRY_ASSERT_MSG(!(a_is_point && b_is_point), "both segments shouldn't be degenerated");
337 // for degenerated segments the second is always true because this function
338 // shouldn't be called if both segments were degenerated
342 return std::make_pair(abs_robust_dx_b >= abs_robust_dy_b, true);
346 return std::make_pair(abs_robust_dx_a >= abs_robust_dy_a, true);
350 RobustCoordinateType const min_dx = (std::min)(abs_robust_dx_a, abs_robust_dx_b);
351 RobustCoordinateType const min_dy = (std::min)(abs_robust_dy_a, abs_robust_dy_b);
352 return min_dx == min_dy ?
353 std::make_pair(true, min_dx > RobustCoordinateType(0)) :
354 std::make_pair(min_dx > min_dy, true);
360 std::size_t Dimension,
364 typename RobustPoint1,
365 typename RobustPoint2
367 static inline return_type relate_collinear(Segment1 const& a,
369 RobustPoint1 const& robust_a1, RobustPoint1 const& robust_a2,
370 RobustPoint2 const& robust_b1, RobustPoint2 const& robust_b2,
371 bool a_is_point, bool b_is_point)
375 return relate_one_degenerate<RatioType>(a,
376 get<Dimension>(robust_a1),
377 get<Dimension>(robust_b1), get<Dimension>(robust_b2),
382 return relate_one_degenerate<RatioType>(b,
383 get<Dimension>(robust_b1),
384 get<Dimension>(robust_a1), get<Dimension>(robust_a2),
387 return relate_collinear<RatioType>(a, b,
388 get<Dimension>(robust_a1),
389 get<Dimension>(robust_a2),
390 get<Dimension>(robust_b1),
391 get<Dimension>(robust_b2));
394 /// Relate segments known collinear
400 typename RobustType1,
403 static inline return_type relate_collinear(Segment1 const& a
405 , RobustType1 oa_1, RobustType1 oa_2
406 , RobustType2 ob_1, RobustType2 ob_2
409 // Calculate the ratios where a starts in b, b starts in a
410 // a1--------->a2 (2..7)
414 // b1 is located w.r.t. a at ratio: (5-2)/5=3/5 (on a)
415 // b2 is located w.r.t. a at ratio: (8-2)/5=6/5 (right of a)
416 // a1 is located w.r.t. b at ratio: (2-5)/3=-3/3 (left of b)
417 // a2 is located w.r.t. b at ratio: (7-5)/3=2/3 (on b)
418 // A arrives (a2 on b), B departs (b1 on a)
420 // If both are reversed:
421 // a2<---------a1 (7..2)
425 // b1 is located w.r.t. a at ratio: (8-7)/-5=-1/5 (before a starts)
426 // b2 is located w.r.t. a at ratio: (5-7)/-5=2/5 (on a)
427 // a1 is located w.r.t. b at ratio: (7-8)/-3=1/3 (on b)
428 // a2 is located w.r.t. b at ratio: (2-8)/-3=6/3 (after b ends)
430 // If both one is reversed:
431 // a1--------->a2 (2..7)
435 // b1 is located w.r.t. a at ratio: (8-2)/5=6/5 (after a ends)
436 // b2 is located w.r.t. a at ratio: (5-2)/5=3/5 (on a)
437 // a1 is located w.r.t. b at ratio: (2-8)/-3=6/3 (after b ends)
438 // a2 is located w.r.t. b at ratio: (7-8)/-3=1/3 (on b)
439 RobustType1 const length_a = oa_2 - oa_1; // no abs, see above
440 RobustType2 const length_b = ob_2 - ob_1;
442 RatioType ra_from(oa_1 - ob_1, length_b);
443 RatioType ra_to(oa_2 - ob_1, length_b);
444 RatioType rb_from(ob_1 - oa_1, length_a);
445 RatioType rb_to(ob_2 - oa_1, length_a);
447 // use absolute measure to detect endpoints intersection
448 // NOTE: it'd be possible to calculate bx_wrt_a using ax_wrt_b values
449 int const a1_wrt_b = position_value(oa_1, ob_1, ob_2);
450 int const a2_wrt_b = position_value(oa_2, ob_1, ob_2);
451 int const b1_wrt_a = position_value(ob_1, oa_1, oa_2);
452 int const b2_wrt_a = position_value(ob_2, oa_1, oa_2);
454 // fix the ratios if necessary
455 // CONSIDER: fixing ratios also in other cases, if they're inconsistent
456 // e.g. if ratio == 1 or 0 (so IP at the endpoint)
457 // but position value indicates that the IP is in the middle of the segment
458 // because one of the segments is very long
459 // In such case the ratios could be moved into the middle direction
460 // by some small value (e.g. EPS+1ULP)
463 ra_from.assign(0, 1);
464 rb_from.assign(0, 1);
466 else if (a1_wrt_b == 3)
468 ra_from.assign(1, 1);
475 rb_from.assign(1, 1);
477 else if (a2_wrt_b == 3)
483 if ((a1_wrt_b < 1 && a2_wrt_b < 1) || (a1_wrt_b > 3 && a2_wrt_b > 3))
484 //if ((ra_from.left() && ra_to.left()) || (ra_from.right() && ra_to.right()))
486 return Policy::disjoint();
489 bool const opposite = math::sign(length_a) != math::sign(length_b);
491 return Policy::segments_collinear(a, b, opposite,
492 a1_wrt_b, a2_wrt_b, b1_wrt_a, b2_wrt_a,
493 ra_from, ra_to, rb_from, rb_to);
496 /// Relate segments where one is degenerate
500 typename DegenerateSegment,
501 typename RobustType1,
504 static inline return_type relate_one_degenerate(
505 DegenerateSegment const& degenerate_segment
507 , RobustType2 s1, RobustType2 s2
511 // Calculate the ratios where ds starts in s
512 // a1--------->a2 (2..6)
514 // Ratio: (4-2)/(6-2)
515 RatioType const ratio(d - s1, s2 - s1);
517 if (!ratio.on_segment())
519 return Policy::disjoint();
522 return Policy::one_degenerate(degenerate_segment, ratio, a_degenerate);
525 template <typename ProjCoord1, typename ProjCoord2>
526 static inline int position_value(ProjCoord1 const& ca1,
527 ProjCoord2 const& cb1,
528 ProjCoord2 const& cb2)
532 return math::equals(ca1, cb1) ? 1
533 : math::equals(ca1, cb2) ? 3
545 #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
549 template <typename Policy, typename CalculationType>
550 struct default_strategy<cartesian_tag, Policy, CalculationType>
552 typedef relate_cartesian_segments<Policy, CalculationType> type;
555 } // namespace services
556 #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
559 }} // namespace strategy::intersection
561 }} // namespace boost::geometry
564 #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_INTERSECTION_HPP