]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/geometry/test/algorithms/set_operations/intersection/test_intersection.hpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / libs / geometry / test / algorithms / set_operations / intersection / test_intersection.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 2016, 2017.
7 // Modifications copyright (c) 2016-2017, Oracle and/or its affiliates.
8 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
9
10 // Use, modification and distribution is subject to the Boost Software License,
11 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
12 // http://www.boost.org/LICENSE_1_0.txt)
13
14 #ifndef BOOST_GEOMETRY_TEST_INTERSECTION_HPP
15 #define BOOST_GEOMETRY_TEST_INTERSECTION_HPP
16
17 #include <fstream>
18 #include <iomanip>
19
20 #include <boost/foreach.hpp>
21 #include <boost/variant/variant.hpp>
22
23 #include <boost/geometry/algorithms/intersection.hpp>
24 #include <boost/geometry/algorithms/area.hpp>
25 #include <boost/geometry/algorithms/correct.hpp>
26 #include <boost/geometry/algorithms/is_valid.hpp>
27 #include <boost/geometry/algorithms/length.hpp>
28 #include <boost/geometry/algorithms/num_points.hpp>
29 #include <boost/geometry/algorithms/num_interior_rings.hpp>
30
31 #include <boost/geometry/geometries/geometries.hpp>
32
33 #include <boost/geometry/strategies/strategies.hpp>
34
35 #include <boost/geometry/io/wkt/wkt.hpp>
36
37
38 #if defined(TEST_WITH_SVG)
39 # include <boost/geometry/io/svg/svg_mapper.hpp>
40 #endif
41
42 #include <geometry_test_common.hpp>
43 #include <count_set.hpp>
44 #include <expectation_limits.hpp>
45 #include <algorithms/check_validity.hpp>
46 #include "../setop_output_type.hpp"
47
48 struct ut_settings : ut_base_settings
49 {
50 double percentage;
51 bool debug;
52
53 explicit ut_settings(double p = 0.0001, bool tv = true)
54 : ut_base_settings(tv)
55 , percentage(p)
56 , debug(false)
57 {}
58
59 };
60
61 template<typename IntersectionOutput, typename G1, typename G2>
62 void check_result(IntersectionOutput const& intersection_output,
63 std::string const& caseid,
64 G1 const& g1, G2 const& g2,
65 const count_set& expected_count, const count_set& expected_hole_count,
66 int expected_point_count, expectation_limits const& expected_length_or_area,
67 ut_settings const& settings)
68 {
69 typedef typename boost::range_value<IntersectionOutput>::type OutputType;
70 bool const is_line = bg::geometry_id<OutputType>::type::value == 2;
71
72 typename bg::default_area_result<G1>::type length_or_area = 0;
73 int n = 0;
74 std::size_t nholes = 0;
75 for (typename IntersectionOutput::const_iterator it = intersection_output.begin();
76 it != intersection_output.end();
77 ++it)
78 {
79 if (! expected_count.empty())
80 {
81 // here n should rather be of type std::size_t, but expected_point_count
82 // is set to -1 in some test cases so type int was left for now
83 n += static_cast<int>(bg::num_points(*it, true));
84 }
85
86 if (! expected_hole_count.empty())
87 {
88 nholes += bg::num_interior_rings(*it);
89 }
90
91 // instead of specialization we check it run-time here
92 length_or_area += is_line
93 ? bg::length(*it)
94 : bg::area(*it);
95
96 if (settings.debug)
97 {
98 std::cout << std::setprecision(20) << bg::wkt(*it) << std::endl;
99 }
100 }
101
102 if (settings.test_validity())
103 {
104 std::string message;
105 bool const valid = check_validity<IntersectionOutput>
106 ::apply(intersection_output, caseid, g1, g2, message);
107
108 BOOST_CHECK_MESSAGE(valid,
109 "intersection: " << caseid << " not valid: " << message
110 << " type: " << (type_for_assert_message<G1, G2>()));
111 }
112
113 #if ! defined(BOOST_GEOMETRY_NO_BOOST_TEST)
114 #if defined(BOOST_GEOMETRY_USE_RESCALING)
115 // Without rescaling, point count might easily differ (which is no problem)
116 if (expected_point_count > 0)
117 {
118 BOOST_CHECK_MESSAGE(bg::math::abs(n - expected_point_count) < 3,
119 "intersection: " << caseid
120 << " #points expected: " << expected_point_count
121 << " detected: " << n
122 << " type: " << (type_for_assert_message<G1, G2>())
123 );
124 }
125 #endif
126
127 if (! expected_count.empty())
128 {
129 BOOST_CHECK_MESSAGE(expected_count.has(intersection_output.size()),
130 "intersection: " << caseid
131 << " #outputs expected: " << expected_count
132 << " detected: " << intersection_output.size()
133 << " type: " << (type_for_assert_message<G1, G2>())
134 );
135 }
136
137 if (! expected_hole_count.empty())
138 {
139 BOOST_CHECK_MESSAGE(expected_hole_count.has(nholes),
140 "intersection: " << caseid
141 << " #holes expected: " << expected_hole_count
142 << " detected: " << nholes
143 << " type: " << (type_for_assert_message<G1, G2>())
144 );
145 }
146
147 BOOST_CHECK_MESSAGE(expected_length_or_area.contains(length_or_area, settings.percentage),
148 "intersection: " << caseid << std::setprecision(20)
149 << " #area expected: " << expected_length_or_area
150 << " detected: " << length_or_area
151 << " type: " << (type_for_assert_message<G1, G2>()));
152 #endif
153
154 }
155
156
157 template <typename OutputType, typename CalculationType, typename G1, typename G2>
158 typename bg::default_area_result<G1>::type test_intersection(std::string const& caseid,
159 G1 const& g1, G2 const& g2,
160 const count_set& expected_count = count_set(),
161 const count_set& expected_hole_count = count_set(),
162 int expected_point_count = 0, expectation_limits const& expected_length_or_area = 0,
163 ut_settings const& settings = ut_settings())
164 {
165 if (settings.debug)
166 {
167 std::cout << std::endl << "case " << caseid << std::endl;
168 }
169
170 typedef typename setop_output_type<OutputType>::type result_type;
171
172 typedef typename bg::point_type<G1>::type point_type;
173 boost::ignore_unused<point_type>();
174
175 #if ! defined(BOOST_GEOMETRY_TEST_ONLY_ONE_TYPE)
176 if (! settings.debug)
177 {
178 // Check _inserter behaviour with stratey
179 typedef typename bg::strategy::intersection::services::default_strategy
180 <
181 typename bg::cs_tag<point_type>::type
182 >::type strategy_type;
183 result_type clip;
184 bg::detail::intersection::intersection_insert<OutputType>(g1, g2, std::back_inserter(clip), strategy_type());
185 }
186 #endif
187
188 typename bg::default_area_result<G1>::type length_or_area = 0;
189
190 // Check normal behaviour
191 result_type intersection_output;
192 bg::intersection(g1, g2, intersection_output);
193
194 check_result(intersection_output, caseid, g1, g2, expected_count,
195 expected_hole_count, expected_point_count, expected_length_or_area,
196 settings);
197
198 #if ! defined(BOOST_GEOMETRY_TEST_ONLY_ONE_TYPE)
199 // Check variant behaviour
200 intersection_output.clear();
201 bg::intersection(boost::variant<G1>(g1), g2, intersection_output);
202
203 check_result(intersection_output, caseid, g1, g2, expected_count,
204 expected_hole_count, expected_point_count, expected_length_or_area,
205 settings);
206
207 intersection_output.clear();
208 bg::intersection(g1, boost::variant<G2>(g2), intersection_output);
209
210 check_result(intersection_output, caseid, g1, g2, expected_count,
211 expected_hole_count, expected_point_count, expected_length_or_area,
212 settings);
213
214 intersection_output.clear();
215 bg::intersection(boost::variant<G1>(g1), boost::variant<G2>(g2), intersection_output);
216
217 check_result(intersection_output, caseid, g1, g2, expected_count,
218 expected_hole_count, expected_point_count, expected_length_or_area,
219 settings);
220 #endif
221
222 #if defined(TEST_WITH_SVG)
223 {
224 bool const is_line = bg::geometry_id<OutputType>::type::value == 2;
225 typedef typename bg::coordinate_type<G1>::type coordinate_type;
226
227 bool const ccw =
228 bg::point_order<G1>::value == bg::counterclockwise
229 || bg::point_order<G2>::value == bg::counterclockwise;
230 bool const open =
231 bg::closure<G1>::value == bg::open
232 || bg::closure<G2>::value == bg::open;
233
234 std::ostringstream filename;
235 filename << "intersection_"
236 << caseid << "_"
237 << string_from_type<coordinate_type>::name()
238 << string_from_type<CalculationType>::name()
239 << (ccw ? "_ccw" : "")
240 << (open ? "_open" : "")
241 #if defined(BOOST_GEOMETRY_USE_RESCALING)
242 << "_rescaled"
243 #endif
244 << ".svg";
245
246 std::ofstream svg(filename.str().c_str());
247
248 bg::svg_mapper<point_type> mapper(svg, 500, 500);
249
250 mapper.add(g1);
251 mapper.add(g2);
252
253 mapper.map(g1, is_line
254 ? "opacity:0.6;stroke:rgb(0,255,0);stroke-width:5"
255 : "fill-opacity:0.5;fill:rgb(153,204,0);"
256 "stroke:rgb(153,204,0);stroke-width:3");
257 mapper.map(g2, "fill-opacity:0.3;fill:rgb(51,51,153);"
258 "stroke:rgb(51,51,153);stroke-width:3");
259
260 for (typename result_type::const_iterator it = intersection_output.begin();
261 it != intersection_output.end(); ++it)
262 {
263 mapper.map(*it, "fill-opacity:0.2;stroke-opacity:0.4;fill:rgb(255,0,0);"
264 "stroke:rgb(255,0,255);stroke-width:8");
265 }
266 }
267 #endif
268
269
270 if (settings.debug)
271 {
272 std::cout << "end case " << caseid << std::endl;
273 }
274
275 return length_or_area;
276 }
277
278 template <typename OutputType, typename CalculationType, typename G1, typename G2>
279 typename bg::default_area_result<G1>::type test_intersection(std::string const& caseid,
280 G1 const& g1, G2 const& g2,
281 const count_set& expected_count = count_set(), int expected_point_count = 0,
282 expectation_limits const& expected_length_or_area = 0,
283 ut_settings const& settings = ut_settings())
284 {
285 return test_intersection<OutputType, CalculationType>(
286 caseid, g1, g2, expected_count, count_set(), expected_point_count,
287 expected_length_or_area, settings
288 );
289 }
290
291 // Version with expected hole count
292 template <typename OutputType, typename G1, typename G2>
293 typename bg::default_area_result<G1>::type test_one(std::string const& caseid,
294 std::string const& wkt1, std::string const& wkt2,
295 const count_set& expected_count,
296 const count_set& expected_hole_count,
297 int expected_point_count,
298 expectation_limits const& expected_length_or_area,
299 ut_settings const& settings = ut_settings())
300 {
301 G1 g1;
302 bg::read_wkt(wkt1, g1);
303
304 G2 g2;
305 bg::read_wkt(wkt2, g2);
306
307 // Reverse if necessary
308 bg::correct(g1);
309 bg::correct(g2);
310
311 return test_intersection<OutputType, void>(caseid, g1, g2,
312 expected_count, expected_hole_count, expected_point_count,
313 expected_length_or_area, settings);
314 }
315
316 // Version without expected hole count
317 template <typename OutputType, typename G1, typename G2>
318 typename bg::default_area_result<G1>::type test_one(std::string const& caseid,
319 std::string const& wkt1, std::string const& wkt2,
320 const count_set& expected_count,
321 int expected_point_count,
322 expectation_limits const& expected_length_or_area,
323 ut_settings const& settings = ut_settings())
324 {
325 return test_one<OutputType, G1, G2>(caseid, wkt1, wkt2,
326 expected_count, count_set(), expected_point_count,
327 expected_length_or_area,
328 settings);
329 }
330
331 template <typename OutputType, typename Areal, typename Linear>
332 void test_one_lp(std::string const& caseid,
333 std::string const& wkt_areal, std::string const& wkt_linear,
334 const count_set& expected_count = count_set(), int expected_point_count = 0,
335 expectation_limits const& expected_length = 0,
336 ut_settings const& settings = ut_settings())
337 {
338 #ifdef BOOST_GEOMETRY_TEST_DEBUG
339 std::cout << caseid << " -- start" << std::endl;
340 #endif
341 Areal areal;
342 bg::read_wkt(wkt_areal, areal);
343 bg::correct(areal);
344
345 Linear linear;
346 bg::read_wkt(wkt_linear, linear);
347
348 test_intersection<OutputType, void>(caseid, areal, linear,
349 expected_count, expected_point_count,
350 expected_length, settings);
351
352 // A linestring reversed should deliver exactly the same.
353 bg::reverse(linear);
354
355 test_intersection<OutputType, void>(caseid + "_rev", areal, linear,
356 expected_count, expected_point_count,
357 expected_length, settings);
358 #ifdef BOOST_GEOMETRY_TEST_DEBUG
359 std::cout << caseid << " -- end" << std::endl;
360 #endif
361 }
362
363 template <typename Geometry1, typename Geometry2>
364 void test_point_output(std::string const& wkt1, std::string const& wkt2, unsigned int expected_count)
365 {
366 Geometry1 g1;
367 bg::read_wkt(wkt1, g1);
368 bg::correct(g1);
369
370 Geometry2 g2;
371 bg::read_wkt(wkt2, g2);
372 bg::correct(g2);
373
374 bg::model::multi_point<typename bg::point_type<Geometry1>::type> points;
375 bg::intersection(g1, g2, points);
376 BOOST_CHECK_EQUAL(points.size(), expected_count);
377 }
378
379
380 #endif