intersection.hpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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, 2016, 2017, 2018, 2019.
  5. // Modifications copyright (c) 2014-2019, 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/arithmetic/determinant.hpp>
  18. #include <boost/geometry/algorithms/detail/assign_values.hpp>
  19. #include <boost/geometry/algorithms/detail/assign_indexed_point.hpp>
  20. #include <boost/geometry/algorithms/detail/equals/point_point.hpp>
  21. #include <boost/geometry/algorithms/detail/recalculate.hpp>
  22. #include <boost/geometry/util/math.hpp>
  23. #include <boost/geometry/util/promote_integral.hpp>
  24. #include <boost/geometry/util/select_calculation_type.hpp>
  25. #include <boost/geometry/strategies/cartesian/area.hpp>
  26. #include <boost/geometry/strategies/cartesian/disjoint_box_box.hpp>
  27. #include <boost/geometry/strategies/cartesian/disjoint_segment_box.hpp>
  28. #include <boost/geometry/strategies/cartesian/distance_pythagoras.hpp>
  29. #include <boost/geometry/strategies/cartesian/envelope.hpp>
  30. #include <boost/geometry/strategies/cartesian/expand_box.hpp>
  31. #include <boost/geometry/strategies/cartesian/expand_segment.hpp>
  32. #include <boost/geometry/strategies/cartesian/point_in_point.hpp>
  33. #include <boost/geometry/strategies/cartesian/point_in_poly_winding.hpp>
  34. #include <boost/geometry/strategies/cartesian/side_by_triangle.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. /*!
  51. \see http://mathworld.wolfram.com/Line-LineIntersection.html
  52. */
  53. template
  54. <
  55. typename CalculationType = void
  56. >
  57. struct cartesian_segments
  58. {
  59. typedef cartesian_tag cs_tag;
  60. typedef side::side_by_triangle<CalculationType> side_strategy_type;
  61. static inline side_strategy_type get_side_strategy()
  62. {
  63. return side_strategy_type();
  64. }
  65. template <typename Geometry1, typename Geometry2>
  66. struct point_in_geometry_strategy
  67. {
  68. typedef strategy::within::cartesian_winding
  69. <
  70. typename point_type<Geometry1>::type,
  71. typename point_type<Geometry2>::type,
  72. CalculationType
  73. > type;
  74. };
  75. template <typename Geometry1, typename Geometry2>
  76. static inline typename point_in_geometry_strategy<Geometry1, Geometry2>::type
  77. get_point_in_geometry_strategy()
  78. {
  79. typedef typename point_in_geometry_strategy
  80. <
  81. Geometry1, Geometry2
  82. >::type strategy_type;
  83. return strategy_type();
  84. }
  85. template <typename Geometry>
  86. struct area_strategy
  87. {
  88. typedef area::cartesian
  89. <
  90. CalculationType
  91. > type;
  92. };
  93. template <typename Geometry>
  94. static inline typename area_strategy<Geometry>::type get_area_strategy()
  95. {
  96. typedef typename area_strategy<Geometry>::type strategy_type;
  97. return strategy_type();
  98. }
  99. template <typename Geometry>
  100. struct distance_strategy
  101. {
  102. typedef distance::pythagoras
  103. <
  104. CalculationType
  105. > type;
  106. };
  107. template <typename Geometry>
  108. static inline typename distance_strategy<Geometry>::type get_distance_strategy()
  109. {
  110. typedef typename distance_strategy<Geometry>::type strategy_type;
  111. return strategy_type();
  112. }
  113. typedef envelope::cartesian<CalculationType> envelope_strategy_type;
  114. static inline envelope_strategy_type get_envelope_strategy()
  115. {
  116. return envelope_strategy_type();
  117. }
  118. typedef expand::cartesian_segment expand_strategy_type;
  119. static inline expand_strategy_type get_expand_strategy()
  120. {
  121. return expand_strategy_type();
  122. }
  123. typedef within::cartesian_point_point point_in_point_strategy_type;
  124. static inline point_in_point_strategy_type get_point_in_point_strategy()
  125. {
  126. return point_in_point_strategy_type();
  127. }
  128. typedef within::cartesian_point_point equals_point_point_strategy_type;
  129. static inline equals_point_point_strategy_type get_equals_point_point_strategy()
  130. {
  131. return equals_point_point_strategy_type();
  132. }
  133. typedef disjoint::cartesian_box_box disjoint_box_box_strategy_type;
  134. static inline disjoint_box_box_strategy_type get_disjoint_box_box_strategy()
  135. {
  136. return disjoint_box_box_strategy_type();
  137. }
  138. typedef disjoint::segment_box disjoint_segment_box_strategy_type;
  139. static inline disjoint_segment_box_strategy_type get_disjoint_segment_box_strategy()
  140. {
  141. return disjoint_segment_box_strategy_type();
  142. }
  143. typedef covered_by::cartesian_point_box disjoint_point_box_strategy_type;
  144. typedef covered_by::cartesian_point_box covered_by_point_box_strategy_type;
  145. typedef within::cartesian_point_box within_point_box_strategy_type;
  146. typedef envelope::cartesian_box envelope_box_strategy_type;
  147. typedef expand::cartesian_box expand_box_strategy_type;
  148. template <typename CoordinateType, typename SegmentRatio>
  149. struct segment_intersection_info
  150. {
  151. private :
  152. typedef typename select_most_precise
  153. <
  154. CoordinateType, double
  155. >::type promoted_type;
  156. promoted_type comparable_length_a() const
  157. {
  158. return dx_a * dx_a + dy_a * dy_a;
  159. }
  160. promoted_type comparable_length_b() const
  161. {
  162. return dx_b * dx_b + dy_b * dy_b;
  163. }
  164. template <typename Point, typename Segment1, typename Segment2>
  165. void assign_a(Point& point, Segment1 const& a, Segment2 const& ) const
  166. {
  167. assign(point, a, dx_a, dy_a, robust_ra);
  168. }
  169. template <typename Point, typename Segment1, typename Segment2>
  170. void assign_b(Point& point, Segment1 const& , Segment2 const& b) const
  171. {
  172. assign(point, b, dx_b, dy_b, robust_rb);
  173. }
  174. template <typename Point, typename Segment>
  175. void assign(Point& point, Segment const& segment, CoordinateType const& dx, CoordinateType const& dy, SegmentRatio const& ratio) const
  176. {
  177. // Calculate the intersection point based on segment_ratio
  178. // Up to now, division was postponed. Here we divide using numerator/
  179. // denominator. In case of integer this results in an integer
  180. // division.
  181. BOOST_GEOMETRY_ASSERT(ratio.denominator() != 0);
  182. typedef typename promote_integral<CoordinateType>::type calc_type;
  183. calc_type const numerator
  184. = boost::numeric_cast<calc_type>(ratio.numerator());
  185. calc_type const denominator
  186. = boost::numeric_cast<calc_type>(ratio.denominator());
  187. calc_type const dx_calc = boost::numeric_cast<calc_type>(dx);
  188. calc_type const dy_calc = boost::numeric_cast<calc_type>(dy);
  189. set<0>(point, get<0, 0>(segment)
  190. + boost::numeric_cast<CoordinateType>(numerator * dx_calc
  191. / denominator));
  192. set<1>(point, get<0, 1>(segment)
  193. + boost::numeric_cast<CoordinateType>(numerator * dy_calc
  194. / denominator));
  195. }
  196. public :
  197. template <typename Point, typename Segment1, typename Segment2>
  198. void calculate(Point& point, Segment1 const& a, Segment2 const& b) const
  199. {
  200. bool use_a = true;
  201. // Prefer one segment if one is on or near an endpoint
  202. bool const a_near_end = robust_ra.near_end();
  203. bool const b_near_end = robust_rb.near_end();
  204. if (a_near_end && ! b_near_end)
  205. {
  206. use_a = true;
  207. }
  208. else if (b_near_end && ! a_near_end)
  209. {
  210. use_a = false;
  211. }
  212. else
  213. {
  214. // Prefer shorter segment
  215. promoted_type const len_a = comparable_length_a();
  216. promoted_type const len_b = comparable_length_b();
  217. if (len_b < len_a)
  218. {
  219. use_a = false;
  220. }
  221. // else use_a is true but was already assigned like that
  222. }
  223. if (use_a)
  224. {
  225. assign_a(point, a, b);
  226. }
  227. else
  228. {
  229. assign_b(point, a, b);
  230. }
  231. }
  232. CoordinateType dx_a, dy_a;
  233. CoordinateType dx_b, dy_b;
  234. SegmentRatio robust_ra;
  235. SegmentRatio robust_rb;
  236. };
  237. template <typename D, typename W, typename ResultType>
  238. static inline void cramers_rule(D const& dx_a, D const& dy_a,
  239. D const& dx_b, D const& dy_b, W const& wx, W const& wy,
  240. // out:
  241. ResultType& d, ResultType& da)
  242. {
  243. // Cramers rule
  244. d = geometry::detail::determinant<ResultType>(dx_a, dy_a, dx_b, dy_b);
  245. da = geometry::detail::determinant<ResultType>(dx_b, dy_b, wx, wy);
  246. // Ratio is da/d , collinear if d == 0, intersecting if 0 <= r <= 1
  247. // IntersectionPoint = (x1 + r * dx_a, y1 + r * dy_a)
  248. }
  249. // Version for non-rescaled policies
  250. template
  251. <
  252. typename UniqueSubRange1,
  253. typename UniqueSubRange2,
  254. typename Policy
  255. >
  256. static inline typename Policy::return_type
  257. apply(UniqueSubRange1 const& range_p,
  258. UniqueSubRange2 const& range_q,
  259. Policy const& policy)
  260. {
  261. // Pass the same ranges both as normal ranges and as robust ranges
  262. return apply(range_p, range_q, policy, range_p, range_q);
  263. }
  264. // Main entry-routine, calculating intersections of segments p / q
  265. template
  266. <
  267. typename UniqueSubRange1,
  268. typename UniqueSubRange2,
  269. typename Policy,
  270. typename RobustUniqueSubRange1,
  271. typename RobustUniqueSubRange2
  272. >
  273. static inline typename Policy::return_type
  274. apply(UniqueSubRange1 const& range_p,
  275. UniqueSubRange2 const& range_q,
  276. Policy const&,
  277. RobustUniqueSubRange1 const& robust_range_p,
  278. RobustUniqueSubRange2 const& robust_range_q)
  279. {
  280. typedef typename UniqueSubRange1::point_type point1_type;
  281. typedef typename UniqueSubRange2::point_type point2_type;
  282. BOOST_CONCEPT_ASSERT( (concepts::ConstPoint<point1_type>) );
  283. BOOST_CONCEPT_ASSERT( (concepts::ConstPoint<point2_type>) );
  284. // Get robust points (to be omitted later)
  285. typedef typename RobustUniqueSubRange1::point_type robust_point1_type;
  286. typedef typename RobustUniqueSubRange2::point_type robust_point2_type;
  287. point1_type const& p1 = range_p.at(0);
  288. point1_type const& p2 = range_p.at(1);
  289. point2_type const& q1 = range_q.at(0);
  290. point2_type const& q2 = range_q.at(1);
  291. robust_point1_type const& robust_a1 = robust_range_p.at(0);
  292. robust_point1_type const& robust_a2 = robust_range_p.at(1);
  293. robust_point2_type const& robust_b1 = robust_range_q.at(0);
  294. robust_point2_type const& robust_b2 = robust_range_q.at(1);
  295. using geometry::detail::equals::equals_point_point;
  296. bool const a_is_point = equals_point_point(robust_a1, robust_a2, point_in_point_strategy_type());
  297. bool const b_is_point = equals_point_point(robust_b1, robust_b2, point_in_point_strategy_type());
  298. if(a_is_point && b_is_point)
  299. {
  300. // Take either a or b
  301. model::referring_segment<point1_type const> const d(p1, p2);
  302. return equals_point_point(robust_a1, robust_b2, point_in_point_strategy_type())
  303. ? Policy::degenerate(d, true)
  304. : Policy::disjoint()
  305. ;
  306. }
  307. side_info sides;
  308. sides.set<0>(side_strategy_type::apply(robust_b1, robust_b2, robust_a1),
  309. side_strategy_type::apply(robust_b1, robust_b2, robust_a2));
  310. if (sides.same<0>())
  311. {
  312. // Both points are at same side of other segment, we can leave
  313. return Policy::disjoint();
  314. }
  315. sides.set<1>(side_strategy_type::apply(robust_a1, robust_a2, robust_b1),
  316. side_strategy_type::apply(robust_a1, robust_a2, robust_b2));
  317. if (sides.same<1>())
  318. {
  319. // Both points are at same side of other segment, we can leave
  320. return Policy::disjoint();
  321. }
  322. bool collinear = sides.collinear();
  323. typedef typename select_most_precise
  324. <
  325. typename geometry::coordinate_type<robust_point1_type>::type,
  326. typename geometry::coordinate_type<robust_point2_type>::type
  327. >::type robust_coordinate_type;
  328. typedef segment_ratio<robust_coordinate_type> ratio_type;
  329. segment_intersection_info
  330. <
  331. typename select_calculation_type<point1_type, point2_type, CalculationType>::type,
  332. ratio_type
  333. > sinfo;
  334. sinfo.dx_a = get<0>(p2) - get<0>(p1); // distance in x-dir
  335. sinfo.dx_b = get<0>(q2) - get<0>(q1);
  336. sinfo.dy_a = get<1>(p2) - get<1>(p1); // distance in y-dir
  337. sinfo.dy_b = get<1>(q2) - get<1>(q1);
  338. robust_coordinate_type const robust_dx_a = get<0>(robust_a2) - get<0>(robust_a1);
  339. robust_coordinate_type const robust_dx_b = get<0>(robust_b2) - get<0>(robust_b1);
  340. robust_coordinate_type const robust_dy_a = get<1>(robust_a2) - get<1>(robust_a1);
  341. robust_coordinate_type const robust_dy_b = get<1>(robust_b2) - get<1>(robust_b1);
  342. // r: ratio 0-1 where intersection divides A/B
  343. // (only calculated for non-collinear segments)
  344. if (! collinear)
  345. {
  346. robust_coordinate_type robust_da0, robust_da;
  347. robust_coordinate_type robust_db0, robust_db;
  348. cramers_rule(robust_dx_a, robust_dy_a, robust_dx_b, robust_dy_b,
  349. get<0>(robust_a1) - get<0>(robust_b1),
  350. get<1>(robust_a1) - get<1>(robust_b1),
  351. robust_da0, robust_da);
  352. cramers_rule(robust_dx_b, robust_dy_b, robust_dx_a, robust_dy_a,
  353. get<0>(robust_b1) - get<0>(robust_a1),
  354. get<1>(robust_b1) - get<1>(robust_a1),
  355. robust_db0, robust_db);
  356. math::detail::equals_factor_policy<robust_coordinate_type>
  357. policy(robust_dx_a, robust_dy_a, robust_dx_b, robust_dy_b);
  358. robust_coordinate_type const zero = 0;
  359. if (math::detail::equals_by_policy(robust_da0, zero, policy)
  360. || math::detail::equals_by_policy(robust_db0, zero, policy))
  361. {
  362. // If this is the case, no rescaling is done for FP precision.
  363. // We set it to collinear, but it indicates a robustness issue.
  364. sides.set<0>(0,0);
  365. sides.set<1>(0,0);
  366. collinear = true;
  367. }
  368. else
  369. {
  370. sinfo.robust_ra.assign(robust_da, robust_da0);
  371. sinfo.robust_rb.assign(robust_db, robust_db0);
  372. }
  373. }
  374. // Declare segments, currently necessary for the policies
  375. // (segment_crosses, segment_colinear, degenerate, one_degenerate, etc)
  376. model::referring_segment<point1_type const> const p(p1, p2);
  377. model::referring_segment<point2_type const> const q(q1, q2);
  378. if (collinear)
  379. {
  380. std::pair<bool, bool> const collinear_use_first
  381. = is_x_more_significant(geometry::math::abs(robust_dx_a),
  382. geometry::math::abs(robust_dy_a),
  383. geometry::math::abs(robust_dx_b),
  384. geometry::math::abs(robust_dy_b),
  385. a_is_point, b_is_point);
  386. if (collinear_use_first.second)
  387. {
  388. // Degenerate cases: segments of single point, lying on other segment, are not disjoint
  389. // This situation is collinear too
  390. if (collinear_use_first.first)
  391. {
  392. return relate_collinear<0, Policy, ratio_type>(p, q,
  393. robust_a1, robust_a2, robust_b1, robust_b2,
  394. a_is_point, b_is_point);
  395. }
  396. else
  397. {
  398. // Y direction contains larger segments (maybe dx is zero)
  399. return relate_collinear<1, Policy, ratio_type>(p, q,
  400. robust_a1, robust_a2, robust_b1, robust_b2,
  401. a_is_point, b_is_point);
  402. }
  403. }
  404. }
  405. return Policy::segments_crosses(sides, sinfo, p, q);
  406. }
  407. private:
  408. // first is true if x is more significant
  409. // second is true if the more significant difference is not 0
  410. template <typename RobustCoordinateType>
  411. static inline std::pair<bool, bool>
  412. is_x_more_significant(RobustCoordinateType const& abs_robust_dx_a,
  413. RobustCoordinateType const& abs_robust_dy_a,
  414. RobustCoordinateType const& abs_robust_dx_b,
  415. RobustCoordinateType const& abs_robust_dy_b,
  416. bool const a_is_point,
  417. bool const b_is_point)
  418. {
  419. //BOOST_GEOMETRY_ASSERT_MSG(!(a_is_point && b_is_point), "both segments shouldn't be degenerated");
  420. // for degenerated segments the second is always true because this function
  421. // shouldn't be called if both segments were degenerated
  422. if (a_is_point)
  423. {
  424. return std::make_pair(abs_robust_dx_b >= abs_robust_dy_b, true);
  425. }
  426. else if (b_is_point)
  427. {
  428. return std::make_pair(abs_robust_dx_a >= abs_robust_dy_a, true);
  429. }
  430. else
  431. {
  432. RobustCoordinateType const min_dx = (std::min)(abs_robust_dx_a, abs_robust_dx_b);
  433. RobustCoordinateType const min_dy = (std::min)(abs_robust_dy_a, abs_robust_dy_b);
  434. return min_dx == min_dy ?
  435. std::make_pair(true, min_dx > RobustCoordinateType(0)) :
  436. std::make_pair(min_dx > min_dy, true);
  437. }
  438. }
  439. template
  440. <
  441. std::size_t Dimension,
  442. typename Policy,
  443. typename RatioType,
  444. typename Segment1,
  445. typename Segment2,
  446. typename RobustPoint1,
  447. typename RobustPoint2
  448. >
  449. static inline typename Policy::return_type
  450. relate_collinear(Segment1 const& a,
  451. Segment2 const& b,
  452. RobustPoint1 const& robust_a1, RobustPoint1 const& robust_a2,
  453. RobustPoint2 const& robust_b1, RobustPoint2 const& robust_b2,
  454. bool a_is_point, bool b_is_point)
  455. {
  456. if (a_is_point)
  457. {
  458. return relate_one_degenerate<Policy, RatioType>(a,
  459. get<Dimension>(robust_a1),
  460. get<Dimension>(robust_b1), get<Dimension>(robust_b2),
  461. true);
  462. }
  463. if (b_is_point)
  464. {
  465. return relate_one_degenerate<Policy, RatioType>(b,
  466. get<Dimension>(robust_b1),
  467. get<Dimension>(robust_a1), get<Dimension>(robust_a2),
  468. false);
  469. }
  470. return relate_collinear<Policy, RatioType>(a, b,
  471. get<Dimension>(robust_a1),
  472. get<Dimension>(robust_a2),
  473. get<Dimension>(robust_b1),
  474. get<Dimension>(robust_b2));
  475. }
  476. /// Relate segments known collinear
  477. template
  478. <
  479. typename Policy,
  480. typename RatioType,
  481. typename Segment1,
  482. typename Segment2,
  483. typename RobustType1,
  484. typename RobustType2
  485. >
  486. static inline typename Policy::return_type
  487. relate_collinear(Segment1 const& a, Segment2 const& b,
  488. RobustType1 oa_1, RobustType1 oa_2,
  489. RobustType2 ob_1, RobustType2 ob_2)
  490. {
  491. // Calculate the ratios where a starts in b, b starts in a
  492. // a1--------->a2 (2..7)
  493. // b1----->b2 (5..8)
  494. // length_a: 7-2=5
  495. // length_b: 8-5=3
  496. // b1 is located w.r.t. a at ratio: (5-2)/5=3/5 (on a)
  497. // b2 is located w.r.t. a at ratio: (8-2)/5=6/5 (right of a)
  498. // a1 is located w.r.t. b at ratio: (2-5)/3=-3/3 (left of b)
  499. // a2 is located w.r.t. b at ratio: (7-5)/3=2/3 (on b)
  500. // A arrives (a2 on b), B departs (b1 on a)
  501. // If both are reversed:
  502. // a2<---------a1 (7..2)
  503. // b2<-----b1 (8..5)
  504. // length_a: 2-7=-5
  505. // length_b: 5-8=-3
  506. // b1 is located w.r.t. a at ratio: (8-7)/-5=-1/5 (before a starts)
  507. // b2 is located w.r.t. a at ratio: (5-7)/-5=2/5 (on a)
  508. // a1 is located w.r.t. b at ratio: (7-8)/-3=1/3 (on b)
  509. // a2 is located w.r.t. b at ratio: (2-8)/-3=6/3 (after b ends)
  510. // If both one is reversed:
  511. // a1--------->a2 (2..7)
  512. // b2<-----b1 (8..5)
  513. // length_a: 7-2=+5
  514. // length_b: 5-8=-3
  515. // b1 is located w.r.t. a at ratio: (8-2)/5=6/5 (after a ends)
  516. // b2 is located w.r.t. a at ratio: (5-2)/5=3/5 (on a)
  517. // a1 is located w.r.t. b at ratio: (2-8)/-3=6/3 (after b ends)
  518. // a2 is located w.r.t. b at ratio: (7-8)/-3=1/3 (on b)
  519. RobustType1 const length_a = oa_2 - oa_1; // no abs, see above
  520. RobustType2 const length_b = ob_2 - ob_1;
  521. RatioType ra_from(oa_1 - ob_1, length_b);
  522. RatioType ra_to(oa_2 - ob_1, length_b);
  523. RatioType rb_from(ob_1 - oa_1, length_a);
  524. RatioType rb_to(ob_2 - oa_1, length_a);
  525. // use absolute measure to detect endpoints intersection
  526. // NOTE: it'd be possible to calculate bx_wrt_a using ax_wrt_b values
  527. int const a1_wrt_b = position_value(oa_1, ob_1, ob_2);
  528. int const a2_wrt_b = position_value(oa_2, ob_1, ob_2);
  529. int const b1_wrt_a = position_value(ob_1, oa_1, oa_2);
  530. int const b2_wrt_a = position_value(ob_2, oa_1, oa_2);
  531. // fix the ratios if necessary
  532. // CONSIDER: fixing ratios also in other cases, if they're inconsistent
  533. // e.g. if ratio == 1 or 0 (so IP at the endpoint)
  534. // but position value indicates that the IP is in the middle of the segment
  535. // because one of the segments is very long
  536. // In such case the ratios could be moved into the middle direction
  537. // by some small value (e.g. EPS+1ULP)
  538. if (a1_wrt_b == 1)
  539. {
  540. ra_from.assign(0, 1);
  541. rb_from.assign(0, 1);
  542. }
  543. else if (a1_wrt_b == 3)
  544. {
  545. ra_from.assign(1, 1);
  546. rb_to.assign(0, 1);
  547. }
  548. if (a2_wrt_b == 1)
  549. {
  550. ra_to.assign(0, 1);
  551. rb_from.assign(1, 1);
  552. }
  553. else if (a2_wrt_b == 3)
  554. {
  555. ra_to.assign(1, 1);
  556. rb_to.assign(1, 1);
  557. }
  558. if ((a1_wrt_b < 1 && a2_wrt_b < 1) || (a1_wrt_b > 3 && a2_wrt_b > 3))
  559. //if ((ra_from.left() && ra_to.left()) || (ra_from.right() && ra_to.right()))
  560. {
  561. return Policy::disjoint();
  562. }
  563. bool const opposite = math::sign(length_a) != math::sign(length_b);
  564. return Policy::segments_collinear(a, b, opposite,
  565. a1_wrt_b, a2_wrt_b, b1_wrt_a, b2_wrt_a,
  566. ra_from, ra_to, rb_from, rb_to);
  567. }
  568. /// Relate segments where one is degenerate
  569. template
  570. <
  571. typename Policy,
  572. typename RatioType,
  573. typename DegenerateSegment,
  574. typename RobustType1,
  575. typename RobustType2
  576. >
  577. static inline typename Policy::return_type
  578. relate_one_degenerate(DegenerateSegment const& degenerate_segment,
  579. RobustType1 d, RobustType2 s1, RobustType2 s2,
  580. bool a_degenerate)
  581. {
  582. // Calculate the ratios where ds starts in s
  583. // a1--------->a2 (2..6)
  584. // b1/b2 (4..4)
  585. // Ratio: (4-2)/(6-2)
  586. RatioType const ratio(d - s1, s2 - s1);
  587. if (!ratio.on_segment())
  588. {
  589. return Policy::disjoint();
  590. }
  591. return Policy::one_degenerate(degenerate_segment, ratio, a_degenerate);
  592. }
  593. template <typename ProjCoord1, typename ProjCoord2>
  594. static inline int position_value(ProjCoord1 const& ca1,
  595. ProjCoord2 const& cb1,
  596. ProjCoord2 const& cb2)
  597. {
  598. // S1x 0 1 2 3 4
  599. // S2 |---------->
  600. return math::equals(ca1, cb1) ? 1
  601. : math::equals(ca1, cb2) ? 3
  602. : cb1 < cb2 ?
  603. ( ca1 < cb1 ? 0
  604. : ca1 > cb2 ? 4
  605. : 2 )
  606. : ( ca1 > cb1 ? 0
  607. : ca1 < cb2 ? 4
  608. : 2 );
  609. }
  610. };
  611. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  612. namespace services
  613. {
  614. template <typename CalculationType>
  615. struct default_strategy<cartesian_tag, CalculationType>
  616. {
  617. typedef cartesian_segments<CalculationType> type;
  618. };
  619. } // namespace services
  620. #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  621. }} // namespace strategy::intersection
  622. namespace strategy
  623. {
  624. namespace within { namespace services
  625. {
  626. template <typename Geometry1, typename Geometry2, typename AnyTag1, typename AnyTag2>
  627. struct default_strategy<Geometry1, Geometry2, AnyTag1, AnyTag2, linear_tag, linear_tag, cartesian_tag, cartesian_tag>
  628. {
  629. typedef strategy::intersection::cartesian_segments<> type;
  630. };
  631. template <typename Geometry1, typename Geometry2, typename AnyTag1, typename AnyTag2>
  632. struct default_strategy<Geometry1, Geometry2, AnyTag1, AnyTag2, linear_tag, polygonal_tag, cartesian_tag, cartesian_tag>
  633. {
  634. typedef strategy::intersection::cartesian_segments<> type;
  635. };
  636. template <typename Geometry1, typename Geometry2, typename AnyTag1, typename AnyTag2>
  637. struct default_strategy<Geometry1, Geometry2, AnyTag1, AnyTag2, polygonal_tag, linear_tag, cartesian_tag, cartesian_tag>
  638. {
  639. typedef strategy::intersection::cartesian_segments<> type;
  640. };
  641. template <typename Geometry1, typename Geometry2, typename AnyTag1, typename AnyTag2>
  642. struct default_strategy<Geometry1, Geometry2, AnyTag1, AnyTag2, polygonal_tag, polygonal_tag, cartesian_tag, cartesian_tag>
  643. {
  644. typedef strategy::intersection::cartesian_segments<> type;
  645. };
  646. }} // within::services
  647. namespace covered_by { namespace services
  648. {
  649. template <typename Geometry1, typename Geometry2, typename AnyTag1, typename AnyTag2>
  650. struct default_strategy<Geometry1, Geometry2, AnyTag1, AnyTag2, linear_tag, linear_tag, cartesian_tag, cartesian_tag>
  651. {
  652. typedef strategy::intersection::cartesian_segments<> type;
  653. };
  654. template <typename Geometry1, typename Geometry2, typename AnyTag1, typename AnyTag2>
  655. struct default_strategy<Geometry1, Geometry2, AnyTag1, AnyTag2, linear_tag, polygonal_tag, cartesian_tag, cartesian_tag>
  656. {
  657. typedef strategy::intersection::cartesian_segments<> type;
  658. };
  659. template <typename Geometry1, typename Geometry2, typename AnyTag1, typename AnyTag2>
  660. struct default_strategy<Geometry1, Geometry2, AnyTag1, AnyTag2, polygonal_tag, linear_tag, cartesian_tag, cartesian_tag>
  661. {
  662. typedef strategy::intersection::cartesian_segments<> type;
  663. };
  664. template <typename Geometry1, typename Geometry2, typename AnyTag1, typename AnyTag2>
  665. struct default_strategy<Geometry1, Geometry2, AnyTag1, AnyTag2, polygonal_tag, polygonal_tag, cartesian_tag, cartesian_tag>
  666. {
  667. typedef strategy::intersection::cartesian_segments<> type;
  668. };
  669. }} // within::services
  670. } // strategy
  671. }} // namespace boost::geometry
  672. #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_INTERSECTION_HPP