]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Boost.Geometry (aka GGL, Generic Geometry Library) |
2 | // Unit Test | |
3 | ||
4 | // Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands. | |
5 | ||
b32b8144 FG |
6 | // This file was modified by Oracle on 2016, 2017. |
7 | // Modifications copyright (c) 2016-2017, Oracle and/or its affiliates. | |
7c673cae FG |
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_DIFFERENCE_HPP | |
15 | #define BOOST_GEOMETRY_TEST_DIFFERENCE_HPP | |
16 | ||
17 | #include <fstream> | |
18 | #include <iomanip> | |
19 | ||
20 | #include <geometry_test_common.hpp> | |
21 | #include "../setop_output_type.hpp" | |
22 | ||
23 | #include <boost/core/ignore_unused.hpp> | |
24 | #include <boost/foreach.hpp> | |
25 | ||
26 | #include <boost/range/algorithm/copy.hpp> | |
27 | ||
28 | #include <boost/geometry/algorithms/correct.hpp> | |
29 | #include <boost/geometry/algorithms/difference.hpp> | |
30 | #include <boost/geometry/algorithms/sym_difference.hpp> | |
31 | ||
32 | #include <boost/geometry/algorithms/area.hpp> | |
33 | #include <boost/geometry/algorithms/is_valid.hpp> | |
34 | #include <boost/geometry/algorithms/length.hpp> | |
35 | #include <boost/geometry/algorithms/num_points.hpp> | |
36 | #include <boost/geometry/algorithms/num_interior_rings.hpp> | |
37 | #include <boost/geometry/algorithms/remove_spikes.hpp> | |
38 | ||
39 | #include <boost/geometry/geometries/geometries.hpp> | |
40 | ||
41 | ||
42 | #include <boost/geometry/geometries/multi_point.hpp> | |
43 | #include <boost/geometry/geometries/multi_linestring.hpp> | |
44 | #include <boost/geometry/geometries/multi_polygon.hpp> | |
45 | ||
46 | #include <boost/geometry/strategies/strategies.hpp> | |
47 | ||
48 | #include <boost/geometry/io/wkt/wkt.hpp> | |
49 | ||
50 | ||
51 | #if defined(TEST_WITH_SVG) | |
52 | # define BOOST_GEOMETRY_DEBUG_SEGMENT_IDENTIFIER | |
53 | # define BOOST_GEOMETRY_DEBUG_IDENTIFIER | |
54 | # include <boost/geometry/io/svg/svg_mapper.hpp> | |
55 | # include <boost/geometry/algorithms/detail/overlay/debug_turn_info.hpp> | |
56 | #endif | |
57 | ||
58 | ||
59 | struct ut_settings | |
60 | { | |
61 | double percentage; | |
62 | bool sym_difference; | |
63 | bool remove_spikes; | |
64 | ||
65 | bool test_validity; | |
66 | ||
67 | ut_settings() | |
68 | : percentage(0.0001) | |
69 | , sym_difference(true) | |
70 | , remove_spikes(false) | |
71 | , test_validity(true) | |
72 | {} | |
73 | ||
74 | }; | |
75 | ||
76 | inline ut_settings tolerance(double percentage) | |
77 | { | |
78 | ut_settings result; | |
79 | result.percentage = percentage; | |
80 | return result; | |
81 | } | |
82 | ||
83 | ||
84 | template <typename Output, typename G1, typename G2> | |
85 | void difference_output(std::string const& caseid, G1 const& g1, G2 const& g2, Output const& output) | |
86 | { | |
87 | boost::ignore_unused(caseid, g1, g2, output); | |
88 | ||
89 | #if defined(TEST_WITH_SVG) | |
90 | { | |
91 | typedef typename bg::coordinate_type<G1>::type coordinate_type; | |
92 | typedef typename bg::point_type<G1>::type point_type; | |
93 | ||
b32b8144 FG |
94 | bool const ccw = |
95 | bg::point_order<G1>::value == bg::counterclockwise | |
96 | || bg::point_order<G2>::value == bg::counterclockwise; | |
97 | bool const open = | |
98 | bg::closure<G1>::value == bg::open | |
99 | || bg::closure<G2>::value == bg::open; | |
100 | ||
7c673cae FG |
101 | std::ostringstream filename; |
102 | filename << "difference_" | |
103 | << caseid << "_" | |
104 | << string_from_type<coordinate_type>::name() | |
b32b8144 FG |
105 | << (ccw ? "_ccw" : "") |
106 | << (open ? "_open" : "") | |
107 | #if defined(BOOST_GEOMETRY_INCLUDE_SELF_TURNS) | |
108 | << "_self" | |
109 | #endif | |
7c673cae FG |
110 | #if defined(BOOST_GEOMETRY_NO_ROBUSTNESS) |
111 | << "_no_rob" | |
112 | #endif | |
113 | << ".svg"; | |
114 | ||
115 | std::ofstream svg(filename.str().c_str()); | |
116 | ||
117 | bg::svg_mapper<point_type> mapper(svg, 500, 500); | |
118 | ||
119 | mapper.add(g1); | |
120 | mapper.add(g2); | |
121 | ||
122 | mapper.map(g1, "fill-opacity:0.3;fill:rgb(51,51,153);stroke:rgb(51,51,153);stroke-width:3"); | |
123 | mapper.map(g2, "fill-opacity:0.5;fill:rgb(153,204,0);stroke:rgb(153,204,0);stroke-width:3"); | |
124 | ||
125 | ||
126 | for (typename Output::const_iterator it = output.begin(); it != output.end(); ++it) | |
127 | { | |
128 | mapper.map(*it, | |
129 | //sym ? "fill-opacity:0.2;stroke-opacity:0.4;fill:rgb(255,255,0);stroke:rgb(255,0,255);stroke-width:8" : | |
130 | "fill-opacity:0.2;stroke-opacity:0.4;fill:rgb(255,0,0);stroke:rgb(255,0,255);stroke-width:8"); | |
131 | } | |
132 | } | |
133 | #endif | |
134 | } | |
135 | ||
136 | template <typename OutputType, typename G1, typename G2> | |
137 | std::string test_difference(std::string const& caseid, G1 const& g1, G2 const& g2, | |
138 | int expected_count, int expected_rings_count, int expected_point_count, | |
139 | double expected_area, | |
140 | bool sym, | |
141 | ut_settings const& settings) | |
142 | { | |
143 | typedef typename bg::coordinate_type<G1>::type coordinate_type; | |
144 | boost::ignore_unused<coordinate_type>(); | |
145 | ||
146 | bg::model::multi_polygon<OutputType> result; | |
147 | ||
b32b8144 | 148 | |
7c673cae FG |
149 | if (sym) |
150 | { | |
151 | bg::sym_difference(g1, g2, result); | |
152 | } | |
153 | else | |
154 | { | |
155 | bg::difference(g1, g2, result); | |
156 | } | |
157 | ||
158 | if (settings.remove_spikes) | |
159 | { | |
160 | bg::remove_spikes(result); | |
161 | } | |
162 | ||
b32b8144 FG |
163 | #if ! defined(BOOST_GEOMETRY_TEST_ONLY_ONE_TYPE) |
164 | { | |
165 | bg::model::multi_polygon<OutputType> result_s; | |
166 | typedef typename bg::strategy::relate::services::default_strategy | |
167 | < | |
168 | G1, G2 | |
169 | >::type strategy_type; | |
170 | if (sym) | |
171 | { | |
172 | bg::sym_difference(g1, g2, result_s, strategy_type()); | |
173 | } | |
174 | else | |
175 | { | |
176 | bg::difference(g1, g2, result_s, strategy_type()); | |
177 | } | |
178 | ||
179 | if (settings.remove_spikes) | |
180 | { | |
181 | bg::remove_spikes(result_s); | |
182 | } | |
183 | BOOST_CHECK_EQUAL(bg::num_points(result), bg::num_points(result_s)); | |
184 | } | |
185 | #endif | |
186 | ||
187 | ||
7c673cae FG |
188 | std::ostringstream return_string; |
189 | return_string << bg::wkt(result); | |
190 | ||
191 | typename bg::default_area_result<G1>::type const area = bg::area(result); | |
192 | std::size_t const n = expected_point_count >= 0 | |
193 | ? bg::num_points(result) : 0; | |
194 | ||
195 | #if ! defined(BOOST_GEOMETRY_NO_BOOST_TEST) | |
196 | if (settings.test_validity) | |
197 | { | |
198 | // std::cout << bg::dsv(result) << std::endl; | |
199 | std::string message; | |
200 | bool const valid = bg::is_valid(result, message); | |
201 | BOOST_CHECK_MESSAGE(valid, | |
b32b8144 FG |
202 | "difference: " << caseid << " not valid " << message |
203 | << " type: " << (type_for_assert_message<G1, G2>())); | |
7c673cae FG |
204 | } |
205 | #endif | |
206 | ||
207 | difference_output(caseid, g1, g2, result); | |
208 | ||
b32b8144 FG |
209 | #if ! (defined(BOOST_GEOMETRY_TEST_ONLY_ONE_TYPE) \ |
210 | || defined(BOOST_GEOMETRY_DEBUG_ASSEMBLE)) | |
7c673cae FG |
211 | { |
212 | // Test inserter functionality | |
213 | // Test if inserter returns output-iterator (using Boost.Range copy) | |
214 | typedef typename bg::point_type<G1>::type point_type; | |
215 | typedef typename bg::rescale_policy_type<point_type>::type | |
216 | rescale_policy_type; | |
217 | ||
218 | rescale_policy_type rescale_policy | |
219 | = bg::get_rescale_policy<rescale_policy_type>(g1, g2); | |
220 | ||
221 | typename setop_output_type<OutputType>::type | |
222 | inserted, array_with_one_empty_geometry; | |
223 | array_with_one_empty_geometry.push_back(OutputType()); | |
224 | if (sym) | |
225 | { | |
226 | boost::copy(array_with_one_empty_geometry, | |
227 | bg::detail::sym_difference::sym_difference_insert<OutputType> | |
228 | (g1, g2, rescale_policy, std::back_inserter(inserted))); | |
229 | } | |
230 | else | |
231 | { | |
232 | boost::copy(array_with_one_empty_geometry, | |
233 | bg::detail::difference::difference_insert<OutputType>( | |
234 | g1, g2, rescale_policy, std::back_inserter(inserted))); | |
235 | } | |
236 | ||
237 | BOOST_CHECK_EQUAL(boost::size(result), boost::size(inserted) - 1); | |
238 | } | |
239 | #endif | |
240 | ||
241 | ||
242 | ||
243 | #if ! defined(BOOST_GEOMETRY_NO_BOOST_TEST) | |
244 | if (expected_point_count >= 0) | |
245 | { | |
246 | BOOST_CHECK_MESSAGE(bg::math::abs(int(n) - expected_point_count) < 3, | |
247 | "difference: " << caseid | |
248 | << " #points expected: " << expected_point_count | |
249 | << " detected: " << n | |
250 | << " type: " << (type_for_assert_message<G1, G2>()) | |
251 | ); | |
252 | } | |
253 | ||
254 | if (expected_count >= 0) | |
255 | { | |
256 | BOOST_CHECK_MESSAGE(int(result.size()) == expected_count, | |
257 | "difference: " << caseid | |
258 | << " #outputs expected: " << expected_count | |
259 | << " detected: " << result.size() | |
260 | << " type: " << (type_for_assert_message<G1, G2>()) | |
261 | ); | |
262 | } | |
263 | ||
264 | if (expected_rings_count >= 0) | |
265 | { | |
266 | int nrings = expected_count + bg::num_interior_rings(result); | |
267 | BOOST_CHECK_MESSAGE(nrings == expected_rings_count, | |
268 | "difference: " << caseid | |
269 | << " #rings expected: " << expected_rings_count | |
270 | << " detected: " << nrings | |
271 | << " type: " << (type_for_assert_message<G1, G2>()) | |
272 | ); | |
273 | } | |
274 | ||
275 | BOOST_CHECK_CLOSE(area, expected_area, settings.percentage); | |
276 | #endif | |
277 | ||
278 | ||
279 | return return_string.str(); | |
280 | } | |
281 | ||
282 | template <typename OutputType, typename G1, typename G2> | |
283 | std::string test_difference(std::string const& caseid, G1 const& g1, G2 const& g2, | |
284 | int expected_count, int expected_point_count, | |
285 | double expected_area, | |
286 | bool sym, | |
287 | ut_settings const& settings) | |
288 | { | |
289 | return test_difference<OutputType>(caseid, g1, g2, | |
290 | expected_count, -1, expected_point_count, expected_area, | |
291 | sym, settings); | |
292 | } | |
293 | ||
294 | #ifdef BOOST_GEOMETRY_CHECK_WITH_POSTGIS | |
295 | static int counter = 0; | |
296 | #endif | |
297 | ||
298 | ||
299 | template <typename OutputType, typename G1, typename G2> | |
300 | std::string test_one(std::string const& caseid, | |
301 | std::string const& wkt1, std::string const& wkt2, | |
302 | int expected_count1, | |
303 | int expected_rings_count1, | |
304 | int expected_point_count1, | |
305 | double expected_area1, | |
306 | int expected_count2, | |
307 | int expected_rings_count2, | |
308 | int expected_point_count2, | |
309 | double expected_area2, | |
310 | int expected_count_s, | |
311 | int expected_rings_count_s, | |
312 | int expected_point_count_s, | |
313 | double expected_area_s, | |
314 | ut_settings const& settings = ut_settings()) | |
315 | { | |
316 | G1 g1; | |
317 | bg::read_wkt(wkt1, g1); | |
318 | ||
319 | G2 g2; | |
320 | bg::read_wkt(wkt2, g2); | |
321 | ||
322 | bg::correct(g1); | |
323 | bg::correct(g2); | |
324 | ||
325 | std::string result = test_difference<OutputType>(caseid + "_a", g1, g2, | |
326 | expected_count1, expected_rings_count1, expected_point_count1, | |
327 | expected_area1, false, settings); | |
328 | ||
329 | #ifdef BOOST_GEOMETRY_DEBUG_ASSEMBLE | |
330 | return result; | |
331 | #endif | |
332 | ||
333 | test_difference<OutputType>(caseid + "_b", g2, g1, | |
334 | expected_count2, expected_rings_count2, expected_point_count2, | |
335 | expected_area2, false, settings); | |
336 | ||
337 | if (settings.sym_difference) | |
338 | { | |
339 | test_difference<OutputType>(caseid + "_s", g1, g2, | |
340 | expected_count_s, | |
341 | expected_rings_count_s, | |
342 | expected_point_count_s, | |
343 | expected_area_s, | |
344 | true, settings); | |
345 | } | |
346 | return result; | |
347 | } | |
348 | ||
349 | template <typename OutputType, typename G1, typename G2> | |
350 | std::string test_one(std::string const& caseid, | |
351 | std::string const& wkt1, std::string const& wkt2, | |
352 | int expected_count1, | |
353 | int expected_rings_count1, | |
354 | int expected_point_count1, | |
355 | double expected_area1, | |
356 | int expected_count2, | |
357 | int expected_rings_count2, | |
358 | int expected_point_count2, | |
359 | double expected_area2, | |
360 | ut_settings const& settings = ut_settings()) | |
361 | { | |
362 | return test_one<OutputType, G1, G2>(caseid, wkt1, wkt2, | |
363 | expected_count1, expected_rings_count1, expected_point_count1, expected_area1, | |
364 | expected_count2, expected_rings_count2, expected_point_count2, expected_area2, | |
365 | expected_count1 + expected_count2, | |
366 | expected_rings_count1 + expected_rings_count2, | |
367 | expected_point_count1 >= 0 && expected_point_count2 >= 0 | |
368 | ? (expected_point_count1 + expected_point_count2) : -1, | |
369 | expected_area1 + expected_area2, | |
370 | settings); | |
371 | } | |
372 | ||
373 | template <typename OutputType, typename G1, typename G2> | |
374 | std::string test_one(std::string const& caseid, | |
375 | std::string const& wkt1, std::string const& wkt2, | |
376 | int expected_count1, | |
377 | int expected_point_count1, | |
378 | double expected_area1, | |
379 | int expected_count2, | |
380 | int expected_point_count2, | |
381 | double expected_area2, | |
382 | int expected_count_s, | |
383 | int expected_point_count_s, | |
384 | double expected_area_s, | |
385 | ut_settings const& settings = ut_settings()) | |
386 | { | |
387 | return test_one<OutputType, G1, G2>(caseid, wkt1, wkt2, | |
388 | expected_count1, -1, expected_point_count1, expected_area1, | |
389 | expected_count2, -1, expected_point_count2, expected_area2, | |
390 | expected_count_s, -1, expected_point_count_s, expected_area_s, | |
391 | settings); | |
392 | } | |
393 | ||
394 | template <typename OutputType, typename G1, typename G2> | |
395 | std::string test_one(std::string const& caseid, | |
396 | std::string const& wkt1, std::string const& wkt2, | |
397 | int expected_count1, | |
398 | int expected_point_count1, | |
399 | double expected_area1, | |
400 | int expected_count2, | |
401 | int expected_point_count2, | |
402 | double expected_area2, | |
403 | ut_settings const& settings = ut_settings()) | |
404 | { | |
405 | return test_one<OutputType, G1, G2>(caseid, wkt1, wkt2, | |
406 | expected_count1, expected_point_count1, expected_area1, | |
407 | expected_count2, expected_point_count2, expected_area2, | |
408 | expected_count1 + expected_count2, | |
409 | expected_point_count1 >= 0 && expected_point_count2 >= 0 | |
410 | ? (expected_point_count1 + expected_point_count2) : -1, | |
411 | expected_area1 + expected_area2, | |
412 | settings); | |
413 | } | |
414 | ||
415 | template <typename OutputType, typename G1, typename G2> | |
416 | void test_one_lp(std::string const& caseid, | |
417 | std::string const& wkt1, std::string const& wkt2, | |
418 | std::size_t expected_count, | |
419 | int expected_point_count, | |
420 | double expected_length) | |
421 | { | |
422 | G1 g1; | |
423 | bg::read_wkt(wkt1, g1); | |
424 | ||
425 | G2 g2; | |
426 | bg::read_wkt(wkt2, g2); | |
427 | ||
428 | bg::correct(g1); | |
b32b8144 | 429 | bg::correct(g2); |
7c673cae FG |
430 | |
431 | typedef typename setop_output_type<OutputType>::type result_type; | |
432 | result_type pieces; | |
433 | bg::difference(g1, g2, pieces); | |
434 | ||
435 | typename bg::default_length_result<G1>::type length = 0; | |
436 | std::size_t n = 0; | |
437 | std::size_t piece_count = 0; | |
438 | for (typename result_type::iterator it = pieces.begin(); | |
439 | it != pieces.end(); | |
440 | ++it) | |
441 | { | |
442 | if (expected_point_count >= 0) | |
443 | { | |
444 | n += bg::num_points(*it); | |
445 | } | |
446 | piece_count++; | |
447 | length += bg::length(*it); | |
448 | } | |
449 | ||
450 | BOOST_CHECK_MESSAGE(piece_count == expected_count, | |
451 | "difference: " << caseid | |
452 | << " #outputs expected: " << expected_count | |
453 | << " detected: " << pieces.size() | |
454 | ); | |
455 | ||
456 | if (expected_point_count >= 0) | |
457 | { | |
458 | BOOST_CHECK_MESSAGE(n == std::size_t(expected_point_count), | |
459 | "difference: " << caseid | |
460 | << " #points expected: " << std::size_t(expected_point_count) | |
461 | << " detected: " << n | |
462 | << " type: " << (type_for_assert_message<G1, G2>()) | |
463 | ); | |
464 | } | |
465 | ||
466 | BOOST_CHECK_CLOSE(length, expected_length, 0.001); | |
467 | ||
468 | std::string lp = "lp_"; | |
469 | difference_output(lp + caseid, g1, g2, pieces); | |
470 | } | |
471 | ||
472 | ||
473 | #endif |