Imported Upstream version 1.64.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 "../setop_output_type.hpp"
22
23 #include <boost/core/ignore_unused.hpp>
24 #include <boost/range/algorithm/copy.hpp>
25
26 #include <boost/geometry/algorithms/union.hpp>
27
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>
34
35 #include <boost/geometry/geometries/geometries.hpp>
36
37 #include <boost/geometry/strategies/strategies.hpp>
38
39 #include <boost/geometry/io/wkt/wkt.hpp>
40
41
42 #if defined(TEST_WITH_SVG)
43 #  include <boost/geometry/io/svg/svg_mapper.hpp>
44 #endif
45
46 struct ut_settings
47 {
48     double percentage;
49     bool test_validity;
50
51     ut_settings()
52         : percentage(0.001)
53         , test_validity(true)
54     {}
55
56 };
57
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)
62 {
63     std::string message;
64     if (!bg::is_valid(geometry, message))
65     {
66         std::cout << caseid << " Input ["
67                   << case_index << "] not valid" << std::endl
68                   << "   ("  << message << ")" << std::endl;
69     }
70 }
71 #endif
72
73 template <typename Range>
74 inline std::size_t num_points(Range const& rng, bool add_for_open = false)
75 {
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)
79     {
80         result += bg::num_points(*it, add_for_open);
81     }
82     return result;
83 }
84
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)
90 {
91     typedef typename bg::coordinate_type<G1>::type coordinate_type;
92     boost::ignore_unused<coordinate_type>();
93     boost::ignore_unused(expected_point_count);
94
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;
98
99 #if defined(BOOST_GEOMETRY_DEBUG_ROBUSTNESS)
100     std::cout << "*** UNION " << caseid << std::endl;
101 #endif
102
103 #if defined(BOOST_GEOMETRY_TEST_CHECK_VALID_INPUT)
104     check_input_validity(caseid, 0, g1);
105     check_input_validity(caseid, 1, g2);
106 #endif
107
108     // Check normal behaviour
109     bg::union_(g1, g2, clip);
110
111     // Check strategy passed explicitly
112     typedef typename bg::strategy::intersection::services::default_strategy
113         <
114             typename bg::cs_tag<OutputType>::type
115         >::type strategy_type;
116     bg::union_(g1, g2, clip_s, strategy_type());
117
118     typename bg::default_area_result<OutputType>::type area = 0;
119     std::size_t n = 0;
120     std::size_t holes = 0;
121     for (typename result_type::iterator it = clip.begin();
122             it != clip.end(); ++it)
123     {
124         area += bg::area(*it);
125         holes += bg::num_interior_rings(*it);
126         n += bg::num_points(*it, true);
127
128         if (settings.test_validity)
129         {
130             // Check validity (currently on separate clips only)
131             // std::cout << bg::dsv(*it) << std::endl;
132             std::string message;
133             bool const valid = bg::is_valid(*it, message);
134             BOOST_CHECK_MESSAGE(valid,
135                 "union: " << caseid << " not valid " << message);
136         }
137     }
138
139
140 #if ! defined(BOOST_GEOMETRY_TEST_ONLY_ONE_TYPE)
141     {
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)));
147
148         typename bg::default_area_result<OutputType>::type area_inserted = 0;
149         int index = 0;
150         for (typename result_type::iterator it = inserted.begin();
151                 it != inserted.end();
152                 ++it, ++index)
153         {
154             // Skip the empty polygon created above to avoid the empty_input_exception
155             if (! bg::is_empty(*it))
156             {
157                 area_inserted += bg::area(*it);
158             }
159         }
160         BOOST_CHECK_EQUAL(boost::size(clip), boost::size(inserted) - 1);
161         BOOST_CHECK_CLOSE(area_inserted, expected_area, settings.percentage);
162     }
163 #endif
164
165
166
167 #if defined(BOOST_GEOMETRY_DEBUG_ROBUSTNESS)
168     std::cout << "*** case: " << caseid
169         << " area: " << area
170         << " points: " << n
171         << " polygons: " << boost::size(clip)
172         << " holes: " << holes
173         << std::endl;
174 #endif
175
176     BOOST_CHECK_MESSAGE(expected_count < 0 || int(clip.size()) == expected_count,
177             "union: " << caseid
178             << " #clips expected: " << expected_count
179             << " detected: " << clip.size()
180             << " type: " << (type_for_assert_message<G1, G2>())
181             );
182
183     BOOST_CHECK_MESSAGE(expected_hole_count < 0 || int(holes) == expected_hole_count,
184             "union: " << caseid
185             << " #holes expected: " << expected_hole_count
186             << " detected: " << holes
187             << " type: " << (type_for_assert_message<G1, G2>())
188             );
189
190 #if ! defined(BOOST_GEOMETRY_NO_ROBUSTNESS)
191     BOOST_CHECK_MESSAGE(expected_point_count < 0 || std::abs(int(n) - expected_point_count) < 3,
192             "union: " << caseid
193             << " #points expected: " << expected_point_count
194             << " detected: " << n
195             << " type: " << (type_for_assert_message<G1, G2>())
196             );
197 #endif
198
199     BOOST_CHECK_CLOSE(area, expected_area, settings.percentage);
200
201     BOOST_CHECK_EQUAL(num_points(clip), num_points(clip_s));
202
203 #if defined(TEST_WITH_SVG)
204     {
205         bool const ccw =
206             bg::point_order<G1>::value == bg::counterclockwise
207             || bg::point_order<G2>::value == bg::counterclockwise;
208         bool const open =
209             bg::closure<G1>::value == bg::open
210             || bg::closure<G2>::value == bg::open;
211
212         std::ostringstream filename;
213         filename << "union_"
214             << caseid << "_"
215             << string_from_type<coordinate_type>::name()
216             << (ccw ? "_ccw" : "")
217             << (open ? "_open" : "")
218 #if defined(BOOST_GEOMETRY_NO_ROBUSTNESS)
219             << "_no_rob"
220 #endif
221             << ".svg";
222
223         std::ofstream svg(filename.str().c_str());
224
225         bg::svg_mapper
226             <
227                 typename bg::point_type<G2>::type
228             > mapper(svg, 500, 500);
229         mapper.add(g1);
230         mapper.add(g2);
231
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");
238
239         for (typename result_type::const_iterator it = clip.begin();
240                 it != clip.end(); ++it)
241         {
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");
245         }
246     }
247 #endif
248 }
249
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())
256 {
257     G1 g1;
258     bg::read_wkt(wkt1, g1);
259
260     G2 g2;
261     bg::read_wkt(wkt2, g2);
262
263     // Reverse/close if necessary (e.g. G1/G2 are ccw and/or open)
264     bg::correct(g1);
265     bg::correct(g2);
266
267     test_union<OutputType>(caseid, g1, g2,
268         expected_count, expected_hole_count, expected_point_count,
269         expected_area, settings);
270 }
271
272 #endif