dissolver.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2010-2015 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Use, modification and distribution is subject to the Boost Software License,
  5. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #include <iostream>
  8. #include <string>
  9. #include <geometry_test_common.hpp>
  10. #include <boost/geometry/algorithms/detail/overlay/dissolver.hpp>
  11. #include <boost/geometry/strategies/strategies.hpp>
  12. #include <boost/geometry/geometries/point_xy.hpp>
  13. #include <boost/geometry/geometries/multi_polygon.hpp>
  14. #include <boost/geometry/io/wkt/read.hpp>
  15. #include <test_common/test_point.hpp>
  16. #if defined(TEST_WITH_SVG)
  17. # include <boost/geometry/io/svg/svg_mapper.hpp>
  18. # include <boost/geometry/io/svg/write_svg_multi.hpp>
  19. #endif
  20. // Collection might be a multi-geometry, or std::vector<ring>
  21. template <typename GeometryOut, typename Collection, typename T>
  22. void test_dissolve_plusmin(std::string const& caseid, Collection const& input,
  23. T const& expected_positive_area,
  24. T const& expected_negative_area)
  25. {
  26. typedef typename boost::range_value<GeometryOut>::type geometry_type;
  27. typedef typename bg::point_type<geometry_type>::type point_type;
  28. GeometryOut output;
  29. bg::dissolver(input, output);
  30. T zero = T();
  31. T positive_area = T();
  32. T negative_area = T();
  33. BOOST_FOREACH(geometry_type const& geometry, output)
  34. {
  35. T a = bg::area(geometry);
  36. if (a > zero)
  37. {
  38. positive_area += a;
  39. }
  40. else
  41. {
  42. negative_area += a;
  43. }
  44. }
  45. BOOST_CHECK_CLOSE(positive_area, expected_positive_area, 0.001);
  46. BOOST_CHECK_CLOSE(negative_area, expected_negative_area, 0.001);
  47. #if defined(TEST_WITH_SVG)
  48. {
  49. std::ostringstream filename;
  50. filename << "dissolve_plusmin_"
  51. << caseid << ".svg";
  52. std::ofstream svg(filename.str().c_str());
  53. bg::svg_mapper<point_type> mapper(svg, 500, 500);
  54. typedef typename boost::range_value<Collection>::type value_type;
  55. BOOST_FOREACH(value_type const& geometry, input)
  56. {
  57. mapper.add(geometry);
  58. }
  59. BOOST_FOREACH(value_type const& geometry, input)
  60. {
  61. mapper.map(geometry,
  62. "opacity:0.6;fill:rgb(0,255,0);stroke:rgb(0,0,0);stroke-width:0.5");
  63. }
  64. BOOST_FOREACH(geometry_type const& geometry, output)
  65. {
  66. mapper.map(geometry,
  67. bg::area(geometry) > 0
  68. ? "opacity:0.5;fill:none;stroke:rgb(255,0,0);stroke-width:5"
  69. : "opacity:0.5;fill:none;stroke:rgb(0,0,255);stroke-width:5"
  70. );
  71. }
  72. }
  73. #endif
  74. }
  75. template <typename MultiPolygon, typename T>
  76. void test_geometry(std::string const& caseid, std::string const& wkt,
  77. T const& expected_positive_area,
  78. T const& expected_negative_area = T())
  79. {
  80. MultiPolygon multi_polygon;
  81. bg::read_wkt(wkt, multi_polygon);
  82. // Test std::vector<Polygon> (= multi_polygon)
  83. test_dissolve_plusmin<MultiPolygon>(caseid, multi_polygon,
  84. expected_positive_area,
  85. expected_negative_area);
  86. // Test std::vector<ring>
  87. {
  88. typedef typename boost::range_value<MultiPolygon>::type polygon_type;
  89. typedef typename bg::ring_type<MultiPolygon>::type ring_type;
  90. std::vector<ring_type> rings;
  91. BOOST_FOREACH(polygon_type const& polygon, multi_polygon)
  92. {
  93. rings.push_back(bg::exterior_ring(polygon));
  94. }
  95. test_dissolve_plusmin<MultiPolygon>(caseid + "_rings", rings,
  96. expected_positive_area,
  97. expected_negative_area);
  98. }
  99. // Test different combinations
  100. #define BOOST_GEOMETRY_TEST_PERMUTATIONS
  101. #ifdef BOOST_GEOMETRY_TEST_PERMUTATIONS
  102. int n = multi_polygon.size();
  103. // test them in all orders
  104. std::vector<int> indices;
  105. for (int i = 0; i < n; i++)
  106. {
  107. indices.push_back(i);
  108. }
  109. int permutation = 0;
  110. do
  111. {
  112. std::ostringstream out;
  113. out << caseid;
  114. MultiPolygon multi_polygon2;
  115. for (int i = 0; i < n; i++)
  116. {
  117. int index = indices[i];
  118. out << "_" << index;
  119. multi_polygon2.push_back(multi_polygon[index]);
  120. }
  121. test_dissolve_plusmin<MultiPolygon>(out.str(), multi_polygon2, expected_positive_area,
  122. expected_negative_area);
  123. } while (std::next_permutation(indices.begin(), indices.end()));
  124. #endif
  125. }
  126. template <typename Point>
  127. void test_all()
  128. {
  129. typedef bg::model::polygon<Point> polygon;
  130. typedef bg::model::multi_polygon<polygon> multi_polygon;
  131. test_geometry<multi_polygon>("simplex_one",
  132. "MULTIPOLYGON(((0 0,1 4,4 1,0 0)))",
  133. 7.5);
  134. test_geometry<multi_polygon>("simplex_two",
  135. "MULTIPOLYGON(((0 0,1 4,4 1,0 0)),((2 2,3 6,6 3,2 2)))",
  136. 14.7);
  137. test_geometry<multi_polygon>("simplex_three",
  138. "MULTIPOLYGON(((0 0,1 4,4 1,0 0)),((2 2,3 6,6 3,2 2)),((3 4,5 6,6 2,3 4)))",
  139. 16.7945);
  140. test_geometry<multi_polygon>("simplex_four",
  141. "MULTIPOLYGON(((0 0,1 4,4 1,0 0)),((2 2,3 6,6 3,2 2)),((3 4,5 6,6 2,3 4)),((5 5,7 7,8 4,5 5)))",
  142. 20.7581);
  143. // disjoint
  144. test_geometry<multi_polygon>("simplex_disjoint",
  145. "MULTIPOLYGON(((0 0,1 4,4 1,0 0)),((1 6,2 10,5 7,1 6)),((3 4,5 6,6 2,3 4)),((6 5,8 7,9 4,6 5)))",
  146. 24.0);
  147. // new hole of four
  148. test_geometry<multi_polygon>("new_hole",
  149. "MULTIPOLYGON(((0 0,1 4,4 1,0 0)),((2 2,3 6,6 3,2 2)),((3 4,5 6,6 2,3 4)),((3 1,5 4,8 4,3 1)))",
  150. 19.5206);
  151. // intersection of positive/negative ring
  152. test_geometry<multi_polygon>("plus_min_one",
  153. "MULTIPOLYGON(((0 0,1 4,4 1,0 0)),((2 2,6 3,3 6,2 2)))",
  154. 7.5, -7.2);
  155. // negative ring within a positive ring
  156. test_geometry<multi_polygon>("plus_min_one_within",
  157. "MULTIPOLYGON(((0 0,1 7,7 3,0 0)),((1 2,4 4,2 5,1 2)))",
  158. 23.0, -3.5);
  159. // from buffer
  160. test_geometry<multi_polygon>("from_buffer_1",
  161. "MULTIPOLYGON(((2.4 3.03431,1.71716 3.71716,2.4 4,2.4 3.03431))"
  162. ",((2.4 1.96569,2.4 1,1.71716 1.28284,2.4 1.96569))"
  163. ",((2.93431 2.5,2.4 3.03431,2.4 1.96569,2.93431 2.5))"
  164. ",((3.06569 2.5,3 2.43431,2.93431 2.5,3 2.56569,3.06569 2.5))"
  165. ",((-0.4 5.4,4.4 5.4,4.4 3.83431,3.06569 2.5,4.4 1.16569,4.4 -0.4,-0.4 -0.4,-0.4 5.4)))"
  166. ,
  167. 26.0596168239, -0.2854871761);
  168. }
  169. int test_main(int, char* [])
  170. {
  171. test_all<bg::model::d2::point_xy<double> >();
  172. return 0;
  173. }