distance_ca_l_ar.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  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_linear_areal
  11. #endif
  12. #include <boost/test/included/unit_test.hpp>
  13. #include "test_distance_common.hpp"
  14. typedef bg::model::point<double,2,bg::cs::cartesian> point_type;
  15. typedef bg::model::point<int,2,bg::cs::cartesian> int_point_type;
  16. typedef bg::model::segment<point_type> segment_type;
  17. typedef bg::model::segment<int_point_type> int_segment_type;
  18. typedef bg::model::linestring<point_type> linestring_type;
  19. typedef bg::model::multi_linestring<linestring_type> multi_linestring_type;
  20. typedef bg::model::polygon<point_type, false> polygon_type;
  21. typedef bg::model::polygon<point_type, false, false> open_polygon_type;
  22. typedef bg::model::multi_polygon<polygon_type> multi_polygon_type;
  23. typedef bg::model::multi_polygon<open_polygon_type> open_multipolygon_type;
  24. typedef bg::model::ring<point_type, false> ring_type;
  25. typedef bg::model::box<point_type> box_type;
  26. typedef bg::model::box<int_point_type> int_box_type;
  27. namespace services = bg::strategy::distance::services;
  28. typedef bg::default_distance_result<point_type>::type return_type;
  29. typedef bg::strategy::distance::pythagoras<> point_point_strategy;
  30. typedef bg::strategy::distance::projected_point<> point_segment_strategy;
  31. typedef bg::strategy::distance::cartesian_segment_box<> segment_box_strategy;
  32. //===========================================================================
  33. template <typename Strategy>
  34. void test_distance_segment_polygon(Strategy const& strategy)
  35. {
  36. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  37. std::cout << std::endl;
  38. std::cout << "segment/polygon distance tests" << std::endl;
  39. #endif
  40. typedef test_distance_of_geometries<segment_type, polygon_type> tester;
  41. tester::apply("segment(-1 20,1 20)",
  42. "polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
  43. 10, 100, strategy);
  44. tester::apply("segment(1 20,2 40)",
  45. "polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
  46. 10, 100, strategy);
  47. tester::apply("segment(-1 20,-1 5)",
  48. "polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
  49. 0, 0, strategy);
  50. tester::apply("segment(-1 20,-1 -20)",
  51. "polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
  52. 0, 0, strategy);
  53. tester::apply("segment(0 0,1 1)",
  54. "polygon((2 2))",
  55. sqrt(2.0), 2, strategy);
  56. }
  57. //===========================================================================
  58. template <typename Strategy>
  59. void test_distance_linestring_polygon(Strategy const& strategy)
  60. {
  61. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  62. std::cout << std::endl;
  63. std::cout << "linestring/polygon distance tests" << std::endl;
  64. #endif
  65. typedef test_distance_of_geometries<linestring_type, polygon_type> tester;
  66. tester::apply("linestring(-1 20,1 20,1 30)",
  67. "polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
  68. 10, 100, strategy);
  69. tester::apply("linestring(-5 1,-2 1)",
  70. "polygon((0 0,10 0,10 10,0 10,0 0))",
  71. 2, 4, strategy);
  72. tester::apply("linestring(-1 20,1 20,1 5)",
  73. "polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
  74. 0, 0, strategy);
  75. tester::apply("linestring(-1 20,1 20,1 -20)",
  76. "polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
  77. 0, 0, strategy);
  78. tester::apply("linestring(-2 1)",
  79. "polygon((0 0,10 0,10 10,0 10,0 0))",
  80. 2, 4, strategy);
  81. tester::apply("linestring(-5 1,-2 1)",
  82. "polygon((0 0))",
  83. sqrt(5.0), 5, strategy);
  84. }
  85. //===========================================================================
  86. template <typename Strategy>
  87. void test_distance_linestring_open_polygon(Strategy const& strategy)
  88. {
  89. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  90. std::cout << std::endl;
  91. std::cout << "linestring/open polygon distance tests" << std::endl;
  92. #endif
  93. typedef test_distance_of_geometries
  94. <
  95. linestring_type, open_polygon_type
  96. > tester;
  97. tester::apply("linestring(-5 1,-2 1)",
  98. "polygon((0 0,10 0,10 10,0 10))",
  99. 2, 4, strategy);
  100. }
  101. //===========================================================================
  102. template <typename Strategy>
  103. void test_distance_multilinestring_polygon(Strategy const& strategy)
  104. {
  105. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  106. std::cout << std::endl;
  107. std::cout << "multilinestring/polygon distance tests" << std::endl;
  108. #endif
  109. typedef test_distance_of_geometries
  110. <
  111. multi_linestring_type, polygon_type
  112. > tester;
  113. tester::apply("multilinestring((-100 -100,-90 -90),(-1 20,1 20,1 30))",
  114. "polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
  115. 10, 100, strategy);
  116. tester::apply("multilinestring((-1 20,1 20,1 30),(-1 20,1 20,1 5))",
  117. "polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
  118. 0, 0, strategy);
  119. tester::apply("multilinestring((-1 20,1 20,1 30),(-1 20,1 20,1 -20))",
  120. "polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
  121. 0, 0, strategy);
  122. tester::apply("multilinestring((-100 -100,-90 -90),(1 20))",
  123. "polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
  124. 10, 100, strategy);
  125. tester::apply("multilinestring((-100 -100,-90 -90),(-1 20,1 20,1 30))",
  126. "polygon((-110 -110))",
  127. sqrt(200.0), 200, strategy);
  128. }
  129. //===========================================================================
  130. template <typename Strategy>
  131. void test_distance_segment_multipolygon(Strategy const& strategy)
  132. {
  133. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  134. std::cout << std::endl;
  135. std::cout << "segment/multipolygon distance tests" << std::endl;
  136. #endif
  137. typedef test_distance_of_geometries
  138. <
  139. segment_type, multi_polygon_type
  140. > tester;
  141. tester::apply("segment(-1 20,1 20)",
  142. "multipolygon(((-10 -10,10 -10,10 10,-10 10,-10 -10)),\
  143. ((0 22,-1 30, 2 40,0 22)))",
  144. 2, 4, strategy);
  145. tester::apply("segment(12 0,14 0)",
  146. "multipolygon(((-10 -10,10 -10,10 10,-10 10,-10 -10)),\
  147. ((20 -1,21 2,30 -10,20 -1)))",
  148. 2, 4, strategy);
  149. tester::apply("segment(12 0,20.5 0.5)",
  150. "multipolygon(((-10 -10,10 -10,10 10,-10 10,-10 -10)),\
  151. ((20 -1,21 2,30 -10,20 -1)))",
  152. 0, 0, strategy);
  153. tester::apply("segment(12 0,50 0)",
  154. "multipolygon(((-10 -10,10 -10,10 10,-10 10,-10 -10)),\
  155. ((20 -1,21 2,30 -10,20 -1)))",
  156. 0, 0, strategy);
  157. }
  158. //===========================================================================
  159. template <typename Strategy>
  160. void test_distance_linestring_multipolygon(Strategy const& strategy)
  161. {
  162. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  163. std::cout << std::endl;
  164. std::cout << "linestring/multipolygon distance tests" << std::endl;
  165. #endif
  166. typedef test_distance_of_geometries
  167. <
  168. linestring_type, multi_polygon_type
  169. > tester;
  170. tester::apply("linestring(-1 20,1 20)",
  171. "multipolygon(((-10 -10,10 -10,10 10,-10 10,-10 -10)),\
  172. ((0 22,-1 30, 2 40,0 22)))",
  173. 2, 4, strategy);
  174. tester::apply("linestring(12 0,14 0)",
  175. "multipolygon(((-10 -10,10 -10,10 10,-10 10,-10 -10)),\
  176. ((20 -1,21 2,30 -10,20 -1)))",
  177. 2, 4, strategy);
  178. tester::apply("linestring(12 0,20.5 0.5)",
  179. "multipolygon(((-10 -10,10 -10,10 10,-10 10,-10 -10)),\
  180. ((20 -1,21 2,30 -10,20 -1)))",
  181. 0, 0, strategy);
  182. tester::apply("linestring(12 0,50 0)",
  183. "multipolygon(((-10 -10,10 -10,10 10,-10 10,-10 -10)),\
  184. ((20 -1,21 2,30 -10,20 -1)))",
  185. 0, 0, strategy);
  186. }
  187. //===========================================================================
  188. template <typename Strategy>
  189. void test_distance_linestring_open_multipolygon(Strategy const& strategy)
  190. {
  191. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  192. std::cout << std::endl;
  193. std::cout << "linestring/open multipolygon distance tests" << std::endl;
  194. #endif
  195. typedef test_distance_of_geometries
  196. <
  197. linestring_type, open_multipolygon_type
  198. > tester;
  199. tester::apply("linestring(-5 1,-2 1)",
  200. "multipolygon(((0 0,10 0,10 10,0 10)))",
  201. 2, 4, strategy);
  202. tester::apply("linestring(-5 1,-3 1)",
  203. "multipolygon(((20 20,21 20,21 21,20 21)),((0 0,10 0,10 10,0 10)))",
  204. 3, 9, strategy);
  205. }
  206. //===========================================================================
  207. template <typename Strategy>
  208. void test_distance_multilinestring_multipolygon(Strategy const& strategy)
  209. {
  210. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  211. std::cout << std::endl;
  212. std::cout << "multilinestring/multipolygon distance tests" << std::endl;
  213. #endif
  214. typedef test_distance_of_geometries
  215. <
  216. multi_linestring_type, multi_polygon_type
  217. > tester;
  218. tester::apply("multilinestring((12 0,14 0),(19 0,19.9 -1))",
  219. "multipolygon(((-10 -10,10 -10,10 10,-10 10,-10 -10)),\
  220. ((20 -1,21 2,30 -10)))",
  221. 0.1, 0.01, strategy);
  222. tester::apply("multilinestring((19 0,19.9 -1),(12 0,20.5 0.5))",
  223. "multipolygon(((-10 -10,10 -10,10 10,-10 10,-10 -10)),\
  224. ((20 -1,21 2,30 -10,20 -1)))",
  225. 0, 0, strategy);
  226. }
  227. //===========================================================================
  228. template <typename Strategy>
  229. void test_distance_segment_ring(Strategy const& strategy)
  230. {
  231. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  232. std::cout << std::endl;
  233. std::cout << "segment/ring distance tests" << std::endl;
  234. #endif
  235. typedef test_distance_of_geometries<segment_type, ring_type> tester;
  236. tester::apply("segment(-1 20,1 20)",
  237. "polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
  238. 10, 100, strategy);
  239. tester::apply("segment(1 20,2 40)",
  240. "polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
  241. 10, 100, strategy);
  242. tester::apply("segment(-1 20,-1 5)",
  243. "polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
  244. 0, 0, strategy);
  245. tester::apply("segment(-1 20,-1 -20)",
  246. "polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
  247. 0, 0, strategy);
  248. }
  249. //===========================================================================
  250. template <typename Strategy>
  251. void test_distance_linestring_ring(Strategy const& strategy)
  252. {
  253. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  254. std::cout << std::endl;
  255. std::cout << "linestring/ring distance tests" << std::endl;
  256. #endif
  257. typedef test_distance_of_geometries<linestring_type, ring_type> tester;
  258. tester::apply("linestring(-1 20,1 20,1 30)",
  259. "polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
  260. 10, 100, strategy);
  261. tester::apply("linestring(-1 20,1 20,1 5)",
  262. "polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
  263. 0, 0, strategy);
  264. tester::apply("linestring(-1 20,1 20,1 -20)",
  265. "polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
  266. 0, 0, strategy);
  267. }
  268. //===========================================================================
  269. template <typename Strategy>
  270. void test_distance_multilinestring_ring(Strategy const& strategy)
  271. {
  272. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  273. std::cout << std::endl;
  274. std::cout << "multilinestring/ring distance tests" << std::endl;
  275. #endif
  276. typedef test_distance_of_geometries
  277. <
  278. multi_linestring_type, ring_type
  279. > tester;
  280. tester::apply("multilinestring((-100 -100,-90 -90),(-1 20,1 20,1 30))",
  281. "polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
  282. 10, 100, strategy);
  283. tester::apply("multilinestring((-1 20,1 20,1 30),(-1 20,1 20,1 5))",
  284. "polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
  285. 0, 0, strategy);
  286. tester::apply("multilinestring((-1 20,1 20,1 30),(-1 20,1 20,1 -20))",
  287. "polygon((-10 -10,10 -10,10 10,-10 10,-10 -10))",
  288. 0, 0, strategy);
  289. }
  290. //===========================================================================
  291. template <typename Strategy>
  292. void test_distance_segment_box(Strategy const& strategy)
  293. {
  294. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  295. std::cout << std::endl;
  296. std::cout << "2D segment/box distance tests" << std::endl;
  297. #endif
  298. typedef int_box_type B;
  299. typedef segment_type S;
  300. typedef int_segment_type IS;
  301. typedef test_distance_of_geometries<B, S> tester;
  302. typedef test_distance_of_geometries<B, IS> itester;
  303. // 1st example by Adam Wulkiewicz
  304. tester::apply("BOX(5 51,42 96)",
  305. "SEGMENT(6.6799994 95.260002,35.119999 56.340004)",
  306. 0, 0, strategy);
  307. // 2nd example by Adam Wulkiewicz
  308. tester::apply("BOX(51 55,94 100)",
  309. "SEGMENT(92.439995 50.130001,59.959999 80.870003)",
  310. 0, 0, strategy);
  311. // segments that intersect the box
  312. tester::apply("box(0 0,1 1)",
  313. "segment(-1 0.5,0.5 0.75)",
  314. 0, 0, strategy);
  315. tester::apply("box(0 0,1 1)",
  316. "segment(-1 0.5,1.5 0.75)",
  317. 0, 0, strategy);
  318. tester::apply("box(0 0,1 1)",
  319. "segment(0.5 -1,0.5 2)",
  320. 0, 0, strategy);
  321. tester::apply("box(0 0,1 1)",
  322. "segment(1 1,1.5 0.75)",
  323. 0, 0, strategy);
  324. tester::apply("box(0 0,1 1)",
  325. "segment(2 0,0 2)",
  326. 0, 0, strategy);
  327. // segment that has closest point on box boundary
  328. tester::apply("box(0 0,1 1)",
  329. "segment(4 0.5,5 0.75)",
  330. 3, 9, strategy);
  331. // segment that has closest point on box corner
  332. tester::apply("box(0 0,1 1)",
  333. "segment(4 0,0 4)",
  334. sqrt(2.0), 2, strategy);
  335. itester::apply("box(0 0,1 1)",
  336. "segment(-4 0,0 -4)",
  337. sqrt(8.0), 8, strategy);
  338. itester::apply("box(0 0,1 1)",
  339. "segment(-8 4,4 -8)",
  340. sqrt(8.0), 8, strategy);
  341. tester::apply("box(0 0,1 1)",
  342. "segment(-4 0,0 4)",
  343. 1.5 * sqrt(2.0), 4.5, strategy);
  344. tester::apply("box(0 0,1 1)",
  345. "segment(-4 0,1 5)",
  346. 1.5 * sqrt(2.0), 4.5, strategy);
  347. itester::apply("box(0 0,1 1)",
  348. "segment(0 -2,3 1)",
  349. 0.5 * sqrt(2.0), 0.5, strategy);
  350. itester::apply("box(0 0,1 1)",
  351. "segment(0 -2,2 2)",
  352. 0, 0, strategy);
  353. // horizontal segments
  354. itester::apply("box(0 0,1 1)",
  355. "segment(-2 -1,-1 -1)",
  356. sqrt(2.0), 2, strategy);
  357. itester::apply("box(0 0,1 1)",
  358. "segment(-1 -1,0 -1)",
  359. 1, 1, strategy);
  360. tester::apply("box(0 0,1 1)",
  361. "segment(-0.5 -1,0.5 -1)",
  362. 1, 1, strategy);
  363. tester::apply("box(0 0,1 1)",
  364. "segment(0.5 -1,0.75 -1)",
  365. 1, 1, strategy);
  366. tester::apply("box(0 0,1 1)",
  367. "segment(0.5 -1,1.25 -1)",
  368. 1, 1, strategy);
  369. tester::apply("box(0 0,1 1)",
  370. "segment(1 -1,2 -1)",
  371. 1, 1, strategy);
  372. tester::apply("box(0 0,1 1)",
  373. "segment(2 -1,3 -1)",
  374. sqrt(2.0), 2, strategy);
  375. tester::apply("box(0 0,1 1)",
  376. "segment(-2 -1,2 -1)",
  377. 1, 1, strategy);
  378. tester::apply("box(0 0,1 1)",
  379. "segment(-2 0,-1 0)",
  380. 1, 1, strategy);
  381. tester::apply("box(0 0,1 1)",
  382. "segment(-1 0,0 0)",
  383. 0, 0, strategy);
  384. tester::apply("box(0 0,1 1)",
  385. "segment(-0.5 0,0.5 0)",
  386. 0, 0, strategy);
  387. tester::apply("box(0 0,1 1)",
  388. "segment(0.5 0,0.75 0)",
  389. 0, 0, strategy);
  390. tester::apply("box(0 0,1 1)",
  391. "segment(0.5 0,1.25 0)",
  392. 0, 0, strategy);
  393. tester::apply("box(0 0,1 1)",
  394. "segment(1 0,2 0)",
  395. 0, 0, strategy);
  396. tester::apply("box(0 0,1 1)",
  397. "segment(2 0,3 0)",
  398. 1, 1, strategy);
  399. tester::apply("box(0 0,1 1)",
  400. "segment(-2 0,2 0)",
  401. 0, 0, strategy);
  402. tester::apply("box(0 0,1 1)",
  403. "segment(-2 0.5,-1 0.5)",
  404. 1, 1, strategy);
  405. tester::apply("box(0 0,1 1)",
  406. "segment(-1 0.5,0 0.5)",
  407. 0, 0, strategy);
  408. tester::apply("box(0 0,1 1)",
  409. "segment(-0.5 0.5,0.5 0.5)",
  410. 0, 0, strategy);
  411. tester::apply("box(0 0,1 1)",
  412. "segment(0.5 0.5,0.75 0.5)",
  413. 0, 0, strategy);
  414. tester::apply("box(0 0,1 1)",
  415. "segment(0.5 0.5,1.25 0.5)",
  416. 0, 0, strategy);
  417. tester::apply("box(0 0,1 1)",
  418. "segment(1 0.5,2 0.5)",
  419. 0, 0, strategy);
  420. tester::apply("box(0 0,1 1)",
  421. "segment(2 0.5,3 0.5)",
  422. 1, 1, strategy);
  423. tester::apply("box(0 0,1 1)",
  424. "segment(-2 0.5,2 0.5)",
  425. 0, 0, strategy);
  426. tester::apply("box(0 0,1 1)",
  427. "segment(-2 1,-1 1)",
  428. 1, 1, strategy);
  429. tester::apply("box(0 0,1 1)",
  430. "segment(-1 1,0 1)",
  431. 0, 0, strategy);
  432. tester::apply("box(0 0,1 1)",
  433. "segment(-0.5 1,0.5 1)",
  434. 0, 0, strategy);
  435. tester::apply("box(0 0,1 1)",
  436. "segment(0.5 1,0.75 1)",
  437. 0, 0, strategy);
  438. tester::apply("box(0 0,1 1)",
  439. "segment(0.5 1,1.25 1)",
  440. 0, 0, strategy);
  441. tester::apply("box(0 0,1 1)",
  442. "segment(1 1,2 1)",
  443. 0, 0, strategy);
  444. tester::apply("box(0 0,1 1)",
  445. "segment(2 1,3 1)",
  446. 1, 1, strategy);
  447. tester::apply("box(0 0,1 1)",
  448. "segment(-2 1,2 1)",
  449. 0, 0, strategy);
  450. tester::apply("box(0 0,1 1)",
  451. "segment(-2 3,-1 3)",
  452. sqrt(5.0), 5, strategy);
  453. itester::apply("box(0 0,1 1)",
  454. "segment(-1 3,0 3)",
  455. 2, 4, strategy);
  456. tester::apply("box(0 0,1 1)",
  457. "segment(-0.5 3,0.5 3)",
  458. 2, 4, strategy);
  459. tester::apply("box(0 0,1 1)",
  460. "segment(0.5 3,0.75 3)",
  461. 2, 4, strategy);
  462. tester::apply("box(0 0,1 1)",
  463. "segment(0.5 3,1.25 3)",
  464. 2, 4, strategy);
  465. tester::apply("box(0 0,1 1)",
  466. "segment(1 3,2 3)",
  467. 2, 4, strategy);
  468. tester::apply("box(0 0,1 1)",
  469. "segment(2 3,3 3)",
  470. sqrt(5.0), 5, strategy);
  471. tester::apply("box(0 0,1 1)",
  472. "segment(-2 3,2 3)",
  473. 2, 4, strategy);
  474. // vertical segments
  475. tester::apply("box(0 0,1 1)",
  476. "segment(-1 -2,-1 -1)",
  477. sqrt(2.0), 2, strategy);
  478. tester::apply("box(0 0,1 1)",
  479. "segment(-1 -1,-1 0)",
  480. 1, 1, strategy);
  481. tester::apply("box(0 0,1 1)",
  482. "segment(-1 -0.5,-1 0.5)",
  483. 1, 1, strategy);
  484. tester::apply("box(0 0,1 1)",
  485. "segment(-1 0.5,-1 0.75)",
  486. 1, 1, strategy);
  487. tester::apply("box(0 0,1 1)",
  488. "segment(-1 0.5,-1 1.25)",
  489. 1, 1, strategy);
  490. tester::apply("box(0 0,1 1)",
  491. "segment(-1 1,-1 2)",
  492. 1, 1, strategy);
  493. tester::apply("box(0 0,1 1)",
  494. "segment(-1 2,-1 3)",
  495. sqrt(2.0), 2, strategy);
  496. tester::apply("box(0 0,1 1)",
  497. "segment(-1 -2,-1 2)",
  498. 1, 1, strategy);
  499. tester::apply("box(0 0,1 1)",
  500. "segment(0 -2,0 -1)",
  501. 1, 1, strategy);
  502. tester::apply("box(0 0,1 1)",
  503. "segment(0 -1,0 0)",
  504. 0, 0, strategy);
  505. tester::apply("box(0 0,1 1)",
  506. "segment(0 -0.5,0 0.5)",
  507. 0, 0, strategy);
  508. tester::apply("box(0 0,1 1)",
  509. "segment(0 0.5,0 0.75)",
  510. 0, 0, strategy);
  511. tester::apply("box(0 0,1 1)",
  512. "segment(0 0.5,0 1.25)",
  513. 0, 0, strategy);
  514. tester::apply("box(0 0,1 1)",
  515. "segment(0 1,0 2)",
  516. 0, 0, strategy);
  517. tester::apply("box(0 0,1 1)",
  518. "segment(0 2,0 3)",
  519. 1, 1, strategy);
  520. tester::apply("box(0 0,1 1)",
  521. "segment(0 -2,0 2)",
  522. 0, 0, strategy);
  523. tester::apply("box(0 0,1 1)",
  524. "segment(0.5 -2,0.5 -1)",
  525. 1, 1, strategy);
  526. tester::apply("box(0 0,1 1)",
  527. "segment(0.5 -1,0.5 0)",
  528. 0, 0, strategy);
  529. tester::apply("box(0 0,1 1)",
  530. "segment(0.5 -0.5,0.5 0.5)",
  531. 0, 0, strategy);
  532. tester::apply("box(0 0,1 1)",
  533. "segment(0.5 0.5,0.5 0.75)",
  534. 0, 0, strategy);
  535. tester::apply("box(0 0,1 1)",
  536. "segment(0.5 0.5,0.5 1.25)",
  537. 0, 0, strategy);
  538. tester::apply("box(0 0,1 1)",
  539. "segment(0.5 1,0.5 2)",
  540. 0, 0, strategy);
  541. tester::apply("box(0 0,1 1)",
  542. "segment(0.5 2,0.5 3)",
  543. 1, 1, strategy);
  544. tester::apply("box(0 0,1 1)",
  545. "segment(0.5 -2,0.5 2)",
  546. 0, 0, strategy);
  547. tester::apply("box(0 0,1 1)",
  548. "segment(1 -2,1 -1)",
  549. 1, 1, strategy);
  550. tester::apply("box(0 0,1 1)",
  551. "segment(1 -1,1 0)",
  552. 0, 0, strategy);
  553. tester::apply("box(0 0,1 1)",
  554. "segment(1 -0.5,1 0.5)",
  555. 0, 0, strategy);
  556. tester::apply("box(0 0,1 1)",
  557. "segment(1 0.5,1 0.75)",
  558. 0, 0, strategy);
  559. tester::apply("box(0 0,1 1)",
  560. "segment(1 0.5,1 1.25)",
  561. 0, 0, strategy);
  562. tester::apply("box(0 0,1 1)",
  563. "segment(1 1,1 2)",
  564. 0, 0, strategy);
  565. tester::apply("box(0 0,1 1)",
  566. "segment(1 2,1 3)",
  567. 1, 1, strategy);
  568. tester::apply("box(0 0,1 1)",
  569. "segment(1 -2,1 2)",
  570. 0, 0, strategy);
  571. tester::apply("box(0 0,1 1)",
  572. "segment(3 -2,3 -1)",
  573. sqrt(5.0), 5, strategy);
  574. tester::apply("box(0 0,1 1)",
  575. "segment(3 -1,3 0)",
  576. 2, 4, strategy);
  577. tester::apply("box(0 0,1 1)",
  578. "segment(3 -0.5,3 0.5)",
  579. 2, 4, strategy);
  580. tester::apply("box(0 0,1 1)",
  581. "segment(3 0.5,3 0.75)",
  582. 2, 4, strategy);
  583. tester::apply("box(0 0,1 1)",
  584. "segment(3 0.5,3 1.25)",
  585. 2, 4, strategy);
  586. tester::apply("box(0 0,1 1)",
  587. "segment(3 1,3 2)",
  588. 2, 4, strategy);
  589. tester::apply("box(0 0,1 1)",
  590. "segment(3 2,3 3)",
  591. sqrt(5.0), 5, strategy);
  592. tester::apply("box(0 0,1 1)",
  593. "segment(3 -2,3 2)",
  594. 2, 4, strategy);
  595. // positive slope
  596. itester::apply("box(0 0,1 1)",
  597. "segment(-2 -2,-1 -1)",
  598. sqrt(2.0), 2, strategy);
  599. tester::apply("box(0 0,1 1)",
  600. "segment(-2 -2,0 -0.5)",
  601. 0.5, 0.25, strategy);
  602. tester::apply("box(0 0,1 1)",
  603. "segment(-2 -2,0.5 -0.5)",
  604. 0.5, 0.25, strategy);
  605. tester::apply("box(0 0,1 1)",
  606. "segment(-2 -2,1 -0.5)",
  607. 0.5, 0.25, strategy);
  608. tester::apply("box(0 0,1 1)",
  609. "segment(-2 -2,2 0)",
  610. sqrt(0.2), 0.2, strategy);
  611. tester::apply("box(0 0,1 1)",
  612. "segment(-2 -2,4 1)",
  613. sqrt(0.2), 0.2, strategy);
  614. tester::apply("box(0 0,1 1)",
  615. "segment(-2 -2,-1.5 0)",
  616. 1.5, 2.25, strategy);
  617. tester::apply("box(0 0,1 1)",
  618. "segment(-2 -2,-1.5 0.5)",
  619. 1.5, 2.25, strategy);
  620. tester::apply("box(0 0,1 1)",
  621. "segment(-2 -2,-1.5 1)",
  622. 1.5, 2.25, strategy);
  623. tester::apply("box(0 0,1 1)",
  624. "segment(-2 -2,0 2)",
  625. sqrt(0.2), 0.2, strategy);
  626. tester::apply("box(0 0,1 1)",
  627. "segment(-2 -2,1 4)",
  628. sqrt(0.2), 0.2, strategy);
  629. tester::apply("box(0 0,1 1)",
  630. "segment(-2 -2,4 2)",
  631. 0, 0, strategy);
  632. tester::apply("box(0 0,1 1)",
  633. "segment(-2 -2,2 4)",
  634. 0, 0, strategy);
  635. tester::apply("box(0 0,1 1)",
  636. "segment(-2 -2,4 3)",
  637. 0, 0, strategy);
  638. tester::apply("box(0 0,1 1)",
  639. "segment(-2 -2,3 4)",
  640. 0, 0, strategy);
  641. tester::apply("box(0 0,1 1)",
  642. "segment(-2 -2,3 3)",
  643. 0, 0, strategy);
  644. // negative slope
  645. tester::apply("box(0 0,1 1)",
  646. "segment(-2 -2,-1 -3)",
  647. sqrt(8.0), 8, strategy);
  648. tester::apply("box(0 0,1 1)",
  649. "segment(-3 -1,0 -4)",
  650. sqrt(8.0), 8, strategy);
  651. tester::apply("box(0 0,1 1)",
  652. "segment(-2 0.75,-1.5 0.5)",
  653. 1.5, 2.25, strategy);
  654. tester::apply("box(0 0,1 1)",
  655. "segment(-2 1.5,-1.5 0.5)",
  656. 1.5, 2.25, strategy);
  657. tester::apply("box(0 0,1 1)",
  658. "segment(0.5 2,0.75 1.5)",
  659. 0.5, 0.25, strategy);
  660. tester::apply("box(0 0,1 1)",
  661. "segment(-1 2,0.75 1.5)",
  662. 0.5, 0.25, strategy);
  663. tester::apply("box(0 0,1 1)",
  664. "segment(0 2,2 0)",
  665. 0, 0, strategy);
  666. tester::apply("box(0 0,1 1)",
  667. "segment(0 3,3 0)",
  668. sqrt(0.5), 0.5, strategy);
  669. tester::apply("box(0 0,1 1)",
  670. "segment(-1 4,4 -1)",
  671. sqrt(0.5), 0.5, strategy);
  672. tester::apply("box(0 0,1 1)",
  673. "segment(-1 4,0 3)",
  674. 2, 4, strategy);
  675. tester::apply("box(0 0,1 1)",
  676. "segment(-2 5,-1 4)",
  677. sqrt(10.0), 10, strategy);
  678. tester::apply("box(0 0,1 1)",
  679. "segment(3 -1,4 -4)",
  680. sqrt(5.0), 5, strategy);
  681. tester::apply("box(0 0,1 1)",
  682. "segment(1 2,2 1)",
  683. sqrt(0.5), 0.5, strategy);
  684. tester::apply("box(0 0,1 1)",
  685. "segment(0.5 -2,2 -3)",
  686. 2, 4, strategy);
  687. tester::apply("box(0 0,1 1)",
  688. "segment(-1 -2,0 -3)",
  689. sqrt(5.0), 5, strategy);
  690. tester::apply("box(0 0,1 1)",
  691. "segment(-1 -2,0.5 -3.5)",
  692. sqrt(5.0), 5, strategy);
  693. tester::apply("box(0 0,1 1)",
  694. "segment(-1 -2,0.5 -3.5)",
  695. sqrt(5.0), 5, strategy);
  696. tester::apply("box(0 0,1 1)",
  697. "segment(0.5 3,2.5 2)",
  698. sqrt(2.45), 2.45, strategy);
  699. tester::apply("box(0 0,1 1)",
  700. "segment(0.5 1.5,1.5 -1.5)",
  701. 0, 0, strategy);
  702. // test degenerate segment
  703. tester::apply("box(0 0,2 2)",
  704. "segment(4 1,4 1)",
  705. 2, 4, strategy);
  706. }
  707. //===========================================================================
  708. template <typename Strategy>
  709. void test_distance_linestring_box(Strategy const& strategy)
  710. {
  711. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  712. std::cout << std::endl;
  713. std::cout << "linestring/box distance tests" << std::endl;
  714. #endif
  715. typedef test_distance_of_geometries<linestring_type, box_type> tester;
  716. // linestrings that intersect the box
  717. tester::apply("linestring(-1 0.5,0.5 0.75)",
  718. "box(0 0,1 1)",
  719. 0, 0, strategy);
  720. tester::apply("linestring(-1 0.5,1.5 0.75)",
  721. "box(0 0,1 1)",
  722. 0, 0, strategy);
  723. // linestring that has closest point on box boundary
  724. tester::apply("linestring(4 0.5,5 0.75)",
  725. "box(0 0,1 1)",
  726. 3, 9, strategy);
  727. // linestring that has closest point on box corner
  728. tester::apply("linestring(4 0,0 4)",
  729. "box(0 0,1 1)",
  730. sqrt(2.0), 2, strategy);
  731. }
  732. //===========================================================================
  733. template <typename Strategy>
  734. void test_distance_multilinestring_box(Strategy const& strategy)
  735. {
  736. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  737. std::cout << std::endl;
  738. std::cout << "multilinestring/box distance tests" << std::endl;
  739. #endif
  740. typedef test_distance_of_geometries<multi_linestring_type, box_type> tester;
  741. // multilinestring that intersects the box
  742. tester::apply("multilinestring((-1 0.5,0.5 0.75),(4 0.5,5 0.75))",
  743. "box(0 0,1 1)",
  744. 0, 0, strategy);
  745. // multilinestring that has closest point on box boundary
  746. tester::apply("multilinestring((4 0.5,5 0.75))",
  747. "box(0 0,1 1)",
  748. 3, 9, strategy);
  749. // multilinestring that has closest point on box corner
  750. tester::apply("multilinestring((5 0,0 5),(4 0,0 4))",
  751. "box(0 0,1 1)",
  752. sqrt(2.0), 2, strategy);
  753. }
  754. //===========================================================================
  755. template <typename Point, typename Strategy>
  756. void test_more_empty_input_linear_areal(Strategy const& strategy)
  757. {
  758. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  759. std::cout << std::endl;
  760. std::cout << "testing on empty inputs... " << std::flush;
  761. #endif
  762. bg::model::linestring<Point> line_empty;
  763. bg::model::polygon<Point> polygon_empty;
  764. bg::model::multi_linestring<bg::model::linestring<Point> > multiline_empty;
  765. bg::model::multi_polygon<bg::model::polygon<Point> > multipolygon_empty;
  766. bg::model::ring<Point> ring_empty;
  767. bg::model::linestring<Point> line =
  768. from_wkt<bg::model::linestring<Point> >("linestring(0 0,1 1)");
  769. bg::model::polygon<Point> polygon =
  770. from_wkt<bg::model::polygon<Point> >("polygon((0 0,1 0,0 1))");
  771. bg::model::ring<Point> ring =
  772. from_wkt<bg::model::ring<Point> >("polygon((0 0,1 0,0 1))");
  773. // 1st geometry is empty
  774. test_empty_input(line_empty, polygon, strategy);
  775. test_empty_input(line_empty, ring, strategy);
  776. test_empty_input(multiline_empty, polygon, strategy);
  777. test_empty_input(multiline_empty, ring, strategy);
  778. // 2nd geometry is empty
  779. test_empty_input(line, polygon_empty, strategy);
  780. test_empty_input(line, multipolygon_empty, strategy);
  781. test_empty_input(line, ring_empty, strategy);
  782. // both geometries are empty
  783. test_empty_input(line_empty, polygon_empty, strategy);
  784. test_empty_input(line_empty, multipolygon_empty, strategy);
  785. test_empty_input(line_empty, ring_empty, strategy);
  786. test_empty_input(multiline_empty, polygon_empty, strategy);
  787. test_empty_input(multiline_empty, multipolygon_empty, strategy);
  788. test_empty_input(multiline_empty, ring_empty, strategy);
  789. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  790. std::cout << "done!" << std::endl;
  791. #endif
  792. }
  793. //===========================================================================
  794. BOOST_AUTO_TEST_CASE( test_all_segment_polygon )
  795. {
  796. test_distance_segment_polygon(point_segment_strategy());
  797. }
  798. BOOST_AUTO_TEST_CASE( test_all_linestring_polygon )
  799. {
  800. test_distance_linestring_polygon(point_segment_strategy());
  801. test_distance_linestring_open_polygon(point_segment_strategy());
  802. }
  803. BOOST_AUTO_TEST_CASE( test_all_multilinestring_polygon )
  804. {
  805. test_distance_multilinestring_polygon(point_segment_strategy());
  806. }
  807. BOOST_AUTO_TEST_CASE( test_all_segment_multipolygon )
  808. {
  809. test_distance_segment_multipolygon(point_segment_strategy());
  810. }
  811. BOOST_AUTO_TEST_CASE( test_all_linestring_multipolygon )
  812. {
  813. test_distance_linestring_multipolygon(point_segment_strategy());
  814. test_distance_linestring_open_multipolygon(point_segment_strategy());
  815. }
  816. BOOST_AUTO_TEST_CASE( test_all_multilinestring_multipolygon )
  817. {
  818. test_distance_multilinestring_multipolygon(point_segment_strategy());
  819. }
  820. BOOST_AUTO_TEST_CASE( test_all_segment_ring )
  821. {
  822. test_distance_segment_ring(point_segment_strategy());
  823. }
  824. BOOST_AUTO_TEST_CASE( test_all_linestring_ring )
  825. {
  826. test_distance_linestring_ring(point_segment_strategy());
  827. }
  828. BOOST_AUTO_TEST_CASE( test_all_multilinestring_ring )
  829. {
  830. test_distance_multilinestring_ring(point_segment_strategy());
  831. }
  832. BOOST_AUTO_TEST_CASE( test_all_segment_box )
  833. {
  834. test_distance_segment_box(segment_box_strategy());
  835. }
  836. BOOST_AUTO_TEST_CASE( test_all_linestring_box )
  837. {
  838. test_distance_linestring_box(point_segment_strategy());
  839. }
  840. BOOST_AUTO_TEST_CASE( test_all_multilinestring_box )
  841. {
  842. test_distance_multilinestring_box(point_segment_strategy());
  843. }
  844. BOOST_AUTO_TEST_CASE( test_all_empty_input_linear_areal )
  845. {
  846. test_more_empty_input_linear_areal<point_type>(point_segment_strategy());
  847. }