difference_areal_linear.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2010-2015 Barend Gehrels, Amsterdam, the Netherlands.
  4. // This file was modified by Oracle on 2015, 2017.
  5. // Modifications copyright (c) 2015-2017, Oracle and/or its affiliates.
  6. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  7. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  8. // Use, modification and distribution is subject to the Boost Software License,
  9. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. #include <iomanip>
  12. #include <iostream>
  13. #include <sstream>
  14. #include <string>
  15. #include <boost/geometry/algorithms/correct.hpp>
  16. #include <boost/geometry/algorithms/is_valid.hpp>
  17. #include <boost/geometry/io/wkt/wkt.hpp>
  18. #include <boost/geometry/geometries/point_xy.hpp>
  19. #include "test_difference.hpp"
  20. #include <algorithms/test_overlay.hpp>
  21. #include <algorithms/overlay/overlay_cases.hpp>
  22. #include <algorithms/overlay/multi_overlay_cases.hpp>
  23. #ifdef HAVE_TTMATH
  24. # include <boost/geometry/extensions/contrib/ttmath_stub.hpp>
  25. #endif
  26. template <typename CoordinateType>
  27. void test_ticket_10835(std::string const& wkt_out1, std::string const& wkt_out2)
  28. {
  29. typedef bg::model::point<CoordinateType, 2, bg::cs::cartesian> point_type;
  30. typedef bg::model::linestring<point_type> linestring_type;
  31. typedef bg::model::multi_linestring<linestring_type> multilinestring_type;
  32. typedef bg::model::polygon
  33. <
  34. point_type, /*ClockWise*/false, /*Closed*/false
  35. > polygon_type;
  36. multilinestring_type multilinestring;
  37. bg::read_wkt(ticket_10835[0], multilinestring);
  38. polygon_type polygon1;
  39. bg::read_wkt(ticket_10835[1], polygon1);
  40. polygon_type polygon2;
  41. bg::read_wkt(ticket_10835[2], polygon2);
  42. multilinestring_type multilinestringOut1;
  43. bg::difference(multilinestring, polygon1, multilinestringOut1);
  44. std::stringstream stream;
  45. stream << bg::wkt(multilinestringOut1);
  46. BOOST_CHECK_EQUAL(stream.str(), wkt_out1);
  47. multilinestring_type multilinestringOut2;
  48. bg::difference(multilinestringOut1, polygon2, multilinestringOut2);
  49. stream.str("");
  50. stream.clear();
  51. stream << bg::wkt(multilinestringOut2);
  52. BOOST_CHECK_EQUAL(stream.str(), wkt_out2);
  53. }
  54. template <typename Polygon, typename LineString>
  55. void test_areal_linear()
  56. {
  57. typedef typename bg::point_type<Polygon>::type point;
  58. typedef typename bg::coordinate_type<point>::type ct;
  59. std::string const poly_simplex = "POLYGON((1 1,1 3,3 3,3 1,1 1))";
  60. test_one_lp<LineString, LineString, Polygon>("simplex", "LINESTRING(0 2,4 2)", poly_simplex, 2, 4, 2.0);
  61. test_one_lp<LineString, LineString, Polygon>("case2", "LINESTRING(0 1,4 3)", poly_simplex, 2, 4, sqrt(5.0));
  62. test_one_lp<LineString, LineString, Polygon>("case3", "LINESTRING(0 1,1 2,3 2,4 3,6 3,7 4)", "POLYGON((2 0,2 5,5 5,5 0,2 0))", 2, 6, 2.0 + 2.0 * sqrt(2.0));
  63. test_one_lp<LineString, LineString, Polygon>("case4", "LINESTRING(1 1,3 2,1 3)", "POLYGON((0 0,0 4,2 4,2 0,0 0))", 1, 3, sqrt(5.0));
  64. test_one_lp<LineString, LineString, Polygon>("case5", "LINESTRING(0 1,3 4)", poly_simplex, 2, 4, 2.0 * sqrt(2.0));
  65. test_one_lp<LineString, LineString, Polygon>("case6", "LINESTRING(1 1,10 3)", "POLYGON((2 0,2 4,3 4,3 1,4 1,4 3,5 3,5 1,6 1,6 3,7 3,7 1,8 1,8 3,9 3,9 0,2 0))", 5, 10,
  66. // Pieces are 1 x 2/9:
  67. 5.0 * sqrt(1.0 + 4.0/81.0));
  68. test_one_lp<LineString, LineString, Polygon>("case7", "LINESTRING(1.5 1.5,2.5 2.5)", poly_simplex, 0, 0, 0.0);
  69. test_one_lp<LineString, LineString, Polygon>("case8", "LINESTRING(1 0,2 0)", poly_simplex, 1, 2, 1.0);
  70. std::string const poly_9 = "POLYGON((1 1,1 4,4 4,4 1,1 1))";
  71. test_one_lp<LineString, LineString, Polygon>("case9", "LINESTRING(0 1,1 2,2 2)", poly_9, 1, 2, sqrt(2.0));
  72. test_one_lp<LineString, LineString, Polygon>("case10", "LINESTRING(0 1,1 2,0 2)", poly_9, 1, 3, 1.0 + sqrt(2.0));
  73. test_one_lp<LineString, LineString, Polygon>("case11", "LINESTRING(2 2,4 2,3 3)", poly_9, 0, 0, 0.0);
  74. test_one_lp<LineString, LineString, Polygon>("case12", "LINESTRING(2 3,4 4,5 6)", poly_9, 1, 2, sqrt(5.0));
  75. test_one_lp<LineString, LineString, Polygon>("case13", "LINESTRING(3 2,4 4,2 3)", poly_9, 0, 0, 0.0);
  76. test_one_lp<LineString, LineString, Polygon>("case14", "LINESTRING(5 6,4 4,6 5)", poly_9, 1, 3, 2.0 * sqrt(5.0));
  77. test_one_lp<LineString, LineString, Polygon>("case15", "LINESTRING(0 2,1 2,1 3,0 3)", poly_9, 2, 4, 2.0);
  78. test_one_lp<LineString, LineString, Polygon>("case16", "LINESTRING(2 2,1 2,1 3,2 3)", poly_9, 0, 0, 0.0);
  79. std::string const angly = "LINESTRING(2 2,2 1,4 1,4 2,5 2,5 3,4 3,4 4,5 4,3 6,3 5,2 5,2 6,0 4)";
  80. test_one_lp<LineString, LineString, Polygon>("case17", angly, "POLYGON((1 1,1 5,4 5,4 1,1 1))", 3, 11, 6.0 + 4.0 * sqrt(2.0));
  81. test_one_lp<LineString, LineString, Polygon>("case18", angly, "POLYGON((1 1,1 5,5 5,5 1,1 1))", 2, 6, 2.0 + 3.0 * sqrt(2.0));
  82. test_one_lp<LineString, LineString, Polygon>("case19", "LINESTRING(1 2,1 3,0 3)", poly_9, 1, 2, 1.0);
  83. test_one_lp<LineString, LineString, Polygon>("case20", "LINESTRING(1 2,1 3,2 3)", poly_9, 0, 0, 0.0);
  84. // PROPERTIES CHANGED BY switch_to_integer
  85. // TODO test_one_lp<LineString, LineString, Polygon>("case21", "LINESTRING(1 2,1 4,4 4,4 1,2 1,2 2)", poly_9, 0, 0, 0.0);
  86. // More collinear (opposite) cases
  87. test_one_lp<LineString, LineString, Polygon>("case22", "LINESTRING(4 1,4 4,7 4)", poly_9, 1, 2, 3.0);
  88. test_one_lp<LineString, LineString, Polygon>("case23", "LINESTRING(4 0,4 4,7 4)", poly_9, 2, 4, 4.0);
  89. test_one_lp<LineString, LineString, Polygon>("case24", "LINESTRING(4 1,4 5,7 5)", poly_9, 1, 3, 4.0);
  90. test_one_lp<LineString, LineString, Polygon>("case25", "LINESTRING(4 0,4 5,7 5)", poly_9, 2, 5, 5.0);
  91. test_one_lp<LineString, LineString, Polygon>("case26", "LINESTRING(4 0,4 3,4 5,7 5)", poly_9, 2, 5, 5.0);
  92. test_one_lp<LineString, LineString, Polygon>("case27", "LINESTRING(4 4,4 5,5 5)", poly_9, 1, 3, 2.0);
  93. if (BOOST_GEOMETRY_CONDITION( (! boost::is_same<ct, float>::value)) )
  94. {
  95. // Fails for float
  96. test_one_lp<LineString, LineString, Polygon>("case28",
  97. "LINESTRING(-1.3 0,-15 0,-1.3 0)",
  98. "POLYGON((2 3,-9 -7,12 -13,2 3))",
  99. 1, 3, 27.4);
  100. }
  101. test_one_lp<LineString, LineString, Polygon>("case29",
  102. "LINESTRING(5 5,-10 5,5 5)",
  103. "POLYGON((0 0,0 10,10 10,10 0,0 0))",
  104. 1, 3, 20);
  105. test_one_lp<LineString, LineString, Polygon>("case29a",
  106. "LINESTRING(1 1,5 5,-10 5,5 5,6 6)",
  107. "POLYGON((0 0,0 10,10 10,10 0,0 0))",
  108. 1, 3, 20);
  109. test_one_lp<LineString, LineString, Polygon>("case30",
  110. "LINESTRING(-10 5,5 5,-10 5)",
  111. "POLYGON((0 0,0 10,10 10,10 0,0 0))",
  112. 2, 4, 20);
  113. test_one_lp<LineString, LineString, Polygon>("case30a",
  114. "LINESTRING(-20 10,-10 5,5 5,-10 5,-20 -10)",
  115. "POLYGON((0 0,0 10,10 10,10 0,0 0))",
  116. 2, 6, 49.208096);
  117. test_one_lp<LineString, LineString, Polygon>("case31",
  118. "LINESTRING(0 5,5 5,0 5)",
  119. "POLYGON((0 0,0 10,10 10,10 0,0 0))",
  120. 0, 0, 0);
  121. test_one_lp<LineString, LineString, Polygon>("case31",
  122. "LINESTRING(0 5,5 5,1 1,9 1,5 5,0 5)",
  123. "POLYGON((0 0,0 10,10 10,10 0,0 0))",
  124. 0, 0, 0);
  125. test_one_lp<LineString, LineString, Polygon>("case32",
  126. "LINESTRING(5 5,0 5,5 5)",
  127. "POLYGON((0 0,0 10,10 10,10 0,0 0))",
  128. 0, 0, 0);
  129. test_one_lp<LineString, LineString, Polygon>("case32a",
  130. "LINESTRING(-10 10,5 5,0 5,5 5,20 10)",
  131. "POLYGON((0 0,0 10,10 10,10 0,0 0))",
  132. 2, 4, 21.081851);
  133. test_one_lp<LineString, LineString, Polygon>("case33",
  134. "LINESTRING(-5 5,0 5,-5 5)",
  135. "POLYGON((0 0,0 10,10 10,10 0,0 0))",
  136. 1, 3, 10);
  137. test_one_lp<LineString, LineString, Polygon>("case33a",
  138. "LINESTRING(-10 10,-5 5,0 5,-5 5,-10 -10)",
  139. "POLYGON((0 0,0 10,10 10,10 0,0 0))",
  140. 1, 5, 32.882456);
  141. test_one_lp<LineString, LineString, Polygon>("case33b",
  142. "LINESTRING(0 5,-5 5,0 5)",
  143. "POLYGON((0 0,0 10,10 10,10 0,0 0))",
  144. 1, 3, 10);
  145. test_one_lp<LineString, LineString, Polygon>("case34",
  146. "LINESTRING(5 5,0 5,5 5,5 4,0 4,5 4)",
  147. "POLYGON((0 0,0 10,10 10,10 0,0 0))",
  148. 0, 0, 0);
  149. test_one_lp<LineString, LineString, Polygon>("case35",
  150. "LINESTRING(5 5,0 5,5 5,5 4,0 4,5 3)",
  151. "POLYGON((0 0,0 10,10 10,10 0,0 0))",
  152. 0, 0, 0);
  153. test_one_lp<LineString, LineString, Polygon>("case36",
  154. "LINESTRING(-1 -1,10 10)",
  155. "POLYGON((5 5,15 15,15 5,5 5))",
  156. 1, 2, 6 * std::sqrt(2.0));
  157. test_one_lp<LineString, LineString, Polygon>("case37_1",
  158. "LINESTRING(1 1,2 2)",
  159. "POLYGON((0 0,0 3,3 3,3 0,0 0),(1 1,1 2,2 2,2 1,1 1))",
  160. 1, 2, std::sqrt(2.0));
  161. test_one_lp<LineString, LineString, Polygon>("case37_2",
  162. "LINESTRING(1 1,2 2,3 3)",
  163. "POLYGON((0 0,0 3,3 3,3 0,0 0),(1 1,1 2,2 2,2 1,1 1))",
  164. 1, 2, std::sqrt(2.0));
  165. test_one_lp<LineString, LineString, Polygon>("case38",
  166. "LINESTRING(0 0,1 1,2 2,3 3)",
  167. "POLYGON((0 0,0 9,9 9,9 0,0 0),(0 0,2 1,2 2,1 2,0 0))",
  168. 1, 3, 2 * std::sqrt(2.0));
  169. // several linestrings are in the output, the result is geometrically correct
  170. // still single linestring could be generated
  171. test_one_lp<LineString, LineString, Polygon>("case39",
  172. "LINESTRING(0 0,1 1,2 2,3 3)",
  173. "POLYGON((0 0,0 9,9 9,9 0,0 0),(0 0,2 1,2 2,1 2,0 0),(2 2,3 2,3 3,2 3,2 2))",
  174. 2, 5, 3 * std::sqrt(2.0));
  175. test_one_lp<LineString, LineString, Polygon>("case40",
  176. "LINESTRING(0 0,1 1,2 2,4 4)",
  177. "POLYGON((0 0,0 9,9 9,9 0,0 0),(0 0,2 1,2 2,1 2,0 0),(2 2,3 2,3 3,2 3,2 2))",
  178. 2, 5, 3 * std::sqrt(2.0));
  179. test_one_lp<LineString, LineString, Polygon>("case41",
  180. "LINESTRING(0 0,1 1,2 2,9 9)",
  181. "POLYGON((0 0,0 9,9 9,9 0,0 0),(0 0,2 1,2 2,1 2,0 0),(2 2,3 2,3 3,2 3,2 2),(7 7,8 7,9 9,7 8,7 7))",
  182. 3, 7, 5 * std::sqrt(2.0));
  183. }
  184. template <typename P>
  185. void test_all()
  186. {
  187. typedef bg::model::polygon<P> polygon;
  188. typedef bg::model::linestring<P> linestring;
  189. test_areal_linear<polygon, linestring>();
  190. }
  191. int test_main(int, char* [])
  192. {
  193. test_all<bg::model::d2::point_xy<double> >();
  194. test_ticket_10835<int>
  195. ("MULTILINESTRING((5239 2113,5233 2114),(4794 2205,1020 2986))",
  196. "MULTILINESTRING((5239 2113,5233 2114),(4794 2205,1460 2895))");
  197. test_ticket_10835<double>
  198. ("MULTILINESTRING((5239 2113,5232.52 2114.34),(4794.39 2205,1020 2986))",
  199. "MULTILINESTRING((5239 2113,5232.52 2114.34),(4794.39 2205,1459.78 2895))");
  200. #if ! defined(BOOST_GEOMETRY_TEST_ONLY_ONE_TYPE)
  201. test_all<bg::model::d2::point_xy<float> >();
  202. #endif
  203. return 0;
  204. }