在rtree中的填充算法

时间:2015-07-09 14:34:38

标签: c++ algorithm boost packing

大家都明白,如果rtree是在boost中使用范围值创建的,那么它将使用打包算法。我需要一个使用打包算法的rtree示例。这是我的代码使用二次算法

    using  point = bg::model::point < int, 2, bg::cs::cartesian >;
    using  pointI = std::pair<point, std::size_t>;
 vector<point> contourCenters // has some value
bgi::rtree< pointI, bgi::quadratic<16> > rtree;
vector< pointI > cloud;

for (size_t i = 0; i < contourCenters.size(); ++i)
{
    int x = contourCenters[i].get < 0 >();
    int y = contourCenters[i].get < 1 >();

    cout << "Contour Centers: (" << x << "," << y << ")";
    cloud.push_back(mp(x, y, i));
    rtree.insert(make_pair(contourCenters[i], i));
}

我想用打包算法创建rtree,因为它似乎是boost中最快的。请指导我如何使用包装算法在boost中创建rtree。

1 个答案:

答案 0 :(得分:9)

您只需要使用range constructor

要使其工作,必须在构建rtree之前创建范围。在您的示例中实现该方法的最简单方法是首先构建#include <boost/geometry/index/rtree.hpp> #include <boost/geometry.hpp> #include <boost/geometry/geometries/point.hpp> #include <boost/geometry/geometries/box.hpp> #include <vector> #include <iostream> namespace bg = boost::geometry; namespace bgi = bg::index; using point = bg::model::point <int, 2, bg::cs::cartesian>; using pointI = std::pair<point, std::size_t>; pointI mp(int x, int y, size_t v) { return std::make_pair(point(x,y), v); } int main() { std::vector<point> contourCenters; // has some value std::vector<pointI> cloud; size_t id_gen = 0; std::transform( contourCenters.begin(), contourCenters.end(), back_inserter(cloud), [&](point const& p) { return std::make_pair(p, id_gen++); } ); for(pointI& pi : cloud) std::cout << "Contour Centers: (" << bg::get<0>(pi.first) << "," << bg::get<1>(pi.first) << ")"; bgi::rtree<pointI, bgi::quadratic<16> > rtree(cloud); } 向量,然后从中构造索引:

<强> Live On Coliru

std::transform

我用git+https替换了循环以获得良好的风格,但你可以保持循环。