大家好,我知道,如果使用在boost中的范围值创建rtree,它将使用打包算法。我需要一个使用打包算法的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中的打包算法创建rtree。
您只需要使用range构造函数即可。
为此,必须在构造rtree之前创建范围。在您的示例中,实现此目标的最简单方法是先构建cloud向量,然后从中构建索引:
cloud
[Live On Coliru](http://coliru.stacked-crooked.com/a/e69d98d9e716ff7b)
#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); }
我用std::transform好的样式替换了循环,但是您可以按原样保留循环。
std::transform