disjoint_coverage_l_a.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2014-2015, Oracle and/or its affiliates.
  3. // Licensed under the Boost Software License version 1.0.
  4. // http://www.boost.org/users/license.html
  5. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  6. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  7. #ifndef BOOST_TEST_MODULE
  8. #define BOOST_TEST_MODULE test_disjoint_coverage
  9. #endif
  10. // unit test to test disjoint for all geometry combinations
  11. #include <iostream>
  12. #include <boost/test/included/unit_test.hpp>
  13. #include <boost/geometry/core/tag.hpp>
  14. #include <boost/geometry/core/tags.hpp>
  15. #include <boost/geometry/strategies/strategies.hpp>
  16. #include <boost/geometry/io/wkt/wkt.hpp>
  17. #include <boost/geometry/io/dsv/write.hpp>
  18. #include <boost/geometry/geometries/geometries.hpp>
  19. #include <boost/geometry/algorithms/disjoint.hpp>
  20. #include <from_wkt.hpp>
  21. #ifdef HAVE_TTMATH
  22. #include <boost/geometry/extensions/contrib/ttmath_stub.hpp>
  23. #endif
  24. namespace bg = ::boost::geometry;
  25. //============================================================================
  26. struct test_disjoint
  27. {
  28. template <typename Geometry1, typename Geometry2>
  29. static inline void apply(std::string const& case_id,
  30. Geometry1 const& geometry1,
  31. Geometry2 const& geometry2,
  32. bool expected_result)
  33. {
  34. bool result = bg::disjoint(geometry1, geometry2);
  35. BOOST_CHECK_MESSAGE(result == expected_result,
  36. "case ID: " << case_id << ", G1: " << bg::wkt(geometry1)
  37. << ", G2: " << bg::wkt(geometry2) << " -> Expected: "
  38. << expected_result << ", detected: " << result);
  39. result = bg::disjoint(geometry2, geometry1);
  40. BOOST_CHECK_MESSAGE(result == expected_result,
  41. "case ID: " << case_id << ", G1: " << bg::wkt(geometry2)
  42. << ", G2: " << bg::wkt(geometry1) << " -> Expected: "
  43. << expected_result << ", detected: " << result);
  44. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  45. std::cout << "case ID: " << case_id << "; G1 - G2: ";
  46. std::cout << bg::wkt(geometry1) << " - ";
  47. std::cout << bg::wkt(geometry2) << std::endl;
  48. std::cout << std::boolalpha;
  49. std::cout << "expected/computed result: "
  50. << expected_result << " / " << result << std::endl;
  51. std::cout << std::endl;
  52. std::cout << std::noboolalpha;
  53. #endif
  54. }
  55. };
  56. //============================================================================
  57. // linear-areal geometries
  58. template <typename P>
  59. inline void test_segment_box()
  60. {
  61. typedef bg::model::segment<P> S;
  62. typedef bg::model::box<P> B;
  63. typedef test_disjoint tester;
  64. tester::apply("s-b-01",
  65. from_wkt<S>("SEGMENT(0 0,2 0)"),
  66. from_wkt<B>("BOX(0 0,2 2)"),
  67. false);
  68. tester::apply("s-b-02",
  69. from_wkt<S>("SEGMENT(1 1,3 3)"),
  70. from_wkt<B>("BOX(0 0,2 2)"),
  71. false);
  72. tester::apply("s-b-03",
  73. from_wkt<S>("SEGMENT(2 2,3 3)"),
  74. from_wkt<B>("BOX(0 0,2 2)"),
  75. false);
  76. tester::apply("s-b-04",
  77. from_wkt<S>("SEGMENT(4 4,3 3)"),
  78. from_wkt<B>("BOX(0 0,2 2)"),
  79. true);
  80. tester::apply("s-b-05",
  81. from_wkt<S>("SEGMENT(0 4,4 4)"),
  82. from_wkt<B>("BOX(0 0,2 2)"),
  83. true);
  84. tester::apply("s-b-06",
  85. from_wkt<S>("SEGMENT(4 0,4 4)"),
  86. from_wkt<B>("BOX(0 0,2 2)"),
  87. true);
  88. tester::apply("s-b-07",
  89. from_wkt<S>("SEGMENT(0 -2,0 -1)"),
  90. from_wkt<B>("BOX(0 0,1 1)"),
  91. true);
  92. tester::apply("s-b-08",
  93. from_wkt<S>("SEGMENT(-2 -2,-2 -1)"),
  94. from_wkt<B>("BOX(0 0,1 1)"),
  95. true);
  96. tester::apply("s-b-09",
  97. from_wkt<S>("SEGMENT(-2 -2,-2 -2)"),
  98. from_wkt<B>("BOX(0 0,1 1)"),
  99. true);
  100. tester::apply("s-b-10",
  101. from_wkt<S>("SEGMENT(-2 0,-2 0)"),
  102. from_wkt<B>("BOX(0 0,1 1)"),
  103. true);
  104. tester::apply("s-b-11",
  105. from_wkt<S>("SEGMENT(0 -2,0 -2)"),
  106. from_wkt<B>("BOX(0 0,1 1)"),
  107. true);
  108. tester::apply("s-b-12",
  109. from_wkt<S>("SEGMENT(-2 0,-1 0)"),
  110. from_wkt<B>("BOX(0 0,1 1)"),
  111. true);
  112. // segment degenerates to a point
  113. tester::apply("s-b-13",
  114. from_wkt<S>("SEGMENT(0 0,0 0)"),
  115. from_wkt<B>("BOX(0 0,1 1)"),
  116. false);
  117. tester::apply("s-b-14",
  118. from_wkt<S>("SEGMENT(1 1,1 1)"),
  119. from_wkt<B>("BOX(0 0,2 2)"),
  120. false);
  121. tester::apply("s-b-15",
  122. from_wkt<S>("SEGMENT(2 2,2 2)"),
  123. from_wkt<B>("BOX(0 0,2 2)"),
  124. false);
  125. tester::apply("s-b-16",
  126. from_wkt<S>("SEGMENT(2 0,2 0)"),
  127. from_wkt<B>("BOX(0 0,2 2)"),
  128. false);
  129. tester::apply("s-b-17",
  130. from_wkt<S>("SEGMENT(0 2,0 2)"),
  131. from_wkt<B>("BOX(0 0,2 2)"),
  132. false);
  133. tester::apply("s-b-18",
  134. from_wkt<S>("SEGMENT(2 2,2 2)"),
  135. from_wkt<B>("BOX(0 0,1 1)"),
  136. true);
  137. }
  138. template <typename P>
  139. inline void test_segment_ring()
  140. {
  141. typedef bg::model::segment<P> S;
  142. typedef bg::model::ring<P, false, false> R; // ccw, open
  143. typedef test_disjoint tester;
  144. tester::apply("s-r-01",
  145. from_wkt<S>("SEGMENT(0 0,2 0)"),
  146. from_wkt<R>("POLYGON((0 0,2 0,0 2))"),
  147. false);
  148. tester::apply("s-r-02",
  149. from_wkt<S>("SEGMENT(1 0,3 3)"),
  150. from_wkt<R>("POLYGON((0 0,2 0,0 2))"),
  151. false);
  152. tester::apply("s-r-03",
  153. from_wkt<S>("SEGMENT(1 1,3 3)"),
  154. from_wkt<R>("POLYGON((0 0,2 0,0 2))"),
  155. false);
  156. tester::apply("s-r-04",
  157. from_wkt<S>("SEGMENT(2 2,3 3)"),
  158. from_wkt<R>("POLYGON((0 0,2 0,0 2))"),
  159. true);
  160. }
  161. template <typename P>
  162. inline void test_segment_polygon()
  163. {
  164. typedef bg::model::segment<P> S;
  165. typedef bg::model::polygon<P, false, false> PL; // ccw, open
  166. typedef test_disjoint tester;
  167. tester::apply("s-pg-01",
  168. from_wkt<S>("SEGMENT(0 0,2 0)"),
  169. from_wkt<PL>("POLYGON((0 0,2 0,0 2))"),
  170. false);
  171. tester::apply("s-pg-02",
  172. from_wkt<S>("SEGMENT(1 0,3 3)"),
  173. from_wkt<PL>("POLYGON((0 0,2 0,0 2))"),
  174. false);
  175. tester::apply("s-pg-03",
  176. from_wkt<S>("SEGMENT(1 1,3 3)"),
  177. from_wkt<PL>("POLYGON((0 0,2 0,0 2))"),
  178. false);
  179. tester::apply("s-pg-04",
  180. from_wkt<S>("SEGMENT(2 2,3 3)"),
  181. from_wkt<PL>("POLYGON((0 0,2 0,0 2))"),
  182. true);
  183. }
  184. template <typename P>
  185. inline void test_segment_multipolygon()
  186. {
  187. typedef bg::model::segment<P> S;
  188. typedef bg::model::polygon<P, false, false> PL; // ccw, open
  189. typedef bg::model::multi_polygon<PL> MPL;
  190. typedef test_disjoint tester;
  191. tester::apply("s-mpg-01",
  192. from_wkt<S>("SEGMENT(0 0,2 0)"),
  193. from_wkt<MPL>("MULTIPOLYGON(((0 0,2 0,0 2)))"),
  194. false);
  195. tester::apply("s-mpg-02",
  196. from_wkt<S>("SEGMENT(1 0,3 3)"),
  197. from_wkt<MPL>("MULTIPOLYGON(((0 0,2 0,0 2)))"),
  198. false);
  199. tester::apply("s-mpg-03",
  200. from_wkt<S>("SEGMENT(1 1,3 3)"),
  201. from_wkt<MPL>("MULTIPOLYGON(((0 0,2 0,0 2)))"),
  202. false);
  203. tester::apply("s-mpg-04",
  204. from_wkt<S>("SEGMENT(2 2,3 3)"),
  205. from_wkt<MPL>("MULTIPOLYGON(((0 0,2 0,0 2)))"),
  206. true);
  207. }
  208. template <typename P>
  209. inline void test_linestring_box()
  210. {
  211. typedef bg::model::linestring<P> L;
  212. typedef bg::model::box<P> B;
  213. typedef test_disjoint tester;
  214. tester::apply("l-b-01",
  215. from_wkt<L>("LINESTRING(0 0,2 0)"),
  216. from_wkt<B>("BOX(0 0,2 2)"),
  217. false);
  218. tester::apply("l-b-02",
  219. from_wkt<L>("LINESTRING(1 1,3 3)"),
  220. from_wkt<B>("BOX(0 0,2 2)"),
  221. false);
  222. tester::apply("l-b-03",
  223. from_wkt<L>("LINESTRING(2 2,3 3)"),
  224. from_wkt<B>("BOX(0 0,2 2)"),
  225. false);
  226. tester::apply("l-b-04",
  227. from_wkt<L>("LINESTRING(4 4,3 3)"),
  228. from_wkt<B>("BOX(0 0,2 2)"),
  229. true);
  230. }
  231. template <typename P>
  232. inline void test_linestring_ring()
  233. {
  234. typedef bg::model::linestring<P> L;
  235. typedef bg::model::ring<P, false, false> R; // ccw, open
  236. typedef test_disjoint tester;
  237. tester::apply("l-r-01",
  238. from_wkt<L>("LINESTRING(0 0,2 0)"),
  239. from_wkt<R>("POLYGON((0 0,2 0,0 2))"),
  240. false);
  241. tester::apply("l-r-02",
  242. from_wkt<L>("LINESTRING(1 0,3 3)"),
  243. from_wkt<R>("POLYGON((0 0,2 0,0 2))"),
  244. false);
  245. tester::apply("l-r-03",
  246. from_wkt<L>("LINESTRING(1 1,3 3)"),
  247. from_wkt<R>("POLYGON((0 0,2 0,0 2))"),
  248. false);
  249. tester::apply("l-r-04",
  250. from_wkt<L>("LINESTRING(2 2,3 3)"),
  251. from_wkt<R>("POLYGON((0 0,2 0,0 2))"),
  252. true);
  253. }
  254. template <typename P>
  255. inline void test_linestring_polygon()
  256. {
  257. typedef bg::model::linestring<P> L;
  258. typedef bg::model::polygon<P, false, false> PL; // ccw, open
  259. typedef test_disjoint tester;
  260. tester::apply("l-pg-01",
  261. from_wkt<L>("LINESTRING(0 0,2 0)"),
  262. from_wkt<PL>("POLYGON((0 0,2 0,0 2))"),
  263. false);
  264. tester::apply("l-pg-02",
  265. from_wkt<L>("LINESTRING(1 0,3 3)"),
  266. from_wkt<PL>("POLYGON((0 0,2 0,0 2))"),
  267. false);
  268. tester::apply("l-pg-03",
  269. from_wkt<L>("LINESTRING(1 1,3 3)"),
  270. from_wkt<PL>("POLYGON((0 0,2 0,0 2))"),
  271. false);
  272. tester::apply("l-pg-04",
  273. from_wkt<L>("LINESTRING(2 2,3 3)"),
  274. from_wkt<PL>("POLYGON((0 0,2 0,0 2))"),
  275. true);
  276. }
  277. template <typename P>
  278. inline void test_linestring_multipolygon()
  279. {
  280. typedef bg::model::linestring<P> L;
  281. typedef bg::model::polygon<P, false, false> PL; // ccw, open
  282. typedef bg::model::multi_polygon<PL> MPL;
  283. typedef test_disjoint tester;
  284. tester::apply("l-mpg-01",
  285. from_wkt<L>("LINESTRING(0 0,2 0)"),
  286. from_wkt<MPL>("MULTIPOLYGON(((0 0,2 0,0 2)))"),
  287. false);
  288. tester::apply("l-mpg-02",
  289. from_wkt<L>("LINESTRING(1 0,3 3)"),
  290. from_wkt<MPL>("MULTIPOLYGON(((0 0,2 0,0 2)))"),
  291. false);
  292. tester::apply("l-mpg-03",
  293. from_wkt<L>("LINESTRING(1 1,3 3)"),
  294. from_wkt<MPL>("MULTIPOLYGON(((0 0,2 0,0 2)))"),
  295. false);
  296. tester::apply("l-mpg-04",
  297. from_wkt<L>("LINESTRING(2 2,3 3)"),
  298. from_wkt<MPL>("MULTIPOLYGON(((0 0,2 0,0 2)))"),
  299. true);
  300. }
  301. template <typename P>
  302. inline void test_multilinestring_box()
  303. {
  304. typedef bg::model::linestring<P> L;
  305. typedef bg::model::multi_linestring<L> ML;
  306. typedef bg::model::box<P> B;
  307. typedef test_disjoint tester;
  308. tester::apply("ml-b-01",
  309. from_wkt<ML>("MULTILINESTRING((0 0,2 0))"),
  310. from_wkt<B>("BOX(0 0,2 2)"),
  311. false);
  312. tester::apply("ml-b-02",
  313. from_wkt<ML>("MULTILINESTRING((1 1,3 3))"),
  314. from_wkt<B>("BOX(0 0,2 2)"),
  315. false);
  316. tester::apply("ml-b-03",
  317. from_wkt<ML>("MULTILINESTRING((2 2,3 3))"),
  318. from_wkt<B>("BOX(0 0,2 2)"),
  319. false);
  320. tester::apply("ml-b-04",
  321. from_wkt<ML>("MULTILINESTRING((4 4,3 3))"),
  322. from_wkt<B>("BOX(0 0,2 2)"),
  323. true);
  324. }
  325. template <typename P>
  326. inline void test_multilinestring_ring()
  327. {
  328. typedef bg::model::linestring<P> L;
  329. typedef bg::model::multi_linestring<L> ML;
  330. typedef bg::model::ring<P, false, false> R; // ccw, open
  331. typedef test_disjoint tester;
  332. tester::apply("ml-r-01",
  333. from_wkt<ML>("MULTILINESTRING((0 0,2 0))"),
  334. from_wkt<R>("POLYGON((0 0,2 0,0 2))"),
  335. false);
  336. tester::apply("ml-r-02",
  337. from_wkt<ML>("MULTILINESTRING((1 0,3 3))"),
  338. from_wkt<R>("POLYGON((0 0,2 0,0 2))"),
  339. false);
  340. tester::apply("ml-r-03",
  341. from_wkt<ML>("MULTILINESTRING((1 1,3 3))"),
  342. from_wkt<R>("POLYGON((0 0,2 0,0 2))"),
  343. false);
  344. tester::apply("ml-r-04",
  345. from_wkt<ML>("MULTILINESTRING((2 2,3 3))"),
  346. from_wkt<R>("POLYGON((0 0,2 0,0 2))"),
  347. true);
  348. }
  349. template <typename P>
  350. inline void test_multilinestring_polygon()
  351. {
  352. typedef bg::model::linestring<P> L;
  353. typedef bg::model::multi_linestring<L> ML;
  354. typedef bg::model::polygon<P, false, false> PL; // ccw, open
  355. typedef test_disjoint tester;
  356. tester::apply("ml-pg-01",
  357. from_wkt<ML>("MULTILINESTRING((0 0,2 0))"),
  358. from_wkt<PL>("POLYGON((0 0,2 0,0 2))"),
  359. false);
  360. tester::apply("ml-pg-02",
  361. from_wkt<ML>("MULTILINESTRING((1 0,3 3))"),
  362. from_wkt<PL>("POLYGON((0 0,2 0,0 2))"),
  363. false);
  364. tester::apply("ml-pg-03",
  365. from_wkt<ML>("MULTILINESTRING((1 1,3 3))"),
  366. from_wkt<PL>("POLYGON((0 0,2 0,0 2))"),
  367. false);
  368. tester::apply("ml-pg-04",
  369. from_wkt<ML>("MULTILINESTRING((2 2,3 3))"),
  370. from_wkt<PL>("POLYGON((0 0,2 0,0 2))"),
  371. true);
  372. }
  373. template <typename P>
  374. inline void test_multilinestring_multipolygon()
  375. {
  376. typedef bg::model::linestring<P> L;
  377. typedef bg::model::multi_linestring<L> ML;
  378. typedef bg::model::polygon<P, false, false> PL; // ccw, open
  379. typedef bg::model::multi_polygon<PL> MPL;
  380. typedef test_disjoint tester;
  381. tester::apply("ml-mpg-01",
  382. from_wkt<ML>("MULTILINESTRING((0 0,2 0))"),
  383. from_wkt<MPL>("MULTIPOLYGON(((0 0,2 0,0 2)))"),
  384. false);
  385. tester::apply("ml-mpg-02",
  386. from_wkt<ML>("MULTILINESTRING((1 0,3 3))"),
  387. from_wkt<MPL>("MULTIPOLYGON(((0 0,2 0,0 2)))"),
  388. false);
  389. tester::apply("ml-mpg-03",
  390. from_wkt<ML>("MULTILINESTRING((1 1,3 3))"),
  391. from_wkt<MPL>("MULTIPOLYGON(((0 0,2 0,0 2)))"),
  392. false);
  393. tester::apply("ml-mpg-04",
  394. from_wkt<ML>("MULTILINESTRING((2 2,3 3))"),
  395. from_wkt<MPL>("MULTIPOLYGON(((0 0,2 0,0 2)))"),
  396. true);
  397. }
  398. //============================================================================
  399. template <typename CoordinateType>
  400. inline void test_linear_areal()
  401. {
  402. typedef bg::model::point<CoordinateType, 2, bg::cs::cartesian> point_type;
  403. test_segment_polygon<point_type>();
  404. test_segment_multipolygon<point_type>();
  405. test_segment_ring<point_type>();
  406. test_segment_box<point_type>();
  407. test_linestring_polygon<point_type>();
  408. test_linestring_multipolygon<point_type>();
  409. test_linestring_ring<point_type>();
  410. test_linestring_box<point_type>();
  411. test_multilinestring_polygon<point_type>();
  412. test_multilinestring_multipolygon<point_type>();
  413. test_multilinestring_ring<point_type>();
  414. test_multilinestring_box<point_type>();
  415. }
  416. //============================================================================
  417. BOOST_AUTO_TEST_CASE( test_linear_areal_all )
  418. {
  419. test_linear_areal<double>();
  420. test_linear_areal<int>();
  421. #ifdef HAVE_TTMATH
  422. test_linear_areal<ttmath_big>();
  423. #endif
  424. }