I try to figure out how boost::geometry‘s for_each_segment ist working. The documentation tells me, that for_each_segment expects a geometry and a Functor. This functor is called polylength_helper in my example, as long as this snippet is not compiling I just increment a number there to keep things simple until it compiles.
// foo.h
typedef boost::geometry::model::point<double, 2, bg::cs::cartesian> GeographicPoint;
typedef boost::geometry::model::linestring<GeographicPoint> GeographicPolyLine;
typedef boost::geometry::model::segment<GeographicPoint> GeographicSegment;
double poly_length(const GeographicPolyLine&);
template<typename Segment>
struct polylength_helper{
polylength_helper() : length(0){};
inline void operator()(Segment s){
length += 1;
};
double length;
};
// foo.cpp
double poly_length(GeographicPolyLine &poly){
polylength_helper<GeographicSegment> helper;
bg::for_each_segment(poly, helper);
return helper.length;
}
Well, this does not compile. I used clang for a more understandable output, it says:
note: candidate function not viable: no known
conversion from 'model::referring_segment<point_type>' to
'boost::geometry::model::segment<boost::geometry::model::point<double, 2,
boost::geometry::cs::cartesian> >' for 1st argument
inline void operator()(Segment s){
^
Can anyone help me out? Especially I have no idea where the referring_segment in the message comes from.
Here is an example from the docs:
But I cannot figure out how this differs from my version, except for the typedefs.
Change the line
to
That will get you compiling.
From the documentation on segment and referring_segment, the only difference between the two is that referring_segment holds a reference to the points. This is what is needed in a for each that modifies the segment since the points modified should be reflected in the
linestring. In a for each that does not modify the points, it should still take a reference (most likely aconstreference) since it reduces the amount of copying.