distance_ca_l_l.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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_linear
  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::segment<point_type> segment_type;
  16. typedef bg::model::linestring<point_type> linestring_type;
  17. typedef bg::model::multi_linestring<linestring_type> multi_linestring_type;
  18. namespace services = bg::strategy::distance::services;
  19. typedef bg::default_distance_result<point_type>::type return_type;
  20. typedef bg::strategy::distance::projected_point<> point_segment_strategy;
  21. //===========================================================================
  22. template <typename Strategy>
  23. void test_distance_segment_segment(Strategy const& strategy)
  24. {
  25. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  26. std::cout << std::endl;
  27. std::cout << "segment/segment distance tests" << std::endl;
  28. #endif
  29. typedef test_distance_of_geometries<segment_type, segment_type> tester;
  30. tester::apply("segment(0 0,10 0)",
  31. "segment(4 2,4 0.5)",
  32. return_type(0.5), return_type(0.25), strategy);
  33. tester::apply("segment(0 0,10 0)",
  34. "segment(4 2,4 -0.5)",
  35. return_type(0), return_type(0), strategy);
  36. tester::apply("segment(0 0,10 0)",
  37. "segment(4 2,0 0)",
  38. return_type(0), return_type(0), strategy);
  39. tester::apply("segment(0 0,10 0)",
  40. "segment(-2 3,1 2)",
  41. return_type(2), return_type(4), strategy);
  42. }
  43. //===========================================================================
  44. template <typename Strategy>
  45. void test_distance_segment_linestring(Strategy const& strategy)
  46. {
  47. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  48. std::cout << std::endl;
  49. std::cout << "segment/linestring distance tests" << std::endl;
  50. #endif
  51. typedef test_distance_of_geometries<segment_type, linestring_type> tester;
  52. tester::apply("segment(-1 -1,-2 -2)",
  53. "linestring(2 1,1 2,4 0)",
  54. sqrt(12.5), 12.5, strategy);
  55. tester::apply("segment(1 1,2 2)",
  56. "linestring(2 1,1 2,4 0)",
  57. 0, 0, strategy);
  58. tester::apply("segment(1 1,2 2)",
  59. "linestring(3 3,3 3)",
  60. sqrt(2.0), 2, strategy);
  61. tester::apply("segment(1 1,2 2)",
  62. "linestring(3 3)",
  63. sqrt(2.0), 2, strategy);
  64. }
  65. //===========================================================================
  66. template <typename Strategy>
  67. void test_distance_linestring_linestring(Strategy const& strategy)
  68. {
  69. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  70. std::cout << std::endl;
  71. std::cout << "linestring/linestring distance tests" << std::endl;
  72. #endif
  73. typedef test_distance_of_geometries
  74. <
  75. linestring_type, linestring_type
  76. > tester;
  77. // It is not obvious that linestrings with only one point are valid
  78. tester::apply("linestring(1 1)", "linestring(2 1)",
  79. 1, 1, strategy);
  80. tester::apply("linestring(1 1,3 1)", "linestring(2 1)",
  81. 0, 0, strategy);
  82. tester::apply("linestring(1 1)", "linestring(0 0,-2 0,2 -2,2 0)",
  83. sqrt(2.0), 2, strategy);
  84. tester::apply("linestring(1 1,1 1)", "linestring(2 1,2 1)",
  85. 1, 1, strategy);
  86. tester::apply("linestring(1 1,1 1,1 1)", "linestring(2 1,2 1,2 1,2 1)",
  87. 1, 1, strategy);
  88. tester::apply("linestring(1 1,3 1)", "linestring(2 1,2 1)",
  89. 0, 0, strategy);
  90. tester::apply("linestring(1 1,1 1)", "linestring(0 0,-2 0,2 -2,2 0)",
  91. sqrt(2.0), 2, strategy);
  92. tester::apply("linestring(1 1,3 1)", "linestring(2 1, 4 1)",
  93. 0, 0, strategy);
  94. tester::apply("linestring(1 1,2 2,3 3)", "linestring(2 1,1 2,4 0)",
  95. 0, 0, strategy);
  96. tester::apply("linestring(1 1,2 2,3 3)", "linestring(1 0,2 -1,4 0)",
  97. 1, 1, strategy);
  98. tester::apply("linestring(1 1,2 2,3 3)",
  99. "linestring(1 -10,2 0,2.1 -10,4 0)",
  100. sqrt(2.0), 2, strategy);
  101. tester::apply("linestring(1 1,2 2,3 3)",
  102. "linestring(1 -10,2 1.9,2.1 -10,4 0)",
  103. sqrt(0.005), 0.005, strategy);
  104. tester::apply("linestring(1 1,1 2)", "linestring(0 0,-2 0,2 -2,2 0)",
  105. sqrt(2.0), 2, strategy);
  106. }
  107. //===========================================================================
  108. template <typename Strategy>
  109. void test_distance_segment_multilinestring(Strategy const& strategy)
  110. {
  111. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  112. std::cout << std::endl;
  113. std::cout << "segment/multilinestring distance tests" << std::endl;
  114. #endif
  115. typedef test_distance_of_geometries
  116. <
  117. segment_type, multi_linestring_type
  118. > tester;
  119. tester::apply("segment(-1 -1,-2 -2)",
  120. "multilinestring((2 1,1 2),(4 0,4 10))",
  121. sqrt(12.5), 12.5, strategy);
  122. tester::apply("segment(1 1,2 2)",
  123. "multilinestring((2 1,1 2),(4 0,4 10))",
  124. 0, 0, strategy);
  125. tester::apply("segment(1 1,2 2)",
  126. "multilinestring((2.5 0,4 0,5 0),(3 3,3 3))",
  127. sqrt(2.0), 2, strategy);
  128. tester::apply("segment(1 1,2 2)",
  129. "multilinestring((2.5 0),(3 3,3 3))",
  130. sqrt(2.0), 2, strategy);
  131. tester::apply("segment(1 1,2 2)",
  132. "multilinestring((2.5 0,4 0,5 0),(3 3))",
  133. sqrt(2.0), 2, strategy);
  134. }
  135. //===========================================================================
  136. template <typename Strategy>
  137. void test_distance_linestring_multilinestring(Strategy const& strategy)
  138. {
  139. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  140. std::cout << std::endl;
  141. std::cout << "linestring/multilinestring distance tests" << std::endl;
  142. #endif
  143. typedef test_distance_of_geometries
  144. <
  145. linestring_type, multi_linestring_type
  146. > tester;
  147. tester::apply("linestring(1 1,2 2,3 3)",
  148. "multilinestring((2 1,1 2,4 0),(1 -10,2 1.9,2.1 -10,4 0))",
  149. 0, 0, strategy);
  150. tester::apply("linestring(1 1,2 2,3 3)",
  151. "multilinestring((1 -10,2 0,2.1 -10,4 0),(1 -10,2 1.9,2.1 -10,4 0))",
  152. sqrt(0.005), 0.005, strategy);
  153. tester::apply("linestring(1 1,2 2)",
  154. "multilinestring((2.5 0,4 0,5 0),(3 3,3 3))",
  155. sqrt(2.0), 2, strategy);
  156. tester::apply("linestring(2 2)",
  157. "multilinestring((2.5 0,4 0,5 0),(3 3,3 3))",
  158. sqrt(2.0), 2, strategy);
  159. tester::apply("linestring(1 1,2 2)",
  160. "multilinestring((2.5 0,4 0,5 0),(3 3))",
  161. sqrt(2.0), 2, strategy);
  162. tester::apply("linestring(1 1,2 2,3 3,4 4,5 5,6 6,7 7,8 8,9 9)",
  163. "multilinestring((2.5 0,4 0,5 0),(10 10,10 10))",
  164. sqrt(2.0), 2, strategy);
  165. }
  166. //===========================================================================
  167. template <typename Strategy>
  168. void test_distance_multilinestring_multilinestring(Strategy const& strategy)
  169. {
  170. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  171. std::cout << std::endl;
  172. std::cout << "multilinestring/multilinestring distance tests" << std::endl;
  173. #endif
  174. typedef test_distance_of_geometries
  175. <
  176. multi_linestring_type, multi_linestring_type
  177. > tester;
  178. tester::apply("multilinestring((0 0,0 1,1 1),(10 0,11 1,12 2))",
  179. "multilinestring((0.5 0.5,0.75 0.75),(11 0,11 7))",
  180. 0, 0, strategy);
  181. tester::apply("multilinestring((0 0,0 1,1 1),(10 0,11 1,12 2))",
  182. "multilinestring((0.5 0.5,0.75 0.75),(11 0,11 0.9))",
  183. sqrt(0.005), 0.005, strategy);
  184. tester::apply("multilinestring((0 0,0 1,1 1),(10 0,11 1,12 2))",
  185. "multilinestring((0.5 0.5,0.75 0.75),(11.1 0,11.1 0.9))",
  186. sqrt(0.02), 0.02, strategy);
  187. tester::apply("multilinestring((0 0),(1 1))",
  188. "multilinestring((2 2),(3 3))",
  189. sqrt(2.0), 2, strategy);
  190. }
  191. //===========================================================================
  192. template <typename Point, typename Strategy>
  193. void test_more_empty_input_linear_linear(Strategy const& strategy)
  194. {
  195. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  196. std::cout << std::endl;
  197. std::cout << "testing on empty inputs... " << std::flush;
  198. #endif
  199. bg::model::linestring<Point> line_empty;
  200. bg::model::multi_linestring<bg::model::linestring<Point> > multiline_empty;
  201. bg::model::linestring<Point> line =
  202. from_wkt<bg::model::linestring<Point> >("linestring(0 0,1 1)");
  203. // 1st geometry is empty
  204. test_empty_input(line_empty, line, strategy);
  205. test_empty_input(multiline_empty, line, strategy);
  206. // 2nd geometry is empty
  207. test_empty_input(line, line_empty, strategy);
  208. test_empty_input(line, multiline_empty, strategy);
  209. // both geometries are empty
  210. test_empty_input(line_empty, line_empty, strategy);
  211. test_empty_input(line_empty, multiline_empty, strategy);
  212. test_empty_input(multiline_empty, multiline_empty, strategy);
  213. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  214. std::cout << "done!" << std::endl;
  215. #endif
  216. }
  217. //===========================================================================
  218. BOOST_AUTO_TEST_CASE( test_all_segment_segment )
  219. {
  220. test_distance_segment_segment(point_segment_strategy());
  221. }
  222. BOOST_AUTO_TEST_CASE( test_all_segment_linestring )
  223. {
  224. test_distance_segment_linestring(point_segment_strategy());
  225. }
  226. BOOST_AUTO_TEST_CASE( test_all_linestring_linestring )
  227. {
  228. test_distance_linestring_linestring(point_segment_strategy());
  229. }
  230. BOOST_AUTO_TEST_CASE( test_all_segment_multilinestring )
  231. {
  232. test_distance_segment_multilinestring(point_segment_strategy());
  233. }
  234. BOOST_AUTO_TEST_CASE( test_all_linestring_multilinestring )
  235. {
  236. test_distance_linestring_multilinestring(point_segment_strategy());
  237. }
  238. BOOST_AUTO_TEST_CASE( test_all_multilinestring_multilinestring )
  239. {
  240. test_distance_multilinestring_multilinestring(point_segment_strategy());
  241. }
  242. BOOST_AUTO_TEST_CASE( test_all_empty_input_linear_linear )
  243. {
  244. test_more_empty_input_linear_linear<point_type>(point_segment_strategy());
  245. }