#### /Src/Dependencies/Boost/libs/geometry/test/algorithms/test_for_each.hpp

C++ Header | 217 lines | 157 code | 46 blank | 14 comment | 4 complexity | b617aa2a5eba81f3777bc51689343e70 MD5 | raw file
```  1// Boost.Geometry (aka GGL, Generic Geometry Library)
2// Unit Test
3
4// Copyright (c) 2007-2011 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
8
9#ifndef BOOST_GEOMETRY_TEST_FOR_EACH_HPP
10#define BOOST_GEOMETRY_TEST_FOR_EACH_HPP
11
12#include <geometry_test_common.hpp>
13
14#include <boost/geometry/algorithms/for_each.hpp>
15
16#include <boost/geometry/algorithms/distance.hpp>
17#include <boost/geometry/strategies/strategies.hpp>
18#include <boost/geometry/domains/gis/io/wkt/wkt.hpp>
19#include <boost/geometry/util/write_dsv.hpp>
20
21
22template<typename Point>
23inline void translate_x_function(Point& p)
24{
25    bg::set<0>(p, bg::get<0>(p) + 100.0);
26}
27
28template<typename Point>
29struct scale_y_functor
30{
31    inline void operator()(Point& p)
32    {
33        bg::set<1>(p, bg::get<1>(p) * 100.0);
34    }
35};
36
37template<typename Point>
38struct sum_x_functor
39{
40    typename bg::coordinate_type<Point>::type sum;
41
42    sum_x_functor()
43        : sum(0)
44    {}
45
46    inline void operator()(Point const& p)
47    {
48        sum += bg::get<0>(p);
49    }
50};
51
52// Per segment
53static std::ostringstream g_out;
54
55template<typename Segment>
56inline void stream_segment(Segment const& s)
57{
58    g_out << bg::dsv(s) << " ";
59}
60
61template<typename Segment>
62struct sum_segment_length
63{
64    typename bg::coordinate_type<Segment>::type sum;
65
66    sum_segment_length()
67        : sum(0)
68    {}
69    inline void operator()(Segment const& s)
70    {
71        sum += bg::distance(s.first, s.second);
72    }
73};
74
75template<typename Segment>
76inline void modify_segment(Segment& s)
77{
78    if (bg::math::equals(bg::get<0,0>(s), 1.0))
79    {
80        bg::set<0,0>(s, 10.0);
81    }
82}
83
84
85template <typename Geometry>
86void test_per_point_const(Geometry const& geometry, int expected)
87{
88    typedef typename bg::point_type<Geometry>::type point_type;
89
90    sum_x_functor<point_type> functor;
91    functor = bg::for_each_point(geometry, functor);
92    BOOST_CHECK_EQUAL(functor.sum, expected);
93}
94
95template <typename Geometry>
96void test_per_point_non_const(Geometry& geometry,
97    std::string const& expected1,
98    std::string const& expected2)
99{
100    typedef typename bg::point_type<Geometry>::type point_type;
101
102    // function
103    bg::for_each_point(geometry, translate_x_function<point_type>);
104    std::ostringstream out1;
105    out1 << bg::wkt(geometry);
106
107    BOOST_CHECK_MESSAGE(out1.str() == expected1,
108        "for_each_point: "
109        << " expected " << expected1
110        << " got " << bg::wkt(geometry));
111
112    // functor
113    bg::for_each_point(geometry, scale_y_functor<point_type>());
114
115    std::ostringstream out2;
116    out2 << bg::wkt(geometry);
117
118    BOOST_CHECK_MESSAGE(out2.str() == expected2,
119        "for_each_point: "
120        << " expected " << expected2
121        << " got " << bg::wkt(geometry));
122}
123
124
125template <typename Geometry>
126void test_per_point(std::string const& wkt
127    , int expected_sum_x
128    , std::string const& expected1
129    , std::string const& expected2
130    )
131{
132    Geometry geometry;
134    test_per_point_const(geometry, expected_sum_x);
135    test_per_point_non_const(geometry, expected1, expected2);
136}
137
138
139
140template <typename Geometry>
141void test_per_segment_const(Geometry const& geometry,
142        std::string const& expected_dsv,
143        double expected_length)
144{
145    typedef typename bg::point_type<Geometry>::type point_type;
146
147    // function
148    g_out.str("");
149    g_out.clear();
150    bg::for_each_segment(geometry,
151            stream_segment<bg::model::referring_segment<point_type const> >);
152    std::string out = g_out.str();
153    boost::trim(out);
154    BOOST_CHECK_EQUAL(out, expected_dsv);
155
156    // functor
157    sum_segment_length<bg::model::referring_segment<point_type const> > functor;
158    functor = bg::for_each_segment(geometry, functor);
159
160    BOOST_CHECK_CLOSE(functor.sum, expected_length, 0.0001);
161}
162
163
164template <typename Geometry>
165void test_per_segment_non_const(Geometry& geometry,
166        std::string const& expected_wkt)
167{
168    typedef typename bg::point_type<Geometry>::type point_type;
169
170    // function
171    bg::for_each_segment(geometry,
172            modify_segment<bg::model::referring_segment<point_type> >);
173
174    std::ostringstream out;
175    out << bg::wkt(geometry);
176
177    BOOST_CHECK_MESSAGE(out.str() == expected_wkt,
178        "for_each_segment: "
179        << " expected " << expected_wkt
180        << " got " << bg::wkt(geometry));
181
182    // function is working here, functor works for all others,
183    // it will also work here.
184}
185
186
187template <typename Geometry>
188void test_per_segment(std::string const& wkt
189        , std::string const& expected_dsv
190        , double expected_length
191        , std::string const& expected_wkt
192        )
193{
194    Geometry geometry;
196    test_per_segment_const(geometry, expected_dsv, expected_length);
197    test_per_segment_non_const(geometry, expected_wkt);
198}
199
200
201
202template <typename Geometry>
203void test_geometry(std::string const& wkt
204    , int expected_sum_x
205    , std::string const& expected1
206    , std::string const& expected2
207    , std::string const& expected_dsv
208    , double expected_length
209    , std::string const& expected_wkt
210    )
211{
212    test_per_point<Geometry>(wkt, expected_sum_x, expected1, expected2);
213    test_per_segment<Geometry>(wkt, expected_dsv, expected_length, expected_wkt);
214}
215
216
217#endif
```