intersection.hpp 32 KB

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