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_FOR_EACH_HPP
10 #define BOOST_GEOMETRY_TEST_FOR_EACH_HPP
12 #include <geometry_test_common.hpp>
14 #include <boost/config.hpp>
15 #include <boost/geometry/algorithms/for_each.hpp>
17 #include <boost/geometry/algorithms/distance.hpp>
18 #include <boost/geometry/strategies/strategies.hpp>
19 #include <boost/geometry/io/wkt/wkt.hpp>
20 #include <boost/geometry/io/dsv/write.hpp>
23 template<typename Point>
24 inline void translate_x_function(Point& p)
26 bg::set<0>(p, bg::get<0>(p) + 100.0);
29 template<typename Point>
30 struct scale_y_functor
32 inline void operator()(Point& p)
34 bg::set<1>(p, bg::get<1>(p) * 100.0);
38 template<typename Point>
41 typename bg::coordinate_type<Point>::type sum;
47 inline void operator()(Point const& p)
54 static std::ostringstream g_out;
56 template<typename Segment>
57 inline void stream_segment(Segment const& s)
59 g_out << bg::dsv(s) << " ";
62 template<typename Segment>
63 struct sum_segment_length
65 typename bg::coordinate_type<Segment>::type sum;
70 inline void operator()(Segment const& s)
72 sum += bg::distance(s.first, s.second);
76 template<typename Segment>
77 inline void modify_segment(Segment& s)
79 if (bg::math::equals(bg::get<0,0>(s), 1.0))
81 bg::set<0,0>(s, 10.0);
86 template <typename Geometry>
87 void test_per_point_const(Geometry const& geometry, int expected)
89 typedef typename bg::point_type<Geometry>::type point_type;
92 sum_x_functor<point_type> functor;
93 functor = bg::for_each_point(geometry, functor);
94 BOOST_CHECK_EQUAL(functor.sum, expected);
98 #if !defined(BOOST_NO_CXX11_LAMBDAS)
100 typename bg::coordinate_type<point_type>::type sum_x = 0;
105 [&sum_x](point_type const& p)
107 sum_x += bg::get<0>(p);
112 BOOST_CHECK_EQUAL(sum_x, expected);
116 template <typename Geometry>
117 void test_per_point_non_const(Geometry& geometry,
118 std::string const& expected1,
119 std::string const& expected2)
121 #if !defined(BOOST_NO_CXX11_LAMBDAS)
122 Geometry copy = geometry;
125 typedef typename bg::point_type<Geometry>::type point_type;
128 bg::for_each_point(geometry, translate_x_function<point_type>);
129 std::ostringstream out1;
130 out1 << bg::wkt(geometry);
132 BOOST_CHECK_MESSAGE(out1.str() == expected1,
134 << " expected " << expected1
135 << " got " << bg::wkt(geometry));
138 bg::for_each_point(geometry, scale_y_functor<point_type>());
140 std::ostringstream out2;
141 out2 << bg::wkt(geometry);
143 BOOST_CHECK_MESSAGE(out2.str() == expected2,
145 << " expected " << expected2
146 << " got " << bg::wkt(geometry));
148 #if !defined(BOOST_NO_CXX11_LAMBDAS)
149 // Lambda, both functions above together. Without / with capturing
157 bg::set<0>(p, bg::get<0>(p) + 100);
162 typename bg::coordinate_type<point_type>::type scale = 100;
168 bg::set<1>(p, bg::get<1>(p) * scale);
173 std::ostringstream out3;
174 out3 << bg::wkt(geometry);
176 BOOST_CHECK_MESSAGE(out3.str() == expected2,
177 "for_each_point (lambda): "
178 << " expected " << expected2
179 << " got " << bg::wkt(geometry));
185 template <typename Geometry>
186 void test_per_point(std::string const& wkt
188 , std::string const& expected1
189 , std::string const& expected2
193 bg::read_wkt(wkt, geometry);
194 test_per_point_const(geometry, expected_sum_x);
195 test_per_point_non_const(geometry, expected1, expected2);
200 template <typename Geometry>
201 void test_per_segment_const(Geometry const& geometry,
202 std::string const& expected_dsv,
203 double expected_length)
205 typedef typename bg::point_type<Geometry>::type point_type;
210 bg::for_each_segment(geometry,
211 stream_segment<bg::model::referring_segment<point_type const> >);
212 std::string out = g_out.str();
214 BOOST_CHECK_EQUAL(out, expected_dsv);
217 sum_segment_length<bg::model::referring_segment<point_type const> > functor;
218 functor = bg::for_each_segment(geometry, functor);
220 BOOST_CHECK_CLOSE(functor.sum, expected_length, 0.0001);
224 template <typename Geometry>
225 void test_per_segment_non_const(Geometry& geometry,
226 std::string const& expected_wkt)
228 typedef typename bg::point_type<Geometry>::type point_type;
231 bg::for_each_segment(geometry,
232 modify_segment<bg::model::referring_segment<point_type> >);
234 std::ostringstream out;
235 out << bg::wkt(geometry);
237 BOOST_CHECK_MESSAGE(out.str() == expected_wkt,
239 << " expected " << expected_wkt
240 << " got " << bg::wkt(geometry));
242 // function is working here, functor works for all others,
243 // it will also work here.
247 template <typename Geometry>
248 void test_per_segment(std::string const& wkt
249 , std::string const& expected_dsv
250 , double expected_length
251 , std::string const& expected_wkt
255 bg::read_wkt(wkt, geometry);
256 test_per_segment_const(geometry, expected_dsv, expected_length);
257 test_per_segment_non_const(geometry, expected_wkt);
262 template <typename Geometry>
263 void test_geometry(std::string const& wkt
265 , std::string const& expected1
266 , std::string const& expected2
267 , std::string const& expected_dsv
268 , double expected_length
269 , std::string const& expected_wkt
272 test_per_point<Geometry>(wkt, expected_sum_x, expected1, expected2);
273 test_per_segment<Geometry>(wkt, expected_dsv, expected_length, expected_wkt);