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 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
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)
14 #ifndef BOOST_GEOMETRY_TEST_DIFFERENCE_HPP
15 #define BOOST_GEOMETRY_TEST_DIFFERENCE_HPP
20 #include <geometry_test_common.hpp>
21 #include "../setop_output_type.hpp"
23 #include <boost/core/ignore_unused.hpp>
24 #include <boost/foreach.hpp>
26 #include <boost/range/algorithm/copy.hpp>
28 #include <boost/geometry/algorithms/correct.hpp>
29 #include <boost/geometry/algorithms/difference.hpp>
30 #include <boost/geometry/algorithms/sym_difference.hpp>
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>
39 #include <boost/geometry/geometries/geometries.hpp>
42 #include <boost/geometry/geometries/multi_point.hpp>
43 #include <boost/geometry/geometries/multi_linestring.hpp>
44 #include <boost/geometry/geometries/multi_polygon.hpp>
46 #include <boost/geometry/strategies/strategies.hpp>
48 #include <boost/geometry/io/wkt/wkt.hpp>
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>
69 , sym_difference(true)
70 , remove_spikes(false)
76 inline ut_settings tolerance(double percentage)
79 result.percentage = percentage;
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)
87 boost::ignore_unused(caseid, g1, g2, output);
89 #if defined(TEST_WITH_SVG)
91 typedef typename bg::coordinate_type<G1>::type coordinate_type;
92 typedef typename bg::point_type<G1>::type point_type;
95 bg::point_order<G1>::value == bg::counterclockwise
96 || bg::point_order<G2>::value == bg::counterclockwise;
98 bg::closure<G1>::value == bg::open
99 || bg::closure<G2>::value == bg::open;
101 std::ostringstream filename;
102 filename << "difference_"
104 << string_from_type<coordinate_type>::name()
105 << (ccw ? "_ccw" : "")
106 << (open ? "_open" : "")
107 #if defined(BOOST_GEOMETRY_INCLUDE_SELF_TURNS)
110 #if defined(BOOST_GEOMETRY_NO_ROBUSTNESS)
115 std::ofstream svg(filename.str().c_str());
117 bg::svg_mapper<point_type> mapper(svg, 500, 500);
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");
126 for (typename Output::const_iterator it = output.begin(); it != output.end(); ++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");
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,
141 ut_settings const& settings)
143 typedef typename bg::coordinate_type<G1>::type coordinate_type;
144 boost::ignore_unused<coordinate_type>();
146 bg::model::multi_polygon<OutputType> result;
151 bg::sym_difference(g1, g2, result);
155 bg::difference(g1, g2, result);
158 if (settings.remove_spikes)
160 bg::remove_spikes(result);
163 #if ! defined(BOOST_GEOMETRY_TEST_ONLY_ONE_TYPE)
165 bg::model::multi_polygon<OutputType> result_s;
166 typedef typename bg::strategy::relate::services::default_strategy
169 >::type strategy_type;
172 bg::sym_difference(g1, g2, result_s, strategy_type());
176 bg::difference(g1, g2, result_s, strategy_type());
179 if (settings.remove_spikes)
181 bg::remove_spikes(result_s);
183 BOOST_CHECK_EQUAL(bg::num_points(result), bg::num_points(result_s));
188 std::ostringstream return_string;
189 return_string << bg::wkt(result);
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;
195 #if ! defined(BOOST_GEOMETRY_NO_BOOST_TEST)
196 if (settings.test_validity)
198 // std::cout << bg::dsv(result) << std::endl;
200 bool const valid = bg::is_valid(result, message);
201 BOOST_CHECK_MESSAGE(valid,
202 "difference: " << caseid << " not valid " << message
203 << " type: " << (type_for_assert_message<G1, G2>()));
207 difference_output(caseid, g1, g2, result);
209 #if ! (defined(BOOST_GEOMETRY_TEST_ONLY_ONE_TYPE) \
210 || defined(BOOST_GEOMETRY_DEBUG_ASSEMBLE))
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
218 rescale_policy_type rescale_policy
219 = bg::get_rescale_policy<rescale_policy_type>(g1, g2);
221 typename setop_output_type<OutputType>::type
222 inserted, array_with_one_empty_geometry;
223 array_with_one_empty_geometry.push_back(OutputType());
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)));
232 boost::copy(array_with_one_empty_geometry,
233 bg::detail::difference::difference_insert<OutputType>(
234 g1, g2, rescale_policy, std::back_inserter(inserted)));
237 BOOST_CHECK_EQUAL(boost::size(result), boost::size(inserted) - 1);
243 #if ! defined(BOOST_GEOMETRY_NO_BOOST_TEST)
244 if (expected_point_count >= 0)
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>())
254 if (expected_count >= 0)
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>())
264 if (expected_rings_count >= 0)
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>())
275 BOOST_CHECK_CLOSE(area, expected_area, settings.percentage);
279 return return_string.str();
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,
287 ut_settings const& settings)
289 return test_difference<OutputType>(caseid, g1, g2,
290 expected_count, -1, expected_point_count, expected_area,
294 #ifdef BOOST_GEOMETRY_CHECK_WITH_POSTGIS
295 static int counter = 0;
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,
303 int expected_rings_count1,
304 int expected_point_count1,
305 double expected_area1,
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())
317 bg::read_wkt(wkt1, g1);
320 bg::read_wkt(wkt2, g2);
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);
329 #ifdef BOOST_GEOMETRY_DEBUG_ASSEMBLE
333 test_difference<OutputType>(caseid + "_b", g2, g1,
334 expected_count2, expected_rings_count2, expected_point_count2,
335 expected_area2, false, settings);
337 if (settings.sym_difference)
339 test_difference<OutputType>(caseid + "_s", g1, g2,
341 expected_rings_count_s,
342 expected_point_count_s,
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,
353 int expected_rings_count1,
354 int expected_point_count1,
355 double expected_area1,
357 int expected_rings_count2,
358 int expected_point_count2,
359 double expected_area2,
360 ut_settings const& settings = ut_settings())
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,
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,
377 int expected_point_count1,
378 double expected_area1,
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())
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,
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,
398 int expected_point_count1,
399 double expected_area1,
401 int expected_point_count2,
402 double expected_area2,
403 ut_settings const& settings = ut_settings())
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,
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)
423 bg::read_wkt(wkt1, g1);
426 bg::read_wkt(wkt2, g2);
431 typedef typename setop_output_type<OutputType>::type result_type;
433 bg::difference(g1, g2, pieces);
435 typename bg::default_length_result<G1>::type length = 0;
437 std::size_t piece_count = 0;
438 for (typename result_type::iterator it = pieces.begin();
442 if (expected_point_count >= 0)
444 n += bg::num_points(*it);
447 length += bg::length(*it);
450 BOOST_CHECK_MESSAGE(piece_count == expected_count,
451 "difference: " << caseid
452 << " #outputs expected: " << expected_count
453 << " detected: " << pieces.size()
456 if (expected_point_count >= 0)
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>())
466 BOOST_CHECK_CLOSE(length, expected_length, 0.001);
468 std::string lp = "lp_";
469 difference_output(lp + caseid, g1, g2, pieces);