disjoint_coverage_a_a.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. // areal-areal geometries
  58. template <typename P>
  59. inline void test_box_box()
  60. {
  61. typedef bg::model::box<P> B;
  62. typedef test_disjoint tester;
  63. tester::apply("b-b-01",
  64. from_wkt<B>("BOX(2 2,3 3)"),
  65. from_wkt<B>("BOX(0 0,2 2)"),
  66. false);
  67. tester::apply("b-b-02",
  68. from_wkt<B>("BOX(1 1,3 3)"),
  69. from_wkt<B>("BOX(0 0,2 2)"),
  70. false);
  71. tester::apply("b-b-03",
  72. from_wkt<B>("BOX(3 3,4 4)"),
  73. from_wkt<B>("BOX(0 0,2 2)"),
  74. true);
  75. }
  76. template <typename P>
  77. inline void test_ring_box()
  78. {
  79. typedef bg::model::box<P> B;
  80. typedef bg::model::ring<P, false, false> R; // ccw, open
  81. typedef test_disjoint tester;
  82. tester::apply("r-b-01",
  83. from_wkt<B>("BOX(2 2,3 3)"),
  84. from_wkt<R>("POLYGON((0 0,2 0,2 2,0 2))"),
  85. false);
  86. tester::apply("r-b-02",
  87. from_wkt<B>("BOX(1 1,3 3)"),
  88. from_wkt<R>("POLYGON((0 0,2 0,2 2,0 2))"),
  89. false);
  90. tester::apply("r-b-03",
  91. from_wkt<B>("BOX(3 3,4 4)"),
  92. from_wkt<R>("POLYGON((0 0,2 0,2 2,0 2))"),
  93. true);
  94. }
  95. template <typename P>
  96. inline void test_polygon_box()
  97. {
  98. typedef bg::model::box<P> B;
  99. typedef bg::model::polygon<P, false, false> PL; // ccw, open
  100. typedef test_disjoint tester;
  101. tester::apply("pg-b-01",
  102. from_wkt<B>("BOX(2 2,3 3)"),
  103. from_wkt<PL>("POLYGON((0 0,2 0,2 2,0 2))"),
  104. false);
  105. tester::apply("pg-b-02",
  106. from_wkt<B>("BOX(1 1,3 3)"),
  107. from_wkt<PL>("POLYGON((0 0,2 0,2 2,0 2))"),
  108. false);
  109. tester::apply("pg-b-03",
  110. from_wkt<B>("BOX(3 3,4 4)"),
  111. from_wkt<PL>("POLYGON((0 0,2 0,2 2,0 2))"),
  112. true);
  113. }
  114. template <typename P>
  115. inline void test_multipolygon_box()
  116. {
  117. typedef bg::model::box<P> B;
  118. typedef bg::model::polygon<P, false, false> PL; // ccw, open
  119. typedef bg::model::multi_polygon<PL> MPL;
  120. typedef test_disjoint tester;
  121. tester::apply("mpg-b-01",
  122. from_wkt<B>("BOX(2 2,3 3)"),
  123. from_wkt<MPL>("MULTIPOLYGON(((0 0,2 0,2 2,0 2)))"),
  124. false);
  125. tester::apply("mpg-b-02",
  126. from_wkt<B>("BOX(1 1,3 3)"),
  127. from_wkt<MPL>("MULTIPOLYGON(((0 0,2 0,2 2,0 2)))"),
  128. false);
  129. tester::apply("mpg-b-03",
  130. from_wkt<B>("BOX(3 3,4 4)"),
  131. from_wkt<MPL>("MULTIPOLYGON(((0 0,2 0,2 2,0 2)))"),
  132. true);
  133. }
  134. template <typename P>
  135. inline void test_ring_ring()
  136. {
  137. typedef bg::model::ring<P, false, false> R; // ccw, open
  138. typedef test_disjoint tester;
  139. tester::apply("r-r-01",
  140. from_wkt<R>("POLYGON((2 2,2 3,3 3,3 2))"),
  141. from_wkt<R>("POLYGON((0 0,2 0,2 2,0 2))"),
  142. false);
  143. tester::apply("r-r-02",
  144. from_wkt<R>("POLYGON((1 1,1 3,3 3,3 1))"),
  145. from_wkt<R>("POLYGON((0 0,2 0,2 2,0 2))"),
  146. false);
  147. tester::apply("r-r-03",
  148. from_wkt<R>("POLYGON((3 3,3 4,4 4,4 3))"),
  149. from_wkt<R>("POLYGON((0 0,2 0,2 2,0 2))"),
  150. true);
  151. }
  152. template <typename P>
  153. inline void test_polygon_ring()
  154. {
  155. typedef bg::model::ring<P, false, false> R; // ccw, open
  156. typedef bg::model::polygon<P, false, false> PL; // ccw, open
  157. typedef test_disjoint tester;
  158. tester::apply("pg-r-01",
  159. from_wkt<R>("POLYGON((2 2,2 3,3 3,3 2))"),
  160. from_wkt<PL>("POLYGON((0 0,2 0,2 2,0 2))"),
  161. false);
  162. tester::apply("pg-r-02",
  163. from_wkt<R>("POLYGON((1 1,1 3,3 3,3 1))"),
  164. from_wkt<PL>("POLYGON((0 0,2 0,2 2,0 2))"),
  165. false);
  166. tester::apply("pg-r-03",
  167. from_wkt<R>("POLYGON((3 3,3 4,4 4,4 3))"),
  168. from_wkt<PL>("POLYGON((0 0,2 0,2 2,0 2))"),
  169. true);
  170. }
  171. template <typename P>
  172. inline void test_multipolygon_ring()
  173. {
  174. typedef bg::model::ring<P, false, false> R; // ccw, open
  175. typedef bg::model::polygon<P, false, false> PL; // ccw, open
  176. typedef bg::model::multi_polygon<PL> MPL;
  177. typedef test_disjoint tester;
  178. tester::apply("mpg-r-01",
  179. from_wkt<R>("POLYGON((2 2,2 3,3 3,3 2))"),
  180. from_wkt<MPL>("MULTIPOLYGON(((0 0,2 0,2 2,0 2)))"),
  181. false);
  182. tester::apply("mpg-r-02",
  183. from_wkt<R>("POLYGON((1 1,1 3,3 3,3 1))"),
  184. from_wkt<MPL>("MULTIPOLYGON(((0 0,2 0,2 2,0 2)))"),
  185. false);
  186. tester::apply("mpg-r-03",
  187. from_wkt<R>("POLYGON((3 3,3 4,4 4,4 3))"),
  188. from_wkt<MPL>("MULTIPOLYGON(((0 0,2 0,2 2,0 2)))"),
  189. true);
  190. }
  191. template <typename P>
  192. inline void test_polygon_polygon()
  193. {
  194. typedef bg::model::polygon<P, false, false> PL; // ccw, open
  195. typedef test_disjoint tester;
  196. tester::apply("pg-pg-01",
  197. from_wkt<PL>("POLYGON((2 2,2 3,3 3,3 2))"),
  198. from_wkt<PL>("POLYGON((0 0,2 0,2 2,0 2))"),
  199. false);
  200. tester::apply("pg-pg-02",
  201. from_wkt<PL>("POLYGON((1 1,1 3,3 3,3 1))"),
  202. from_wkt<PL>("POLYGON((0 0,2 0,2 2,0 2))"),
  203. false);
  204. tester::apply("pg-pg-03",
  205. from_wkt<PL>("POLYGON((3 3,3 4,4 4,4 3))"),
  206. from_wkt<PL>("POLYGON((0 0,2 0,2 2,0 2))"),
  207. true);
  208. tester::apply("pg-pg-04",
  209. from_wkt<PL>("POLYGON((0 0,9 0,9 9,0 9))"),
  210. from_wkt<PL>("POLYGON((3 3,6 3,6 6,3 6))"),
  211. false);
  212. // polygon with a hole which entirely contains the other polygon
  213. tester::apply("pg-pg-05",
  214. from_wkt<PL>("POLYGON((0 0,9 0,9 9,0 9),(2 2,2 7,7 7,7 2))"),
  215. from_wkt<PL>("POLYGON((3 3,6 3,6 6,3 6))"),
  216. true);
  217. // polygon with a hole, but the inner ring intersects the other polygon
  218. tester::apply("pg-pg-06",
  219. from_wkt<PL>("POLYGON((0 0,9 0,9 9,0 9),(3 2,3 7,7 7,7 2))"),
  220. from_wkt<PL>("POLYGON((2 3,6 3,6 6,2 6))"),
  221. false);
  222. // polygon with a hole, but the other polygon is entirely contained
  223. // between the inner and outer rings.
  224. tester::apply("pg-pg-07",
  225. from_wkt<PL>("POLYGON((0 0,9 0,9 9,0 9),(6 2,6 7,7 7,7 2))"),
  226. from_wkt<PL>("POLYGON((3 3,5 3,5 6,3 6))"),
  227. false);
  228. // polygon with a hole and the outer ring of the other polygon lies
  229. // between the inner and outer, but without touching either.
  230. tester::apply("pg-pg-08",
  231. from_wkt<PL>("POLYGON((0 0,9 0,9 9,0 9),(3 3,3 6,6 6,6 3))"),
  232. from_wkt<PL>("POLYGON((2 2,7 2,7 7,2 7))"),
  233. false);
  234. {
  235. typedef bg::model::polygon<P> PL; // cw, closed
  236. // https://svn.boost.org/trac/boost/ticket/10647
  237. tester::apply("ticket-10647",
  238. from_wkt<PL>("POLYGON((0 0, 0 5, 5 5, 5 0, 0 0)(1 1, 4 1, 4 4, 1 4, 1 1))"),
  239. from_wkt<PL>("POLYGON((2 2, 2 3, 3 3, 3 2, 2 2))"),
  240. true);
  241. }
  242. }
  243. template <typename P>
  244. inline void test_polygon_multipolygon()
  245. {
  246. typedef bg::model::polygon<P, false, false> PL; // ccw, open
  247. typedef bg::model::multi_polygon<PL> MPL;
  248. typedef test_disjoint tester;
  249. tester::apply("pg-mpg-01",
  250. from_wkt<PL>("POLYGON((2 2,2 3,3 3,3 2))"),
  251. from_wkt<MPL>("MULTIPOLYGON(((0 0,2 0,2 2,0 2)))"),
  252. false);
  253. tester::apply("pg-mpg-02",
  254. from_wkt<PL>("POLYGON((1 1,1 3,3 3,3 1))"),
  255. from_wkt<MPL>("MULTIPOLYGON(((0 0,2 0,2 2,0 2)))"),
  256. false);
  257. tester::apply("pg-mpg-03",
  258. from_wkt<PL>("POLYGON((3 3,3 4,4 4,4 3))"),
  259. from_wkt<MPL>("MULTIPOLYGON(((0 0,2 0,2 2,0 2)))"),
  260. true);
  261. }
  262. template <typename P>
  263. inline void test_multipolygon_multipolygon()
  264. {
  265. typedef bg::model::polygon<P, false, false> PL; // ccw, open
  266. typedef bg::model::multi_polygon<PL> MPL;
  267. typedef test_disjoint tester;
  268. tester::apply("mpg-mpg-01",
  269. from_wkt<MPL>("MULTIPOLYGON(((2 2,2 3,3 3,3 2)))"),
  270. from_wkt<MPL>("MULTIPOLYGON(((0 0,2 0,2 2,0 2)))"),
  271. false);
  272. tester::apply("mpg-mpg-02",
  273. from_wkt<MPL>("MULTIPOLYGON(((1 1,1 3,3 3,3 1)))"),
  274. from_wkt<MPL>("MULTIPOLYGON(((0 0,2 0,2 2,0 2)))"),
  275. false);
  276. tester::apply("mpg-mpg-03",
  277. from_wkt<MPL>("MULTIPOLYGON(((3 3,3 4,4 4,4 3)))"),
  278. from_wkt<MPL>("MULTIPOLYGON(((0 0,2 0,2 2,0 2)))"),
  279. true);
  280. }
  281. //============================================================================
  282. template <typename CoordinateType>
  283. inline void test_areal_areal()
  284. {
  285. typedef bg::model::point<CoordinateType, 2, bg::cs::cartesian> point_type;
  286. test_polygon_polygon<point_type>();
  287. test_polygon_multipolygon<point_type>();
  288. test_polygon_ring<point_type>();
  289. test_polygon_box<point_type>();
  290. test_multipolygon_multipolygon<point_type>();
  291. test_multipolygon_ring<point_type>();
  292. test_multipolygon_box<point_type>();
  293. test_ring_ring<point_type>();
  294. test_ring_box<point_type>();
  295. test_box_box<point_type>();
  296. }
  297. //============================================================================
  298. BOOST_AUTO_TEST_CASE( test_areal_areal_all )
  299. {
  300. test_areal_areal<double>();
  301. test_areal_areal<int>();
  302. #ifdef HAVE_TTMATH
  303. test_areal_areal<ttmath_big>();
  304. #endif
  305. }