segment_intersection_collinear.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  5. // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
  6. // Copyright (c) 2017, Oracle and/or its affiliates.
  7. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  8. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  9. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  10. // Use, modification and distribution is subject to the Boost Software License,
  11. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. #define BOOST_GEOMETRY_DEFINE_STREAM_OPERATOR_SEGMENT_RATIO
  14. #include <geometry_test_common.hpp>
  15. #include <boost/geometry/algorithms/assign.hpp>
  16. #include <boost/geometry/strategies/cartesian/intersection.hpp>
  17. #include <boost/geometry/strategies/intersection_result.hpp>
  18. #include <boost/geometry/policies/relate/intersection_points.hpp>
  19. #include <boost/geometry/policies/relate/direction.hpp>
  20. #include <boost/geometry/policies/relate/tupled.hpp>
  21. #include <boost/geometry/algorithms/intersection.hpp>
  22. #include <boost/geometry/algorithms/detail/overlay/segment_as_subrange.hpp>
  23. #include <boost/geometry/geometries/point.hpp>
  24. #include <boost/geometry/geometries/segment.hpp>
  25. template <typename IntersectionPoints>
  26. static int check(IntersectionPoints const& is,
  27. std::size_t index, double expected_x, double expected_y)
  28. {
  29. if (expected_x != -99 && expected_y != -99 && is.count > index)
  30. {
  31. double x = bg::get<0>(is.intersections[index]);
  32. double y = bg::get<1>(is.intersections[index]);
  33. BOOST_CHECK_CLOSE(x, expected_x, 0.001);
  34. BOOST_CHECK_CLOSE(y, expected_y, 0.001);
  35. return 1;
  36. }
  37. return 0;
  38. }
  39. template <typename P>
  40. static void test_segment_intersection(std::string const& case_id,
  41. int x1, int y1, int x2, int y2,
  42. int x3, int y3, int x4, int y4,
  43. char expected_how, bool expected_opposite,
  44. int expected_arrival1, int expected_arrival2,
  45. int expected_x1, int expected_y1,
  46. int expected_x2 = -99, int expected_y2 = -99)
  47. {
  48. boost::ignore_unused(case_id);
  49. typedef bg::model::referring_segment<const P> segment_type;
  50. P p1, p2, p3, p4;
  51. bg::assign_values(p1, x1, y1);
  52. bg::assign_values(p2, x2, y2);
  53. bg::assign_values(p3, x3, y3);
  54. bg::assign_values(p4, x4, y4);
  55. segment_type s12(p1, p2);
  56. segment_type s34(p3, p4);
  57. bg::detail::segment_as_subrange<segment_type> sr12(s12);
  58. bg::detail::segment_as_subrange<segment_type> sr34(s34);
  59. typedef bg::segment_intersection_points<P> result_type;
  60. typedef bg::policies::relate::segments_intersection_points
  61. <
  62. result_type
  63. > points_policy_type;
  64. // Get the intersection point (or two points)
  65. result_type is
  66. = bg::strategy::intersection::cartesian_segments<>
  67. ::apply(sr12, sr34, points_policy_type());
  68. // Get just a character for Left/Right/intersects/etc, purpose is more for debugging
  69. bg::policies::relate::direction_type dir
  70. = bg::strategy::intersection::cartesian_segments<>
  71. ::apply(sr12, sr34, bg::policies::relate::segments_direction());
  72. std::size_t expected_count =
  73. check(is, 0, expected_x1, expected_y1)
  74. + check(is, 1, expected_x2, expected_y2);
  75. BOOST_CHECK_EQUAL(is.count, expected_count);
  76. BOOST_CHECK_EQUAL(dir.how, expected_how);
  77. BOOST_CHECK_EQUAL(dir.opposite, expected_opposite);
  78. BOOST_CHECK_EQUAL(dir.arrival[0], expected_arrival1);
  79. BOOST_CHECK_EQUAL(dir.arrival[1], expected_arrival2);
  80. }
  81. template <typename P, typename Pair>
  82. static void test_segment_ratio(std::string const& case_id,
  83. int x1, int y1, int x2, int y2,
  84. int x3, int y3, int x4, int y4,
  85. Pair expected_pair_a1, Pair expected_pair_a2,
  86. Pair expected_pair_b1, Pair expected_pair_b2,
  87. int exp_ax1, int exp_ay1, int exp_ax2, int exp_ay2,
  88. std::size_t expected_count = 2)
  89. {
  90. boost::ignore_unused(case_id);
  91. typedef bg::model::referring_segment<const P> segment_type;
  92. P p1, p2, p3, p4;
  93. bg::assign_values(p1, x1, y1);
  94. bg::assign_values(p2, x2, y2);
  95. bg::assign_values(p3, x3, y3);
  96. bg::assign_values(p4, x4, y4);
  97. segment_type s12(p1, p2);
  98. segment_type s34(p3, p4);
  99. bg::detail::segment_as_subrange<segment_type> sr12(s12);
  100. bg::detail::segment_as_subrange<segment_type> sr34(s34);
  101. typedef bg::segment_intersection_points<P> result_type;
  102. typedef bg::policies::relate::segments_intersection_points
  103. <
  104. result_type
  105. > points_policy_type;
  106. // Get the intersection point (or two points)
  107. result_type is
  108. = bg::strategy::intersection::cartesian_segments<>
  109. ::apply(sr12, sr34, points_policy_type());
  110. typedef bg::segment_ratio<typename bg::coordinate_type<P>::type> ratio_type;
  111. ratio_type expected_a1(expected_pair_a1.first, expected_pair_a1.second);
  112. ratio_type expected_a2(expected_pair_a2.first, expected_pair_a2.second);
  113. ratio_type expected_b1(expected_pair_b1.first, expected_pair_b1.second);
  114. ratio_type expected_b2(expected_pair_b2.first, expected_pair_b2.second);
  115. BOOST_CHECK_EQUAL(is.count, expected_count);
  116. BOOST_CHECK_EQUAL(is.fractions[0].robust_ra, expected_a1);
  117. BOOST_CHECK_EQUAL(is.fractions[0].robust_rb, expected_b1);
  118. BOOST_CHECK_EQUAL(bg::get<0>(is.intersections[0]), exp_ax1);
  119. BOOST_CHECK_EQUAL(bg::get<1>(is.intersections[0]), exp_ay1);
  120. if (expected_count == 2)
  121. {
  122. BOOST_CHECK_EQUAL(bg::get<0>(is.intersections[1]), exp_ax2);
  123. BOOST_CHECK_EQUAL(bg::get<1>(is.intersections[1]), exp_ay2);
  124. BOOST_CHECK_EQUAL(is.fractions[1].robust_ra, expected_a2);
  125. BOOST_CHECK_EQUAL(is.fractions[1].robust_rb, expected_b2);
  126. }
  127. }
  128. template <typename P>
  129. void test_all()
  130. {
  131. // Collinear - non opposite
  132. // a1---------->a2
  133. // b1--->b2
  134. test_segment_intersection<P>("n1",
  135. 2, 0, 6, 0,
  136. 0, 0, 2, 0,
  137. 'a', false, -1, 0,
  138. 2, 0);
  139. // a1---------->a2
  140. // b1--->b2
  141. test_segment_intersection<P>("n2",
  142. 2, 0, 6, 0,
  143. 1, 0, 3, 0,
  144. 'c', false, -1, 1,
  145. 2, 0, 3, 0);
  146. // a1---------->a2
  147. // b1--->b2
  148. test_segment_intersection<P>("n3",
  149. 2, 0, 6, 0,
  150. 2, 0, 4, 0,
  151. 'c', false, -1, 1,
  152. 2, 0, 4, 0);
  153. // a1---------->a2
  154. // b1--->b2
  155. test_segment_intersection<P>("n4",
  156. 2, 0, 6, 0,
  157. 3, 0, 5, 0,
  158. 'c', false, -1, 1,
  159. 3, 0, 5, 0);
  160. // a1---------->a2
  161. // b1--->b2
  162. test_segment_intersection<P>("n5",
  163. 2, 0, 6, 0,
  164. 4, 0, 6, 0,
  165. 'c', false, 0, 0,
  166. 4, 0, 6, 0);
  167. // a1---------->a2
  168. // b1--->b2
  169. test_segment_intersection<P>("n6",
  170. 2, 0, 6, 0,
  171. 5, 0, 7, 0,
  172. 'c', false, 1, -1,
  173. 5, 0, 6, 0);
  174. // a1---------->a2
  175. // b1--->b2
  176. test_segment_intersection<P>("n7",
  177. 2, 0, 6, 0,
  178. 6, 0, 8, 0,
  179. 'a', false, 0, -1,
  180. 6, 0);
  181. // Collinear - opposite
  182. // a1---------->a2
  183. // b2<---b1
  184. test_segment_intersection<P>("o1",
  185. 2, 0, 6, 0,
  186. 2, 0, 0, 0,
  187. 'f', true, -1, -1,
  188. 2, 0);
  189. // a1---------->a2
  190. // b2<---b1
  191. test_segment_intersection<P>("o2",
  192. 2, 0, 6, 0,
  193. 3, 0, 1, 0,
  194. 'c', true, -1, -1,
  195. 2, 0, 3, 0);
  196. // a1---------->a2
  197. // b2<---b1
  198. test_segment_intersection<P>("o3",
  199. 2, 0, 6, 0,
  200. 4, 0, 2, 0,
  201. 'c', true, -1, 0,
  202. 2, 0, 4, 0);
  203. // a1---------->a2
  204. // b2<---b1
  205. test_segment_intersection<P>("o4",
  206. 2, 0, 6, 0,
  207. 5, 0, 3, 0,
  208. 'c', true, -1, 1,
  209. 3, 0, 5, 0);
  210. // a1---------->a2
  211. // b2<---b1
  212. test_segment_intersection<P>("o5",
  213. 2, 0, 6, 0,
  214. 6, 0, 4, 0,
  215. 'c', true, 0, 1,
  216. 4, 0, 6, 0);
  217. // a1---------->a2
  218. // b2<---b1
  219. test_segment_intersection<P>("o6",
  220. 2, 0, 6, 0,
  221. 7, 0, 5, 0,
  222. 'c', true, 1, 1,
  223. 5, 0, 6, 0);
  224. // a1---------->a2
  225. // b2<---b1
  226. test_segment_intersection<P>("o7",
  227. 2, 0, 6, 0,
  228. 8, 0, 6, 0,
  229. 't', true, 0, 0,
  230. 6, 0);
  231. // a1---------->a2
  232. // b1---------->b2
  233. test_segment_intersection<P>("e1",
  234. 2, 0, 6, 0,
  235. 2, 0, 6, 0,
  236. 'e', false, 0, 0,
  237. 2, 0, 6, 0);
  238. // a1---------->a2
  239. // b2<----------b1
  240. test_segment_intersection<P>("e1",
  241. 2, 0, 6, 0,
  242. 6, 0, 2, 0,
  243. 'e', true, 0, 0,
  244. 2, 0, 6, 0);
  245. // Disjoint (in vertical direction, picture still horizontal)
  246. // a2<---a1
  247. // b2<---b1
  248. test_segment_intersection<P>("case_recursive_boxes_1",
  249. 10, 7, 10, 6,
  250. 10, 10, 10, 9,
  251. 'd', false, 0, 0,
  252. -1, -1, -1, -1);
  253. }
  254. template <typename P>
  255. void test_ratios()
  256. {
  257. // B inside A
  258. // a1------------>a2
  259. // b1--->b2
  260. test_segment_ratio<P>("n4",
  261. 2, 0, 7, 0,
  262. 3, 0, 5, 0,
  263. std::make_pair(1, 5), std::make_pair(3, 5), // IP located on 1/5, 3/5 w.r.t A
  264. std::make_pair(0, 1), std::make_pair(1, 1), // IP located on 0, 1 w.r.t. B
  265. // IP's are ordered as in A (currently)
  266. 3, 0, 5, 0);
  267. // a1------------>a2
  268. // b2<---b1
  269. test_segment_ratio<P>("o4",
  270. 2, 0, 7, 0,
  271. 5, 0, 3, 0,
  272. std::make_pair(1, 5), std::make_pair(3, 5),
  273. std::make_pair(1, 1), std::make_pair(0, 1),
  274. 3, 0, 5, 0);
  275. // a2<------------a1
  276. // b2<---b1
  277. test_segment_ratio<P>("o4b",
  278. 7, 0, 2, 0,
  279. 5, 0, 3, 0,
  280. std::make_pair(2, 5), std::make_pair(4, 5),
  281. std::make_pair(0, 1), std::make_pair(1, 1),
  282. 5, 0, 3, 0);
  283. // a2<------------a1
  284. // b1--->b2
  285. test_segment_ratio<P>("o4c",
  286. 7, 0, 2, 0,
  287. 3, 0, 5, 0,
  288. std::make_pair(2, 5), std::make_pair(4, 5),
  289. std::make_pair(1, 1), std::make_pair(0, 1),
  290. 5, 0, 3, 0);
  291. // Touch-interior
  292. // a1---------->a2
  293. // b1--->b2
  294. test_segment_ratio<P>("n3",
  295. 2, 0, 7, 0,
  296. 2, 0, 4, 0,
  297. std::make_pair(0, 1), std::make_pair(2, 5),
  298. std::make_pair(0, 1), std::make_pair(1, 1),
  299. 2, 0, 4, 0);
  300. // a2<-------------a1
  301. // b2<----b1
  302. test_segment_ratio<P>("n3b",
  303. 7, 0, 2, 0,
  304. 7, 0, 5, 0,
  305. std::make_pair(0, 1), std::make_pair(2, 5),
  306. std::make_pair(0, 1), std::make_pair(1, 1),
  307. 7, 0, 5, 0);
  308. // A inside B
  309. // a1--->a2
  310. // b1------------>b2
  311. test_segment_ratio<P>("rn4",
  312. 3, 0, 5, 0,
  313. 2, 0, 7, 0,
  314. std::make_pair(0, 1), std::make_pair(1, 1),
  315. std::make_pair(1, 5), std::make_pair(3, 5),
  316. 3, 0, 5, 0);
  317. // a2<---a1
  318. // b1------------>b2
  319. test_segment_ratio<P>("ro4",
  320. 5, 0, 3, 0,
  321. 2, 0, 7, 0,
  322. std::make_pair(0, 1), std::make_pair(1, 1),
  323. std::make_pair(3, 5), std::make_pair(1, 5),
  324. 5, 0, 3, 0);
  325. // a2<---a1
  326. // b2<------------b1
  327. test_segment_ratio<P>("ro4b",
  328. 5, 0, 3, 0,
  329. 7, 0, 2, 0,
  330. std::make_pair(0, 1), std::make_pair(1, 1),
  331. std::make_pair(2, 5), std::make_pair(4, 5),
  332. 5, 0, 3, 0);
  333. // a1--->a2
  334. // b2<------------b1
  335. test_segment_ratio<P>("ro4c",
  336. 3, 0, 5, 0,
  337. 7, 0, 2, 0,
  338. std::make_pair(0, 1), std::make_pair(1, 1),
  339. std::make_pair(4, 5), std::make_pair(2, 5),
  340. 3, 0, 5, 0);
  341. // B inside A, boundaries intersect
  342. // We change the coordinates a bit (w.r.t. n3 above) to have it asymmetrical
  343. // a1---------->a2
  344. // b1--->b2
  345. test_segment_ratio<P>("n3",
  346. 2, 0, 7, 0,
  347. 2, 0, 4, 0,
  348. std::make_pair(0, 1), std::make_pair(2, 5),
  349. std::make_pair(0, 1), std::make_pair(1, 1),
  350. 2, 0, 4, 0);
  351. // a1---------->a2
  352. // b2<---b1
  353. test_segment_ratio<P>("o3",
  354. 2, 0, 7, 0,
  355. 4, 0, 2, 0,
  356. std::make_pair(0, 1), std::make_pair(2, 5),
  357. std::make_pair(1, 1), std::make_pair(0, 1),
  358. 2, 0, 4, 0);
  359. // a1---------->a2
  360. // b1--->b2
  361. test_segment_ratio<P>("n5",
  362. 2, 0, 7, 0,
  363. 5, 0, 7, 0,
  364. std::make_pair(3, 5), std::make_pair(1, 1),
  365. std::make_pair(0, 1), std::make_pair(1, 1),
  366. 5, 0, 7, 0);
  367. // a1---------->a2
  368. // b2<---b1
  369. test_segment_ratio<P>("o5",
  370. 2, 0, 7, 0,
  371. 7, 0, 5, 0,
  372. std::make_pair(3, 5), std::make_pair(1, 1),
  373. std::make_pair(1, 1), std::make_pair(0, 1),
  374. 5, 0, 7, 0);
  375. // Generic (overlaps)
  376. // a1---------->a2
  377. // b1----->b2
  378. test_segment_ratio<P>("n2",
  379. 2, 0, 7, 0,
  380. 1, 0, 4, 0,
  381. std::make_pair(0, 1), std::make_pair(2, 5),
  382. std::make_pair(1, 3), std::make_pair(1, 1),
  383. 2, 0, 4, 0);
  384. // Same, b reversed
  385. test_segment_ratio<P>("n2_b",
  386. 2, 0, 7, 0,
  387. 4, 0, 1, 0,
  388. std::make_pair(0, 1), std::make_pair(2, 5),
  389. std::make_pair(2, 3), std::make_pair(0, 1),
  390. 2, 0, 4, 0);
  391. // Same, both reversed
  392. test_segment_ratio<P>("n2_c",
  393. 7, 0, 2, 0,
  394. 4, 0, 1, 0,
  395. std::make_pair(3, 5), std::make_pair(1, 1),
  396. std::make_pair(0, 1), std::make_pair(2, 3),
  397. 4, 0, 2, 0);
  398. // a1---------->a2
  399. // b1----->b2
  400. test_segment_ratio<P>("n6",
  401. 2, 0, 6, 0,
  402. 5, 0, 8, 0,
  403. std::make_pair(3, 4), std::make_pair(1, 1),
  404. std::make_pair(0, 1), std::make_pair(1, 3),
  405. 5, 0, 6, 0);
  406. // Degenerated one
  407. // a1---------->a2
  408. // b1/b2
  409. const int ignored = 99;
  410. test_segment_ratio<P>("degenerated1",
  411. 2, 0, 6, 0,
  412. 5, 0, 5, 0,
  413. std::make_pair(3, 4), // IP located on 3/4 w.r.t A
  414. std::make_pair(ignored, 1), // not checked
  415. std::make_pair(0, 1), // IP located at any place w.r.t B, so 0
  416. std::make_pair(ignored, 1), // not checked
  417. 5, 0,
  418. ignored, ignored,
  419. 1);
  420. test_segment_ratio<P>("degenerated2",
  421. 5, 0, 5, 0,
  422. 2, 0, 6, 0,
  423. std::make_pair(0, 1), std::make_pair(ignored, 1),
  424. std::make_pair(3, 4), std::make_pair(ignored, 1),
  425. 5, 0,
  426. ignored, ignored,
  427. 1);
  428. // Vertical one like in box_poly5 but in integer
  429. test_segment_ratio<P>("box_poly5",
  430. 45, 25, 45, 15,
  431. 45, 22, 45, 19,
  432. std::make_pair(3, 10), std::make_pair(6, 10),
  433. std::make_pair(0, 1), std::make_pair(1, 1),
  434. 45, 22, 45, 19);
  435. }
  436. int test_main(int, char* [])
  437. {
  438. // We don't rescale but use integer points as, by nature, robust points
  439. test_all<bg::model::point<int, 2, bg::cs::cartesian> >();
  440. test_ratios<bg::model::point<int, 2, bg::cs::cartesian> >();
  441. return 0;
  442. }