PageRenderTime 29ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/External/boost/boost/geometry/strategies/cartesian/cart_intersect.hpp

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