从Boost Rtree删除项目的最快方法

时间:2019-06-17 22:41:44

标签: c++ boost

我已经测试了[1]中的代码以从Boost Rtree中删除项目。但是,它很慢。插入1M记录大约需要1秒,而删除它们大约需要12秒。

可以更快地完成吗?

这是测试代码:

#include <vector>
#include <iostream>
#include <stdio.h>

#include <boost/geometry.hpp>
#include <boost/geometry/index/rtree.hpp>

#include <boost/timer.hpp>

struct Rect
{
    Rect()  {}

    Rect(int a_minX, int a_minY, int a_maxX, int a_maxY)
    {
        min[0] = a_minX;
        min[1] = a_minY;

        max[0] = a_maxX;
        max[1] = a_maxY;
    }

    int min[2];
    int max[2];
};

int main()
{
    // randomize rectangles
    std::vector<Rect> rects;
    for (size_t i = 0 ; i < 1000000 ; ++i)
    {
        int min_x = rand() % 100000;
        int min_y = rand() % 100000;
        int w = 1 + rand() % 100;
        int h = 1 + rand() % 100;
        rects.push_back(Rect(min_x, min_y, min_x+w, min_y+h));
    }

    // create the rectangle passed into the query
    Rect search_rect(4, 4, 6, 6);


    // create the Boost.Geometry R-tree
    namespace bg = boost::geometry;
    namespace bgi = boost::geometry::index;
    typedef bg::model::point<double, 2, bg::cs::cartesian> point_t;
    typedef bg::model::box<point_t> box_t;
    typedef std::pair<box_t, uint64_t> value_t;
    bgi::rtree<value_t, bgi::quadratic<8,4> > bg_tree;


    {
        boost::timer t;
        for(size_t i = 0; i < rects.size(); i++)
        {
            Rect const& r = rects[i];
            box_t b(point_t(r.min[0], r.min[1]), point_t(r.max[0], r.max[1]));
            bg_tree.insert(value_t(b, i));
        }
        double s = t.elapsed();
        std::cout << s << " Boost insert time" << std::endl;
    }

    // test BG Rtree
    {
        std::vector<value_t> res;
        box_t search_box(
                         point_t(search_rect.min[0], search_rect.min[1]),
                         point_t(search_rect.max[0], search_rect.max[1]));
        size_t sum = 0;
        boost::timer t;
        for (size_t i = 0 ; i < 10000 ; ++i)
        {
            res.clear();
            sum += bg_tree.query(bgi::intersects(search_box), std::back_inserter(res));
        }
        double s = t.elapsed();
        std::cout << s << " Boost query " << sum << std::endl;
    }

    {
        boost::timer t;
        std::cout << "Tree contains " << bg_tree.size() << " box-id values." << std::endl;
        while (!bg_tree.empty()) {
            // 1. Choose arbitrary BoxIdPair to be the leader of a new canopy.
            //    Remove it from the tree. Insert it into the canopy map, with its
            //    corresponding id.
            Rect const& r = rects[rects.size()-1];
            point_t origin(r.min[0], r.min[0]);
            rects.pop_back();

            auto first = bgi::qbegin(bg_tree, bgi::nearest(origin, 1)),
            last  = bgi::qend(bg_tree);

            if (first != last) {
                bg_tree.remove(*first); // assuming single result
            }
        }
        double s = t.elapsed();
        std::cout << s << " Boost tree emptied " << std::endl;
    }
}

示例输出:

1.09421 Boost insert time
0.000371 Boost query 0
Tree contains 1000000 box-id values.
12.7237 Boost tree emptied 

[1] Issue with removing points from a boost::geometry::index::rtree

0 个答案:

没有答案
相关问题