intersection.hpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2014 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2013-2023 Adam Wulkiewicz, Lodz, Poland.
  4. // This file was modified by Oracle on 2014-2024.
  5. // Modifications copyright (c) 2014-2024, Oracle and/or its affiliates.
  6. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  7. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  8. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  9. // Use, modification and distribution is subject to the Boost Software License,
  10. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. #ifndef BOOST_GEOMETRY_STRATEGIES_CARTESIAN_INTERSECTION_HPP
  13. #define BOOST_GEOMETRY_STRATEGIES_CARTESIAN_INTERSECTION_HPP
  14. #include <algorithm>
  15. #include <boost/geometry/core/exception.hpp>
  16. #include <boost/geometry/geometries/concepts/point_concept.hpp>
  17. #include <boost/geometry/geometries/concepts/segment_concept.hpp>
  18. #include <boost/geometry/geometries/segment.hpp>
  19. #include <boost/geometry/arithmetic/determinant.hpp>
  20. #include <boost/geometry/algorithms/detail/assign_values.hpp>
  21. #include <boost/geometry/algorithms/detail/assign_indexed_point.hpp>
  22. #include <boost/geometry/algorithms/detail/equals/point_point.hpp>
  23. #include <boost/geometry/util/math.hpp>
  24. #include <boost/geometry/util/numeric_cast.hpp>
  25. #include <boost/geometry/util/promote_integral.hpp>
  26. #include <boost/geometry/util/select_calculation_type.hpp>
  27. #include <boost/geometry/strategy/cartesian/area.hpp>
  28. #include <boost/geometry/strategy/cartesian/envelope.hpp>
  29. #include <boost/geometry/strategy/cartesian/expand_box.hpp>
  30. #include <boost/geometry/strategy/cartesian/expand_segment.hpp>
  31. #include <boost/geometry/strategies/cartesian/disjoint_box_box.hpp>
  32. #include <boost/geometry/strategies/cartesian/disjoint_segment_box.hpp>
  33. #include <boost/geometry/strategies/cartesian/distance_pythagoras.hpp>
  34. #include <boost/geometry/strategies/cartesian/point_in_point.hpp>
  35. #include <boost/geometry/strategies/cartesian/point_in_poly_winding.hpp>
  36. #include <boost/geometry/strategies/covered_by.hpp>
  37. #include <boost/geometry/strategies/intersection.hpp>
  38. #include <boost/geometry/strategies/intersection_result.hpp>
  39. #include <boost/geometry/strategies/side.hpp>
  40. #include <boost/geometry/strategies/side_info.hpp>
  41. #include <boost/geometry/strategies/within.hpp>
  42. namespace boost { namespace geometry
  43. {
  44. namespace strategy { namespace intersection
  45. {
  46. namespace detail_usage
  47. {
  48. // When calculating the intersection, the information of "a" or "b" can be used.
  49. // Theoretically this gives equal results, but due to floating point precision
  50. // there might be tiny differences. These are edge cases.
  51. // This structure is to determine if "a" or "b" should be used.
  52. // Prefer the segment closer to the endpoint.
  53. // If both are about equally close, then prefer the longer segment
  54. // To avoid hard thresholds, behavior is made fluent.
  55. // Calculate comparable length indications,
  56. // the longer the segment (relatively), the lower the value
  57. // such that the shorter lengths are evaluated higher and will
  58. // be preferred.
  59. template <bool IsArithmetic>
  60. struct use_a
  61. {
  62. template <typename Ct, typename Ev>
  63. static bool apply(Ct const& cla, Ct const& clb, Ev const& eva, Ev const& evb)
  64. {
  65. auto const clm = (std::max)(cla, clb);
  66. if (clm <= 0)
  67. {
  68. return true;
  69. }
  70. // Relative comparible length
  71. auto const rcla = Ct(1.0) - cla / clm;
  72. auto const rclb = Ct(1.0) - clb / clm;
  73. // Multipliers for edgevalue (ev) and relative comparible length (rcl)
  74. // They determine the balance between edge value (should be larger)
  75. // and segment length. In 99.9xx% of the cases there is no difference
  76. // at all (if either a or b is used). Therefore the values of the
  77. // constants are not sensitive for the majority of the situations.
  78. // One known case is #mysql_23023665_6 (difference) which needs mev >= 2
  79. Ev const mev = 5;
  80. Ev const mrcl = 1;
  81. return mev * eva + mrcl * rcla > mev * evb + mrcl * rclb;
  82. }
  83. };
  84. // Specialization for non arithmetic types. They will always use "a"
  85. template <>
  86. struct use_a<false>
  87. {
  88. template <typename Ct, typename Ev>
  89. static bool apply(Ct const& , Ct const& , Ev const& , Ev const& )
  90. {
  91. return true;
  92. }
  93. };
  94. }
  95. /*!
  96. \see http://mathworld.wolfram.com/Line-LineIntersection.html
  97. */
  98. template
  99. <
  100. typename CalculationType = void
  101. >
  102. struct cartesian_segments
  103. {
  104. using cs_tag = cartesian_tag;
  105. template <typename CoordinateType, typename SegmentRatio>
  106. struct segment_intersection_info
  107. {
  108. private :
  109. typedef typename select_most_precise
  110. <
  111. CoordinateType, double
  112. >::type promoted_type;
  113. promoted_type comparable_length_a() const
  114. {
  115. return dx_a * dx_a + dy_a * dy_a;
  116. }
  117. promoted_type comparable_length_b() const
  118. {
  119. return dx_b * dx_b + dy_b * dy_b;
  120. }
  121. template <typename Point, typename Segment1, typename Segment2>
  122. void assign_a(Point& point, Segment1 const& a, Segment2 const& ) const
  123. {
  124. assign(point, a, dx_a, dy_a, ra);
  125. }
  126. template <typename Point, typename Segment1, typename Segment2>
  127. void assign_b(Point& point, Segment1 const& , Segment2 const& b) const
  128. {
  129. assign(point, b, dx_b, dy_b, rb);
  130. }
  131. template <typename Point, typename Segment>
  132. void assign(Point& point, Segment const& segment,
  133. CoordinateType const& dx, CoordinateType const& dy,
  134. SegmentRatio const& ratio) const
  135. {
  136. // Calculate the intersection point based on segment_ratio
  137. // The division, postponed until here, is done now. In case of integer this
  138. // results in an integer which rounds to the nearest integer.
  139. BOOST_GEOMETRY_ASSERT(ratio.denominator() != typename SegmentRatio::int_type(0));
  140. typedef typename promote_integral<CoordinateType>::type calc_type;
  141. calc_type const numerator
  142. = util::numeric_cast<calc_type>(ratio.numerator());
  143. calc_type const denominator
  144. = util::numeric_cast<calc_type>(ratio.denominator());
  145. calc_type const dx_calc = util::numeric_cast<calc_type>(dx);
  146. calc_type const dy_calc = util::numeric_cast<calc_type>(dy);
  147. set<0>(point, get<0, 0>(segment)
  148. + util::numeric_cast<CoordinateType>(
  149. math::divide<calc_type>(numerator * dx_calc, denominator)));
  150. set<1>(point, get<0, 1>(segment)
  151. + util::numeric_cast<CoordinateType>(
  152. math::divide<calc_type>(numerator * dy_calc, denominator)));
  153. }
  154. template <int Index, int Dim, typename Point, typename Segment>
  155. static bool exceeds_side_in_dimension(Point& p, Segment const& s)
  156. {
  157. // Situation a (positive)
  158. // 0>-------------->1 segment
  159. // * point left of segment<I> in D x or y
  160. // Situation b (negative)
  161. // 1<--------------<0 segment
  162. // * point right of segment<I>
  163. // Situation c (degenerate), return false (check other dimension)
  164. auto const& c = get<Dim>(p);
  165. auto const& c0 = get<Index, Dim>(s);
  166. auto const& c1 = get<1 - Index, Dim>(s);
  167. return c0 < c1 ? math::smaller(c, c0)
  168. : c0 > c1 ? math::larger(c, c0)
  169. : false;
  170. }
  171. template <int Index, typename Point, typename Segment>
  172. static bool exceeds_side_of_segment(Point& p, Segment const& s)
  173. {
  174. return exceeds_side_in_dimension<Index, 0>(p, s)
  175. || exceeds_side_in_dimension<Index, 1>(p, s);
  176. }
  177. template <typename Point, typename Segment>
  178. static void assign_if_exceeds(Point& point, Segment const& s)
  179. {
  180. if (exceeds_side_of_segment<0>(point, s))
  181. {
  182. detail::assign_point_from_index<0>(s, point);
  183. }
  184. else if (exceeds_side_of_segment<1>(point, s))
  185. {
  186. detail::assign_point_from_index<1>(s, point);
  187. }
  188. }
  189. public :
  190. template <typename Point, typename Segment1, typename Segment2>
  191. void calculate(Point& point, Segment1 const& a, Segment2 const& b) const
  192. {
  193. bool const use_a
  194. = detail_usage::use_a
  195. <
  196. std::is_arithmetic<CoordinateType>::value
  197. >::apply(comparable_length_a(), comparable_length_b(),
  198. ra.edge_value(), rb.edge_value());
  199. if (use_a)
  200. {
  201. assign_a(point, a, b);
  202. }
  203. else
  204. {
  205. assign_b(point, a, b);
  206. }
  207. // Verify nearly collinear cases (the threshold is arbitrary
  208. // but influences performance). If the intersection is located
  209. // outside the segments, then it should be moved.
  210. if (ra.possibly_collinear(1.0e-3) && rb.possibly_collinear(1.0e-3))
  211. {
  212. // The segments are nearly collinear and because of the calculation
  213. // method with very small denominator, the IP appears outside the
  214. // segment(s). Correct it to the end point.
  215. // Because they are nearly collinear, it doesn't really matter to
  216. // to which endpoint (or it is corrected twice).
  217. assign_if_exceeds(point, a);
  218. assign_if_exceeds(point, b);
  219. }
  220. }
  221. CoordinateType dx_a, dy_a;
  222. CoordinateType dx_b, dy_b;
  223. SegmentRatio ra;
  224. SegmentRatio rb;
  225. };
  226. template <typename D, typename W, typename ResultType>
  227. static inline void cramers_rule(D const& dx_a, D const& dy_a,
  228. D const& dx_b, D const& dy_b, W const& wx, W const& wy,
  229. // out:
  230. ResultType& nominator, ResultType& denominator)
  231. {
  232. // Cramers rule
  233. nominator = geometry::detail::determinant<ResultType>(dx_b, dy_b, wx, wy);
  234. denominator = geometry::detail::determinant<ResultType>(dx_a, dy_a, dx_b, dy_b);
  235. // Ratio r = nominator/denominator
  236. // Collinear if denominator == 0, intersecting if 0 <= r <= 1
  237. // IntersectionPoint = (x1 + r * dx_a, y1 + r * dy_a)
  238. }
  239. template
  240. <
  241. typename UniqueSubRange1,
  242. typename UniqueSubRange2,
  243. typename Policy
  244. >
  245. static inline typename Policy::return_type apply(UniqueSubRange1 const& range_p,
  246. UniqueSubRange2 const& range_q,
  247. Policy const& policy)
  248. {
  249. typedef typename UniqueSubRange1::point_type point1_type;
  250. typedef typename UniqueSubRange2::point_type point2_type;
  251. BOOST_CONCEPT_ASSERT( (concepts::ConstPoint<point1_type>) );
  252. BOOST_CONCEPT_ASSERT( (concepts::ConstPoint<point2_type>) );
  253. point1_type const& p1 = range_p.at(0);
  254. point1_type const& p2 = range_p.at(1);
  255. point2_type const& q1 = range_q.at(0);
  256. point2_type const& q2 = range_q.at(1);
  257. using coordinate_type = typename select_most_precise
  258. <
  259. geometry::coordinate_type_t<point1_type>,
  260. geometry::coordinate_type_t<point2_type>
  261. >::type;
  262. using ratio_type = segment_ratio<coordinate_type>;
  263. segment_intersection_info
  264. <
  265. typename select_calculation_type<point1_type, point2_type, CalculationType>::type,
  266. ratio_type
  267. > sinfo;
  268. sinfo.dx_a = get<0>(p2) - get<0>(p1); // distance in x-dir
  269. sinfo.dx_b = get<0>(q2) - get<0>(q1);
  270. sinfo.dy_a = get<1>(p2) - get<1>(p1); // distance in y-dir
  271. sinfo.dy_b = get<1>(q2) - get<1>(q1);
  272. return unified<ratio_type>(sinfo, range_p, range_q, policy);
  273. }
  274. //! Returns true if two segments do not overlap.
  275. //! If not, then no further calculations need to be done.
  276. template
  277. <
  278. std::size_t Dimension,
  279. typename PointP,
  280. typename PointQ
  281. >
  282. static inline bool disjoint_by_range(PointP const& p1, PointP const& p2,
  283. PointQ const& q1, PointQ const& q2)
  284. {
  285. auto minp = get<Dimension>(p1);
  286. auto maxp = get<Dimension>(p2);
  287. auto minq = get<Dimension>(q1);
  288. auto maxq = get<Dimension>(q2);
  289. if (minp > maxp)
  290. {
  291. std::swap(minp, maxp);
  292. }
  293. if (minq > maxq)
  294. {
  295. std::swap(minq, maxq);
  296. }
  297. // In this case, max(p) < min(q)
  298. // P Q
  299. // <-------> <------->
  300. // (and the space in between is not extremely small)
  301. return math::smaller(maxp, minq) || math::smaller(maxq, minp);
  302. }
  303. template
  304. <
  305. typename RatioType,
  306. typename SegmentInfo,
  307. typename UniqueSubRange1,
  308. typename UniqueSubRange2,
  309. typename Policy
  310. >
  311. static inline typename Policy::return_type
  312. unified(SegmentInfo& sinfo,
  313. UniqueSubRange1 const& range_p,
  314. UniqueSubRange2 const& range_q,
  315. Policy const&)
  316. {
  317. using point1_type = typename UniqueSubRange1::point_type;
  318. using point2_type = typename UniqueSubRange2::point_type;
  319. point1_type const& p1 = range_p.at(0);
  320. point1_type const& p2 = range_p.at(1);
  321. point2_type const& q1 = range_q.at(0);
  322. point2_type const& q2 = range_q.at(1);
  323. bool const p_is_point = equals_point_point(p1, p2);
  324. bool const q_is_point = equals_point_point(q1, q2);
  325. // Declare segments, currently necessary for the policies
  326. // (segment_crosses, segment_colinear, degenerate, one_degenerate, etc)
  327. model::referring_segment<point1_type const> const p(p1, p2);
  328. model::referring_segment<point2_type const> const q(q1, q2);
  329. if (p_is_point && q_is_point)
  330. {
  331. return equals_point_point(p1, q2)
  332. ? Policy::degenerate(p, true)
  333. : Policy::disjoint()
  334. ;
  335. }
  336. if (disjoint_by_range<0>(p1, p2, q1, q2)
  337. || disjoint_by_range<1>(p1, p2, q1, q2))
  338. {
  339. return Policy::disjoint();
  340. }
  341. using side_strategy_type
  342. = typename side::services::default_strategy
  343. <cartesian_tag, CalculationType>::type;
  344. side_info sides;
  345. sides.set<0>(side_strategy_type::apply(q1, q2, p1),
  346. side_strategy_type::apply(q1, q2, p2));
  347. if (sides.same<0>())
  348. {
  349. // Both points are at same side of other segment, we can leave
  350. return Policy::disjoint();
  351. }
  352. sides.set<1>(side_strategy_type::apply(p1, p2, q1),
  353. side_strategy_type::apply(p1, p2, q2));
  354. if (sides.same<1>())
  355. {
  356. // Both points are at same side of other segment, we can leave
  357. return Policy::disjoint();
  358. }
  359. bool collinear = sides.collinear();
  360. // r: ratio 0-1 where intersection divides A/B
  361. // (only calculated for non-collinear segments)
  362. if (! collinear)
  363. {
  364. using coordinate_type = typename select_most_precise
  365. <
  366. geometry::coordinate_type_t<point1_type>,
  367. geometry::coordinate_type_t<point2_type>
  368. >::type;
  369. coordinate_type denominator_a;
  370. coordinate_type nominator_a;
  371. coordinate_type denominator_b;
  372. coordinate_type nominator_b;
  373. cramers_rule(sinfo.dx_a, sinfo.dy_a, sinfo.dx_b, sinfo.dy_b,
  374. get<0>(p1) - get<0>(q1),
  375. get<1>(p1) - get<1>(q1),
  376. nominator_a, denominator_a);
  377. cramers_rule(sinfo.dx_b, sinfo.dy_b, sinfo.dx_a, sinfo.dy_a,
  378. get<0>(q1) - get<0>(p1),
  379. get<1>(q1) - get<1>(p1),
  380. nominator_b, denominator_b);
  381. math::detail::equals_factor_policy<coordinate_type>
  382. policy(sinfo.dx_a, sinfo.dy_a, sinfo.dx_b, sinfo.dy_b);
  383. coordinate_type const zero = 0;
  384. if (math::detail::equals_by_policy(denominator_a, zero, policy)
  385. || math::detail::equals_by_policy(denominator_b, zero, policy))
  386. {
  387. // We set it to collinear, but it indicates a robustness issue.
  388. sides.set<0>(0, 0);
  389. sides.set<1>(0, 0);
  390. collinear = true;
  391. }
  392. else
  393. {
  394. sinfo.ra.assign(nominator_a, denominator_a);
  395. sinfo.rb.assign(nominator_b, denominator_b);
  396. }
  397. }
  398. if (collinear)
  399. {
  400. std::pair<bool, bool> const collinear_use_first
  401. = is_x_more_significant(geometry::math::abs(sinfo.dx_a),
  402. geometry::math::abs(sinfo.dy_a),
  403. geometry::math::abs(sinfo.dx_b),
  404. geometry::math::abs(sinfo.dy_b),
  405. p_is_point, q_is_point);
  406. if (collinear_use_first.second)
  407. {
  408. // Degenerate cases: segments of single point, lying on other segment, are not disjoint
  409. // This situation is collinear too
  410. if (collinear_use_first.first)
  411. {
  412. return relate_collinear<0, Policy, RatioType>(p, q,
  413. p1, p2, q1, q2,
  414. p_is_point, q_is_point);
  415. }
  416. else
  417. {
  418. // Y direction contains larger segments (maybe dx is zero)
  419. return relate_collinear<1, Policy, RatioType>(p, q,
  420. p1, p2, q1, q2,
  421. p_is_point, q_is_point);
  422. }
  423. }
  424. }
  425. if (equals_point_point(p1, q1) || equals_point_point(p1, q2))
  426. {
  427. return Policy::segments_share_common_point(sides, sinfo, p1);
  428. }
  429. if (equals_point_point(p2, q1) || equals_point_point(p2, q2))
  430. {
  431. return Policy::segments_share_common_point(sides, sinfo, p2);
  432. }
  433. return Policy::segments_crosses(sides, sinfo, p, q);
  434. }
  435. private:
  436. // first is true if x is more significant
  437. // second is true if the more significant difference is not 0
  438. template <typename CoordinateType>
  439. static inline std::pair<bool, bool>
  440. is_x_more_significant(CoordinateType const& abs_dx_a,
  441. CoordinateType const& abs_dy_a,
  442. CoordinateType const& abs_dx_b,
  443. CoordinateType const& abs_dy_b,
  444. bool const a_is_point,
  445. bool const b_is_point)
  446. {
  447. //BOOST_GEOMETRY_ASSERT_MSG(!(a_is_point && b_is_point), "both segments shouldn't be degenerated");
  448. // for degenerated segments the second is always true because this function
  449. // shouldn't be called if both segments were degenerated
  450. if (a_is_point)
  451. {
  452. return std::make_pair(abs_dx_b >= abs_dy_b, true);
  453. }
  454. else if (b_is_point)
  455. {
  456. return std::make_pair(abs_dx_a >= abs_dy_a, true);
  457. }
  458. else
  459. {
  460. CoordinateType const min_dx = (std::min)(abs_dx_a, abs_dx_b);
  461. CoordinateType const min_dy = (std::min)(abs_dy_a, abs_dy_b);
  462. return min_dx == min_dy ?
  463. std::make_pair(true, min_dx > CoordinateType(0)) :
  464. std::make_pair(min_dx > min_dy, true);
  465. }
  466. }
  467. template
  468. <
  469. std::size_t Dimension,
  470. typename Policy,
  471. typename RatioType,
  472. typename Segment1,
  473. typename Segment2,
  474. typename Point1,
  475. typename Point2
  476. >
  477. static inline typename Policy::return_type
  478. relate_collinear(Segment1 const& a,
  479. Segment2 const& b,
  480. Point1 const& a1, Point1 const& a2,
  481. Point2 const& b1, Point2 const& b2,
  482. bool a_is_point, bool b_is_point)
  483. {
  484. if (a_is_point)
  485. {
  486. return relate_one_degenerate<Policy, RatioType>(a,
  487. get<Dimension>(a1),
  488. get<Dimension>(b1), get<Dimension>(b2),
  489. true);
  490. }
  491. if (b_is_point)
  492. {
  493. return relate_one_degenerate<Policy, RatioType>(b,
  494. get<Dimension>(b1),
  495. get<Dimension>(a1), get<Dimension>(a2),
  496. false);
  497. }
  498. return relate_collinear<Policy, RatioType>(a, b,
  499. get<Dimension>(a1),
  500. get<Dimension>(a2),
  501. get<Dimension>(b1),
  502. get<Dimension>(b2));
  503. }
  504. /// Relate segments known collinear
  505. template
  506. <
  507. typename Policy,
  508. typename RatioType,
  509. typename Segment1,
  510. typename Segment2,
  511. typename Type1,
  512. typename Type2
  513. >
  514. static inline typename Policy::return_type
  515. relate_collinear(Segment1 const& a, Segment2 const& b,
  516. Type1 oa_1, Type1 oa_2,
  517. Type2 ob_1, Type2 ob_2)
  518. {
  519. // Calculate the ratios where a starts in b, b starts in a
  520. // a1--------->a2 (2..7)
  521. // b1----->b2 (5..8)
  522. // length_a: 7-2=5
  523. // length_b: 8-5=3
  524. // b1 is located w.r.t. a at ratio: (5-2)/5=3/5 (on a)
  525. // b2 is located w.r.t. a at ratio: (8-2)/5=6/5 (right of a)
  526. // a1 is located w.r.t. b at ratio: (2-5)/3=-3/3 (left of b)
  527. // a2 is located w.r.t. b at ratio: (7-5)/3=2/3 (on b)
  528. // A arrives (a2 on b), B departs (b1 on a)
  529. // If both are reversed:
  530. // a2<---------a1 (7..2)
  531. // b2<-----b1 (8..5)
  532. // length_a: 2-7=-5
  533. // length_b: 5-8=-3
  534. // b1 is located w.r.t. a at ratio: (8-7)/-5=-1/5 (before a starts)
  535. // b2 is located w.r.t. a at ratio: (5-7)/-5=2/5 (on a)
  536. // a1 is located w.r.t. b at ratio: (7-8)/-3=1/3 (on b)
  537. // a2 is located w.r.t. b at ratio: (2-8)/-3=6/3 (after b ends)
  538. // If both one is reversed:
  539. // a1--------->a2 (2..7)
  540. // b2<-----b1 (8..5)
  541. // length_a: 7-2=+5
  542. // length_b: 5-8=-3
  543. // b1 is located w.r.t. a at ratio: (8-2)/5=6/5 (after a ends)
  544. // b2 is located w.r.t. a at ratio: (5-2)/5=3/5 (on a)
  545. // a1 is located w.r.t. b at ratio: (2-8)/-3=6/3 (after b ends)
  546. // a2 is located w.r.t. b at ratio: (7-8)/-3=1/3 (on b)
  547. Type1 const length_a = oa_2 - oa_1; // no abs, see above
  548. Type2 const length_b = ob_2 - ob_1;
  549. RatioType ra_from(oa_1 - ob_1, length_b);
  550. RatioType ra_to(oa_2 - ob_1, length_b);
  551. RatioType rb_from(ob_1 - oa_1, length_a);
  552. RatioType rb_to(ob_2 - oa_1, length_a);
  553. // use absolute measure to detect endpoints intersection
  554. // NOTE: it'd be possible to calculate bx_wrt_a using ax_wrt_b values
  555. int const a1_wrt_b = position_value(oa_1, ob_1, ob_2);
  556. int const a2_wrt_b = position_value(oa_2, ob_1, ob_2);
  557. int const b1_wrt_a = position_value(ob_1, oa_1, oa_2);
  558. int const b2_wrt_a = position_value(ob_2, oa_1, oa_2);
  559. // fix the ratios if necessary
  560. // CONSIDER: fixing ratios also in other cases, if they're inconsistent
  561. // e.g. if ratio == 1 or 0 (so IP at the endpoint)
  562. // but position value indicates that the IP is in the middle of the segment
  563. // because one of the segments is very long
  564. // In such case the ratios could be moved into the middle direction
  565. // by some small value (e.g. EPS+1ULP)
  566. if (a1_wrt_b == 1)
  567. {
  568. ra_from.assign(0, 1);
  569. rb_from.assign(0, 1);
  570. }
  571. else if (a1_wrt_b == 3)
  572. {
  573. ra_from.assign(1, 1);
  574. rb_to.assign(0, 1);
  575. }
  576. if (a2_wrt_b == 1)
  577. {
  578. ra_to.assign(0, 1);
  579. rb_from.assign(1, 1);
  580. }
  581. else if (a2_wrt_b == 3)
  582. {
  583. ra_to.assign(1, 1);
  584. rb_to.assign(1, 1);
  585. }
  586. if ((a1_wrt_b < 1 && a2_wrt_b < 1) || (a1_wrt_b > 3 && a2_wrt_b > 3))
  587. //if ((ra_from.left() && ra_to.left()) || (ra_from.right() && ra_to.right()))
  588. {
  589. return Policy::disjoint();
  590. }
  591. bool const opposite = math::sign(length_a) != math::sign(length_b);
  592. return Policy::segments_collinear(a, b, opposite,
  593. a1_wrt_b, a2_wrt_b, b1_wrt_a, b2_wrt_a,
  594. ra_from, ra_to, rb_from, rb_to);
  595. }
  596. /// Relate segments where one is degenerate
  597. template
  598. <
  599. typename Policy,
  600. typename RatioType,
  601. typename DegenerateSegment,
  602. typename Type1,
  603. typename Type2
  604. >
  605. static inline typename Policy::return_type
  606. relate_one_degenerate(DegenerateSegment const& degenerate_segment,
  607. Type1 d, Type2 s1, Type2 s2,
  608. bool a_degenerate)
  609. {
  610. // Calculate the ratios where ds starts in s
  611. // a1--------->a2 (2..6)
  612. // b1/b2 (4..4)
  613. // Ratio: (4-2)/(6-2)
  614. RatioType const ratio(d - s1, s2 - s1);
  615. if (!ratio.on_segment())
  616. {
  617. return Policy::disjoint();
  618. }
  619. return Policy::one_degenerate(degenerate_segment, ratio, a_degenerate);
  620. }
  621. template <typename ProjCoord1, typename ProjCoord2>
  622. static inline int position_value(ProjCoord1 const& ca1,
  623. ProjCoord2 const& cb1,
  624. ProjCoord2 const& cb2)
  625. {
  626. // S1x 0 1 2 3 4
  627. // S2 |---------->
  628. return math::equals(ca1, cb1) ? 1
  629. : math::equals(ca1, cb2) ? 3
  630. : cb1 < cb2 ?
  631. ( ca1 < cb1 ? 0
  632. : ca1 > cb2 ? 4
  633. : 2 )
  634. : ( ca1 > cb1 ? 0
  635. : ca1 < cb2 ? 4
  636. : 2 );
  637. }
  638. template <typename Point1, typename Point2>
  639. static inline bool equals_point_point(Point1 const& point1, Point2 const& point2)
  640. {
  641. return strategy::within::cartesian_point_point::apply(point1, point2);
  642. }
  643. };
  644. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  645. namespace services
  646. {
  647. template <typename CalculationType>
  648. struct default_strategy<cartesian_tag, CalculationType>
  649. {
  650. typedef cartesian_segments<CalculationType> type;
  651. };
  652. } // namespace services
  653. #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  654. }} // namespace strategy::intersection
  655. namespace strategy
  656. {
  657. namespace within { namespace services
  658. {
  659. template <typename Geometry1, typename Geometry2, typename AnyTag1, typename AnyTag2>
  660. struct default_strategy<Geometry1, Geometry2, AnyTag1, AnyTag2, linear_tag, linear_tag, cartesian_tag, cartesian_tag>
  661. {
  662. typedef strategy::intersection::cartesian_segments<> type;
  663. };
  664. template <typename Geometry1, typename Geometry2, typename AnyTag1, typename AnyTag2>
  665. struct default_strategy<Geometry1, Geometry2, AnyTag1, AnyTag2, linear_tag, polygonal_tag, cartesian_tag, cartesian_tag>
  666. {
  667. typedef strategy::intersection::cartesian_segments<> type;
  668. };
  669. template <typename Geometry1, typename Geometry2, typename AnyTag1, typename AnyTag2>
  670. struct default_strategy<Geometry1, Geometry2, AnyTag1, AnyTag2, polygonal_tag, linear_tag, cartesian_tag, cartesian_tag>
  671. {
  672. typedef strategy::intersection::cartesian_segments<> type;
  673. };
  674. template <typename Geometry1, typename Geometry2, typename AnyTag1, typename AnyTag2>
  675. struct default_strategy<Geometry1, Geometry2, AnyTag1, AnyTag2, polygonal_tag, polygonal_tag, cartesian_tag, cartesian_tag>
  676. {
  677. typedef strategy::intersection::cartesian_segments<> type;
  678. };
  679. }} // within::services
  680. namespace covered_by { namespace services
  681. {
  682. template <typename Geometry1, typename Geometry2, typename AnyTag1, typename AnyTag2>
  683. struct default_strategy<Geometry1, Geometry2, AnyTag1, AnyTag2, linear_tag, linear_tag, cartesian_tag, cartesian_tag>
  684. {
  685. typedef strategy::intersection::cartesian_segments<> type;
  686. };
  687. template <typename Geometry1, typename Geometry2, typename AnyTag1, typename AnyTag2>
  688. struct default_strategy<Geometry1, Geometry2, AnyTag1, AnyTag2, linear_tag, polygonal_tag, cartesian_tag, cartesian_tag>
  689. {
  690. typedef strategy::intersection::cartesian_segments<> type;
  691. };
  692. template <typename Geometry1, typename Geometry2, typename AnyTag1, typename AnyTag2>
  693. struct default_strategy<Geometry1, Geometry2, AnyTag1, AnyTag2, polygonal_tag, linear_tag, cartesian_tag, cartesian_tag>
  694. {
  695. typedef strategy::intersection::cartesian_segments<> type;
  696. };
  697. template <typename Geometry1, typename Geometry2, typename AnyTag1, typename AnyTag2>
  698. struct default_strategy<Geometry1, Geometry2, AnyTag1, AnyTag2, polygonal_tag, polygonal_tag, cartesian_tag, cartesian_tag>
  699. {
  700. typedef strategy::intersection::cartesian_segments<> type;
  701. };
  702. }} // within::services
  703. } // strategy
  704. }} // namespace boost::geometry
  705. #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_INTERSECTION_HPP