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
|
// Boost.Geometry (aka GGL, Generic Geometry Library)
// Unit Test
// Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
// This file was modified by Oracle on 2021-2023.
// Modifications copyright (c) 2021-2023 Oracle and/or its affiliates.
// Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
// 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_SIMPLIFY_HPP
#define BOOST_GEOMETRY_TEST_SIMPLIFY_HPP
// Test-functionality, shared between single and multi tests
#include <iomanip>
#include <sstream>
#include <geometry_test_common.hpp>
#include <boost/geometry/algorithms/correct_closure.hpp>
#include <boost/geometry/algorithms/equals.hpp>
#include <boost/geometry/algorithms/simplify.hpp>
#include <boost/geometry/algorithms/distance.hpp>
#include <boost/geometry/geometries/geometry_collection.hpp>
#include <boost/geometry/strategies/concepts/simplify_concept.hpp>
#include <boost/geometry/strategies/strategies.hpp>
#include <boost/geometry/io/wkt/wkt.hpp>
#include <boost/variant/variant.hpp>
template <typename Geometry, typename Tag = typename bg::tag<Geometry>::type>
struct boost_variant_type
{
using type = boost::variant<Geometry, typename bg::point_type<Geometry>::type>;
};
template <typename Geometry>
struct boost_variant_type<Geometry, bg::point_tag>
{
using type = boost::variant<Geometry>;
};
template
<
typename GeometryForTag,
typename Tag = typename bg::tag<GeometryForTag>::type
>
struct test_equality
{
template <typename Geometry, typename Expected>
static void apply(Geometry const& geometry, Expected const& expected)
{
// Verify both spatially equal AND number of points, because several
// of the tests only check explicitly on collinear points being
// simplified away
bool const result
= bg::equals(geometry, expected)
&& bg::num_points(geometry) == bg::num_points(expected);
BOOST_CHECK_MESSAGE(result,
" result: " << bg::wkt(geometry) << " " << bg::area(geometry)
<< " expected: " << bg::wkt(expected) << " " << bg::area(expected));
}
};
// Linestring does NOT yet have "geometry::equals" implemented
// Until then, WKT's are compared (which is acceptable for linestrings, but not
// for polygons, because simplify might rotate them)
template <typename GeometryForTag>
struct test_equality<GeometryForTag, bg::linestring_tag>
{
template <typename Geometry, typename Expected>
static void apply(Geometry const& geometry, Expected const& expected)
{
std::ostringstream out1, out2;
out1 << bg::wkt(geometry);
out2 << bg::wkt(expected);
BOOST_CHECK_EQUAL(out1.str(), out2.str());
}
};
template <typename Tag>
struct test_inserter
{
template <typename Geometry, typename Expected>
static void apply(Geometry& , Expected const& , double )
{}
};
template <>
struct test_inserter<bg::linestring_tag>
{
template <typename Geometry, typename Expected, typename DistanceMeasure>
static void apply(Geometry& geometry,
Expected const& expected,
DistanceMeasure const& distance)
{
{
Geometry simplified;
bg::detail::simplify::simplify_insert(geometry,
std::back_inserter(simplified), distance);
test_equality<Geometry>::apply(simplified, expected);
}
#ifdef TEST_PULL89
{
typedef typename bg::point_type<Geometry>::type point_type;
typedef typename bg::strategy::distance::detail::projected_point_ax<>::template result_type<point_type, point_type>::type distance_type;
typedef bg::strategy::distance::detail::projected_point_ax_less<distance_type> less_comparator;
distance_type max_distance(distance);
less_comparator less(max_distance);
bg::strategy::simplify::detail::douglas_peucker
<
point_type,
bg::strategy::distance::detail::projected_point_ax<>,
less_comparator
> strategy(less);
Geometry simplified;
bg::detail::simplify::simplify_insert(geometry,
std::back_inserter(simplified), max_distance, strategy);
test_equality<Geometry>::apply(simplified, expected);
}
#endif
}
};
template <typename Geometry, typename Expected, typename DistanceMeasure>
void check_geometry(Geometry const& geometry,
Expected const& expected,
DistanceMeasure const& distance)
{
Geometry simplified;
bg::simplify(geometry, simplified, distance);
test_equality<Expected>::apply(simplified, expected);
}
template <typename Geometry, typename Expected, typename Strategy, typename DistanceMeasure>
void check_geometry(Geometry const& geometry,
Expected const& expected,
DistanceMeasure const& distance,
Strategy const& strategy)
{
Geometry simplified;
bg::simplify(geometry, simplified, distance, strategy);
test_equality<Expected>::apply(simplified, expected);
}
template <typename Geometry, typename DistanceMeasure>
void check_geometry_with_area(Geometry const& geometry,
double expected_area,
DistanceMeasure const& distance)
{
Geometry simplified;
bg::simplify(geometry, simplified, distance);
BOOST_CHECK_CLOSE(bg::area(simplified), expected_area, 0.01);
}
template <typename Geometry, typename DistanceMeasure>
void test_geometry(std::string const& wkt,
std::string const& expected_wkt,
DistanceMeasure distance)
{
typedef typename bg::point_type<Geometry>::type point_type;
Geometry geometry, expected;
bg::read_wkt(wkt, geometry);
bg::read_wkt(expected_wkt, expected);
using variant_t = typename boost_variant_type<Geometry>::type;
variant_t v(geometry);
// Define default strategy for testing
typedef bg::strategy::simplify::douglas_peucker
<
typename bg::point_type<Geometry>::type,
bg::strategy::distance::projected_point<double>
> dp;
BOOST_CONCEPT_ASSERT((bg::concepts::SimplifyStrategy<dp, point_type>));
check_geometry(geometry, expected, distance);
check_geometry(v, expected, distance);
check_geometry(geometry, expected, distance, dp());
check_geometry(v, expected, distance, dp());
// For now check GC here because it's not supported by equals()
{
using gc_t = bg::model::geometry_collection<variant_t>;
gc_t gc{v};
gc_t gc_simplified;
bg::simplify(gc, gc_simplified, distance);
bg::detail::visit_breadth_first([&](auto const& g)
{
test_equality<Geometry>::apply(g, expected);
return false;
}, gc_simplified);
}
// Check inserter (if applicable)
test_inserter
<
typename bg::tag<Geometry>::type
>::apply(geometry, expected, distance);
#ifdef TEST_PULL89
// Check using non-default less comparator in douglass_peucker
typedef typename bg::strategy::distance::detail::projected_point_ax<>::template result_type<point_type, point_type>::type distance_type;
typedef bg::strategy::distance::detail::projected_point_ax_less<distance_type> less_comparator;
distance_type const max_distance(distance);
less_comparator const less(max_distance);
typedef bg::strategy::simplify::detail::douglas_peucker
<
point_type,
bg::strategy::distance::detail::projected_point_ax<>,
less_comparator
> douglass_peucker_with_less;
BOOST_CONCEPT_ASSERT( (bg::concepts::SimplifyStrategy<douglass_peucker_with_less, point_type>) );
check_geometry(geometry, expected, distance, douglass_peucker_with_less(less));
check_geometry(v, expected, distance, douglass_peucker_with_less(less));
#endif
}
template <typename Geometry, typename Strategy, typename DistanceMeasure>
void test_geometry(std::string const& wkt,
std::string const& expected_wkt,
DistanceMeasure const& distance,
Strategy const& strategy)
{
Geometry geometry, expected;
bg::read_wkt(wkt, geometry);
bg::read_wkt(expected_wkt, expected);
bg::correct_closure(geometry);
bg::correct_closure(expected);
typename boost_variant_type<Geometry>::type v(geometry);
BOOST_CONCEPT_ASSERT( (bg::concepts::SimplifyStrategy<Strategy,
typename bg::point_type<Geometry>::type>) );
check_geometry(geometry, expected, distance, strategy);
check_geometry(v, expected, distance, strategy);
}
template <typename Geometry, typename DistanceMeasure>
void test_geometry(std::string const& wkt,
double expected_area,
DistanceMeasure const& distance)
{
Geometry geometry;
bg::read_wkt(wkt, geometry);
bg::correct_closure(geometry);
check_geometry_with_area(geometry, expected_area, distance);
}
#endif
|