intersection.hpp 27 KB

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