Imported Upstream version 1.49.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 <geometry_test_common.hpp>
15 #include <boost/geometry/algorithms/simplify.hpp>
16 #include <boost/geometry/algorithms/distance.hpp>
17 #include <boost/geometry/strategies/strategies.hpp>
18
19 #include <boost/geometry/io/wkt/wkt.hpp>
20
21 template <typename Tag, typename Geometry>
22 struct test_inserter
23 {
24     static void apply(Geometry& , std::string const& , double )
25     {}
26 };
27
28 template <typename Geometry>
29 struct test_inserter<bg::linestring_tag, Geometry>
30 {
31     static void apply(Geometry& geometry, std::string const& expected, double distance)
32     {
33         Geometry simplified;
34         bg::detail::simplify::simplify_insert(geometry,
35             std::back_inserter(simplified), distance);
36
37         std::ostringstream out;
38         out << bg::wkt(simplified);
39         BOOST_CHECK_EQUAL(out.str(), expected);
40     }
41 };
42
43
44 template <typename Geometry>
45 void test_geometry(std::string const& wkt, std::string const& expected, double distance)
46 {
47     Geometry geometry, simplified;
48
49     // Generate polygon using only integer coordinates and obvious results
50     // Polygon is a hexagon, having one extra point (2,1) on a line which should be filtered out.
51     bg::read_wkt(wkt, geometry);
52     bg::simplify(geometry, simplified, distance);
53
54     {
55         std::ostringstream out;
56         out << bg::wkt(simplified);
57
58         BOOST_CHECK_MESSAGE(out.str() == expected,
59             "simplify: " << bg::wkt(geometry)
60             << " expected " << expected
61             << " got " << bg::wkt(simplified));
62     }
63
64     // Check using user-specified strategy
65     typedef typename bg::point_type<Geometry>::type point_type;
66     typedef typename bg::cs_tag<point_type>::type tag;
67     typedef bg::strategy::distance::projected_point
68         <
69             point_type,
70             point_type
71         > strategy;
72     typedef bg::strategy::simplify::douglas_peucker
73         <
74             point_type,
75             strategy
76         > simplify_strategy_type;
77
78     BOOST_CONCEPT_ASSERT( (bg::concept::SimplifyStrategy<simplify_strategy_type>) );
79     bg::simplify(geometry, simplified, distance, simplify_strategy_type());
80
81     {
82         std::ostringstream out;
83         out << bg::wkt(simplified);
84         BOOST_CHECK_EQUAL(out.str(), expected);
85     }
86
87     // Check inserter (if applicable)
88     test_inserter
89         <
90             typename bg::tag<Geometry>::type,
91             Geometry
92         >::apply(geometry, expected, distance);
93 }
94
95
96 #endif