test_union.hpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands.
  4. // This file was modified by Oracle on 2015, 2016, 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. #ifndef BOOST_GEOMETRY_TEST_UNION_HPP
  12. #define BOOST_GEOMETRY_TEST_UNION_HPP
  13. #include <fstream>
  14. #include <geometry_test_common.hpp>
  15. #include <algorithms/check_validity.hpp>
  16. #include "../setop_output_type.hpp"
  17. #include <boost/core/ignore_unused.hpp>
  18. #include <boost/foreach.hpp>
  19. #include <boost/range/algorithm/copy.hpp>
  20. #include <boost/geometry/algorithms/union.hpp>
  21. #include <boost/geometry/algorithms/area.hpp>
  22. #include <boost/geometry/algorithms/correct.hpp>
  23. #include <boost/geometry/algorithms/is_empty.hpp>
  24. #include <boost/geometry/algorithms/length.hpp>
  25. #include <boost/geometry/algorithms/num_points.hpp>
  26. #include <boost/geometry/algorithms/is_valid.hpp>
  27. #include <boost/geometry/geometries/geometries.hpp>
  28. #include <boost/geometry/strategies/strategies.hpp>
  29. #include <boost/geometry/io/wkt/wkt.hpp>
  30. #if defined(TEST_WITH_SVG)
  31. # include <boost/geometry/io/svg/svg_mapper.hpp>
  32. #endif
  33. struct ut_settings
  34. {
  35. double percentage;
  36. bool test_validity;
  37. ut_settings()
  38. : percentage(0.001)
  39. , test_validity(true)
  40. {}
  41. };
  42. #if defined(BOOST_GEOMETRY_TEST_CHECK_VALID_INPUT)
  43. template <typename Geometry>
  44. inline void check_input_validity(std::string const& caseid, int case_index,
  45. Geometry const& geometry)
  46. {
  47. std::string message;
  48. if (!bg::is_valid(geometry, message))
  49. {
  50. std::cout << caseid << " Input ["
  51. << case_index << "] not valid" << std::endl
  52. << " (" << message << ")" << std::endl;
  53. }
  54. }
  55. #endif
  56. template <typename Range>
  57. inline std::size_t num_points(Range const& rng, bool add_for_open = false)
  58. {
  59. std::size_t result = 0;
  60. for (typename boost::range_iterator<Range const>::type it = boost::begin(rng);
  61. it != boost::end(rng); ++it)
  62. {
  63. result += bg::num_points(*it, add_for_open);
  64. }
  65. return result;
  66. }
  67. template <typename OutputType, typename G1, typename G2>
  68. void test_union(std::string const& caseid, G1 const& g1, G2 const& g2,
  69. int expected_count, int expected_hole_count,
  70. int expected_point_count, double expected_area,
  71. ut_settings const& settings)
  72. {
  73. typedef typename bg::coordinate_type<G1>::type coordinate_type;
  74. boost::ignore_unused<coordinate_type>();
  75. boost::ignore_unused(expected_point_count);
  76. // Declare output (vector of rings or multi_polygon)
  77. typedef typename setop_output_type<OutputType>::type result_type;
  78. result_type clip;
  79. #if defined(BOOST_GEOMETRY_DEBUG_ROBUSTNESS)
  80. std::cout << "*** UNION " << caseid << std::endl;
  81. #endif
  82. #if defined(BOOST_GEOMETRY_TEST_CHECK_VALID_INPUT)
  83. check_input_validity(caseid, 0, g1);
  84. check_input_validity(caseid, 1, g2);
  85. #endif
  86. // Check normal behaviour
  87. bg::union_(g1, g2, clip);
  88. #if ! defined(BOOST_GEOMETRY_TEST_ONLY_ONE_TYPE)
  89. {
  90. // Check strategy passed explicitly
  91. result_type clip_s;
  92. typedef typename bg::strategy::intersection::services::default_strategy
  93. <
  94. typename bg::cs_tag<OutputType>::type
  95. >::type strategy_type;
  96. bg::union_(g1, g2, clip_s, strategy_type());
  97. BOOST_CHECK_EQUAL(num_points(clip), num_points(clip_s));
  98. }
  99. #endif
  100. #if ! defined(BOOST_GEOMETRY_TEST_ALWAYS_CHECK_VALIDITY)
  101. if (settings.test_validity)
  102. #endif
  103. {
  104. std::string message;
  105. bool const valid = check_validity<result_type>::apply(clip, caseid, g1, g2, message);
  106. BOOST_CHECK_MESSAGE(valid,
  107. "union: " << caseid << " not valid: " << message
  108. << " type: " << (type_for_assert_message<G1, G2>()));
  109. }
  110. typename bg::default_area_result<OutputType>::type area = 0;
  111. std::size_t n = 0;
  112. std::size_t holes = 0;
  113. for (typename result_type::iterator it = clip.begin();
  114. it != clip.end(); ++it)
  115. {
  116. area += bg::area(*it);
  117. holes += bg::num_interior_rings(*it);
  118. n += bg::num_points(*it, true);
  119. }
  120. #if ! defined(BOOST_GEOMETRY_TEST_ONLY_ONE_TYPE)
  121. {
  122. // Test inserter functionality
  123. // Test if inserter returns output-iterator (using Boost.Range copy)
  124. result_type inserted, array_with_one_empty_geometry;
  125. array_with_one_empty_geometry.push_back(OutputType());
  126. boost::copy(array_with_one_empty_geometry, bg::detail::union_::union_insert<OutputType>(g1, g2, std::back_inserter(inserted)));
  127. typename bg::default_area_result<OutputType>::type area_inserted = 0;
  128. int index = 0;
  129. for (typename result_type::iterator it = inserted.begin();
  130. it != inserted.end();
  131. ++it, ++index)
  132. {
  133. // Skip the empty polygon created above to avoid the empty_input_exception
  134. if (! bg::is_empty(*it))
  135. {
  136. area_inserted += bg::area(*it);
  137. }
  138. }
  139. BOOST_CHECK_EQUAL(boost::size(clip), boost::size(inserted) - 1);
  140. BOOST_CHECK_CLOSE(area_inserted, expected_area, settings.percentage);
  141. }
  142. #endif
  143. #if defined(BOOST_GEOMETRY_DEBUG_ROBUSTNESS)
  144. std::cout << "*** case: " << caseid
  145. << " area: " << area
  146. << " points: " << n
  147. << " polygons: " << boost::size(clip)
  148. << " holes: " << holes
  149. << std::endl;
  150. #endif
  151. BOOST_CHECK_MESSAGE(expected_count < 0 || int(clip.size()) == expected_count,
  152. "union: " << caseid
  153. << " #clips expected: " << expected_count
  154. << " detected: " << clip.size()
  155. << " type: " << (type_for_assert_message<G1, G2>())
  156. );
  157. BOOST_CHECK_MESSAGE(expected_hole_count < 0 || int(holes) == expected_hole_count,
  158. "union: " << caseid
  159. << " #holes expected: " << expected_hole_count
  160. << " detected: " << holes
  161. << " type: " << (type_for_assert_message<G1, G2>())
  162. );
  163. #if defined(BOOST_GEOMETRY_USE_RESCALING)
  164. // Without rescaling, point count might easily differ (which is no problem)
  165. BOOST_CHECK_MESSAGE(expected_point_count < 0 || std::abs(int(n) - expected_point_count) < 3,
  166. "union: " << caseid
  167. << " #points expected: " << expected_point_count
  168. << " detected: " << n
  169. << " type: " << (type_for_assert_message<G1, G2>())
  170. );
  171. #endif
  172. BOOST_CHECK_CLOSE(area, expected_area, settings.percentage);
  173. #if defined(TEST_WITH_SVG)
  174. {
  175. bool const ccw =
  176. bg::point_order<G1>::value == bg::counterclockwise
  177. || bg::point_order<G2>::value == bg::counterclockwise;
  178. bool const open =
  179. bg::closure<G1>::value == bg::open
  180. || bg::closure<G2>::value == bg::open;
  181. std::ostringstream filename;
  182. filename << "union_"
  183. << caseid << "_"
  184. << string_from_type<coordinate_type>::name()
  185. << (ccw ? "_ccw" : "")
  186. << (open ? "_open" : "")
  187. #if defined(BOOST_GEOMETRY_USE_RESCALING)
  188. << "_rescaled"
  189. #endif
  190. << ".svg";
  191. std::ofstream svg(filename.str().c_str());
  192. bg::svg_mapper
  193. <
  194. typename bg::point_type<G2>::type
  195. > mapper(svg, 500, 500);
  196. mapper.add(g1);
  197. mapper.add(g2);
  198. mapper.map(g1, "fill-opacity:0.5;fill:rgb(153,204,0);"
  199. "stroke:rgb(153,204,0);stroke-width:3");
  200. mapper.map(g2, "fill-opacity:0.3;fill:rgb(51,51,153);"
  201. "stroke:rgb(51,51,153);stroke-width:3");
  202. //mapper.map(g1, "opacity:0.6;fill:rgb(0,0,255);stroke:rgb(0,0,0);stroke-width:1");
  203. //mapper.map(g2, "opacity:0.6;fill:rgb(0,255,0);stroke:rgb(0,0,0);stroke-width:1");
  204. for (typename result_type::const_iterator it = clip.begin();
  205. it != clip.end(); ++it)
  206. {
  207. mapper.map(*it, "fill-opacity:0.2;stroke-opacity:0.4;fill:rgb(255,0,0);"
  208. "stroke:rgb(255,0,255);stroke-width:8");
  209. //mapper.map(*it, "opacity:0.6;fill:none;stroke:rgb(255,0,0);stroke-width:5");
  210. }
  211. }
  212. #endif
  213. }
  214. template <typename OutputType, typename G1, typename G2>
  215. void test_one(std::string const& caseid,
  216. std::string const& wkt1, std::string const& wkt2,
  217. int expected_count, int expected_hole_count,
  218. int expected_point_count, double expected_area,
  219. ut_settings const& settings = ut_settings())
  220. {
  221. G1 g1;
  222. bg::read_wkt(wkt1, g1);
  223. G2 g2;
  224. bg::read_wkt(wkt2, g2);
  225. // Reverse/close if necessary (e.g. G1/G2 are ccw and/or open)
  226. bg::correct(g1);
  227. bg::correct(g2);
  228. test_union<OutputType>(caseid, g1, g2,
  229. expected_count, expected_hole_count, expected_point_count,
  230. expected_area, settings);
  231. }
  232. #endif