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 "../setop_output_type.hpp"
23 #include <boost/core/ignore_unused.hpp>
24 #include <boost/range/algorithm/copy.hpp>
26 #include <boost/geometry/algorithms/union.hpp>
28 #include <boost/geometry/algorithms/area.hpp>
29 #include <boost/geometry/algorithms/correct.hpp>
30 #include <boost/geometry/algorithms/is_empty.hpp>
31 #include <boost/geometry/algorithms/length.hpp>
32 #include <boost/geometry/algorithms/num_points.hpp>
33 #include <boost/geometry/algorithms/is_valid.hpp>
35 #include <boost/geometry/geometries/geometries.hpp>
37 #include <boost/geometry/strategies/strategies.hpp>
39 #include <boost/geometry/io/wkt/wkt.hpp>
42 #if defined(TEST_WITH_SVG)
43 # include <boost/geometry/io/svg/svg_mapper.hpp>
58 #if defined(BOOST_GEOMETRY_TEST_CHECK_VALID_INPUT)
59 template <typename Geometry>
60 inline void check_input_validity(std::string const& caseid, int case_index,
61 Geometry const& geometry)
64 if (!bg::is_valid(geometry, message))
66 std::cout << caseid << " Input ["
67 << case_index << "] not valid" << std::endl
68 << " (" << message << ")" << std::endl;
73 template <typename Range>
74 inline std::size_t num_points(Range const& rng, bool add_for_open = false)
76 std::size_t result = 0;
77 for (typename boost::range_iterator<Range const>::type it = boost::begin(rng);
78 it != boost::end(rng); ++it)
80 result += bg::num_points(*it, add_for_open);
85 template <typename OutputType, typename G1, typename G2>
86 void test_union(std::string const& caseid, G1 const& g1, G2 const& g2,
87 int expected_count, int expected_hole_count,
88 int expected_point_count, double expected_area,
89 ut_settings const& settings)
91 typedef typename bg::coordinate_type<G1>::type coordinate_type;
92 boost::ignore_unused<coordinate_type>();
93 boost::ignore_unused(expected_point_count);
95 // Declare output (vector of rings or multi_polygon)
96 typedef typename setop_output_type<OutputType>::type result_type;
97 result_type clip, clip_s;
99 #if defined(BOOST_GEOMETRY_DEBUG_ROBUSTNESS)
100 std::cout << "*** UNION " << caseid << std::endl;
103 #if defined(BOOST_GEOMETRY_TEST_CHECK_VALID_INPUT)
104 check_input_validity(caseid, 0, g1);
105 check_input_validity(caseid, 1, g2);
108 // Check normal behaviour
109 bg::union_(g1, g2, clip);
111 // Check strategy passed explicitly
112 typedef typename bg::strategy::intersection::services::default_strategy
114 typename bg::cs_tag<OutputType>::type
115 >::type strategy_type;
116 bg::union_(g1, g2, clip_s, strategy_type());
118 typename bg::default_area_result<OutputType>::type area = 0;
120 std::size_t holes = 0;
121 for (typename result_type::iterator it = clip.begin();
122 it != clip.end(); ++it)
124 area += bg::area(*it);
125 holes += bg::num_interior_rings(*it);
126 n += bg::num_points(*it, true);
128 if (settings.test_validity)
130 // Check validity (currently on separate clips only)
131 // std::cout << bg::dsv(*it) << std::endl;
133 bool const valid = bg::is_valid(*it, message);
134 BOOST_CHECK_MESSAGE(valid,
135 "union: " << caseid << " not valid " << message);
140 #if ! defined(BOOST_GEOMETRY_TEST_ONLY_ONE_TYPE)
142 // Test inserter functionality
143 // Test if inserter returns output-iterator (using Boost.Range copy)
144 result_type inserted, array_with_one_empty_geometry;
145 array_with_one_empty_geometry.push_back(OutputType());
146 boost::copy(array_with_one_empty_geometry, bg::detail::union_::union_insert<OutputType>(g1, g2, std::back_inserter(inserted)));
148 typename bg::default_area_result<OutputType>::type area_inserted = 0;
150 for (typename result_type::iterator it = inserted.begin();
151 it != inserted.end();
154 // Skip the empty polygon created above to avoid the empty_input_exception
155 if (! bg::is_empty(*it))
157 area_inserted += bg::area(*it);
160 BOOST_CHECK_EQUAL(boost::size(clip), boost::size(inserted) - 1);
161 BOOST_CHECK_CLOSE(area_inserted, expected_area, settings.percentage);
167 #if defined(BOOST_GEOMETRY_DEBUG_ROBUSTNESS)
168 std::cout << "*** case: " << caseid
171 << " polygons: " << boost::size(clip)
172 << " holes: " << holes
176 BOOST_CHECK_MESSAGE(expected_count < 0 || int(clip.size()) == expected_count,
178 << " #clips expected: " << expected_count
179 << " detected: " << clip.size()
180 << " type: " << (type_for_assert_message<G1, G2>())
183 BOOST_CHECK_MESSAGE(expected_hole_count < 0 || int(holes) == expected_hole_count,
185 << " #holes expected: " << expected_hole_count
186 << " detected: " << holes
187 << " type: " << (type_for_assert_message<G1, G2>())
190 #if ! defined(BOOST_GEOMETRY_NO_ROBUSTNESS)
191 BOOST_CHECK_MESSAGE(expected_point_count < 0 || std::abs(int(n) - expected_point_count) < 3,
193 << " #points expected: " << expected_point_count
194 << " detected: " << n
195 << " type: " << (type_for_assert_message<G1, G2>())
199 BOOST_CHECK_CLOSE(area, expected_area, settings.percentage);
201 BOOST_CHECK_EQUAL(num_points(clip), num_points(clip_s));
203 #if defined(TEST_WITH_SVG)
206 bg::point_order<G1>::value == bg::counterclockwise
207 || bg::point_order<G2>::value == bg::counterclockwise;
209 bg::closure<G1>::value == bg::open
210 || bg::closure<G2>::value == bg::open;
212 std::ostringstream filename;
215 << string_from_type<coordinate_type>::name()
216 << (ccw ? "_ccw" : "")
217 << (open ? "_open" : "")
218 #if defined(BOOST_GEOMETRY_NO_ROBUSTNESS)
223 std::ofstream svg(filename.str().c_str());
227 typename bg::point_type<G2>::type
228 > mapper(svg, 500, 500);
232 mapper.map(g1, "fill-opacity:0.5;fill:rgb(153,204,0);"
233 "stroke:rgb(153,204,0);stroke-width:3");
234 mapper.map(g2, "fill-opacity:0.3;fill:rgb(51,51,153);"
235 "stroke:rgb(51,51,153);stroke-width:3");
236 //mapper.map(g1, "opacity:0.6;fill:rgb(0,0,255);stroke:rgb(0,0,0);stroke-width:1");
237 //mapper.map(g2, "opacity:0.6;fill:rgb(0,255,0);stroke:rgb(0,0,0);stroke-width:1");
239 for (typename result_type::const_iterator it = clip.begin();
240 it != clip.end(); ++it)
242 mapper.map(*it, "fill-opacity:0.2;stroke-opacity:0.4;fill:rgb(255,0,0);"
243 "stroke:rgb(255,0,255);stroke-width:8");
244 //mapper.map(*it, "opacity:0.6;fill:none;stroke:rgb(255,0,0);stroke-width:5");
250 template <typename OutputType, typename G1, typename G2>
251 void test_one(std::string const& caseid,
252 std::string const& wkt1, std::string const& wkt2,
253 int expected_count, int expected_hole_count,
254 int expected_point_count, double expected_area,
255 ut_settings const& settings = ut_settings())
258 bg::read_wkt(wkt1, g1);
261 bg::read_wkt(wkt2, g2);
263 // Reverse/close if necessary (e.g. G1/G2 are ccw and/or open)
267 test_union<OutputType>(caseid, g1, g2,
268 expected_count, expected_hole_count, expected_point_count,
269 expected_area, settings);