2

我试图弄清楚它how boost::geometryfor_each_segment工作原理。文档告诉我,它for_each_segment需要一个几何图形和一个仿函数。在我的示例中调用了这个函子polylength_helper,只要这个片段没有编译,我就在那里增加一个数字以保持简单,直到它编译。

// 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;
}

好吧,这不会编译。我使用clang了一个更易于理解的输出,它说:

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){
            ^

谁能帮我吗?特别是我不知道referring_segment消息中的来自哪里。

这是文档中的一个示例:

http://www.boost.org/doc/libs/1_48_0/libs/geometry/doc/html/geometry/reference/algorithms/for_each/for_each_segment_2_const_version.html

但我无法弄清楚这与我的版本有何不同,除了typedefs.

4

1 回答 1

2

换行

typedef boost::geometry::model::segment<GeographicPoint> GeographicSegment;

typedef boost::geometry::model::referring_segment<GeographicPoint> GeographicSegment;

这会让你编译。


从有关segmentReferring_segment的文档中,两者之间的唯一区别是 refering_segment 持有对点的引用。这是修改段的 for each 中需要的,因为修改的点应该反映在linestring. 在不修改点的 for each 中,它仍应采用参考(很可能是const参考),因为它减少了复制量。

于 2011-12-09T23:03:45.843 回答