1 // Boost.Geometry (aka GGL, Generic Geometry Library)
4 // Copyright (c) 2011-2012 Barend Gehrels, Amsterdam, the Netherlands.
6 // Use, modification and distribution is subject to the Boost Software License,
7 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
10 //[for_each_segment_const
11 //` Sample using for_each_segment, using a functor to get the minimum and maximum length of a segment in a linestring
15 #include <boost/geometry.hpp>
16 #include <boost/geometry/geometries/linestring.hpp>
17 #include <boost/geometry/geometries/point_xy.hpp>
19 #include <boost/assign.hpp>
22 template <typename Segment
>
23 struct gather_segment_statistics
25 // Remember that if coordinates are integer, the length might be floating point
26 // So use "double" for integers. In other cases, use coordinate type
27 typedef typename
boost::geometry::select_most_precise
29 typename
boost::geometry::coordinate_type
<Segment
>::type
,
33 type min_length
, max_length
;
35 // Initialize min and max
36 gather_segment_statistics()
41 // This operator is called for each segment
42 inline void operator()(Segment
const& s
)
44 type length
= boost::geometry::length(s
);
45 if (length
< min_length
) min_length
= length
;
46 if (length
> max_length
) max_length
= length
;
52 // Bring "+=" for a vector into scope
53 using namespace boost::assign
;
56 typedef boost::geometry::model::d2::point_xy
<double> point
;
58 // Declare a linestring
59 boost::geometry::model::linestring
<point
> polyline
;
61 // Use Boost.Assign to initialize a linestring
62 polyline
+= point(0, 0), point(3, 3), point(5, 1), point(6, 2),
63 point(8, 0), point(4, -4), point(1, -1), point(3, 2);
66 // Declare the gathering class...
67 gather_segment_statistics
69 boost::geometry::model::referring_segment
<point
>
72 // ... and use it, the essention.
73 // As also in std::for_each it is a const value, so retrieve it as a return value.
74 functor
= boost::geometry::for_each_segment(polyline
, functor
);
78 << "Min segment length: " << functor
.min_length
<< std::endl
79 << "Max segment length: " << functor
.max_length
<< std::endl
;
87 //[for_each_segment_const_output
91 Min segment length: 1.41421
92 Max segment length: 5.65685