intersection.hpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2014 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2013-2017 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. #if defined(BOOST_GEOMETRY_USE_RESCALING)
  212. return;
  213. #endif
  214. // Verify nearly collinear cases (the threshold is arbitrary
  215. // but influences performance). If the intersection is located
  216. // outside the segments, then it should be moved.
  217. if (robust_ra.possibly_collinear(1.0e-3)
  218. && robust_rb.possibly_collinear(1.0e-3))
  219. {
  220. // The segments are nearly collinear and because of the calculation
  221. // method with very small denominator, the IP appears outside the
  222. // segment(s). Correct it to the end point.
  223. // Because they are nearly collinear, it doesn't really matter to
  224. // to which endpoint (or it is corrected twice).
  225. assign_if_exceeds(point, a);
  226. assign_if_exceeds(point, b);
  227. }
  228. }
  229. CoordinateType dx_a, dy_a;
  230. CoordinateType dx_b, dy_b;
  231. SegmentRatio robust_ra;
  232. SegmentRatio robust_rb;
  233. };
  234. template <typename D, typename W, typename ResultType>
  235. static inline void cramers_rule(D const& dx_a, D const& dy_a,
  236. D const& dx_b, D const& dy_b, W const& wx, W const& wy,
  237. // out:
  238. ResultType& nominator, ResultType& denominator)
  239. {
  240. // Cramers rule
  241. nominator = geometry::detail::determinant<ResultType>(dx_b, dy_b, wx, wy);
  242. denominator = geometry::detail::determinant<ResultType>(dx_a, dy_a, dx_b, dy_b);
  243. // Ratio r = nominator/denominator
  244. // Collinear if denominator == 0, intersecting if 0 <= r <= 1
  245. // IntersectionPoint = (x1 + r * dx_a, y1 + r * dy_a)
  246. }
  247. // Version for non-rescaled policies
  248. template
  249. <
  250. typename UniqueSubRange1,
  251. typename UniqueSubRange2,
  252. typename Policy
  253. >
  254. static inline typename Policy::return_type
  255. apply(UniqueSubRange1 const& range_p,
  256. UniqueSubRange2 const& range_q,
  257. Policy const& policy)
  258. {
  259. // Pass the same ranges both as normal ranges and as modelled ranges
  260. return apply(range_p, range_q, policy, range_p, range_q);
  261. }
  262. // Version for non rescaled versions.
  263. // The "modelled" parameter might be rescaled (will be removed later)
  264. template
  265. <
  266. typename UniqueSubRange1,
  267. typename UniqueSubRange2,
  268. typename Policy,
  269. typename ModelledUniqueSubRange1,
  270. typename ModelledUniqueSubRange2
  271. >
  272. static inline typename Policy::return_type
  273. apply(UniqueSubRange1 const& range_p,
  274. UniqueSubRange2 const& range_q,
  275. Policy const& policy,
  276. ModelledUniqueSubRange1 const& modelled_range_p,
  277. ModelledUniqueSubRange2 const& modelled_range_q)
  278. {
  279. typedef typename UniqueSubRange1::point_type point1_type;
  280. typedef typename UniqueSubRange2::point_type point2_type;
  281. BOOST_CONCEPT_ASSERT( (concepts::ConstPoint<point1_type>) );
  282. BOOST_CONCEPT_ASSERT( (concepts::ConstPoint<point2_type>) );
  283. point1_type const& p1 = range_p.at(0);
  284. point1_type const& p2 = range_p.at(1);
  285. point2_type const& q1 = range_q.at(0);
  286. point2_type const& q2 = range_q.at(1);
  287. // Declare segments, currently necessary for the policies
  288. // (segment_crosses, segment_colinear, degenerate, one_degenerate, etc)
  289. model::referring_segment<point1_type const> const p(p1, p2);
  290. model::referring_segment<point2_type const> const q(q1, q2);
  291. typedef typename select_most_precise
  292. <
  293. typename geometry::coordinate_type<typename ModelledUniqueSubRange1::point_type>::type,
  294. typename geometry::coordinate_type<typename ModelledUniqueSubRange1::point_type>::type
  295. >::type modelled_coordinate_type;
  296. typedef segment_ratio<modelled_coordinate_type> ratio_type;
  297. segment_intersection_info
  298. <
  299. typename select_calculation_type<point1_type, point2_type, CalculationType>::type,
  300. ratio_type
  301. > sinfo;
  302. sinfo.dx_a = get<0>(p2) - get<0>(p1); // distance in x-dir
  303. sinfo.dx_b = get<0>(q2) - get<0>(q1);
  304. sinfo.dy_a = get<1>(p2) - get<1>(p1); // distance in y-dir
  305. sinfo.dy_b = get<1>(q2) - get<1>(q1);
  306. return unified<ratio_type>(sinfo, p, q, policy, modelled_range_p, modelled_range_q);
  307. }
  308. //! Returns true if two segments do not overlap.
  309. //! If not, then no further calculations need to be done.
  310. template
  311. <
  312. std::size_t Dimension,
  313. typename PointP,
  314. typename PointQ
  315. >
  316. static inline bool disjoint_by_range(PointP const& p1, PointP const& p2,
  317. PointQ const& q1, PointQ const& q2)
  318. {
  319. auto minp = get<Dimension>(p1);
  320. auto maxp = get<Dimension>(p2);
  321. auto minq = get<Dimension>(q1);
  322. auto maxq = get<Dimension>(q2);
  323. if (minp > maxp)
  324. {
  325. std::swap(minp, maxp);
  326. }
  327. if (minq > maxq)
  328. {
  329. std::swap(minq, maxq);
  330. }
  331. // In this case, max(p) < min(q)
  332. // P Q
  333. // <-------> <------->
  334. // (and the space in between is not extremely small)
  335. return math::smaller(maxp, minq) || math::smaller(maxq, minp);
  336. }
  337. // Implementation for either rescaled or non rescaled versions.
  338. template
  339. <
  340. typename RatioType,
  341. typename SegmentInfo,
  342. typename Segment1,
  343. typename Segment2,
  344. typename Policy,
  345. typename UniqueSubRange1,
  346. typename UniqueSubRange2
  347. >
  348. static inline typename Policy::return_type
  349. unified(SegmentInfo& sinfo,
  350. Segment1 const& p, Segment2 const& q, Policy const&,
  351. UniqueSubRange1 const& range_p,
  352. UniqueSubRange2 const& range_q)
  353. {
  354. typedef typename UniqueSubRange1::point_type point1_type;
  355. typedef typename UniqueSubRange2::point_type point2_type;
  356. typedef typename select_most_precise
  357. <
  358. typename geometry::coordinate_type<point1_type>::type,
  359. typename geometry::coordinate_type<point2_type>::type
  360. >::type coordinate_type;
  361. point1_type const& p1 = range_p.at(0);
  362. point1_type const& p2 = range_p.at(1);
  363. point2_type const& q1 = range_q.at(0);
  364. point2_type const& q2 = range_q.at(1);
  365. bool const p_is_point = equals_point_point(p1, p2);
  366. bool const q_is_point = equals_point_point(q1, q2);
  367. if (p_is_point && q_is_point)
  368. {
  369. return equals_point_point(p1, q2)
  370. ? Policy::degenerate(p, true)
  371. : Policy::disjoint()
  372. ;
  373. }
  374. if (disjoint_by_range<0>(p1, p2, q1, q2)
  375. || disjoint_by_range<1>(p1, p2, q1, q2))
  376. {
  377. return Policy::disjoint();
  378. }
  379. using side_strategy_type
  380. = typename side::services::default_strategy
  381. <cartesian_tag, CalculationType>::type;
  382. side_info sides;
  383. sides.set<0>(side_strategy_type::apply(q1, q2, p1),
  384. side_strategy_type::apply(q1, q2, p2));
  385. if (sides.same<0>())
  386. {
  387. // Both points are at same side of other segment, we can leave
  388. return Policy::disjoint();
  389. }
  390. sides.set<1>(side_strategy_type::apply(p1, p2, q1),
  391. side_strategy_type::apply(p1, p2, q2));
  392. if (sides.same<1>())
  393. {
  394. // Both points are at same side of other segment, we can leave
  395. return Policy::disjoint();
  396. }
  397. bool collinear = sides.collinear();
  398. // Calculate the differences again
  399. // (for rescaled version, this is different from dx_p etc)
  400. coordinate_type const dx_p = get<0>(p2) - get<0>(p1);
  401. coordinate_type const dx_q = get<0>(q2) - get<0>(q1);
  402. coordinate_type const dy_p = get<1>(p2) - get<1>(p1);
  403. coordinate_type const dy_q = get<1>(q2) - get<1>(q1);
  404. // r: ratio 0-1 where intersection divides A/B
  405. // (only calculated for non-collinear segments)
  406. if (! collinear)
  407. {
  408. coordinate_type denominator_a, nominator_a;
  409. coordinate_type denominator_b, nominator_b;
  410. cramers_rule(dx_p, dy_p, dx_q, dy_q,
  411. get<0>(p1) - get<0>(q1),
  412. get<1>(p1) - get<1>(q1),
  413. nominator_a, denominator_a);
  414. cramers_rule(dx_q, dy_q, dx_p, dy_p,
  415. get<0>(q1) - get<0>(p1),
  416. get<1>(q1) - get<1>(p1),
  417. nominator_b, denominator_b);
  418. math::detail::equals_factor_policy<coordinate_type>
  419. policy(dx_p, dy_p, dx_q, dy_q);
  420. coordinate_type const zero = 0;
  421. if (math::detail::equals_by_policy(denominator_a, zero, policy)
  422. || math::detail::equals_by_policy(denominator_b, zero, policy))
  423. {
  424. // If this is the case, no rescaling is done for FP precision.
  425. // We set it to collinear, but it indicates a robustness issue.
  426. sides.set<0>(0, 0);
  427. sides.set<1>(0, 0);
  428. collinear = true;
  429. }
  430. else
  431. {
  432. sinfo.robust_ra.assign(nominator_a, denominator_a);
  433. sinfo.robust_rb.assign(nominator_b, denominator_b);
  434. }
  435. }
  436. if (collinear)
  437. {
  438. std::pair<bool, bool> const collinear_use_first
  439. = is_x_more_significant(geometry::math::abs(dx_p),
  440. geometry::math::abs(dy_p),
  441. geometry::math::abs(dx_q),
  442. geometry::math::abs(dy_q),
  443. p_is_point, q_is_point);
  444. if (collinear_use_first.second)
  445. {
  446. // Degenerate cases: segments of single point, lying on other segment, are not disjoint
  447. // This situation is collinear too
  448. if (collinear_use_first.first)
  449. {
  450. return relate_collinear<0, Policy, RatioType>(p, q,
  451. p1, p2, q1, q2,
  452. p_is_point, q_is_point);
  453. }
  454. else
  455. {
  456. // Y direction contains larger segments (maybe dx is zero)
  457. return relate_collinear<1, Policy, RatioType>(p, q,
  458. p1, p2, q1, q2,
  459. p_is_point, q_is_point);
  460. }
  461. }
  462. }
  463. return Policy::segments_crosses(sides, sinfo, p, q);
  464. }
  465. private:
  466. // first is true if x is more significant
  467. // second is true if the more significant difference is not 0
  468. template <typename CoordinateType>
  469. static inline std::pair<bool, bool>
  470. is_x_more_significant(CoordinateType const& abs_dx_a,
  471. CoordinateType const& abs_dy_a,
  472. CoordinateType const& abs_dx_b,
  473. CoordinateType const& abs_dy_b,
  474. bool const a_is_point,
  475. bool const b_is_point)
  476. {
  477. //BOOST_GEOMETRY_ASSERT_MSG(!(a_is_point && b_is_point), "both segments shouldn't be degenerated");
  478. // for degenerated segments the second is always true because this function
  479. // shouldn't be called if both segments were degenerated
  480. if (a_is_point)
  481. {
  482. return std::make_pair(abs_dx_b >= abs_dy_b, true);
  483. }
  484. else if (b_is_point)
  485. {
  486. return std::make_pair(abs_dx_a >= abs_dy_a, true);
  487. }
  488. else
  489. {
  490. CoordinateType const min_dx = (std::min)(abs_dx_a, abs_dx_b);
  491. CoordinateType const min_dy = (std::min)(abs_dy_a, abs_dy_b);
  492. return min_dx == min_dy ?
  493. std::make_pair(true, min_dx > CoordinateType(0)) :
  494. std::make_pair(min_dx > min_dy, true);
  495. }
  496. }
  497. template
  498. <
  499. std::size_t Dimension,
  500. typename Policy,
  501. typename RatioType,
  502. typename Segment1,
  503. typename Segment2,
  504. typename RobustPoint1,
  505. typename RobustPoint2
  506. >
  507. static inline typename Policy::return_type
  508. relate_collinear(Segment1 const& a,
  509. Segment2 const& b,
  510. RobustPoint1 const& robust_a1, RobustPoint1 const& robust_a2,
  511. RobustPoint2 const& robust_b1, RobustPoint2 const& robust_b2,
  512. bool a_is_point, bool b_is_point)
  513. {
  514. if (a_is_point)
  515. {
  516. return relate_one_degenerate<Policy, RatioType>(a,
  517. get<Dimension>(robust_a1),
  518. get<Dimension>(robust_b1), get<Dimension>(robust_b2),
  519. true);
  520. }
  521. if (b_is_point)
  522. {
  523. return relate_one_degenerate<Policy, RatioType>(b,
  524. get<Dimension>(robust_b1),
  525. get<Dimension>(robust_a1), get<Dimension>(robust_a2),
  526. false);
  527. }
  528. return relate_collinear<Policy, RatioType>(a, b,
  529. get<Dimension>(robust_a1),
  530. get<Dimension>(robust_a2),
  531. get<Dimension>(robust_b1),
  532. get<Dimension>(robust_b2));
  533. }
  534. /// Relate segments known collinear
  535. template
  536. <
  537. typename Policy,
  538. typename RatioType,
  539. typename Segment1,
  540. typename Segment2,
  541. typename Type1,
  542. typename Type2
  543. >
  544. static inline typename Policy::return_type
  545. relate_collinear(Segment1 const& a, Segment2 const& b,
  546. Type1 oa_1, Type1 oa_2,
  547. Type2 ob_1, Type2 ob_2)
  548. {
  549. // Calculate the ratios where a starts in b, b starts in a
  550. // a1--------->a2 (2..7)
  551. // b1----->b2 (5..8)
  552. // length_a: 7-2=5
  553. // length_b: 8-5=3
  554. // b1 is located w.r.t. a at ratio: (5-2)/5=3/5 (on a)
  555. // b2 is located w.r.t. a at ratio: (8-2)/5=6/5 (right of a)
  556. // a1 is located w.r.t. b at ratio: (2-5)/3=-3/3 (left of b)
  557. // a2 is located w.r.t. b at ratio: (7-5)/3=2/3 (on b)
  558. // A arrives (a2 on b), B departs (b1 on a)
  559. // If both are reversed:
  560. // a2<---------a1 (7..2)
  561. // b2<-----b1 (8..5)
  562. // length_a: 2-7=-5
  563. // length_b: 5-8=-3
  564. // b1 is located w.r.t. a at ratio: (8-7)/-5=-1/5 (before a starts)
  565. // b2 is located w.r.t. a at ratio: (5-7)/-5=2/5 (on a)
  566. // a1 is located w.r.t. b at ratio: (7-8)/-3=1/3 (on b)
  567. // a2 is located w.r.t. b at ratio: (2-8)/-3=6/3 (after b ends)
  568. // If both one is reversed:
  569. // a1--------->a2 (2..7)
  570. // b2<-----b1 (8..5)
  571. // length_a: 7-2=+5
  572. // length_b: 5-8=-3
  573. // b1 is located w.r.t. a at ratio: (8-2)/5=6/5 (after a ends)
  574. // b2 is located w.r.t. a at ratio: (5-2)/5=3/5 (on a)
  575. // a1 is located w.r.t. b at ratio: (2-8)/-3=6/3 (after b ends)
  576. // a2 is located w.r.t. b at ratio: (7-8)/-3=1/3 (on b)
  577. Type1 const length_a = oa_2 - oa_1; // no abs, see above
  578. Type2 const length_b = ob_2 - ob_1;
  579. RatioType ra_from(oa_1 - ob_1, length_b);
  580. RatioType ra_to(oa_2 - ob_1, length_b);
  581. RatioType rb_from(ob_1 - oa_1, length_a);
  582. RatioType rb_to(ob_2 - oa_1, length_a);
  583. // use absolute measure to detect endpoints intersection
  584. // NOTE: it'd be possible to calculate bx_wrt_a using ax_wrt_b values
  585. int const a1_wrt_b = position_value(oa_1, ob_1, ob_2);
  586. int const a2_wrt_b = position_value(oa_2, ob_1, ob_2);
  587. int const b1_wrt_a = position_value(ob_1, oa_1, oa_2);
  588. int const b2_wrt_a = position_value(ob_2, oa_1, oa_2);
  589. // fix the ratios if necessary
  590. // CONSIDER: fixing ratios also in other cases, if they're inconsistent
  591. // e.g. if ratio == 1 or 0 (so IP at the endpoint)
  592. // but position value indicates that the IP is in the middle of the segment
  593. // because one of the segments is very long
  594. // In such case the ratios could be moved into the middle direction
  595. // by some small value (e.g. EPS+1ULP)
  596. if (a1_wrt_b == 1)
  597. {
  598. ra_from.assign(0, 1);
  599. rb_from.assign(0, 1);
  600. }
  601. else if (a1_wrt_b == 3)
  602. {
  603. ra_from.assign(1, 1);
  604. rb_to.assign(0, 1);
  605. }
  606. if (a2_wrt_b == 1)
  607. {
  608. ra_to.assign(0, 1);
  609. rb_from.assign(1, 1);
  610. }
  611. else if (a2_wrt_b == 3)
  612. {
  613. ra_to.assign(1, 1);
  614. rb_to.assign(1, 1);
  615. }
  616. if ((a1_wrt_b < 1 && a2_wrt_b < 1) || (a1_wrt_b > 3 && a2_wrt_b > 3))
  617. //if ((ra_from.left() && ra_to.left()) || (ra_from.right() && ra_to.right()))
  618. {
  619. return Policy::disjoint();
  620. }
  621. bool const opposite = math::sign(length_a) != math::sign(length_b);
  622. return Policy::segments_collinear(a, b, opposite,
  623. a1_wrt_b, a2_wrt_b, b1_wrt_a, b2_wrt_a,
  624. ra_from, ra_to, rb_from, rb_to);
  625. }
  626. /// Relate segments where one is degenerate
  627. template
  628. <
  629. typename Policy,
  630. typename RatioType,
  631. typename DegenerateSegment,
  632. typename Type1,
  633. typename Type2
  634. >
  635. static inline typename Policy::return_type
  636. relate_one_degenerate(DegenerateSegment const& degenerate_segment,
  637. Type1 d, Type2 s1, Type2 s2,
  638. bool a_degenerate)
  639. {
  640. // Calculate the ratios where ds starts in s
  641. // a1--------->a2 (2..6)
  642. // b1/b2 (4..4)
  643. // Ratio: (4-2)/(6-2)
  644. RatioType const ratio(d - s1, s2 - s1);
  645. if (!ratio.on_segment())
  646. {
  647. return Policy::disjoint();
  648. }
  649. return Policy::one_degenerate(degenerate_segment, ratio, a_degenerate);
  650. }
  651. template <typename ProjCoord1, typename ProjCoord2>
  652. static inline int position_value(ProjCoord1 const& ca1,
  653. ProjCoord2 const& cb1,
  654. ProjCoord2 const& cb2)
  655. {
  656. // S1x 0 1 2 3 4
  657. // S2 |---------->
  658. return math::equals(ca1, cb1) ? 1
  659. : math::equals(ca1, cb2) ? 3
  660. : cb1 < cb2 ?
  661. ( ca1 < cb1 ? 0
  662. : ca1 > cb2 ? 4
  663. : 2 )
  664. : ( ca1 > cb1 ? 0
  665. : ca1 < cb2 ? 4
  666. : 2 );
  667. }
  668. template <typename Point1, typename Point2>
  669. static inline bool equals_point_point(Point1 const& point1, Point2 const& point2)
  670. {
  671. return strategy::within::cartesian_point_point::apply(point1, point2);
  672. }
  673. };
  674. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  675. namespace services
  676. {
  677. template <typename CalculationType>
  678. struct default_strategy<cartesian_tag, CalculationType>
  679. {
  680. typedef cartesian_segments<CalculationType> type;
  681. };
  682. } // namespace services
  683. #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  684. }} // namespace strategy::intersection
  685. namespace strategy
  686. {
  687. namespace within { namespace services
  688. {
  689. template <typename Geometry1, typename Geometry2, typename AnyTag1, typename AnyTag2>
  690. struct default_strategy<Geometry1, Geometry2, AnyTag1, AnyTag2, linear_tag, linear_tag, cartesian_tag, cartesian_tag>
  691. {
  692. typedef strategy::intersection::cartesian_segments<> type;
  693. };
  694. template <typename Geometry1, typename Geometry2, typename AnyTag1, typename AnyTag2>
  695. struct default_strategy<Geometry1, Geometry2, AnyTag1, AnyTag2, linear_tag, polygonal_tag, cartesian_tag, cartesian_tag>
  696. {
  697. typedef strategy::intersection::cartesian_segments<> type;
  698. };
  699. template <typename Geometry1, typename Geometry2, typename AnyTag1, typename AnyTag2>
  700. struct default_strategy<Geometry1, Geometry2, AnyTag1, AnyTag2, polygonal_tag, linear_tag, cartesian_tag, cartesian_tag>
  701. {
  702. typedef strategy::intersection::cartesian_segments<> type;
  703. };
  704. template <typename Geometry1, typename Geometry2, typename AnyTag1, typename AnyTag2>
  705. struct default_strategy<Geometry1, Geometry2, AnyTag1, AnyTag2, polygonal_tag, polygonal_tag, cartesian_tag, cartesian_tag>
  706. {
  707. typedef strategy::intersection::cartesian_segments<> type;
  708. };
  709. }} // within::services
  710. namespace covered_by { namespace services
  711. {
  712. template <typename Geometry1, typename Geometry2, typename AnyTag1, typename AnyTag2>
  713. struct default_strategy<Geometry1, Geometry2, AnyTag1, AnyTag2, linear_tag, linear_tag, cartesian_tag, cartesian_tag>
  714. {
  715. typedef strategy::intersection::cartesian_segments<> type;
  716. };
  717. template <typename Geometry1, typename Geometry2, typename AnyTag1, typename AnyTag2>
  718. struct default_strategy<Geometry1, Geometry2, AnyTag1, AnyTag2, linear_tag, polygonal_tag, cartesian_tag, cartesian_tag>
  719. {
  720. typedef strategy::intersection::cartesian_segments<> type;
  721. };
  722. template <typename Geometry1, typename Geometry2, typename AnyTag1, typename AnyTag2>
  723. struct default_strategy<Geometry1, Geometry2, AnyTag1, AnyTag2, polygonal_tag, linear_tag, cartesian_tag, cartesian_tag>
  724. {
  725. typedef strategy::intersection::cartesian_segments<> type;
  726. };
  727. template <typename Geometry1, typename Geometry2, typename AnyTag1, typename AnyTag2>
  728. struct default_strategy<Geometry1, Geometry2, AnyTag1, AnyTag2, polygonal_tag, polygonal_tag, cartesian_tag, cartesian_tag>
  729. {
  730. typedef strategy::intersection::cartesian_segments<> type;
  731. };
  732. }} // within::services
  733. } // strategy
  734. }} // namespace boost::geometry
  735. #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_INTERSECTION_HPP