Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / geometry / test / algorithms / test_simplify.hpp
1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 // Unit Test
3
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)
8
9 #ifndef BOOST_GEOMETRY_TEST_SIMPLIFY_HPP
10 #define BOOST_GEOMETRY_TEST_SIMPLIFY_HPP
11
12 // Test-functionality, shared between single and multi tests
13
14 #include <iomanip>
15 #include <sstream>
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>
22
23 template <typename Tag, typename Geometry>
24 struct test_inserter
25 {
26     static void apply(Geometry& , std::string const& , double )
27     {}
28 };
29
30 template <typename Geometry>
31 struct test_inserter<bg::linestring_tag, Geometry>
32 {
33     template <typename DistanceMeasure>
34     static void apply(Geometry& geometry,
35             std::string const& expected,
36             DistanceMeasure const& distance)
37     {
38         {
39             Geometry simplified;
40             bg::detail::simplify::simplify_insert(geometry,
41                 std::back_inserter(simplified), distance);
42
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);
48         }
49
50 #ifdef TEST_PULL89
51         {
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;
55
56             distance_type max_distance(distance);
57             less_comparator less(max_distance);
58
59             bg::strategy::simplify::detail::douglas_peucker
60                 <
61                     point_type,
62                     bg::strategy::distance::detail::projected_point_ax<>,
63                     less_comparator
64                 > strategy(less);
65
66             Geometry simplified;
67             bg::detail::simplify::simplify_insert(geometry,
68                 std::back_inserter(simplified), max_distance, strategy);
69
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);
75         }
76 #endif
77     }
78 };
79
80
81 template <typename Geometry, typename DistanceMeasure>
82 void check_geometry(Geometry const& geometry,
83                     std::string const& expected,
84                     DistanceMeasure const&  distance)
85 {
86     Geometry simplified;
87     bg::simplify(geometry, simplified, distance);
88
89     std::ostringstream out;
90     out << std::setprecision(12) << bg::wkt(simplified);
91     BOOST_CHECK_EQUAL(out.str(), expected);
92 }
93
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)
99 {
100     Geometry simplified;
101     bg::simplify(geometry, simplified, distance, strategy);
102
103     std::ostringstream out;
104     out << std::setprecision(12) << bg::wkt(simplified);
105     BOOST_CHECK_EQUAL(out.str(), expected);
106 }
107
108
109 template <typename Geometry, typename DistanceMeasure>
110 void test_geometry(std::string const& wkt,
111         std::string const& expected,
112         DistanceMeasure distance)
113 {
114     typedef typename bg::point_type<Geometry>::type point_type;
115
116     Geometry geometry;
117     bg::read_wkt(wkt, geometry);
118     boost::variant<Geometry> v(geometry);
119
120     // Define default strategy for testing
121     typedef bg::strategy::simplify::douglas_peucker
122         <
123             typename bg::point_type<Geometry>::type,
124             bg::strategy::distance::projected_point<double>
125         > dp;
126
127     check_geometry(geometry, expected, distance);
128     check_geometry(v, expected, distance);
129
130
131     BOOST_CONCEPT_ASSERT( (bg::concept::SimplifyStrategy<dp, point_type>) );
132
133     check_geometry(geometry, expected, distance, dp());
134     check_geometry(v, expected, distance, dp());
135
136     // Check inserter (if applicable)
137     test_inserter
138         <
139             typename bg::tag<Geometry>::type,
140             Geometry
141         >::apply(geometry, expected, distance);
142
143 #ifdef TEST_PULL89
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;
147
148     distance_type const max_distance(distance);
149     less_comparator const less(max_distance);
150
151     typedef bg::strategy::simplify::detail::douglas_peucker
152         <
153             point_type,
154             bg::strategy::distance::detail::projected_point_ax<>,
155             less_comparator
156         > douglass_peucker_with_less;
157
158     BOOST_CONCEPT_ASSERT( (bg::concept::SimplifyStrategy<douglass_peucker_with_less, point_type>) );
159
160     check_geometry(geometry, expected, distance, douglass_peucker_with_less(less));
161     check_geometry(v, expected, distance, douglass_peucker_with_less(less));
162 #endif
163 }
164
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)
170 {
171     Geometry geometry;
172     bg::read_wkt(wkt, geometry);
173     boost::variant<Geometry> v(geometry);
174
175     BOOST_CONCEPT_ASSERT( (bg::concept::SimplifyStrategy<Strategy,
176                            typename bg::point_type<Geometry>::type>) );
177
178     check_geometry(geometry, expected, distance, strategy);
179     check_geometry(v, expected, distance, strategy);
180 }
181
182 #endif