简体   繁体   English

在rtree中的填充算法

[英]packing algorithm in rtree in boost

Hi all I understand that if rtree is created with range values in boost it would use packing algorithm. 大家好我明白,如果rtree是用boost中的范围值创建的,那么它将使用打包算法。 I need an example of rtree using packing algorithm. 我需要一个使用打包算法的rtree示例。 Here is my code that uses quadratic algorithm 这是我使用二次算法的代码

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

I would like to create rtree with packing algorithm as it seems to be the fastest one in boost. 我想用打包算法创建rtree,因为它似乎是boost中最快的一个。 Kindly guide me how to create a rtree with packing algorithm in boost. 请指导我如何在boost中使用打包算法创建rtree。

You'd just need to use the range constructor . 您只需要使用范围构造函数

For that to work, the range must have been created before constructing the rtree. 要使其工作,必须在构造rtree之前创建范围。 The simplest way to achieve that in your example would be to build your cloud vector first, and then construct the index from it: 在您的示例中实现这一点的最简单方法是首先构建您的cloud矢量,然后从中构建索引:

Live On Coliru 住在Coliru

#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);
}

I replaced the loop with std::transform for good style, but you could keep the loop as you had it. 我用std::transform替换了循环以获得良好的风格,但你可以保持循环,就像你拥有它一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM