1 // Boost.Geometry (aka GGL, Generic Geometry Library)
4 // Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands.
6 // This file was modified by Oracle on 2015, 2016, 2017.
7 // Modifications copyright (c) 2015-2017 Oracle and/or its affiliates.
8 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
9 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
11 // Use, modification and distribution is subject to the Boost Software License,
12 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
13 // http://www.boost.org/LICENSE_1_0.txt)
15 #ifndef BOOST_GEOMETRY_TEST_UNION_HPP
16 #define BOOST_GEOMETRY_TEST_UNION_HPP
20 #include <geometry_test_common.hpp>
21 #include <algorithms/check_validity.hpp>
22 #include "../setop_output_type.hpp"
24 #include <boost/core/ignore_unused.hpp>
25 #include <boost/foreach.hpp>
26 #include <boost/range/algorithm/copy.hpp>
28 #include <boost/geometry/algorithms/union.hpp>
30 #include <boost/geometry/algorithms/area.hpp>
31 #include <boost/geometry/algorithms/correct.hpp>
32 #include <boost/geometry/algorithms/is_empty.hpp>
33 #include <boost/geometry/algorithms/length.hpp>
34 #include <boost/geometry/algorithms/num_points.hpp>
35 #include <boost/geometry/algorithms/is_valid.hpp>
37 #include <boost/geometry/geometries/geometries.hpp>
39 #include <boost/geometry/strategies/strategies.hpp>
41 #include <boost/geometry/io/wkt/wkt.hpp>
44 #if defined(TEST_WITH_SVG)
45 # include <boost/geometry/io/svg/svg_mapper.hpp>
60 #if defined(BOOST_GEOMETRY_TEST_CHECK_VALID_INPUT)
61 template <typename Geometry>
62 inline void check_input_validity(std::string const& caseid, int case_index,
63 Geometry const& geometry)
66 if (!bg::is_valid(geometry, message))
68 std::cout << caseid << " Input ["
69 << case_index << "] not valid" << std::endl
70 << " (" << message << ")" << std::endl;
77 template <typename Range>
78 inline std::size_t num_points(Range const& rng, bool add_for_open = false)
80 std::size_t result = 0;
81 for (typename boost::range_iterator<Range const>::type it = boost::begin(rng);
82 it != boost::end(rng); ++it)
84 result += bg::num_points(*it, add_for_open);
89 template <typename OutputType, typename G1, typename G2>
90 void test_union(std::string const& caseid, G1 const& g1, G2 const& g2,
91 int expected_count, int expected_hole_count,
92 int expected_point_count, double expected_area,
93 ut_settings const& settings)
95 typedef typename bg::coordinate_type<G1>::type coordinate_type;
96 boost::ignore_unused<coordinate_type>();
97 boost::ignore_unused(expected_point_count);
99 // Declare output (vector of rings or multi_polygon)
100 typedef typename setop_output_type<OutputType>::type result_type;
103 #if defined(BOOST_GEOMETRY_DEBUG_ROBUSTNESS)
104 std::cout << "*** UNION " << caseid << std::endl;
107 #if defined(BOOST_GEOMETRY_TEST_CHECK_VALID_INPUT)
108 check_input_validity(caseid, 0, g1);
109 check_input_validity(caseid, 1, g2);
112 // Check normal behaviour
113 bg::union_(g1, g2, clip);
115 #if ! defined(BOOST_GEOMETRY_TEST_ONLY_ONE_TYPE)
117 // Check strategy passed explicitly
119 typedef typename bg::strategy::intersection::services::default_strategy
121 typename bg::cs_tag<OutputType>::type
122 >::type strategy_type;
123 bg::union_(g1, g2, clip_s, strategy_type());
124 BOOST_CHECK_EQUAL(num_points(clip), num_points(clip_s));
128 #if ! defined(BOOST_GEOMETRY_TEST_ALWAYS_CHECK_VALIDITY)
129 if (settings.test_validity)
133 bool const valid = check_validity<result_type>::apply(clip, caseid, g1, g2, message);
134 BOOST_CHECK_MESSAGE(valid,
135 "union: " << caseid << " not valid: " << message
136 << " type: " << (type_for_assert_message<G1, G2>()));
139 typename bg::default_area_result<OutputType>::type area = 0;
141 std::size_t holes = 0;
142 for (typename result_type::iterator it = clip.begin();
143 it != clip.end(); ++it)
145 area += bg::area(*it);
146 holes += bg::num_interior_rings(*it);
147 n += bg::num_points(*it, true);
151 #if ! defined(BOOST_GEOMETRY_TEST_ONLY_ONE_TYPE)
153 // Test inserter functionality
154 // Test if inserter returns output-iterator (using Boost.Range copy)
155 result_type inserted, array_with_one_empty_geometry;
156 array_with_one_empty_geometry.push_back(OutputType());
157 boost::copy(array_with_one_empty_geometry, bg::detail::union_::union_insert<OutputType>(g1, g2, std::back_inserter(inserted)));
159 typename bg::default_area_result<OutputType>::type area_inserted = 0;
161 for (typename result_type::iterator it = inserted.begin();
162 it != inserted.end();
165 // Skip the empty polygon created above to avoid the empty_input_exception
166 if (! bg::is_empty(*it))
168 area_inserted += bg::area(*it);
171 BOOST_CHECK_EQUAL(boost::size(clip), boost::size(inserted) - 1);
172 BOOST_CHECK_CLOSE(area_inserted, expected_area, settings.percentage);
178 #if defined(BOOST_GEOMETRY_DEBUG_ROBUSTNESS)
179 std::cout << "*** case: " << caseid
182 << " polygons: " << boost::size(clip)
183 << " holes: " << holes
187 BOOST_CHECK_MESSAGE(expected_count < 0 || int(clip.size()) == expected_count,
189 << " #clips expected: " << expected_count
190 << " detected: " << clip.size()
191 << " type: " << (type_for_assert_message<G1, G2>())
194 BOOST_CHECK_MESSAGE(expected_hole_count < 0 || int(holes) == expected_hole_count,
196 << " #holes expected: " << expected_hole_count
197 << " detected: " << holes
198 << " type: " << (type_for_assert_message<G1, G2>())
201 #if defined(BOOST_GEOMETRY_USE_RESCALING)
202 // Without rescaling, point count might easily differ (which is no problem)
203 BOOST_CHECK_MESSAGE(expected_point_count < 0 || std::abs(int(n) - expected_point_count) < 3,
205 << " #points expected: " << expected_point_count
206 << " detected: " << n
207 << " type: " << (type_for_assert_message<G1, G2>())
211 BOOST_CHECK_CLOSE(area, expected_area, settings.percentage);
213 #if defined(TEST_WITH_SVG)
216 bg::point_order<G1>::value == bg::counterclockwise
217 || bg::point_order<G2>::value == bg::counterclockwise;
219 bg::closure<G1>::value == bg::open
220 || bg::closure<G2>::value == bg::open;
222 std::ostringstream filename;
225 << string_from_type<coordinate_type>::name()
226 << (ccw ? "_ccw" : "")
227 << (open ? "_open" : "")
228 #if defined(BOOST_GEOMETRY_USE_RESCALING)
233 std::ofstream svg(filename.str().c_str());
237 typename bg::point_type<G2>::type
238 > mapper(svg, 500, 500);
242 mapper.map(g1, "fill-opacity:0.5;fill:rgb(153,204,0);"
243 "stroke:rgb(153,204,0);stroke-width:3");
244 mapper.map(g2, "fill-opacity:0.3;fill:rgb(51,51,153);"
245 "stroke:rgb(51,51,153);stroke-width:3");
246 //mapper.map(g1, "opacity:0.6;fill:rgb(0,0,255);stroke:rgb(0,0,0);stroke-width:1");
247 //mapper.map(g2, "opacity:0.6;fill:rgb(0,255,0);stroke:rgb(0,0,0);stroke-width:1");
249 for (typename result_type::const_iterator it = clip.begin();
250 it != clip.end(); ++it)
252 mapper.map(*it, "fill-opacity:0.2;stroke-opacity:0.4;fill:rgb(255,0,0);"
253 "stroke:rgb(255,0,255);stroke-width:8");
254 //mapper.map(*it, "opacity:0.6;fill:none;stroke:rgb(255,0,0);stroke-width:5");
260 template <typename OutputType, typename G1, typename G2>
261 void test_one(std::string const& caseid,
262 std::string const& wkt1, std::string const& wkt2,
263 int expected_count, int expected_hole_count,
264 int expected_point_count, double expected_area,
265 ut_settings const& settings = ut_settings())
268 bg::read_wkt(wkt1, g1);
271 bg::read_wkt(wkt2, g2);
273 // Reverse/close if necessary (e.g. G1/G2 are ccw and/or open)
277 test_union<OutputType>(caseid, g1, g2,
278 expected_count, expected_hole_count, expected_point_count,
279 expected_area, settings);