PageRenderTime 16ms CodeModel.GetById 9ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/libs/geometry/doc/src/examples/algorithms/for_each_segment_const.cpp

http://hadesmem.googlecode.com/
C++ | 95 lines | 42 code | 23 blank | 30 comment | 2 complexity | 04f1361cc5a783228f172e537c722545 MD5 | raw file
 1// Boost.Geometry (aka GGL, Generic Geometry Library)
 2// QuickBook Example
 3
 4// Copyright (c) 2011 Barend Gehrels, Amsterdam, the Netherlands.
 5
 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)
 9
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
12
13#include <iostream>
14
15#include <boost/geometry.hpp>
16#include <boost/geometry/geometries/linestring.hpp>
17#include <boost/geometry/geometries/point_xy.hpp>
18
19#include <boost/assign.hpp>
20
21
22template <typename Segment>
23struct gather_segment_statistics
24{
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
28        <
29            typename boost::geometry::coordinate_type<Segment>::type,
30            double
31        >::type type;
32
33    type min_length, max_length;
34
35    // Initialize min and max
36    gather_segment_statistics()
37        : min_length(1e38)
38        , max_length(-1)
39    {}
40
41    // This operator is called for each segment
42    inline void operator()(Segment const& s)
43    {
44        type length = boost::geometry::length(s);
45        if (length < min_length) min_length = length;
46        if (length > max_length) max_length = length;
47    }
48};
49
50int main()
51{
52    // Bring "+=" for a vector into scope
53    using namespace boost::assign;
54
55    // Define a type
56    typedef boost::geometry::model::d2::point_xy<double> point;
57
58    // Declare a linestring
59    boost::geometry::model::linestring<point> polyline;
60
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);
64
65
66    // Declare the gathering class...
67    gather_segment_statistics
68        <
69            boost::geometry::model::referring_segment<point>
70        > functor;
71
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);
75
76    // Output the results
77    std::cout
78        << "Min segment length: " << functor.min_length << std::endl
79        << "Max segment length: " << functor.max_length << std::endl;
80
81    return 0;
82}
83
84//]
85
86
87//[for_each_segment_const_output
88/*`
89Output:
90[pre
91Min segment length: 1.41421
92Max segment length: 5.65685
93]
94*/
95//]