Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / geometry / test / algorithms / set_operations / union / test_union.hpp
1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 // Unit Test
3
4 // Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands.
5
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
10
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)
14
15 #ifndef BOOST_GEOMETRY_TEST_UNION_HPP
16 #define BOOST_GEOMETRY_TEST_UNION_HPP
17
18 #include <fstream>
19
20 #include <geometry_test_common.hpp>
21 #include <algorithms/check_validity.hpp>
22 #include "../setop_output_type.hpp"
23
24 #include <boost/core/ignore_unused.hpp>
25 #include <boost/foreach.hpp>
26 #include <boost/range/algorithm/copy.hpp>
27
28 #include <boost/geometry/algorithms/union.hpp>
29
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>
36
37 #include <boost/geometry/geometries/geometries.hpp>
38
39 #include <boost/geometry/strategies/strategies.hpp>
40
41 #include <boost/geometry/io/wkt/wkt.hpp>
42
43
44 #if defined(TEST_WITH_SVG)
45 #  include <boost/geometry/io/svg/svg_mapper.hpp>
46 #endif
47
48 struct ut_settings
49 {
50     double percentage;
51     bool test_validity;
52
53     ut_settings()
54         : percentage(0.001)
55         , test_validity(true)
56     {}
57
58 };
59
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)
64 {
65     std::string message;
66     if (!bg::is_valid(geometry, message))
67     {
68         std::cout << caseid << " Input ["
69                   << case_index << "] not valid" << std::endl
70                   << "   ("  << message << ")" << std::endl;
71     }
72 }
73 #endif
74
75
76
77 template <typename Range>
78 inline std::size_t num_points(Range const& rng, bool add_for_open = false)
79 {
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)
83     {
84         result += bg::num_points(*it, add_for_open);
85     }
86     return result;
87 }
88
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)
94 {
95     typedef typename bg::coordinate_type<G1>::type coordinate_type;
96     boost::ignore_unused<coordinate_type>();
97     boost::ignore_unused(expected_point_count);
98
99     // Declare output (vector of rings or multi_polygon)
100     typedef typename setop_output_type<OutputType>::type result_type;
101     result_type clip;
102
103 #if defined(BOOST_GEOMETRY_DEBUG_ROBUSTNESS)
104     std::cout << "*** UNION " << caseid << std::endl;
105 #endif
106
107 #if defined(BOOST_GEOMETRY_TEST_CHECK_VALID_INPUT)
108     check_input_validity(caseid, 0, g1);
109     check_input_validity(caseid, 1, g2);
110 #endif
111
112     // Check normal behaviour
113     bg::union_(g1, g2, clip);
114
115 #if ! defined(BOOST_GEOMETRY_TEST_ONLY_ONE_TYPE)
116     {
117         // Check strategy passed explicitly
118         result_type clip_s;
119         typedef typename bg::strategy::intersection::services::default_strategy
120             <
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));
125     }
126 #endif
127
128 #if ! defined(BOOST_GEOMETRY_TEST_ALWAYS_CHECK_VALIDITY)
129     if (settings.test_validity)
130 #endif
131     {
132         std::string message;
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>()));
137     }
138
139     typename bg::default_area_result<OutputType>::type area = 0;
140     std::size_t n = 0;
141     std::size_t holes = 0;
142     for (typename result_type::iterator it = clip.begin();
143             it != clip.end(); ++it)
144     {
145         area += bg::area(*it);
146         holes += bg::num_interior_rings(*it);
147         n += bg::num_points(*it, true);
148     }
149
150
151 #if ! defined(BOOST_GEOMETRY_TEST_ONLY_ONE_TYPE)
152     {
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)));
158
159         typename bg::default_area_result<OutputType>::type area_inserted = 0;
160         int index = 0;
161         for (typename result_type::iterator it = inserted.begin();
162                 it != inserted.end();
163                 ++it, ++index)
164         {
165             // Skip the empty polygon created above to avoid the empty_input_exception
166             if (! bg::is_empty(*it))
167             {
168                 area_inserted += bg::area(*it);
169             }
170         }
171         BOOST_CHECK_EQUAL(boost::size(clip), boost::size(inserted) - 1);
172         BOOST_CHECK_CLOSE(area_inserted, expected_area, settings.percentage);
173     }
174 #endif
175
176
177
178 #if defined(BOOST_GEOMETRY_DEBUG_ROBUSTNESS)
179     std::cout << "*** case: " << caseid
180         << " area: " << area
181         << " points: " << n
182         << " polygons: " << boost::size(clip)
183         << " holes: " << holes
184         << std::endl;
185 #endif
186
187     BOOST_CHECK_MESSAGE(expected_count < 0 || int(clip.size()) == expected_count,
188             "union: " << caseid
189             << " #clips expected: " << expected_count
190             << " detected: " << clip.size()
191             << " type: " << (type_for_assert_message<G1, G2>())
192             );
193
194     BOOST_CHECK_MESSAGE(expected_hole_count < 0 || int(holes) == expected_hole_count,
195             "union: " << caseid
196             << " #holes expected: " << expected_hole_count
197             << " detected: " << holes
198             << " type: " << (type_for_assert_message<G1, G2>())
199             );
200
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,
204             "union: " << caseid
205             << " #points expected: " << expected_point_count
206             << " detected: " << n
207             << " type: " << (type_for_assert_message<G1, G2>())
208             );
209 #endif
210
211     BOOST_CHECK_CLOSE(area, expected_area, settings.percentage);
212
213 #if defined(TEST_WITH_SVG)
214     {
215         bool const ccw =
216             bg::point_order<G1>::value == bg::counterclockwise
217             || bg::point_order<G2>::value == bg::counterclockwise;
218         bool const open =
219             bg::closure<G1>::value == bg::open
220             || bg::closure<G2>::value == bg::open;
221
222         std::ostringstream filename;
223         filename << "union_"
224             << caseid << "_"
225             << string_from_type<coordinate_type>::name()
226             << (ccw ? "_ccw" : "")
227             << (open ? "_open" : "")
228 #if defined(BOOST_GEOMETRY_USE_RESCALING)
229             << "_rescaled"
230 #endif
231             << ".svg";
232
233         std::ofstream svg(filename.str().c_str());
234
235         bg::svg_mapper
236             <
237                 typename bg::point_type<G2>::type
238             > mapper(svg, 500, 500);
239         mapper.add(g1);
240         mapper.add(g2);
241
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");
248
249         for (typename result_type::const_iterator it = clip.begin();
250                 it != clip.end(); ++it)
251         {
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");
255         }
256     }
257 #endif
258 }
259
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())
266 {
267     G1 g1;
268     bg::read_wkt(wkt1, g1);
269
270     G2 g2;
271     bg::read_wkt(wkt2, g2);
272
273     // Reverse/close if necessary (e.g. G1/G2 are ccw and/or open)
274     bg::correct(g1);
275     bg::correct(g2);
276
277     test_union<OutputType>(caseid, g1, g2,
278         expected_count, expected_hole_count, expected_point_count,
279         expected_area, settings);
280 }
281
282 #endif