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_INTERSECTION_HPP
15 #define BOOST_GEOMETRY_TEST_INTERSECTION_HPP
20 #include <boost/foreach.hpp>
21 #include <boost/variant/variant.hpp>
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>
31 #include <boost/geometry/geometries/geometries.hpp>
33 #include <boost/geometry/strategies/strategies.hpp>
35 #include <boost/geometry/io/wkt/wkt.hpp>
38 #if defined(TEST_WITH_SVG)
39 # include <boost/geometry/io/svg/svg_mapper.hpp>
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"
48 struct ut_settings : ut_base_settings
53 explicit ut_settings(double p = 0.0001, bool tv = true)
54 : ut_base_settings(tv)
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)
69 typedef typename boost::range_value<IntersectionOutput>::type OutputType;
70 bool const is_line = bg::geometry_id<OutputType>::type::value == 2;
72 typename bg::default_area_result<G1>::type length_or_area = 0;
74 std::size_t nholes = 0;
75 for (typename IntersectionOutput::const_iterator it = intersection_output.begin();
76 it != intersection_output.end();
79 if (! expected_count.empty())
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));
86 if (! expected_hole_count.empty())
88 nholes += bg::num_interior_rings(*it);
91 // instead of specialization we check it run-time here
92 length_or_area += is_line
98 std::cout << std::setprecision(20) << bg::wkt(*it) << std::endl;
102 if (settings.test_validity())
105 bool const valid = check_validity<IntersectionOutput>
106 ::apply(intersection_output, caseid, g1, g2, message);
108 BOOST_CHECK_MESSAGE(valid,
109 "intersection: " << caseid << " not valid: " << message
110 << " type: " << (type_for_assert_message<G1, G2>()));
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)
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>())
127 if (! expected_count.empty())
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>())
137 if (! expected_hole_count.empty())
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>())
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>()));
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())
167 std::cout << std::endl << "case " << caseid << std::endl;
170 typedef typename setop_output_type<OutputType>::type result_type;
172 typedef typename bg::point_type<G1>::type point_type;
173 boost::ignore_unused<point_type>();
175 #if ! defined(BOOST_GEOMETRY_TEST_ONLY_ONE_TYPE)
176 if (! settings.debug)
178 // Check _inserter behaviour with stratey
179 typedef typename bg::strategy::intersection::services::default_strategy
181 typename bg::cs_tag<point_type>::type
182 >::type strategy_type;
184 bg::detail::intersection::intersection_insert<OutputType>(g1, g2, std::back_inserter(clip), strategy_type());
188 typename bg::default_area_result<G1>::type length_or_area = 0;
190 // Check normal behaviour
191 result_type intersection_output;
192 bg::intersection(g1, g2, intersection_output);
194 check_result(intersection_output, caseid, g1, g2, expected_count,
195 expected_hole_count, expected_point_count, expected_length_or_area,
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);
203 check_result(intersection_output, caseid, g1, g2, expected_count,
204 expected_hole_count, expected_point_count, expected_length_or_area,
207 intersection_output.clear();
208 bg::intersection(g1, boost::variant<G2>(g2), intersection_output);
210 check_result(intersection_output, caseid, g1, g2, expected_count,
211 expected_hole_count, expected_point_count, expected_length_or_area,
214 intersection_output.clear();
215 bg::intersection(boost::variant<G1>(g1), boost::variant<G2>(g2), intersection_output);
217 check_result(intersection_output, caseid, g1, g2, expected_count,
218 expected_hole_count, expected_point_count, expected_length_or_area,
222 #if defined(TEST_WITH_SVG)
224 bool const is_line = bg::geometry_id<OutputType>::type::value == 2;
225 typedef typename bg::coordinate_type<G1>::type coordinate_type;
228 bg::point_order<G1>::value == bg::counterclockwise
229 || bg::point_order<G2>::value == bg::counterclockwise;
231 bg::closure<G1>::value == bg::open
232 || bg::closure<G2>::value == bg::open;
234 std::ostringstream filename;
235 filename << "intersection_"
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)
246 std::ofstream svg(filename.str().c_str());
248 bg::svg_mapper<point_type> mapper(svg, 500, 500);
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");
260 for (typename result_type::const_iterator it = intersection_output.begin();
261 it != intersection_output.end(); ++it)
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");
272 std::cout << "end case " << caseid << std::endl;
275 return length_or_area;
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())
285 return test_intersection<OutputType, CalculationType>(
286 caseid, g1, g2, expected_count, count_set(), expected_point_count,
287 expected_length_or_area, settings
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())
302 bg::read_wkt(wkt1, g1);
305 bg::read_wkt(wkt2, g2);
307 // Reverse if necessary
311 return test_intersection<OutputType, void>(caseid, g1, g2,
312 expected_count, expected_hole_count, expected_point_count,
313 expected_length_or_area, settings);
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())
325 return test_one<OutputType, G1, G2>(caseid, wkt1, wkt2,
326 expected_count, count_set(), expected_point_count,
327 expected_length_or_area,
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())
338 #ifdef BOOST_GEOMETRY_TEST_DEBUG
339 std::cout << caseid << " -- start" << std::endl;
342 bg::read_wkt(wkt_areal, areal);
346 bg::read_wkt(wkt_linear, linear);
348 test_intersection<OutputType, void>(caseid, areal, linear,
349 expected_count, expected_point_count,
350 expected_length, settings);
352 // A linestring reversed should deliver exactly the same.
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;
363 template <typename Geometry1, typename Geometry2>
364 void test_point_output(std::string const& wkt1, std::string const& wkt2, unsigned int expected_count)
367 bg::read_wkt(wkt1, g1);
371 bg::read_wkt(wkt2, g2);
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);