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 "../setop_output_type.hpp"
51 explicit ut_settings(double p = 0.0001, bool tv = true)
59 template <typename G1, typename G2, typename IntersectionOutput>
60 typename bg::default_area_result<G1>::type
62 IntersectionOutput const& intersection_output,
63 std::string const& caseid,
64 std::size_t expected_count, std::size_t expected_holes_count,
65 int expected_point_count, double expected_length_or_area,
66 ut_settings const& settings)
68 typedef typename boost::range_value<IntersectionOutput>::type OutputType;
69 bool const is_line = bg::geometry_id<OutputType>::type::value == 2;
71 typename bg::default_area_result<G1>::type length_or_area = 0;
73 std::size_t nholes = 0;
74 for (typename IntersectionOutput::const_iterator it = intersection_output.begin();
75 it != intersection_output.end();
78 if (expected_point_count > 0)
80 // here n should rather be of type std::size_t, but expected_point_count
81 // is set to -1 in some test cases so type int was left for now
82 n += static_cast<int>(bg::num_points(*it, true));
85 if (expected_holes_count > 0)
87 nholes += bg::num_interior_rings(*it);
90 // instead of specialization we check it run-time here
91 length_or_area += is_line
97 std::cout << std::setprecision(20) << bg::wkt(*it) << std::endl;
100 if (settings.test_validity)
103 bool const valid = bg::is_valid(*it, message);
104 BOOST_CHECK_MESSAGE(valid,
105 "intersection: " << caseid << " not valid " << message);
109 #if ! defined(BOOST_GEOMETRY_NO_BOOST_TEST)
110 #if ! defined(BOOST_GEOMETRY_NO_ROBUSTNESS)
111 if (expected_point_count > 0)
113 BOOST_CHECK_MESSAGE(bg::math::abs(n - expected_point_count) < 3,
114 "intersection: " << caseid
115 << " #points expected: " << expected_point_count
116 << " detected: " << n
117 << " type: " << (type_for_assert_message<G1, G2>())
122 if (expected_count > 0)
124 BOOST_CHECK_MESSAGE(intersection_output.size() == expected_count,
125 "intersection: " << caseid
126 << " #outputs expected: " << expected_count
127 << " detected: " << intersection_output.size()
128 << " type: " << (type_for_assert_message<G1, G2>())
132 if (expected_holes_count > 0)
135 BOOST_CHECK_MESSAGE(nholes == expected_holes_count,
136 "intersection: " << caseid
137 << " #holes expected: " << expected_holes_count
138 << " detected: " << nholes
139 << " type: " << (type_for_assert_message<G1, G2>())
143 double const detected_length_or_area = boost::numeric_cast<double>(length_or_area);
144 if (settings.percentage > 0.0)
146 BOOST_CHECK_CLOSE(detected_length_or_area, expected_length_or_area, settings.percentage);
150 // In some cases (geos_2) the intersection is either 0, or a tiny rectangle,
151 // depending on compiler/settings. That cannot be tested by CLOSE
152 BOOST_CHECK_LE(detected_length_or_area, expected_length_or_area);
156 return length_or_area;
160 template <typename OutputType, typename CalculationType, typename G1, typename G2>
161 typename bg::default_area_result<G1>::type test_intersection(std::string const& caseid,
162 G1 const& g1, G2 const& g2,
163 std::size_t expected_count = 0, std::size_t expected_holes_count = 0,
164 int expected_point_count = 0, double expected_length_or_area = 0,
165 ut_settings const& settings = ut_settings())
169 std::cout << std::endl << "case " << caseid << std::endl;
172 typedef typename bg::point_type<G1>::type point_type;
173 typedef typename setop_output_type<OutputType>::type result_type;
175 if (! settings.debug)
177 // Check _inserter behaviour with stratey
178 typedef typename bg::strategy::intersection::services::default_strategy
180 typename bg::cs_tag<point_type>::type
181 >::type strategy_type;
183 bg::detail::intersection::intersection_insert<OutputType>(g1, g2, std::back_inserter(clip), strategy_type());
186 typename bg::default_area_result<G1>::type length_or_area = 0;
188 // Check normal behaviour
189 result_type intersection_output;
190 bg::intersection(g1, g2, intersection_output);
192 check_result<G1, G2>(intersection_output, caseid, expected_count,
193 expected_holes_count, expected_point_count, expected_length_or_area,
196 // Check strategy passed explicitly
197 typedef typename bg::strategy::relate::services::default_strategy
200 >::type strategy_type;
201 intersection_output.clear();
202 bg::intersection(g1, g2, intersection_output, strategy_type());
204 check_result<G1, G2>(intersection_output, caseid, expected_count,
205 expected_holes_count, expected_point_count, expected_length_or_area,
208 // Check variant behaviour
209 intersection_output.clear();
210 bg::intersection(boost::variant<G1>(g1), g2, intersection_output);
212 check_result<G1, G2>(intersection_output, caseid, expected_count,
213 expected_holes_count, expected_point_count, expected_length_or_area,
216 intersection_output.clear();
217 bg::intersection(g1, boost::variant<G2>(g2), intersection_output);
219 check_result<G1, G2>(intersection_output, caseid, expected_count,
220 expected_holes_count, expected_point_count, expected_length_or_area,
223 intersection_output.clear();
224 bg::intersection(boost::variant<G1>(g1), boost::variant<G2>(g2), intersection_output);
226 check_result<G1, G2>(intersection_output, caseid, expected_count,
227 expected_holes_count, expected_point_count, expected_length_or_area,
230 #if defined(TEST_WITH_SVG)
232 bool const is_line = bg::geometry_id<OutputType>::type::value == 2;
233 typedef typename bg::coordinate_type<G1>::type coordinate_type;
236 bg::point_order<G1>::value == bg::counterclockwise
237 || bg::point_order<G2>::value == bg::counterclockwise;
239 bg::closure<G1>::value == bg::open
240 || bg::closure<G2>::value == bg::open;
242 std::ostringstream filename;
243 filename << "intersection_"
245 << string_from_type<coordinate_type>::name()
246 << string_from_type<CalculationType>::name()
247 << (ccw ? "_ccw" : "")
248 << (open ? "_open" : "")
249 #if defined(BOOST_GEOMETRY_NO_ROBUSTNESS)
254 std::ofstream svg(filename.str().c_str());
256 bg::svg_mapper<point_type> mapper(svg, 500, 500);
261 mapper.map(g1, is_line
262 ? "opacity:0.6;stroke:rgb(0,255,0);stroke-width:5"
263 : "fill-opacity:0.5;fill:rgb(153,204,0);"
264 "stroke:rgb(153,204,0);stroke-width:3");
265 mapper.map(g2, "fill-opacity:0.3;fill:rgb(51,51,153);"
266 "stroke:rgb(51,51,153);stroke-width:3");
268 for (typename result_type::const_iterator it = intersection_output.begin();
269 it != intersection_output.end(); ++it)
271 mapper.map(*it, "fill-opacity:0.2;stroke-opacity:0.4;fill:rgb(255,0,0);"
272 "stroke:rgb(255,0,255);stroke-width:8");
280 std::cout << "end case " << caseid << std::endl;
283 return length_or_area;
286 template <typename OutputType, typename CalculationType, typename G1, typename G2>
287 typename bg::default_area_result<G1>::type test_intersection(std::string const& caseid,
288 G1 const& g1, G2 const& g2,
289 std::size_t expected_count = 0, int expected_point_count = 0,
290 double expected_length_or_area = 0,
291 ut_settings const& settings = ut_settings())
293 return test_intersection<OutputType, CalculationType>(
294 caseid, g1, g2, expected_count, 0, expected_point_count,
295 expected_length_or_area, settings
299 template <typename OutputType, typename G1, typename G2>
300 typename bg::default_area_result<G1>::type test_one(std::string const& caseid,
301 std::string const& wkt1, std::string const& wkt2,
302 std::size_t expected_count = 0, std::size_t expected_holes_count = 0,
303 int expected_point_count = 0, double expected_length_or_area = 0,
304 ut_settings const& settings = ut_settings())
307 bg::read_wkt(wkt1, g1);
310 bg::read_wkt(wkt2, g2);
312 // Reverse if necessary
316 return test_intersection<OutputType, void>(caseid, g1, g2,
317 expected_count, expected_holes_count, expected_point_count,
318 expected_length_or_area, settings);
321 template <typename OutputType, typename G1, typename G2>
322 typename bg::default_area_result<G1>::type test_one(std::string const& caseid,
323 std::string const& wkt1, std::string const& wkt2,
324 std::size_t expected_count = 0, int expected_point_count = 0,
325 double expected_length_or_area = 0,
326 ut_settings const& settings = ut_settings())
328 return test_one<OutputType, G1, G2>(caseid, wkt1, wkt2,
329 expected_count, 0, expected_point_count,
330 expected_length_or_area,
334 template <typename OutputType, typename Areal, typename Linear>
335 void test_one_lp(std::string const& caseid,
336 std::string const& wkt_areal, std::string const& wkt_linear,
337 std::size_t expected_count = 0, int expected_point_count = 0,
338 double expected_length = 0,
339 ut_settings const& settings = ut_settings())
341 #ifdef BOOST_GEOMETRY_TEST_DEBUG
342 std::cout << caseid << " -- start" << std::endl;
345 bg::read_wkt(wkt_areal, areal);
349 bg::read_wkt(wkt_linear, linear);
351 test_intersection<OutputType, void>(caseid, areal, linear,
352 expected_count, expected_point_count,
353 expected_length, settings);
355 // A linestring reversed should deliver exactly the same.
358 test_intersection<OutputType, void>(caseid + "_rev", areal, linear,
359 expected_count, expected_point_count,
360 expected_length, settings);
361 #ifdef BOOST_GEOMETRY_TEST_DEBUG
362 std::cout << caseid << " -- end" << std::endl;
366 template <typename Geometry1, typename Geometry2>
367 void test_point_output(std::string const& wkt1, std::string const& wkt2, unsigned int expected_count)
370 bg::read_wkt(wkt1, g1);
374 bg::read_wkt(wkt2, g2);
377 bg::model::multi_point<typename bg::point_type<Geometry1>::type> points;
378 bg::intersection(g1, g2, points);
379 BOOST_CHECK_EQUAL(points.size(), expected_count);