distance_ca_pl_l.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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_pointlike_linear
  11. #endif
  12. #include <boost/test/included/unit_test.hpp>
  13. #include "test_distance_common.hpp"
  14. #include "test_empty_geometry.hpp"
  15. typedef bg::model::point<double,2,bg::cs::cartesian> point_type;
  16. typedef bg::model::multi_point<point_type> multi_point_type;
  17. typedef bg::model::segment<point_type> segment_type;
  18. typedef bg::model::linestring<point_type> linestring_type;
  19. typedef bg::model::multi_linestring<linestring_type> multi_linestring_type;
  20. namespace services = bg::strategy::distance::services;
  21. typedef bg::default_distance_result<point_type>::type return_type;
  22. typedef bg::strategy::distance::pythagoras<> point_point_strategy;
  23. typedef bg::strategy::distance::projected_point<> point_segment_strategy;
  24. //===========================================================================
  25. template <typename Strategy>
  26. void test_distance_point_segment(Strategy const& strategy)
  27. {
  28. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  29. std::cout << std::endl;
  30. std::cout << "point/segment distance tests" << std::endl;
  31. #endif
  32. typedef test_distance_of_geometries<point_type, segment_type> tester;
  33. tester::apply("point(0 0)", "segment(2 0,3 0)", 2, 4, strategy);
  34. tester::apply("point(2.5 3)", "segment(2 0,3 0)", 3, 9, strategy);
  35. tester::apply("point(2 0)", "segment(2 0,3 0)", 0, 0, strategy);
  36. tester::apply("point(3 0)", "segment(2 0,3 0)", 0, 0, strategy);
  37. tester::apply("point(2.5 0)", "segment(2 0,3 0)", 0, 0, strategy);
  38. // distance is a NaN
  39. tester::apply("POINT(4.297374e+307 8.433875e+307)",
  40. "SEGMENT(26 87,13 95)",
  41. 0, 0, strategy, false);
  42. }
  43. //===========================================================================
  44. template <typename Strategy>
  45. void test_distance_point_linestring(Strategy const& strategy)
  46. {
  47. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  48. std::cout << std::endl;
  49. std::cout << "point/linestring distance tests" << std::endl;
  50. #endif
  51. typedef test_distance_of_geometries<point_type, linestring_type> tester;
  52. tester::apply("point(0 0)", "linestring(2 0,3 0)", 2, 4, strategy);
  53. tester::apply("point(2.5 3)", "linestring(2 0,3 0)", 3, 9, strategy);
  54. tester::apply("point(2 0)", "linestring(2 0,3 0)", 0, 0, strategy);
  55. tester::apply("point(3 0)", "linestring(2 0,3 0)", 0, 0, strategy);
  56. tester::apply("point(2.5 0)", "linestring(2 0,3 0)", 0, 0, strategy);
  57. // linestring with a single point
  58. tester::apply("point(0 0)", "linestring(2 0)", 2, 4, strategy);
  59. // distance is a NaN
  60. tester::apply("POINT(4.297374e+307 8.433875e+307)",
  61. "LINESTRING(26 87,13 95)",
  62. 0, 0, strategy, false);
  63. }
  64. //===========================================================================
  65. template <typename Strategy>
  66. void test_distance_point_multilinestring(Strategy const& strategy)
  67. {
  68. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  69. std::cout << std::endl;
  70. std::cout << "point/multilinestring distance tests" << std::endl;
  71. #endif
  72. typedef test_distance_of_geometries
  73. <
  74. point_type, multi_linestring_type
  75. > tester;
  76. tester::apply("point(0 0)",
  77. "multilinestring((-5 0,-3 0),(2 0,3 0))",
  78. 2, 4, strategy);
  79. tester::apply("point(2.5 3)",
  80. "multilinestring((-5 0,-3 0),(2 0,3 0))",
  81. 3, 9, strategy);
  82. tester::apply("point(2 0)",
  83. "multilinestring((-5 0,-3 0),(2 0,3 0))",
  84. 0, 0, strategy);
  85. tester::apply("point(3 0)",
  86. "multilinestring((-5 0,-3 0),(2 0,3 0))",
  87. 0, 0, strategy);
  88. tester::apply("point(2.5 0)",
  89. "multilinestring((-5 0,-3 0),(2 0,3 0))",
  90. 0, 0, strategy);
  91. tester::apply("POINT(0 0)",
  92. "MULTILINESTRING((10 10,10 0),(0.0 -0.0,0.0 -0.0))",
  93. 0, 0, strategy);
  94. tester::apply("POINT(0 0)",
  95. "MULTILINESTRING((10 10,10 0),(1 1,1 1))",
  96. sqrt(2.0), 2, strategy);
  97. tester::apply("POINT(0 0)",
  98. "MULTILINESTRING((10 10,10 0),(1 1,2 2))",
  99. sqrt(2.0), 2, strategy);
  100. tester::apply("POINT(0 0)",
  101. "MULTILINESTRING((10 10,10 0),(20 20,20 20))",
  102. 10, 100, strategy);
  103. // multilinestrings containing an empty linestring
  104. tester::apply("POINT(0 0)",
  105. "MULTILINESTRING((),(10 0),(20 20,20 20))",
  106. 10, 100, strategy);
  107. tester::apply("POINT(0 0)",
  108. "MULTILINESTRING((),(10 0),(),(20 20,20 20))",
  109. 10, 100, strategy);
  110. // multilinestrings containing a linestring with a single point
  111. tester::apply("POINT(0 0)",
  112. "MULTILINESTRING((10 0),(20 20,20 20))",
  113. 10, 100, strategy);
  114. tester::apply("POINT(0 0)",
  115. "MULTILINESTRING((20 20,20 20),(10 0))",
  116. 10, 100, strategy);
  117. // multilinestring with a single-point linestring and empty linestrings
  118. tester::apply("POINT(0 0)",
  119. "MULTILINESTRING((),(20 20,20 20),(),(10 0))",
  120. 10, 100, strategy);
  121. }
  122. //===========================================================================
  123. template <typename Strategy>
  124. void test_distance_linestring_multipoint(Strategy const& strategy)
  125. {
  126. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  127. std::cout << std::endl;
  128. std::cout << "linestring/multipoint distance tests" << std::endl;
  129. #endif
  130. typedef test_distance_of_geometries
  131. <
  132. linestring_type, multi_point_type
  133. > tester;
  134. tester::apply("linestring(2 0,0 2,100 100)",
  135. "multipoint(0 0,1 0,0 1,1 1)",
  136. 0, 0, strategy);
  137. tester::apply("linestring(4 0,0 4,100 100)",
  138. "multipoint(0 0,1 0,0 1,1 1)",
  139. sqrt(2.0), 2, strategy);
  140. tester::apply("linestring(1 1,2 2,100 100)",
  141. "multipoint(0 0,1 0,0 1,1 1)",
  142. 0, 0, strategy);
  143. tester::apply("linestring(3 3,4 4,100 100)",
  144. "multipoint(0 0,1 0,0 1,1 1)",
  145. sqrt(8.0), 8, strategy);
  146. // linestring with a single point
  147. tester::apply("linestring(1 8)",
  148. "multipoint(0 0,3 0,4 -7,10 100)",
  149. sqrt(65.0), 65, strategy);
  150. }
  151. //===========================================================================
  152. template <typename Strategy>
  153. void test_distance_multipoint_multilinestring(Strategy const& strategy)
  154. {
  155. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  156. std::cout << std::endl;
  157. std::cout << "multipoint/multilinestring distance tests" << std::endl;
  158. #endif
  159. typedef test_distance_of_geometries
  160. <
  161. multi_point_type, multi_linestring_type
  162. > tester;
  163. tester::apply("multipoint(0 0,1 0,0 1,1 1)",
  164. "multilinestring((2 0,0 2),(2 2,3 3))",
  165. 0, 0, strategy);
  166. tester::apply("multipoint(0 0,1 0,0 1,1 1)",
  167. "multilinestring((3 0,0 3),(4 4,5 5))",
  168. 0.5 * sqrt(2.0), 0.5, strategy);
  169. tester::apply("multipoint(0 0,1 0,0 1,1 1)",
  170. "multilinestring((4 4,5 5),(1 1,2 2))",
  171. 0, 0, strategy);
  172. tester::apply("multipoint(0 0,1 0,0 1,1 1)",
  173. "multilinestring((3 3,4 4),(4 4,5 5))",
  174. sqrt(8.0), 8, strategy);
  175. // multilinestring with empty linestring
  176. tester::apply("multipoint(0 0,1 0,0 1,1 1)",
  177. "multilinestring((),(3 3,4 4),(4 4,5 5))",
  178. sqrt(8.0), 8, strategy);
  179. tester::apply("multipoint(0 0,1 0,0 1,1 1)",
  180. "multilinestring((3 3,4 4),(),(4 4,5 5))",
  181. sqrt(8.0), 8, strategy);
  182. // multilinestrings with a single-point linestrings
  183. tester::apply("multipoint(0 0,1 0,0 1,1 1)",
  184. "multilinestring((3 3),(4 4,5 5))",
  185. sqrt(8.0), 8, strategy);
  186. tester::apply("multipoint(0 0,1 0,0 1,1 1)",
  187. "multilinestring((4 4,5 5),(3 3))",
  188. sqrt(8.0), 8, strategy);
  189. // multilinestring with a single-point linestring and empty linestring
  190. tester::apply("multipoint(0 0,1 0,0 1,1 1)",
  191. "multilinestring((4 4,5 5),(),(3 3))",
  192. sqrt(8.0), 8, strategy);
  193. // 21890717 - assertion failure in distance(Pt, Box)
  194. {
  195. multi_point_type mpt;
  196. bg::read_wkt("multipoint(1 1,1 1,1 1,1 1,1 1,1 1,1 1,1 1,1 1)", mpt);
  197. multi_linestring_type mls;
  198. linestring_type ls;
  199. point_type pt(std::numeric_limits<double>::quiet_NaN(), 1.0);
  200. ls.push_back(pt);
  201. ls.push_back(pt);
  202. mls.push_back(ls);
  203. bg::distance(mpt, mls);
  204. }
  205. }
  206. //===========================================================================
  207. template <typename Strategy>
  208. void test_distance_multipoint_segment(Strategy const& strategy)
  209. {
  210. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  211. std::cout << std::endl;
  212. std::cout << "multipoint/segment distance tests" << std::endl;
  213. #endif
  214. typedef test_distance_of_geometries<multi_point_type, segment_type> tester;
  215. tester::apply("multipoint(0 0,1 0,0 1,1 1)",
  216. "segment(2 0,0 2)",
  217. 0, 0, strategy);
  218. tester::apply("multipoint(0 0,1 0,0 1,1 1)",
  219. "segment(4 0,0 4)",
  220. sqrt(2.0), 2, strategy);
  221. tester::apply("multipoint(0 0,1 0,0 1,1 1)",
  222. "segment(1 1,2 2)",
  223. 0, 0, strategy);
  224. tester::apply("multipoint(0 0,1 0,0 1,1 1)",
  225. "segment(3 3,4 4)",
  226. sqrt(8.0), 8, strategy);
  227. tester::apply("multipoint(4 4,5 5,2 2,3 3)",
  228. "segment(0 0,1 1)",
  229. sqrt(2.0), 2, strategy);
  230. }
  231. //===========================================================================
  232. //===========================================================================
  233. //===========================================================================
  234. BOOST_AUTO_TEST_CASE( test_all_point_segment )
  235. {
  236. test_distance_point_segment(point_point_strategy()); // back-compatibility
  237. test_distance_point_segment(point_segment_strategy());
  238. }
  239. BOOST_AUTO_TEST_CASE( test_all_point_linestring )
  240. {
  241. test_distance_point_linestring(point_point_strategy()); // back-compatibility
  242. test_distance_point_linestring(point_segment_strategy());
  243. }
  244. BOOST_AUTO_TEST_CASE( test_all_point_multilinestring )
  245. {
  246. test_distance_point_multilinestring(point_point_strategy()); // back-compatibility
  247. test_distance_point_multilinestring(point_segment_strategy());
  248. }
  249. BOOST_AUTO_TEST_CASE( test_all_linestring_multipoint )
  250. {
  251. test_distance_linestring_multipoint(point_point_strategy()); // back-compatibility
  252. test_distance_linestring_multipoint(point_segment_strategy());
  253. }
  254. BOOST_AUTO_TEST_CASE( test_all_multipoint_multilinestring )
  255. {
  256. test_distance_multipoint_multilinestring(point_point_strategy()); // back-compatibility
  257. test_distance_multipoint_multilinestring(point_segment_strategy());
  258. }
  259. BOOST_AUTO_TEST_CASE( test_all_multipoint_segment )
  260. {
  261. test_distance_multipoint_segment(point_segment_strategy());
  262. }
  263. BOOST_AUTO_TEST_CASE( test_all_empty_input_pointlike_linear )
  264. {
  265. test_more_empty_input_pointlike_linear
  266. <
  267. point_type
  268. >(point_segment_strategy());
  269. }