intersection.hpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  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/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. #include <boost/geometry/policies/robustness/rescale_policy_tags.hpp>
  43. #include <boost/geometry/policies/robustness/robust_point_type.hpp>
  44. #if defined(BOOST_GEOMETRY_DEBUG_ROBUSTNESS)
  45. # include <boost/geometry/io/wkt/write.hpp>
  46. #endif
  47. namespace boost { namespace geometry
  48. {
  49. namespace strategy { namespace intersection
  50. {
  51. namespace detail_usage
  52. {
  53. // When calculating the intersection, the information of "a" or "b" can be used.
  54. // Theoretically this gives equal results, but due to floating point precision
  55. // there might be tiny differences. These are edge cases.
  56. // This structure is to determine if "a" or "b" should be used.
  57. // Prefer the segment closer to the endpoint.
  58. // If both are about equally close, then prefer the longer segment
  59. // To avoid hard thresholds, behavior is made fluent.
  60. // Calculate comparable length indications,
  61. // the longer the segment (relatively), the lower the value
  62. // such that the shorter lengths are evaluated higher and will
  63. // be preferred.
  64. template <bool IsArithmetic>
  65. struct use_a
  66. {
  67. template <typename Ct, typename Ev>
  68. static bool apply(Ct const& cla, Ct const& clb, Ev const& eva, Ev const& evb)
  69. {
  70. auto const clm = (std::max)(cla, clb);
  71. if (clm <= 0)
  72. {
  73. return true;
  74. }
  75. // Relative comparible length
  76. auto const rcla = Ct(1.0) - cla / clm;
  77. auto const rclb = Ct(1.0) - clb / clm;
  78. // Multipliers for edgevalue (ev) and relative comparible length (rcl)
  79. // They determine the balance between edge value (should be larger)
  80. // and segment length. In 99.9xx% of the cases there is no difference
  81. // at all (if either a or b is used). Therefore the values of the
  82. // constants are not sensitive for the majority of the situations.
  83. // One known case is #mysql_23023665_6 (difference) which needs mev >= 2
  84. Ev const mev = 5;
  85. Ev const mrcl = 1;
  86. return mev * eva + mrcl * rcla > mev * evb + mrcl * rclb;
  87. }
  88. };
  89. // Specialization for non arithmetic types. They will always use "a"
  90. template <>
  91. struct use_a<false>
  92. {
  93. template <typename Ct, typename Ev>
  94. static bool apply(Ct const& , Ct const& , Ev const& , Ev const& )
  95. {
  96. return true;
  97. }
  98. };
  99. }
  100. /*!
  101. \see http://mathworld.wolfram.com/Line-LineIntersection.html
  102. */
  103. template
  104. <
  105. typename CalculationType = void
  106. >
  107. struct cartesian_segments
  108. {
  109. typedef cartesian_tag cs_tag;
  110. template <typename CoordinateType, typename SegmentRatio>
  111. struct segment_intersection_info
  112. {
  113. private :
  114. typedef typename select_most_precise
  115. <
  116. CoordinateType, double
  117. >::type promoted_type;
  118. promoted_type comparable_length_a() const
  119. {
  120. return dx_a * dx_a + dy_a * dy_a;
  121. }
  122. promoted_type comparable_length_b() const
  123. {
  124. return dx_b * dx_b + dy_b * dy_b;
  125. }
  126. template <typename Point, typename Segment1, typename Segment2>
  127. void assign_a(Point& point, Segment1 const& a, Segment2 const& ) const
  128. {
  129. assign(point, a, dx_a, dy_a, robust_ra);
  130. }
  131. template <typename Point, typename Segment1, typename Segment2>
  132. void assign_b(Point& point, Segment1 const& , Segment2 const& b) const
  133. {
  134. assign(point, b, dx_b, dy_b, robust_rb);
  135. }
  136. template <typename Point, typename Segment>
  137. void assign(Point& point, Segment const& segment,
  138. CoordinateType const& dx, CoordinateType const& dy,
  139. SegmentRatio const& ratio) const
  140. {
  141. // Calculate the intersection point based on segment_ratio
  142. // The division, postponed until here, is done now. In case of integer this
  143. // results in an integer which rounds to the nearest integer.
  144. BOOST_GEOMETRY_ASSERT(ratio.denominator() != typename SegmentRatio::int_type(0));
  145. typedef typename promote_integral<CoordinateType>::type calc_type;
  146. calc_type const numerator
  147. = util::numeric_cast<calc_type>(ratio.numerator());
  148. calc_type const denominator
  149. = util::numeric_cast<calc_type>(ratio.denominator());
  150. calc_type const dx_calc = util::numeric_cast<calc_type>(dx);
  151. calc_type const dy_calc = util::numeric_cast<calc_type>(dy);
  152. set<0>(point, get<0, 0>(segment)
  153. + util::numeric_cast<CoordinateType>(
  154. math::divide<calc_type>(numerator * dx_calc, denominator)));
  155. set<1>(point, get<0, 1>(segment)
  156. + util::numeric_cast<CoordinateType>(
  157. math::divide<calc_type>(numerator * dy_calc, denominator)));
  158. }
  159. template <int Index, int Dim, typename Point, typename Segment>
  160. static bool exceeds_side_in_dimension(Point& p, Segment const& s)
  161. {
  162. // Situation a (positive)
  163. // 0>-------------->1 segment
  164. // * point left of segment<I> in D x or y
  165. // Situation b (negative)
  166. // 1<--------------<0 segment
  167. // * point right of segment<I>
  168. // Situation c (degenerate), return false (check other dimension)
  169. auto const& c = get<Dim>(p);
  170. auto const& c0 = get<Index, Dim>(s);
  171. auto const& c1 = get<1 - Index, Dim>(s);
  172. return c0 < c1 ? math::smaller(c, c0)
  173. : c0 > c1 ? math::larger(c, c0)
  174. : false;
  175. }
  176. template <int Index, typename Point, typename Segment>
  177. static bool exceeds_side_of_segment(Point& p, Segment const& s)
  178. {
  179. return exceeds_side_in_dimension<Index, 0>(p, s)
  180. || exceeds_side_in_dimension<Index, 1>(p, s);
  181. }
  182. template <typename Point, typename Segment>
  183. static void assign_if_exceeds(Point& point, Segment const& s)
  184. {
  185. if (exceeds_side_of_segment<0>(point, s))
  186. {
  187. detail::assign_point_from_index<0>(s, point);
  188. }
  189. else if (exceeds_side_of_segment<1>(point, s))
  190. {
  191. detail::assign_point_from_index<1>(s, point);
  192. }
  193. }
  194. public :
  195. template <typename Point, typename Segment1, typename Segment2>
  196. void calculate(Point& point, Segment1 const& a, Segment2 const& b) const
  197. {
  198. bool const use_a
  199. = detail_usage::use_a
  200. <
  201. std::is_arithmetic<CoordinateType>::value
  202. >::apply(comparable_length_a(), comparable_length_b(),
  203. robust_ra.edge_value(), robust_rb.edge_value());
  204. if (use_a)
  205. {
  206. assign_a(point, a, b);
  207. }
  208. else
  209. {
  210. assign_b(point, a, b);
  211. }
  212. #ifndef BOOST_GEOMETRY_USE_RESCALING
  213. // Verify nearly collinear cases (the threshold is arbitrary
  214. // but influences performance). If the intersection is located
  215. // outside the segments, then it should be moved.
  216. if (robust_ra.possibly_collinear(1.0e-3)
  217. && robust_rb.possibly_collinear(1.0e-3))
  218. {
  219. // The segments are nearly collinear and because of the calculation
  220. // method with very small denominator, the IP appears outside the
  221. // segment(s). Correct it to the end point.
  222. // Because they are nearly collinear, it doesn't really matter to
  223. // to which endpoint (or it is corrected twice).
  224. assign_if_exceeds(point, a);
  225. assign_if_exceeds(point, b);
  226. }
  227. #endif
  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. //TODO: remove this when rescaling is removed
  399. // Calculate the differences again
  400. // (for rescaled version, this is different from dx_p etc)
  401. coordinate_type const dx_p = get<0>(p2) - get<0>(p1);
  402. coordinate_type const dx_q = get<0>(q2) - get<0>(q1);
  403. coordinate_type const dy_p = get<1>(p2) - get<1>(p1);
  404. coordinate_type const dy_q = get<1>(q2) - get<1>(q1);
  405. // r: ratio 0-1 where intersection divides A/B
  406. // (only calculated for non-collinear segments)
  407. if (! collinear)
  408. {
  409. coordinate_type denominator_a, nominator_a;
  410. coordinate_type denominator_b, nominator_b;
  411. cramers_rule(dx_p, dy_p, dx_q, dy_q,
  412. get<0>(p1) - get<0>(q1),
  413. get<1>(p1) - get<1>(q1),
  414. nominator_a, denominator_a);
  415. cramers_rule(dx_q, dy_q, dx_p, dy_p,
  416. get<0>(q1) - get<0>(p1),
  417. get<1>(q1) - get<1>(p1),
  418. nominator_b, denominator_b);
  419. math::detail::equals_factor_policy<coordinate_type>
  420. policy(dx_p, dy_p, dx_q, dy_q);
  421. coordinate_type const zero = 0;
  422. if (math::detail::equals_by_policy(denominator_a, zero, policy)
  423. || math::detail::equals_by_policy(denominator_b, zero, policy))
  424. {
  425. // If this is the case, no rescaling is done for FP precision.
  426. // We set it to collinear, but it indicates a robustness issue.
  427. sides.set<0>(0, 0);
  428. sides.set<1>(0, 0);
  429. collinear = true;
  430. }
  431. else
  432. {
  433. sinfo.robust_ra.assign(nominator_a, denominator_a);
  434. sinfo.robust_rb.assign(nominator_b, denominator_b);
  435. }
  436. }
  437. if (collinear)
  438. {
  439. std::pair<bool, bool> const collinear_use_first
  440. = is_x_more_significant(geometry::math::abs(dx_p),
  441. geometry::math::abs(dy_p),
  442. geometry::math::abs(dx_q),
  443. geometry::math::abs(dy_q),
  444. p_is_point, q_is_point);
  445. if (collinear_use_first.second)
  446. {
  447. // Degenerate cases: segments of single point, lying on other segment, are not disjoint
  448. // This situation is collinear too
  449. if (collinear_use_first.first)
  450. {
  451. return relate_collinear<0, Policy, RatioType>(p, q,
  452. p1, p2, q1, q2,
  453. p_is_point, q_is_point);
  454. }
  455. else
  456. {
  457. // Y direction contains larger segments (maybe dx is zero)
  458. return relate_collinear<1, Policy, RatioType>(p, q,
  459. p1, p2, q1, q2,
  460. p_is_point, q_is_point);
  461. }
  462. }
  463. }
  464. if (equals_point_point(p1, q1) || equals_point_point(p1, q2))
  465. {
  466. return Policy::segments_share_common_point(sides, sinfo, p1);
  467. }
  468. if (equals_point_point(p2, q1) || equals_point_point(p2, q2))
  469. {
  470. return Policy::segments_share_common_point(sides, sinfo, p2);
  471. }
  472. return Policy::segments_crosses(sides, sinfo, p, q);
  473. }
  474. private:
  475. // first is true if x is more significant
  476. // second is true if the more significant difference is not 0
  477. template <typename CoordinateType>
  478. static inline std::pair<bool, bool>
  479. is_x_more_significant(CoordinateType const& abs_dx_a,
  480. CoordinateType const& abs_dy_a,
  481. CoordinateType const& abs_dx_b,
  482. CoordinateType const& abs_dy_b,
  483. bool const a_is_point,
  484. bool const b_is_point)
  485. {
  486. //BOOST_GEOMETRY_ASSERT_MSG(!(a_is_point && b_is_point), "both segments shouldn't be degenerated");
  487. // for degenerated segments the second is always true because this function
  488. // shouldn't be called if both segments were degenerated
  489. if (a_is_point)
  490. {
  491. return std::make_pair(abs_dx_b >= abs_dy_b, true);
  492. }
  493. else if (b_is_point)
  494. {
  495. return std::make_pair(abs_dx_a >= abs_dy_a, true);
  496. }
  497. else
  498. {
  499. CoordinateType const min_dx = (std::min)(abs_dx_a, abs_dx_b);
  500. CoordinateType const min_dy = (std::min)(abs_dy_a, abs_dy_b);
  501. return min_dx == min_dy ?
  502. std::make_pair(true, min_dx > CoordinateType(0)) :
  503. std::make_pair(min_dx > min_dy, true);
  504. }
  505. }
  506. template
  507. <
  508. std::size_t Dimension,
  509. typename Policy,
  510. typename RatioType,
  511. typename Segment1,
  512. typename Segment2,
  513. typename RobustPoint1,
  514. typename RobustPoint2
  515. >
  516. static inline typename Policy::return_type
  517. relate_collinear(Segment1 const& a,
  518. Segment2 const& b,
  519. RobustPoint1 const& robust_a1, RobustPoint1 const& robust_a2,
  520. RobustPoint2 const& robust_b1, RobustPoint2 const& robust_b2,
  521. bool a_is_point, bool b_is_point)
  522. {
  523. if (a_is_point)
  524. {
  525. return relate_one_degenerate<Policy, RatioType>(a,
  526. get<Dimension>(robust_a1),
  527. get<Dimension>(robust_b1), get<Dimension>(robust_b2),
  528. true);
  529. }
  530. if (b_is_point)
  531. {
  532. return relate_one_degenerate<Policy, RatioType>(b,
  533. get<Dimension>(robust_b1),
  534. get<Dimension>(robust_a1), get<Dimension>(robust_a2),
  535. false);
  536. }
  537. return relate_collinear<Policy, RatioType>(a, b,
  538. get<Dimension>(robust_a1),
  539. get<Dimension>(robust_a2),
  540. get<Dimension>(robust_b1),
  541. get<Dimension>(robust_b2));
  542. }
  543. /// Relate segments known collinear
  544. template
  545. <
  546. typename Policy,
  547. typename RatioType,
  548. typename Segment1,
  549. typename Segment2,
  550. typename Type1,
  551. typename Type2
  552. >
  553. static inline typename Policy::return_type
  554. relate_collinear(Segment1 const& a, Segment2 const& b,
  555. Type1 oa_1, Type1 oa_2,
  556. Type2 ob_1, Type2 ob_2)
  557. {
  558. // Calculate the ratios where a starts in b, b starts in a
  559. // a1--------->a2 (2..7)
  560. // b1----->b2 (5..8)
  561. // length_a: 7-2=5
  562. // length_b: 8-5=3
  563. // b1 is located w.r.t. a at ratio: (5-2)/5=3/5 (on a)
  564. // b2 is located w.r.t. a at ratio: (8-2)/5=6/5 (right of a)
  565. // a1 is located w.r.t. b at ratio: (2-5)/3=-3/3 (left of b)
  566. // a2 is located w.r.t. b at ratio: (7-5)/3=2/3 (on b)
  567. // A arrives (a2 on b), B departs (b1 on a)
  568. // If both are reversed:
  569. // a2<---------a1 (7..2)
  570. // b2<-----b1 (8..5)
  571. // length_a: 2-7=-5
  572. // length_b: 5-8=-3
  573. // b1 is located w.r.t. a at ratio: (8-7)/-5=-1/5 (before a starts)
  574. // b2 is located w.r.t. a at ratio: (5-7)/-5=2/5 (on a)
  575. // a1 is located w.r.t. b at ratio: (7-8)/-3=1/3 (on b)
  576. // a2 is located w.r.t. b at ratio: (2-8)/-3=6/3 (after b ends)
  577. // If both one is reversed:
  578. // a1--------->a2 (2..7)
  579. // b2<-----b1 (8..5)
  580. // length_a: 7-2=+5
  581. // length_b: 5-8=-3
  582. // b1 is located w.r.t. a at ratio: (8-2)/5=6/5 (after a ends)
  583. // b2 is located w.r.t. a at ratio: (5-2)/5=3/5 (on a)
  584. // a1 is located w.r.t. b at ratio: (2-8)/-3=6/3 (after b ends)
  585. // a2 is located w.r.t. b at ratio: (7-8)/-3=1/3 (on b)
  586. Type1 const length_a = oa_2 - oa_1; // no abs, see above
  587. Type2 const length_b = ob_2 - ob_1;
  588. RatioType ra_from(oa_1 - ob_1, length_b);
  589. RatioType ra_to(oa_2 - ob_1, length_b);
  590. RatioType rb_from(ob_1 - oa_1, length_a);
  591. RatioType rb_to(ob_2 - oa_1, length_a);
  592. // use absolute measure to detect endpoints intersection
  593. // NOTE: it'd be possible to calculate bx_wrt_a using ax_wrt_b values
  594. int const a1_wrt_b = position_value(oa_1, ob_1, ob_2);
  595. int const a2_wrt_b = position_value(oa_2, ob_1, ob_2);
  596. int const b1_wrt_a = position_value(ob_1, oa_1, oa_2);
  597. int const b2_wrt_a = position_value(ob_2, oa_1, oa_2);
  598. // fix the ratios if necessary
  599. // CONSIDER: fixing ratios also in other cases, if they're inconsistent
  600. // e.g. if ratio == 1 or 0 (so IP at the endpoint)
  601. // but position value indicates that the IP is in the middle of the segment
  602. // because one of the segments is very long
  603. // In such case the ratios could be moved into the middle direction
  604. // by some small value (e.g. EPS+1ULP)
  605. if (a1_wrt_b == 1)
  606. {
  607. ra_from.assign(0, 1);
  608. rb_from.assign(0, 1);
  609. }
  610. else if (a1_wrt_b == 3)
  611. {
  612. ra_from.assign(1, 1);
  613. rb_to.assign(0, 1);
  614. }
  615. if (a2_wrt_b == 1)
  616. {
  617. ra_to.assign(0, 1);
  618. rb_from.assign(1, 1);
  619. }
  620. else if (a2_wrt_b == 3)
  621. {
  622. ra_to.assign(1, 1);
  623. rb_to.assign(1, 1);
  624. }
  625. if ((a1_wrt_b < 1 && a2_wrt_b < 1) || (a1_wrt_b > 3 && a2_wrt_b > 3))
  626. //if ((ra_from.left() && ra_to.left()) || (ra_from.right() && ra_to.right()))
  627. {
  628. return Policy::disjoint();
  629. }
  630. bool const opposite = math::sign(length_a) != math::sign(length_b);
  631. return Policy::segments_collinear(a, b, opposite,
  632. a1_wrt_b, a2_wrt_b, b1_wrt_a, b2_wrt_a,
  633. ra_from, ra_to, rb_from, rb_to);
  634. }
  635. /// Relate segments where one is degenerate
  636. template
  637. <
  638. typename Policy,
  639. typename RatioType,
  640. typename DegenerateSegment,
  641. typename Type1,
  642. typename Type2
  643. >
  644. static inline typename Policy::return_type
  645. relate_one_degenerate(DegenerateSegment const& degenerate_segment,
  646. Type1 d, Type2 s1, Type2 s2,
  647. bool a_degenerate)
  648. {
  649. // Calculate the ratios where ds starts in s
  650. // a1--------->a2 (2..6)
  651. // b1/b2 (4..4)
  652. // Ratio: (4-2)/(6-2)
  653. RatioType const ratio(d - s1, s2 - s1);
  654. if (!ratio.on_segment())
  655. {
  656. return Policy::disjoint();
  657. }
  658. return Policy::one_degenerate(degenerate_segment, ratio, a_degenerate);
  659. }
  660. template <typename ProjCoord1, typename ProjCoord2>
  661. static inline int position_value(ProjCoord1 const& ca1,
  662. ProjCoord2 const& cb1,
  663. ProjCoord2 const& cb2)
  664. {
  665. // S1x 0 1 2 3 4
  666. // S2 |---------->
  667. return math::equals(ca1, cb1) ? 1
  668. : math::equals(ca1, cb2) ? 3
  669. : cb1 < cb2 ?
  670. ( ca1 < cb1 ? 0
  671. : ca1 > cb2 ? 4
  672. : 2 )
  673. : ( ca1 > cb1 ? 0
  674. : ca1 < cb2 ? 4
  675. : 2 );
  676. }
  677. template <typename Point1, typename Point2>
  678. static inline bool equals_point_point(Point1 const& point1, Point2 const& point2)
  679. {
  680. return strategy::within::cartesian_point_point::apply(point1, point2);
  681. }
  682. };
  683. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  684. namespace services
  685. {
  686. template <typename CalculationType>
  687. struct default_strategy<cartesian_tag, CalculationType>
  688. {
  689. typedef cartesian_segments<CalculationType> type;
  690. };
  691. } // namespace services
  692. #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  693. }} // namespace strategy::intersection
  694. namespace strategy
  695. {
  696. namespace within { namespace services
  697. {
  698. template <typename Geometry1, typename Geometry2, typename AnyTag1, typename AnyTag2>
  699. struct default_strategy<Geometry1, Geometry2, AnyTag1, AnyTag2, linear_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, linear_tag, polygonal_tag, cartesian_tag, cartesian_tag>
  705. {
  706. typedef strategy::intersection::cartesian_segments<> type;
  707. };
  708. template <typename Geometry1, typename Geometry2, typename AnyTag1, typename AnyTag2>
  709. struct default_strategy<Geometry1, Geometry2, AnyTag1, AnyTag2, polygonal_tag, linear_tag, cartesian_tag, cartesian_tag>
  710. {
  711. typedef strategy::intersection::cartesian_segments<> type;
  712. };
  713. template <typename Geometry1, typename Geometry2, typename AnyTag1, typename AnyTag2>
  714. struct default_strategy<Geometry1, Geometry2, AnyTag1, AnyTag2, polygonal_tag, polygonal_tag, cartesian_tag, cartesian_tag>
  715. {
  716. typedef strategy::intersection::cartesian_segments<> type;
  717. };
  718. }} // within::services
  719. namespace covered_by { namespace services
  720. {
  721. template <typename Geometry1, typename Geometry2, typename AnyTag1, typename AnyTag2>
  722. struct default_strategy<Geometry1, Geometry2, AnyTag1, AnyTag2, linear_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, linear_tag, polygonal_tag, cartesian_tag, cartesian_tag>
  728. {
  729. typedef strategy::intersection::cartesian_segments<> type;
  730. };
  731. template <typename Geometry1, typename Geometry2, typename AnyTag1, typename AnyTag2>
  732. struct default_strategy<Geometry1, Geometry2, AnyTag1, AnyTag2, polygonal_tag, linear_tag, cartesian_tag, cartesian_tag>
  733. {
  734. typedef strategy::intersection::cartesian_segments<> type;
  735. };
  736. template <typename Geometry1, typename Geometry2, typename AnyTag1, typename AnyTag2>
  737. struct default_strategy<Geometry1, Geometry2, AnyTag1, AnyTag2, polygonal_tag, polygonal_tag, cartesian_tag, cartesian_tag>
  738. {
  739. typedef strategy::intersection::cartesian_segments<> type;
  740. };
  741. }} // within::services
  742. } // strategy
  743. }} // namespace boost::geometry
  744. #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_INTERSECTION_HPP