distance_ca_ar_ar.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2014-2018, Oracle and/or its affiliates.
  4. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  5. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  6. // Licensed under the Boost Software License version 1.0.
  7. // http://www.boost.org/users/license.html
  8. #include <iostream>
  9. #ifndef BOOST_TEST_MODULE
  10. #define BOOST_TEST_MODULE test_distance_cartesian_areal_areal
  11. #endif
  12. #include <boost/test/included/unit_test.hpp>
  13. #include "test_distance_common.hpp"
  14. typedef bg::model::point<int,2,bg::cs::cartesian> int_point_type;
  15. typedef bg::model::point<double,2,bg::cs::cartesian> point_type;
  16. typedef bg::model::polygon<point_type, false> polygon_type;
  17. typedef bg::model::multi_polygon<polygon_type> multi_polygon_type;
  18. typedef bg::model::ring<point_type, false> ring_type;
  19. typedef bg::model::box<int_point_type> int_box_type;
  20. typedef bg::model::box<point_type> box_type;
  21. namespace services = bg::strategy::distance::services;
  22. typedef bg::default_distance_result<point_type>::type return_type;
  23. typedef bg::strategy::distance::projected_point<> point_segment_strategy;
  24. typedef bg::strategy::distance::pythagoras_box_box<> box_box_strategy;
  25. //===========================================================================
  26. template <typename Strategy>
  27. void test_distance_polygon_polygon(Strategy const& strategy)
  28. {
  29. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  30. std::cout << std::endl;
  31. std::cout << "polygon/polygon distance tests" << std::endl;
  32. #endif
  33. typedef test_distance_of_geometries<polygon_type, polygon_type> tester;
  34. tester::apply("polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
  35. "polygon((-5 20,5 20,5 25,-5 25,-5 20))",
  36. 10, 100, strategy);
  37. tester::apply("polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
  38. "polygon((-5 20,-5 5,5 5,5 20,-5 20))",
  39. 0, 0, strategy);
  40. tester::apply("polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
  41. "polygon((-5 20,-5 -20,5 -20,5 20,-5 20))",
  42. 0, 0, strategy);
  43. tester::apply("polygon((-10 -10,10 -10,10 10,-10 10,-10 -10),\
  44. (-5 -5,-5 5,5 5,5 -5,-5 -5))",
  45. "polygon((-1 -1,0 0,-1 0,-1 -1))",
  46. 4, 16, strategy);
  47. }
  48. //===========================================================================
  49. template <typename Strategy>
  50. void test_distance_polygon_multipolygon(Strategy const& strategy)
  51. {
  52. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  53. std::cout << std::endl;
  54. std::cout << "polygon/multipolygon distance tests" << std::endl;
  55. #endif
  56. typedef test_distance_of_geometries
  57. <
  58. polygon_type, multi_polygon_type
  59. > tester;
  60. tester::apply("polygon((12 0,14 0,19 0,19.9 -1,12 0))",
  61. "multipolygon(((-10 -10,10 -10,10 10,-10 10,-10 -10)),\
  62. ((20 -1,21 2,30 -10,20 -1)))",
  63. 0.1, 0.01, strategy);
  64. tester::apply("polygon((19 0,19.9 -1,12 0,20.5 0.5,19 0))",
  65. "multipolygon(((-10 -10,10 -10,10 10,-10 10,-10 -10)),\
  66. ((20 -1,21 2,30 -10,20 -1)))",
  67. 0, 0, strategy);
  68. }
  69. //===========================================================================
  70. template <typename Strategy>
  71. void test_distance_polygon_ring(Strategy const& strategy)
  72. {
  73. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  74. std::cout << std::endl;
  75. std::cout << "polygon/ring distance tests" << std::endl;
  76. #endif
  77. typedef test_distance_of_geometries<polygon_type, ring_type> tester;
  78. tester::apply("polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
  79. "polygon((-5 20,5 20,5 25,-5 25,-5 20))",
  80. 10, 100, strategy);
  81. tester::apply("polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
  82. "polygon((-5 20,-5 5,5 5,5 20,-5 20))",
  83. 0, 0, strategy);
  84. tester::apply("polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
  85. "polygon((-5 20,-5 -20,5 -20,5 20,-5 20))",
  86. 0, 0, strategy);
  87. }
  88. //===========================================================================
  89. template <typename Strategy>
  90. void test_distance_multipolygon_multipolygon(Strategy const& strategy)
  91. {
  92. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  93. std::cout << std::endl;
  94. std::cout << "multipolygon/multipolygon distance tests" << std::endl;
  95. #endif
  96. typedef test_distance_of_geometries
  97. <
  98. multi_polygon_type, multi_polygon_type
  99. > tester;
  100. tester::apply("multipolygon(((12 0,14 0,14 1,12 0)),\
  101. ((18 0,19 0,19.9 -1,18 0)))",
  102. "multipolygon(((-10 -10,10 -10,10 10,-10 10,-10 -10)),\
  103. ((20 -1,21 2,30 -10,20 -1)))",
  104. 0.1, 0.01, strategy);
  105. tester::apply("multipolygon(((18 0,19 0,19.9 -1,18 0)),\
  106. ((12 0,14 0,20.5 0.5,12 0)))",
  107. "multipolygon(((-10 -10,10 -10,10 10,-10 10,-10 -10)),\
  108. ((20 -1,21 2,30 -10,20 -1)))",
  109. 0, 0, strategy);
  110. }
  111. //===========================================================================
  112. template <typename Strategy>
  113. void test_distance_multipolygon_ring(Strategy const& strategy)
  114. {
  115. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  116. std::cout << std::endl;
  117. std::cout << "multipolygon/ring distance tests" << std::endl;
  118. #endif
  119. typedef test_distance_of_geometries
  120. <
  121. multi_polygon_type, ring_type
  122. > tester;
  123. tester::apply("multipolygon(((-10 -10,10 -10,10 10,-10 10,-10 -10)),\
  124. ((20 -1,21 2,30 -10,20 -1)))",
  125. "polygon((12 0,14 0,19 0,19.9 -1,12 0))",
  126. 0.1, 0.01, strategy);
  127. tester::apply("multipolygon(((-10 -10,10 -10,10 10,-10 10,-10 -10)),\
  128. ((20 -1,21 2,30 -10,20 -1)))",
  129. "polygon((19 0,19.9 -1,12 0,20.5 0.5,19 0))",
  130. 0, 0, strategy);
  131. }
  132. //===========================================================================
  133. template <typename Strategy>
  134. void test_distance_ring_ring(Strategy const& strategy)
  135. {
  136. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  137. std::cout << std::endl;
  138. std::cout << "ring/ring distance tests" << std::endl;
  139. #endif
  140. typedef test_distance_of_geometries<ring_type, ring_type> tester;
  141. tester::apply("polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
  142. "polygon((-5 20,5 20,5 25,-5 25,-5 20))",
  143. 10, 100, strategy);
  144. tester::apply("polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
  145. "polygon((-5 20,-5 5,5 5,5 20,-5 20))",
  146. 0, 0, strategy);
  147. tester::apply("polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
  148. "polygon((-5 20,-5 -20,5 -20,5 20,-5 20))",
  149. 0, 0, strategy);
  150. }
  151. //===========================================================================
  152. template <typename Strategy>
  153. void test_distance_box_box(Strategy const& strategy)
  154. {
  155. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  156. std::cout << std::endl;
  157. std::cout << "box/box distance tests" << std::endl;
  158. #endif
  159. typedef test_distance_of_geometries<int_box_type, int_box_type> int_tester;
  160. typedef test_distance_of_geometries<box_type, box_type> tester;
  161. int_tester::apply("box(5 5,10 10)",
  162. "box(0 0,1 1)",
  163. sqrt(32.0), 32, strategy);
  164. tester::apply("box(5 5,10 10)",
  165. "box(0 0,1 1)",
  166. sqrt(32.0), 32, strategy);
  167. tester::apply("box(3 8,13 18)",
  168. "box(0 0,5 5)",
  169. 3, 9, strategy);
  170. tester::apply("box(5 5,10 10)",
  171. "box(0 0,5 5)",
  172. 0, 0, strategy);
  173. tester::apply("box(5 5,10 10)",
  174. "box(0 0,6 6)",
  175. 0, 0, strategy);
  176. tester::apply("box(3 5,13 15)",
  177. "box(0 0,5 5)",
  178. 0, 0, strategy);
  179. }
  180. //===========================================================================
  181. template <typename Strategy>
  182. void test_distance_polygon_box(Strategy const& strategy)
  183. {
  184. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  185. std::cout << std::endl;
  186. std::cout << "polygon/box distance tests" << std::endl;
  187. #endif
  188. typedef test_distance_of_geometries<polygon_type, box_type> tester;
  189. tester::apply("polygon((10 10,10 5,5 5,5 10,10 10))",
  190. "box(0 0,1 1)",
  191. sqrt(32.0), 32, strategy);
  192. tester::apply("polygon((10 10,10 5,5 5,5 10,10 10))",
  193. "box(0 0,5 5)",
  194. 0, 0, strategy);
  195. tester::apply("polygon((10 10,10 5,5 5,5 10,10 10))",
  196. "box(0 0,6 6)",
  197. 0, 0, strategy);
  198. tester::apply("polygon((10 10,15 5,10 0,5 5,10 10))",
  199. "box(5 0,7.5 2.5)",
  200. 0, 0, strategy);
  201. tester::apply("polygon((10 10,15 5,10 0,5 5,10 10))",
  202. "box(5 0,6 1)",
  203. sqrt(4.5), 4.5, strategy);
  204. }
  205. //===========================================================================
  206. template <typename Strategy>
  207. void test_distance_multipolygon_box(Strategy const& strategy)
  208. {
  209. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  210. std::cout << std::endl;
  211. std::cout << "multipolygon/box distance tests" << std::endl;
  212. #endif
  213. typedef test_distance_of_geometries<multi_polygon_type, box_type> tester;
  214. tester::apply("multipolygon(((-10 -10,-10 -9,-9 -9,-9 -10,-10 -10)),\
  215. ((2 2,2 3,3 3,3 2,2 2)))",
  216. "box(0 0,1 1)",
  217. sqrt(2.0), 2, strategy);
  218. tester::apply("multipolygon(((-10 -10,-10 -9,-9 -9,-9 -10,-10 -10)),\
  219. ((2 2,2 3,3 3,3 2,2 2)))",
  220. "box(0 0,2 2)",
  221. 0, 0, strategy);
  222. tester::apply("multipolygon(((-10 -10,-10 -9,-9 -9,-9 -10,-10 -10)),\
  223. ((2 2,2 3,3 3,3 2,2 2)))",
  224. "box(0 0,2.5 2)",
  225. 0, 0, strategy);
  226. }
  227. //===========================================================================
  228. template <typename Strategy>
  229. void test_distance_ring_box(Strategy const& strategy)
  230. {
  231. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  232. std::cout << std::endl;
  233. std::cout << "ring/box distance tests" << std::endl;
  234. #endif
  235. typedef test_distance_of_geometries<ring_type, box_type> tester;
  236. tester::apply("polygon((10 10,10 5,5 5,5 10,10 10))",
  237. "box(0 0,1 1)",
  238. sqrt(32.0), 32, strategy);
  239. tester::apply("polygon((10 10,10 5,5 5,5 10,10 10))",
  240. "box(0 0,5 5)",
  241. 0, 0, strategy);
  242. tester::apply("polygon((10 10,10 5,5 5,5 10,10 10))",
  243. "box(0 0,6 6)",
  244. 0, 0, strategy);
  245. tester::apply("polygon((10 10,15 5,10 0,5 5,10 10))",
  246. "box(5 0,7.5 2.5)",
  247. 0, 0, strategy);
  248. tester::apply("polygon((10 10,15 5,10 0,5 5,10 10))",
  249. "box(5 0,6 1)",
  250. sqrt(4.5), 4.5, strategy);
  251. }
  252. //===========================================================================
  253. template <typename Point, typename Strategy>
  254. void test_more_empty_input_areal_areal(Strategy const& strategy)
  255. {
  256. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  257. std::cout << std::endl;
  258. std::cout << "testing on empty inputs... " << std::flush;
  259. #endif
  260. bg::model::polygon<Point> polygon_empty;
  261. bg::model::multi_polygon<bg::model::polygon<Point> > multipolygon_empty;
  262. bg::model::ring<Point> ring_empty;
  263. bg::model::polygon<Point> polygon =
  264. from_wkt<bg::model::polygon<Point> >("polygon((0 0,1 0,0 1))");
  265. bg::model::multi_polygon<bg::model::polygon<Point> > multipolygon =
  266. from_wkt
  267. <
  268. bg::model::multi_polygon<bg::model::polygon<Point> >
  269. >("multipolygon(((0 0,1 0,0 1)))");
  270. bg::model::ring<Point> ring =
  271. from_wkt<bg::model::ring<Point> >("polygon((0 0,1 0,0 1))");
  272. // 1st geometry is empty
  273. test_empty_input(polygon_empty, polygon, strategy);
  274. test_empty_input(polygon_empty, multipolygon, strategy);
  275. test_empty_input(polygon_empty, ring, strategy);
  276. test_empty_input(multipolygon_empty, polygon, strategy);
  277. test_empty_input(multipolygon_empty, multipolygon, strategy);
  278. test_empty_input(multipolygon_empty, ring, strategy);
  279. test_empty_input(ring_empty, polygon, strategy);
  280. test_empty_input(ring_empty, multipolygon, strategy);
  281. test_empty_input(ring_empty, ring, strategy);
  282. // 2nd geometry is empty
  283. test_empty_input(polygon, polygon_empty, strategy);
  284. test_empty_input(polygon, multipolygon_empty, strategy);
  285. test_empty_input(polygon, ring_empty, strategy);
  286. test_empty_input(multipolygon, polygon_empty, strategy);
  287. test_empty_input(multipolygon, multipolygon_empty, strategy);
  288. test_empty_input(multipolygon, ring_empty, strategy);
  289. test_empty_input(ring, polygon_empty, strategy);
  290. test_empty_input(ring, multipolygon_empty, strategy);
  291. test_empty_input(ring, ring_empty, strategy);
  292. // both geometries are empty
  293. test_empty_input(polygon_empty, polygon_empty, strategy);
  294. test_empty_input(polygon_empty, multipolygon_empty, strategy);
  295. test_empty_input(polygon_empty, ring_empty, strategy);
  296. test_empty_input(multipolygon_empty, polygon_empty, strategy);
  297. test_empty_input(multipolygon_empty, multipolygon_empty, strategy);
  298. test_empty_input(multipolygon_empty, ring_empty, strategy);
  299. test_empty_input(ring_empty, polygon_empty, strategy);
  300. test_empty_input(ring_empty, multipolygon_empty, strategy);
  301. test_empty_input(ring_empty, ring_empty, strategy);
  302. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  303. std::cout << "done!" << std::endl;
  304. #endif
  305. }
  306. //===========================================================================
  307. BOOST_AUTO_TEST_CASE( test_all_polygon_polygon )
  308. {
  309. test_distance_polygon_polygon(point_segment_strategy());
  310. }
  311. BOOST_AUTO_TEST_CASE( test_all_polygon_multipolygon )
  312. {
  313. test_distance_polygon_multipolygon(point_segment_strategy());
  314. }
  315. BOOST_AUTO_TEST_CASE( test_all_polygon_ring )
  316. {
  317. test_distance_polygon_ring(point_segment_strategy());
  318. }
  319. BOOST_AUTO_TEST_CASE( test_all_multipolygon_multipolygon )
  320. {
  321. test_distance_multipolygon_multipolygon(point_segment_strategy());
  322. }
  323. BOOST_AUTO_TEST_CASE( test_all_multipolygon_ring )
  324. {
  325. test_distance_multipolygon_ring(point_segment_strategy());
  326. }
  327. BOOST_AUTO_TEST_CASE( test_all_ring_ring )
  328. {
  329. test_distance_ring_ring(point_segment_strategy());
  330. }
  331. BOOST_AUTO_TEST_CASE( test_all_box_box )
  332. {
  333. test_distance_box_box(box_box_strategy());
  334. }
  335. BOOST_AUTO_TEST_CASE( test_all_polygon_box )
  336. {
  337. test_distance_polygon_box(point_segment_strategy());
  338. }
  339. BOOST_AUTO_TEST_CASE( test_all_multipolygon_box )
  340. {
  341. test_distance_multipolygon_box(point_segment_strategy());
  342. }
  343. BOOST_AUTO_TEST_CASE( test_all_ring_box )
  344. {
  345. test_distance_ring_box(point_segment_strategy());
  346. }
  347. BOOST_AUTO_TEST_CASE( test_all_empty_input_areal_areal )
  348. {
  349. test_more_empty_input_areal_areal<point_type>(point_segment_strategy());
  350. }