cart_intersect.hpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2013 Adam Wulkiewicz, Lodz, Poland.
  4. // Use, modification and distribution is subject to the Boost Software License,
  5. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_GEOMETRY_STRATEGIES_CARTESIAN_INTERSECTION_HPP
  8. #define BOOST_GEOMETRY_STRATEGIES_CARTESIAN_INTERSECTION_HPP
  9. #include <algorithm>
  10. #include <boost/geometry/core/exception.hpp>
  11. #include <boost/geometry/geometries/concepts/point_concept.hpp>
  12. #include <boost/geometry/geometries/concepts/segment_concept.hpp>
  13. #include <boost/geometry/arithmetic/determinant.hpp>
  14. #include <boost/geometry/algorithms/detail/assign_values.hpp>
  15. #include <boost/geometry/util/math.hpp>
  16. #include <boost/geometry/util/select_calculation_type.hpp>
  17. // Temporary / will be Strategy as template parameter
  18. #include <boost/geometry/strategies/side.hpp>
  19. #include <boost/geometry/strategies/cartesian/side_by_triangle.hpp>
  20. #include <boost/geometry/strategies/side_info.hpp>
  21. namespace boost { namespace geometry
  22. {
  23. namespace strategy { namespace intersection
  24. {
  25. #ifndef DOXYGEN_NO_DETAIL
  26. namespace detail
  27. {
  28. template <std::size_t Dimension, typename Segment, typename T>
  29. static inline void segment_arrange(Segment const& s, T& s_1, T& s_2, bool& swapped)
  30. {
  31. s_1 = get<0, Dimension>(s);
  32. s_2 = get<1, Dimension>(s);
  33. if (s_1 > s_2)
  34. {
  35. std::swap(s_1, s_2);
  36. swapped = true;
  37. }
  38. }
  39. template <std::size_t Index, typename Segment>
  40. inline typename geometry::point_type<Segment>::type get_from_index(
  41. Segment const& segment)
  42. {
  43. typedef typename geometry::point_type<Segment>::type point_type;
  44. point_type point;
  45. geometry::detail::assign::assign_point_from_index
  46. <
  47. Segment, point_type, Index, 0, dimension<Segment>::type::value
  48. >::apply(segment, point);
  49. return point;
  50. }
  51. }
  52. #endif
  53. /***
  54. template <typename T>
  55. inline std::string rdebug(T const& value)
  56. {
  57. if (math::equals(value, 0)) return "'0'";
  58. if (math::equals(value, 1)) return "'1'";
  59. if (value < 0) return "<0";
  60. if (value > 1) return ">1";
  61. return "<0..1>";
  62. }
  63. ***/
  64. /*!
  65. \see http://mathworld.wolfram.com/Line-LineIntersection.html
  66. */
  67. template <typename Policy, typename CalculationType = void>
  68. struct relate_cartesian_segments
  69. {
  70. typedef typename Policy::return_type return_type;
  71. typedef typename Policy::segment_type1 segment_type1;
  72. typedef typename Policy::segment_type2 segment_type2;
  73. //typedef typename point_type<segment_type1>::type point_type;
  74. //BOOST_CONCEPT_ASSERT( (concept::Point<point_type>) );
  75. BOOST_CONCEPT_ASSERT( (concept::ConstSegment<segment_type1>) );
  76. BOOST_CONCEPT_ASSERT( (concept::ConstSegment<segment_type2>) );
  77. typedef typename select_calculation_type
  78. <segment_type1, segment_type2, CalculationType>::type coordinate_type;
  79. /// Relate segments a and b
  80. static inline return_type apply(segment_type1 const& a, segment_type2 const& b)
  81. {
  82. coordinate_type const dx_a = get<1, 0>(a) - get<0, 0>(a); // distance in x-dir
  83. coordinate_type const dx_b = get<1, 0>(b) - get<0, 0>(b);
  84. coordinate_type const dy_a = get<1, 1>(a) - get<0, 1>(a); // distance in y-dir
  85. coordinate_type const dy_b = get<1, 1>(b) - get<0, 1>(b);
  86. return apply(a, b, dx_a, dy_a, dx_b, dy_b);
  87. }
  88. // Relate segments a and b using precalculated differences.
  89. // This can save two or four subtractions in many cases
  90. static inline return_type apply(segment_type1 const& a, segment_type2 const& b,
  91. coordinate_type const& dx_a, coordinate_type const& dy_a,
  92. coordinate_type const& dx_b, coordinate_type const& dy_b)
  93. {
  94. typedef side::side_by_triangle<coordinate_type> side;
  95. side_info sides;
  96. coordinate_type const zero = 0;
  97. bool const a_is_point = math::equals(dx_a, zero) && math::equals(dy_a, zero);
  98. bool const b_is_point = math::equals(dx_b, zero) && math::equals(dy_b, zero);
  99. if(a_is_point && b_is_point)
  100. {
  101. if(math::equals(get<1,0>(a), get<1,0>(b)) && math::equals(get<1,1>(a), get<1,1>(b)))
  102. {
  103. Policy::degenerate(a, true);
  104. }
  105. else
  106. {
  107. return Policy::disjoint();
  108. }
  109. }
  110. bool collinear_use_first = math::abs(dx_a) + math::abs(dx_b) >= math::abs(dy_a) + math::abs(dy_b);
  111. sides.set<0>
  112. (
  113. side::apply(detail::get_from_index<0>(b)
  114. , detail::get_from_index<1>(b)
  115. , detail::get_from_index<0>(a)),
  116. side::apply(detail::get_from_index<0>(b)
  117. , detail::get_from_index<1>(b)
  118. , detail::get_from_index<1>(a))
  119. );
  120. sides.set<1>
  121. (
  122. side::apply(detail::get_from_index<0>(a)
  123. , detail::get_from_index<1>(a)
  124. , detail::get_from_index<0>(b)),
  125. side::apply(detail::get_from_index<0>(a)
  126. , detail::get_from_index<1>(a)
  127. , detail::get_from_index<1>(b))
  128. );
  129. bool collinear = sides.collinear();
  130. robustness_verify_collinear(a, b, a_is_point, b_is_point, sides, collinear);
  131. robustness_verify_meeting(a, b, sides, collinear, collinear_use_first);
  132. if (sides.same<0>() || sides.same<1>())
  133. {
  134. // Both points are at same side of other segment, we can leave
  135. if (robustness_verify_same_side(a, b, sides))
  136. {
  137. return Policy::disjoint();
  138. }
  139. }
  140. // Degenerate cases: segments of single point, lying on other segment, non disjoint
  141. if (a_is_point)
  142. {
  143. return Policy::degenerate(a, true);
  144. }
  145. if (b_is_point)
  146. {
  147. return Policy::degenerate(b, false);
  148. }
  149. typedef typename select_most_precise
  150. <
  151. coordinate_type, double
  152. >::type promoted_type;
  153. // r: ratio 0-1 where intersection divides A/B
  154. // (only calculated for non-collinear segments)
  155. promoted_type r;
  156. if (! collinear)
  157. {
  158. // Calculate determinants - Cramers rule
  159. coordinate_type const wx = get<0, 0>(a) - get<0, 0>(b);
  160. coordinate_type const wy = get<0, 1>(a) - get<0, 1>(b);
  161. coordinate_type const d = geometry::detail::determinant<coordinate_type>(dx_a, dy_a, dx_b, dy_b);
  162. coordinate_type const da = geometry::detail::determinant<coordinate_type>(dx_b, dy_b, wx, wy);
  163. coordinate_type const zero = coordinate_type();
  164. if (math::equals(d, zero))
  165. {
  166. // This is still a collinear case (because of FP imprecision this can occur here)
  167. // sides.debug();
  168. sides.set<0>(0,0);
  169. sides.set<1>(0,0);
  170. collinear = true;
  171. }
  172. else
  173. {
  174. r = promoted_type(da) / promoted_type(d);
  175. if (! robustness_verify_r(a, b, r))
  176. {
  177. return Policy::disjoint();
  178. }
  179. //robustness_handle_meeting(a, b, sides, dx_a, dy_a, wx, wy, d, r);
  180. if (robustness_verify_disjoint_at_one_collinear(a, b, sides))
  181. {
  182. return Policy::disjoint();
  183. }
  184. }
  185. }
  186. if(collinear)
  187. {
  188. if (collinear_use_first)
  189. {
  190. return relate_collinear<0>(a, b);
  191. }
  192. else
  193. {
  194. // Y direction contains larger segments (maybe dx is zero)
  195. return relate_collinear<1>(a, b);
  196. }
  197. }
  198. return Policy::segments_intersect(sides, r,
  199. dx_a, dy_a, dx_b, dy_b,
  200. a, b);
  201. }
  202. private :
  203. // Ratio should lie between 0 and 1
  204. // Also these three conditions might be of FP imprecision, the segments were actually (nearly) collinear
  205. template <typename T>
  206. static inline bool robustness_verify_r(
  207. segment_type1 const& a, segment_type2 const& b,
  208. T& r)
  209. {
  210. T const zero = 0;
  211. T const one = 1;
  212. if (r < zero || r > one)
  213. {
  214. if (verify_disjoint<0>(a, b) || verify_disjoint<1>(a, b))
  215. {
  216. // Can still be disjoint (even if not one is left or right from another)
  217. // This is e.g. in case #snake4 of buffer test.
  218. return false;
  219. }
  220. //std::cout << "ROBUSTNESS: correction of r " << r << std::endl;
  221. // sides.debug();
  222. // ROBUSTNESS: the r value can in epsilon-cases much larger than 1, while (with perfect arithmetic)
  223. // it should be one. It can be 1.14 or even 1.98049 or 2 (while still intersecting)
  224. // If segments are crossing (we can see that with the sides)
  225. // and one is inside the other, there must be an intersection point.
  226. // We correct for that.
  227. // This is (only) in case #ggl_list_20110820_christophe in unit tests
  228. // If segments are touching (two sides zero), of course they should intersect
  229. // This is (only) in case #buffer_rt_i in the unit tests)
  230. // If one touches in the middle, they also should intersect (#buffer_rt_j)
  231. // Note that even for ttmath r is occasionally > 1, e.g. 1.0000000000000000000000036191231203575
  232. if (r > one)
  233. {
  234. r = one;
  235. }
  236. else if (r < zero)
  237. {
  238. r = zero;
  239. }
  240. }
  241. return true;
  242. }
  243. static inline void robustness_verify_collinear(
  244. segment_type1 const& , segment_type2 const& ,
  245. bool a_is_point, bool b_is_point,
  246. side_info& sides,
  247. bool& collinear)
  248. {
  249. if ((sides.zero<0>() && ! b_is_point && ! sides.zero<1>()) || (sides.zero<1>() && ! a_is_point && ! sides.zero<0>()))
  250. {
  251. // If one of the segments is collinear, the other must be as well.
  252. // So handle it as collinear.
  253. // (In float/double epsilon margins it can easily occur that one or two of them are -1/1)
  254. // sides.debug();
  255. sides.set<0>(0,0);
  256. sides.set<1>(0,0);
  257. collinear = true;
  258. }
  259. }
  260. static inline void robustness_verify_meeting(
  261. segment_type1 const& a, segment_type2 const& b,
  262. side_info& sides,
  263. bool& collinear, bool& collinear_use_first)
  264. {
  265. if (sides.meeting())
  266. {
  267. // If two segments meet each other at their segment-points, two sides are zero,
  268. // the other two are not (unless collinear but we don't mean those here).
  269. // However, in near-epsilon ranges it can happen that two sides are zero
  270. // but they do not meet at their segment-points.
  271. // In that case they are nearly collinear and handled as such.
  272. if (! point_equals
  273. (
  274. select(sides.zero_index<0>(), a),
  275. select(sides.zero_index<1>(), b)
  276. )
  277. )
  278. {
  279. sides.set<0>(0,0);
  280. sides.set<1>(0,0);
  281. collinear = true;
  282. if (collinear_use_first && analyse_equal<0>(a, b))
  283. {
  284. collinear_use_first = false;
  285. }
  286. else if (! collinear_use_first && analyse_equal<1>(a, b))
  287. {
  288. collinear_use_first = true;
  289. }
  290. }
  291. }
  292. }
  293. // Verifies and if necessary correct missed touch because of robustness
  294. // This is the case at multi_polygon_buffer unittest #rt_m
  295. static inline bool robustness_verify_same_side(
  296. segment_type1 const& a, segment_type2 const& b,
  297. side_info& sides)
  298. {
  299. int corrected = 0;
  300. if (sides.one_touching<0>())
  301. {
  302. if (point_equals(
  303. select(sides.zero_index<0>(), a),
  304. select(0, b)
  305. ))
  306. {
  307. sides.correct_to_zero<1, 0>();
  308. corrected = 1;
  309. }
  310. if (point_equals
  311. (
  312. select(sides.zero_index<0>(), a),
  313. select(1, b)
  314. ))
  315. {
  316. sides.correct_to_zero<1, 1>();
  317. corrected = 2;
  318. }
  319. }
  320. else if (sides.one_touching<1>())
  321. {
  322. if (point_equals(
  323. select(sides.zero_index<1>(), b),
  324. select(0, a)
  325. ))
  326. {
  327. sides.correct_to_zero<0, 0>();
  328. corrected = 3;
  329. }
  330. if (point_equals
  331. (
  332. select(sides.zero_index<1>(), b),
  333. select(1, a)
  334. ))
  335. {
  336. sides.correct_to_zero<0, 1>();
  337. corrected = 4;
  338. }
  339. }
  340. return corrected == 0;
  341. }
  342. static inline bool robustness_verify_disjoint_at_one_collinear(
  343. segment_type1 const& a, segment_type2 const& b,
  344. side_info const& sides)
  345. {
  346. if (sides.one_of_all_zero())
  347. {
  348. if (verify_disjoint<0>(a, b) || verify_disjoint<1>(a, b))
  349. {
  350. return true;
  351. }
  352. }
  353. return false;
  354. }
  355. /*
  356. // If r is one, or zero, segments should meet and their endpoints.
  357. // Robustness issue: check if this is really the case.
  358. // It turns out to be no problem, see buffer test #rt_s1 (and there are many cases generated)
  359. // It generates an "ends in the middle" situation which is correct.
  360. template <typename T, typename R>
  361. static inline void robustness_handle_meeting(segment_type1 const& a, segment_type2 const& b,
  362. side_info& sides,
  363. T const& dx_a, T const& dy_a, T const& wx, T const& wy,
  364. T const& d, R const& r)
  365. {
  366. return;
  367. T const db = geometry::detail::determinant<T>(dx_a, dy_a, wx, wy);
  368. R const zero = 0;
  369. R const one = 1;
  370. if (math::equals(r, zero) || math::equals(r, one))
  371. {
  372. R rb = db / d;
  373. if (rb <= 0 || rb >= 1 || math::equals(rb, 0) || math::equals(rb, 1))
  374. {
  375. if (sides.one_zero<0>() && ! sides.one_zero<1>()) // or vice versa
  376. {
  377. #if defined(BOOST_GEOMETRY_COUNT_INTERSECTION_EQUAL)
  378. extern int g_count_intersection_equal;
  379. g_count_intersection_equal++;
  380. #endif
  381. sides.debug();
  382. std::cout << "E r=" << r << " r.b=" << rb << " ";
  383. }
  384. }
  385. }
  386. }
  387. */
  388. template <std::size_t Dimension>
  389. static inline bool verify_disjoint(segment_type1 const& a,
  390. segment_type2 const& b)
  391. {
  392. coordinate_type a_1, a_2, b_1, b_2;
  393. bool a_swapped = false, b_swapped = false;
  394. detail::segment_arrange<Dimension>(a, a_1, a_2, a_swapped);
  395. detail::segment_arrange<Dimension>(b, b_1, b_2, b_swapped);
  396. return math::smaller(a_2, b_1) || math::larger(a_1, b_2);
  397. }
  398. template <typename Segment>
  399. static inline typename point_type<Segment>::type select(int index, Segment const& segment)
  400. {
  401. return index == 0
  402. ? detail::get_from_index<0>(segment)
  403. : detail::get_from_index<1>(segment)
  404. ;
  405. }
  406. // We cannot use geometry::equals here. Besides that this will be changed
  407. // to compare segment-coordinate-values directly (not necessary to retrieve point first)
  408. template <typename Point1, typename Point2>
  409. static inline bool point_equals(Point1 const& point1, Point2 const& point2)
  410. {
  411. return math::equals(get<0>(point1), get<0>(point2))
  412. && math::equals(get<1>(point1), get<1>(point2))
  413. ;
  414. }
  415. // We cannot use geometry::equals here. Besides that this will be changed
  416. // to compare segment-coordinate-values directly (not necessary to retrieve point first)
  417. template <typename Point1, typename Point2>
  418. static inline bool point_equality(Point1 const& point1, Point2 const& point2,
  419. bool& equals_0, bool& equals_1)
  420. {
  421. equals_0 = math::equals(get<0>(point1), get<0>(point2));
  422. equals_1 = math::equals(get<1>(point1), get<1>(point2));
  423. return equals_0 && equals_1;
  424. }
  425. template <std::size_t Dimension>
  426. static inline bool analyse_equal(segment_type1 const& a, segment_type2 const& b)
  427. {
  428. coordinate_type const a_1 = geometry::get<0, Dimension>(a);
  429. coordinate_type const a_2 = geometry::get<1, Dimension>(a);
  430. coordinate_type const b_1 = geometry::get<0, Dimension>(b);
  431. coordinate_type const b_2 = geometry::get<1, Dimension>(b);
  432. return math::equals(a_1, b_1)
  433. || math::equals(a_2, b_1)
  434. || math::equals(a_1, b_2)
  435. || math::equals(a_2, b_2)
  436. ;
  437. }
  438. template <std::size_t Dimension>
  439. static inline return_type relate_collinear(segment_type1 const& a,
  440. segment_type2 const& b)
  441. {
  442. coordinate_type a_1, a_2, b_1, b_2;
  443. bool a_swapped = false, b_swapped = false;
  444. detail::segment_arrange<Dimension>(a, a_1, a_2, a_swapped);
  445. detail::segment_arrange<Dimension>(b, b_1, b_2, b_swapped);
  446. if (math::smaller(a_2, b_1) || math::larger(a_1, b_2))
  447. //if (a_2 < b_1 || a_1 > b_2)
  448. {
  449. return Policy::disjoint();
  450. }
  451. return relate_collinear(a, b, a_1, a_2, b_1, b_2, a_swapped, b_swapped);
  452. }
  453. /// Relate segments known collinear
  454. static inline return_type relate_collinear(segment_type1 const& a
  455. , segment_type2 const& b
  456. , coordinate_type a_1, coordinate_type a_2
  457. , coordinate_type b_1, coordinate_type b_2
  458. , bool a_swapped, bool b_swapped)
  459. {
  460. // All ca. 150 lines are about collinear rays
  461. // The intersections, if any, are always boundary points of the segments. No need to calculate anything.
  462. // However we want to find out HOW they intersect, there are many cases.
  463. // Most sources only provide the intersection (above) or that there is a collinearity (but not the points)
  464. // or some spare sources give the intersection points (calculated) but not how they align.
  465. // This source tries to give everything and still be efficient.
  466. // It is therefore (and because of the extensive clarification comments) rather long...
  467. // \see http://mpa.itc.it/radim/g50history/CMP/4.2.1-CERL-beta-libes/file475.txt
  468. // \see http://docs.codehaus.org/display/GEOTDOC/Point+Set+Theory+and+the+DE-9IM+Matrix
  469. // \see http://mathworld.wolfram.com/Line-LineIntersection.html
  470. // Because of collinearity the case is now one-dimensional and can be checked using intervals
  471. // This function is called either horizontally or vertically
  472. // We get then two intervals:
  473. // a_1-------------a_2 where a_1 < a_2
  474. // b_1-------------b_2 where b_1 < b_2
  475. // In all figures below a_1/a_2 denotes arranged intervals, a1-a2 or a2-a1 are still unarranged
  476. // Handle "equal", in polygon neighbourhood comparisons a common case
  477. bool const opposite = a_swapped ^ b_swapped;
  478. bool const both_swapped = a_swapped && b_swapped;
  479. // Check if segments are equal or opposite equal...
  480. bool const swapped_a1_eq_b1 = math::equals(a_1, b_1);
  481. bool const swapped_a2_eq_b2 = math::equals(a_2, b_2);
  482. if (swapped_a1_eq_b1 && swapped_a2_eq_b2)
  483. {
  484. return Policy::segment_equal(a, opposite);
  485. }
  486. bool const swapped_a2_eq_b1 = math::equals(a_2, b_1);
  487. bool const swapped_a1_eq_b2 = math::equals(a_1, b_2);
  488. bool const a1_eq_b1 = both_swapped ? swapped_a2_eq_b2 : a_swapped ? swapped_a2_eq_b1 : b_swapped ? swapped_a1_eq_b2 : swapped_a1_eq_b1;
  489. bool const a2_eq_b2 = both_swapped ? swapped_a1_eq_b1 : a_swapped ? swapped_a1_eq_b2 : b_swapped ? swapped_a2_eq_b1 : swapped_a2_eq_b2;
  490. bool const a1_eq_b2 = both_swapped ? swapped_a2_eq_b1 : a_swapped ? swapped_a2_eq_b2 : b_swapped ? swapped_a1_eq_b1 : swapped_a1_eq_b2;
  491. bool const a2_eq_b1 = both_swapped ? swapped_a1_eq_b2 : a_swapped ? swapped_a1_eq_b1 : b_swapped ? swapped_a2_eq_b2 : swapped_a2_eq_b1;
  492. // The rest below will return one or two intersections.
  493. // The delegated class can decide which is the intersection point, or two, build the Intersection Matrix (IM)
  494. // For IM it is important to know which relates to which. So this information is given,
  495. // without performance penalties to intersection calculation
  496. bool const has_common_points = swapped_a1_eq_b1 || swapped_a1_eq_b2 || swapped_a2_eq_b1 || swapped_a2_eq_b2;
  497. // "Touch" -> one intersection point -> one but not two common points
  498. // --------> A (or B)
  499. // <---------- B (or A)
  500. // a_2==b_1 (b_2==a_1 or a_2==b1)
  501. // The check a_2/b_1 is necessary because it excludes cases like
  502. // ------->
  503. // --->
  504. // ... which are handled lateron
  505. // Corresponds to 4 cases, of which the equal points are determined above
  506. // #1: a1---->a2 b1--->b2 (a arrives at b's border)
  507. // #2: a2<----a1 b2<---b1 (b arrives at a's border)
  508. // #3: a1---->a2 b2<---b1 (both arrive at each others border)
  509. // #4: a2<----a1 b1--->b2 (no arrival at all)
  510. // Where the arranged forms have two forms:
  511. // a_1-----a_2/b_1-------b_2 or reverse (B left of A)
  512. if ((swapped_a2_eq_b1 || swapped_a1_eq_b2) && ! swapped_a1_eq_b1 && ! swapped_a2_eq_b2)
  513. {
  514. if (a2_eq_b1) return Policy::collinear_touch(get<1, 0>(a), get<1, 1>(a), 0, -1);
  515. if (a1_eq_b2) return Policy::collinear_touch(get<0, 0>(a), get<0, 1>(a), -1, 0);
  516. if (a2_eq_b2) return Policy::collinear_touch(get<1, 0>(a), get<1, 1>(a), 0, 0);
  517. if (a1_eq_b1) return Policy::collinear_touch(get<0, 0>(a), get<0, 1>(a), -1, -1);
  518. }
  519. // "Touch/within" -> there are common points and also an intersection of interiors:
  520. // Corresponds to many cases:
  521. // #1a: a1------->a2 #1b: a1-->a2
  522. // b1--->b2 b1------->b2
  523. // #2a: a2<-------a1 #2b: a2<--a1
  524. // b1--->b2 b1------->b2
  525. // #3a: a1------->a2 #3b: a1-->a2
  526. // b2<---b1 b2<-------b1
  527. // #4a: a2<-------a1 #4b: a2<--a1
  528. // b2<---b1 b2<-------b1
  529. // Note: next cases are similar and handled by the code
  530. // #4c: a1--->a2
  531. // b1-------->b2
  532. // #4d: a1-------->a2
  533. // b1-->b2
  534. // For case 1-4: a_1 < (b_1 or b_2) < a_2, two intersections are equal to segment B
  535. // For case 5-8: b_1 < (a_1 or a_2) < b_2, two intersections are equal to segment A
  536. if (has_common_points)
  537. {
  538. // Either A is in B, or B is in A, or (in case of robustness/equals)
  539. // both are true, see below
  540. bool a_in_b = (b_1 < a_1 && a_1 < b_2) || (b_1 < a_2 && a_2 < b_2);
  541. bool b_in_a = (a_1 < b_1 && b_1 < a_2) || (a_1 < b_2 && b_2 < a_2);
  542. if (a_in_b && b_in_a)
  543. {
  544. // testcase "ggl_list_20110306_javier"
  545. // In robustness it can occur that a point of A is inside B AND a point of B is inside A,
  546. // still while has_common_points is true (so one point equals the other).
  547. // If that is the case we select on length.
  548. coordinate_type const length_a = geometry::math::abs(a_1 - a_2);
  549. coordinate_type const length_b = geometry::math::abs(b_1 - b_2);
  550. if (length_a > length_b)
  551. {
  552. a_in_b = false;
  553. }
  554. else
  555. {
  556. b_in_a = false;
  557. }
  558. }
  559. int const arrival_a = a_in_b ? 1 : -1;
  560. if (a2_eq_b2) return Policy::collinear_interior_boundary_intersect(a_in_b ? a : b, a_in_b, 0, 0, false);
  561. if (a1_eq_b2) return Policy::collinear_interior_boundary_intersect(a_in_b ? a : b, a_in_b, arrival_a, 0, true);
  562. if (a2_eq_b1) return Policy::collinear_interior_boundary_intersect(a_in_b ? a : b, a_in_b, 0, -arrival_a, true);
  563. if (a1_eq_b1) return Policy::collinear_interior_boundary_intersect(a_in_b ? a : b, a_in_b, arrival_a, -arrival_a, false);
  564. }
  565. // "Inside", a completely within b or b completely within a
  566. // 2 cases:
  567. // case 1:
  568. // a_1---a_2 -> take A's points as intersection points
  569. // b_1------------b_2
  570. // case 2:
  571. // a_1------------a_2
  572. // b_1---b_2 -> take B's points
  573. if (a_1 > b_1 && a_2 < b_2)
  574. {
  575. // A within B
  576. return Policy::collinear_a_in_b(a, opposite);
  577. }
  578. if (b_1 > a_1 && b_2 < a_2)
  579. {
  580. // B within A
  581. return Policy::collinear_b_in_a(b, opposite);
  582. }
  583. /*
  584. Now that all cases with equal,touch,inside,disjoint,
  585. degenerate are handled the only thing left is an overlap
  586. Either a1 is between b1,b2
  587. or a2 is between b1,b2 (a2 arrives)
  588. Next table gives an overview.
  589. The IP's are ordered following the line A1->A2
  590. | |
  591. | a_2 in between | a_1 in between
  592. | |
  593. -----+---------------------------------+--------------------------
  594. | a1--------->a2 | a1--------->a2
  595. | b1----->b2 | b1----->b2
  596. | (b1,a2), a arrives | (a1,b2), b arrives
  597. | |
  598. -----+---------------------------------+--------------------------
  599. a sw.| a2<---------a1* | a2<---------a1*
  600. | b1----->b2 | b1----->b2
  601. | (a1,b1), no arrival | (b2,a2), a and b arrive
  602. | |
  603. -----+---------------------------------+--------------------------
  604. | a1--------->a2 | a1--------->a2
  605. b sw.| b2<-----b1 | b2<-----b1
  606. | (b2,a2), a and b arrive | (a1,b1), no arrival
  607. | |
  608. -----+---------------------------------+--------------------------
  609. a sw.| a2<---------a1* | a2<---------a1*
  610. b sw.| b2<-----b1 | b2<-----b1
  611. | (a1,b2), b arrives | (b1,a2), a arrives
  612. | |
  613. -----+---------------------------------+--------------------------
  614. * Note that a_1 < a_2, and a1 <> a_1; if a is swapped,
  615. the picture might seem wrong but it (supposed to be) is right.
  616. */
  617. if (b_1 < a_2 && a_2 < b_2)
  618. {
  619. // Left column, from bottom to top
  620. return
  621. both_swapped ? Policy::collinear_overlaps(get<0, 0>(a), get<0, 1>(a), get<1, 0>(b), get<1, 1>(b), -1, 1, opposite)
  622. : b_swapped ? Policy::collinear_overlaps(get<1, 0>(b), get<1, 1>(b), get<1, 0>(a), get<1, 1>(a), 1, 1, opposite)
  623. : a_swapped ? Policy::collinear_overlaps(get<0, 0>(a), get<0, 1>(a), get<0, 0>(b), get<0, 1>(b), -1, -1, opposite)
  624. : Policy::collinear_overlaps(get<0, 0>(b), get<0, 1>(b), get<1, 0>(a), get<1, 1>(a), 1, -1, opposite)
  625. ;
  626. }
  627. if (b_1 < a_1 && a_1 < b_2)
  628. {
  629. // Right column, from bottom to top
  630. return
  631. both_swapped ? Policy::collinear_overlaps(get<0, 0>(b), get<0, 1>(b), get<1, 0>(a), get<1, 1>(a), 1, -1, opposite)
  632. : b_swapped ? Policy::collinear_overlaps(get<0, 0>(a), get<0, 1>(a), get<0, 0>(b), get<0, 1>(b), -1, -1, opposite)
  633. : a_swapped ? Policy::collinear_overlaps(get<1, 0>(b), get<1, 1>(b), get<1, 0>(a), get<1, 1>(a), 1, 1, opposite)
  634. : Policy::collinear_overlaps(get<0, 0>(a), get<0, 1>(a), get<1, 0>(b), get<1, 1>(b), -1, 1, opposite)
  635. ;
  636. }
  637. // Nothing should goes through. If any we have made an error
  638. // std::cout << "Robustness issue, non-logical behaviour" << std::endl;
  639. return Policy::error("Robustness issue, non-logical behaviour");
  640. }
  641. };
  642. }} // namespace strategy::intersection
  643. }} // namespace boost::geometry
  644. #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_INTERSECTION_HPP