Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

for_each_segment
PrevUpHomeNext

Applies function f to each segment.

Description

Applies a function f (functor, having operator() defined) to each segment making up the geometry

Synopsis

template<typename Geometry, typename Functor>
Functor for_each_segment(Geometry & geometry, Functor f)

Parameters

Type

Concept

Name

Description

Geometry &

Any type fulfilling a Geometry Concept

geometry

A model of the specified concept

Functor

Function or class with operator()

f

Unary function, taking a segment as argument

Header

Either

#include <boost/geometry.hpp>

Or

#include <boost/geometry/algorithms/for_each.hpp>

Conformance

The function for_each_segment is not defined by OGC.

The function for_each_segment conforms to the std::for_each function of the C++ std-library.

Example

Sample using for_each_segment, using a functor to get the minimum and maximum length of a segment in a linestring

#include <iostream>
#include <limits>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/linestring.hpp>
#include <boost/geometry/geometries/point_xy.hpp>

int main()
{
    // Define a type
    using point = boost::geometry::model::d2::point_xy<double>;

    // Create a linestring
    const boost::geometry::model::linestring<point> polyline =
        {{0, 0}, {3, 3}, {5, 1}, {6, 2}, {8, 0}, {4, -4}, {1, -1}, {3, 2}};

    // Iterate over its segments to find the minimum and maximum length
    double min_length = std::numeric_limits<double>::max();
    double max_length = -std::numeric_limits<double>::max();

    boost::geometry::for_each_segment(polyline,
        [&](auto const& s)
        {
            const auto length = static_cast<double>(boost::geometry::length(s));
            min_length = std::min(min_length, length);
            max_length = std::max(max_length, length);
        });

    // Output the results
    std::cout
        << "Min segment length: " << min_length << std::endl
        << "Max segment length: " << max_length << std::endl;

    return 0;
}

Output:

Min segment length: 1.41421
Max segment length: 5.65685

PrevUpHomeNext