[英]Issue with removing points from a boost::geometry::index::rtree
[英]Finding resulting points using boost rtree in a geometry
我正在尝试在我的一个项目中使用boost::geometry
的rtree DS,但是我发现很难浏览文档。 某些方法的文献资料很少,我找不到足够的示例。 现在,我正在尝试构建示例程序,以便可以进一步构建它。
因此,在下面的示例中,我有一个点数的树和一个框,我需要找到该框内的所有点。 我想问的另一件事是,我找不到packing algorithm
构造函数或方法,因此也如何使用它。 这是我到目前为止所做的-
#include <iostream>
#include <vector>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/register/point.hpp>
#include <boost/geometry/geometries/register/box.hpp>
#include <boost/geometry/index/rtree.hpp>
namespace bg = boost::geometry;
namespace bgi = boost::geometry::index;
struct my_point
{
float x, y;
my_point(float _x, float _y) : x(_x), y(_y) {}
};
struct my_box
{
my_point ll, ur;
my_box(float x1, float y1, float x2, float y2) : ll(x1,y1), ur(x2,y2) {}
};
// Register the point type
BOOST_GEOMETRY_REGISTER_POINT_2D(my_point, float, cs::cartesian, x, y)
// Register the box type, also notifying that it is based on "my_point"
BOOST_GEOMETRY_REGISTER_BOX(my_box, my_point, ll, ur)
int main()
{
std::vector<std::pair<my_point, int>> pts;
pts.emplace_back(std::make_pair(my_point(2,2), 5));
pts.emplace_back(std::make_pair(my_point(3,3), 1));
pts.emplace_back(std::make_pair(my_point(4,5), 3));
pts.emplace_back(std::make_pair(my_point(4,4), 12));
pts.emplace_back(std::make_pair(my_point(1,2), 50));
// ....
bgi::rtree<std::pair<my_point, int>, bgi::dynamic_rstar> rT(bgi::dynamic_rstar(pts.size()));
rT.insert(pts.begin(), pts.end());
my_box box1(1,1,4,4);
// how to retrieve all points or their .second inside this box?
return 0;
}
如果将范围传递给R树的构造函数,则使用打包算法:
可以使用query
方法或查询迭代器从R树中检索值:
另请参见此R树快速入门示例 。
最后一句话。 在代码中,将点数传递给bgi::dynamic_rstar
。 bgi::dynamic_rstar
的参数不是所包含值的数量。 它是存储在R树的单个节点中的最大值数量(对应于基于持久性/基于磁盘的R树的页面大小)。 因此,如果传递bgi::dynamic_rstar(pts.size())
则R树仅包含一个包含所有点的节点,这意味着没有节点层次结构,空间划分,并且rtree的行为实际上像矢量。 取而代之的是使用bgi::rstar<4>
或更大的数字(如果值重叠很大),但由于您要存储点,因此不应该这样做。 这就足够了:
bgi::rtree<std::pair<my_point, int>, bgi::rstar<4> > rT(pts);
您需要在rtree上使用query
方法并通过适当的查询策略,例如-
std::vector<std::pair<my_point, int>> result;
rT.query(bgi::covered_by(box1), std::back_inserter(result));
对于打包算法,虽然我不确定,但这应该可以工作-
bgi::rtree<std::pair<my_point, int>, bgi::dynamic_rstar> rT(pts, bgi::dynamic_rstar(pts.size()));
但是,我建议对照文档进行验证。 这是我在计算机上本地测试的完整程序-
#include <iostream>
#include <vector>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/register/point.hpp>
#include <boost/geometry/geometries/register/box.hpp>
#include <boost/geometry/index/rtree.hpp>
namespace bg = boost::geometry;
namespace bgi = boost::geometry::index;
struct my_point
{
float x, y;
my_point(float _x, float _y) : x(_x), y(_y) {}
};
struct my_box
{
my_point ll, ur;
my_box(float x1, float y1, float x2, float y2) : ll(x1,y1), ur(x2,y2) {}
};
// Register the point type
BOOST_GEOMETRY_REGISTER_POINT_2D(my_point, float, cs::cartesian, x, y)
// Register the box type, also notifying that it is based on "my_point"
BOOST_GEOMETRY_REGISTER_BOX(my_box, my_point, ll, ur)
int main()
{
std::vector<std::pair<my_point, int>> pts;
pts.emplace_back(std::make_pair(my_point(2,2), 5));
pts.emplace_back(std::make_pair(my_point(3,3), 1));
pts.emplace_back(std::make_pair(my_point(4,5), 3));
pts.emplace_back(std::make_pair(my_point(4,4), 12));
pts.emplace_back(std::make_pair(my_point(1,2), 50));
// ....
bgi::rtree<std::pair<my_point, int>, bgi::dynamic_rstar> rT(pts, bgi::dynamic_rstar(pts.size()));
my_box box1(1,1,4,4);
std::vector<std::pair<my_point, int>> result;
rT.query(bgi::covered_by(box1), std::back_inserter(result));
for(const auto &r: result){
std::cout << r.second << ' ' << '\n';
}
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.