1 // Boost.Geometry (aka GGL, Generic Geometry Library)
4 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
5 // Use, modification and distribution is subject to the Boost Software License,
6 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 #ifndef BOOST_GEOMETRY_TEST_SIMPLIFY_HPP
10 #define BOOST_GEOMETRY_TEST_SIMPLIFY_HPP
12 // Test-functionality, shared between single and multi tests
16 #include <geometry_test_common.hpp>
17 #include <boost/geometry/algorithms/simplify.hpp>
18 #include <boost/geometry/algorithms/distance.hpp>
19 #include <boost/geometry/strategies/strategies.hpp>
20 #include <boost/geometry/io/wkt/wkt.hpp>
21 #include <boost/variant/variant.hpp>
23 template <typename Tag, typename Geometry>
26 static void apply(Geometry& , std::string const& , double )
30 template <typename Geometry>
31 struct test_inserter<bg::linestring_tag, Geometry>
33 template <typename DistanceMeasure>
34 static void apply(Geometry& geometry,
35 std::string const& expected,
36 DistanceMeasure const& distance)
40 bg::detail::simplify::simplify_insert(geometry,
41 std::back_inserter(simplified), distance);
43 std::ostringstream out;
44 // TODO: instead of comparing the full string (with more or less decimal digits),
45 // we should call something more robust to check the test for example geometry::equals
46 out << std::setprecision(12) << bg::wkt(simplified);
47 BOOST_CHECK_EQUAL(out.str(), expected);
52 typedef typename bg::point_type<Geometry>::type point_type;
53 typedef typename bg::strategy::distance::detail::projected_point_ax<>::template result_type<point_type, point_type>::type distance_type;
54 typedef bg::strategy::distance::detail::projected_point_ax_less<distance_type> less_comparator;
56 distance_type max_distance(distance);
57 less_comparator less(max_distance);
59 bg::strategy::simplify::detail::douglas_peucker
62 bg::strategy::distance::detail::projected_point_ax<>,
67 bg::detail::simplify::simplify_insert(geometry,
68 std::back_inserter(simplified), max_distance, strategy);
70 std::ostringstream out;
71 // TODO: instead of comparing the full string (with more or less decimal digits),
72 // we should call something more robust to check the test for example geometry::equals
73 out << std::setprecision(12) << bg::wkt(simplified);
74 BOOST_CHECK_EQUAL(out.str(), expected);
81 template <typename Geometry, typename DistanceMeasure>
82 void check_geometry(Geometry const& geometry,
83 std::string const& expected,
84 DistanceMeasure const& distance)
87 bg::simplify(geometry, simplified, distance);
89 std::ostringstream out;
90 out << std::setprecision(12) << bg::wkt(simplified);
91 BOOST_CHECK_EQUAL(out.str(), expected);
94 template <typename Geometry, typename Strategy, typename DistanceMeasure>
95 void check_geometry(Geometry const& geometry,
96 std::string const& expected,
97 DistanceMeasure const& distance,
98 Strategy const& strategy)
101 bg::simplify(geometry, simplified, distance, strategy);
103 std::ostringstream out;
104 out << std::setprecision(12) << bg::wkt(simplified);
105 BOOST_CHECK_EQUAL(out.str(), expected);
109 template <typename Geometry, typename DistanceMeasure>
110 void test_geometry(std::string const& wkt,
111 std::string const& expected,
112 DistanceMeasure distance)
114 typedef typename bg::point_type<Geometry>::type point_type;
117 bg::read_wkt(wkt, geometry);
118 boost::variant<Geometry> v(geometry);
120 // Define default strategy for testing
121 typedef bg::strategy::simplify::douglas_peucker
123 typename bg::point_type<Geometry>::type,
124 bg::strategy::distance::projected_point<double>
127 check_geometry(geometry, expected, distance);
128 check_geometry(v, expected, distance);
131 BOOST_CONCEPT_ASSERT( (bg::concept::SimplifyStrategy<dp, point_type>) );
133 check_geometry(geometry, expected, distance, dp());
134 check_geometry(v, expected, distance, dp());
136 // Check inserter (if applicable)
139 typename bg::tag<Geometry>::type,
141 >::apply(geometry, expected, distance);
144 // Check using non-default less comparator in douglass_peucker
145 typedef typename bg::strategy::distance::detail::projected_point_ax<>::template result_type<point_type, point_type>::type distance_type;
146 typedef bg::strategy::distance::detail::projected_point_ax_less<distance_type> less_comparator;
148 distance_type const max_distance(distance);
149 less_comparator const less(max_distance);
151 typedef bg::strategy::simplify::detail::douglas_peucker
154 bg::strategy::distance::detail::projected_point_ax<>,
156 > douglass_peucker_with_less;
158 BOOST_CONCEPT_ASSERT( (bg::concept::SimplifyStrategy<douglass_peucker_with_less, point_type>) );
160 check_geometry(geometry, expected, distance, douglass_peucker_with_less(less));
161 check_geometry(v, expected, distance, douglass_peucker_with_less(less));
165 template <typename Geometry, typename Strategy, typename DistanceMeasure>
166 void test_geometry(std::string const& wkt,
167 std::string const& expected,
168 DistanceMeasure const& distance,
169 Strategy const& strategy)
172 bg::read_wkt(wkt, geometry);
173 boost::variant<Geometry> v(geometry);
175 BOOST_CONCEPT_ASSERT( (bg::concept::SimplifyStrategy<Strategy,
176 typename bg::point_type<Geometry>::type>) );
178 check_geometry(geometry, expected, distance, strategy);
179 check_geometry(v, expected, distance, strategy);