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