如何检查boost r-tree中的任何2d点是否在给定的矩形中?这是我在boost中了解r-tree的网站。
但是我很困惑如何检查r-tree中的任何点是否在给定的矩形内。c++代码优先
r -树只作用于矩形。
如果你在r-tree中存储非矩形数据,它只会为你提供候选,你然后需要更详细地检查("验证","验证","精炼")。
整个R-tree的思想是用边界框的更简单(有效地存储和管理)几何来近似对象,毕竟。
也许boost库为此提供了一些辅助功能,但可能在rtree包之外,在几何包本身中。
如果我理解正确的话,您希望有一个包含点的r树,并检查它们中是否至少有一个与矩形或三角形相交。下面的代码展示了如何使用空间查询(rtree::query()
)和查询迭代器(rtree::qbegin()
和rtree::qend()
)来实现这一点。
参见文档(http://www.boost.org/libs/geometry),章节空间索引。
我不知道你用的是哪个编译器,所以下面的代码没有使用任何c++ 11特性。例如,在c++ 11中,您可以使用std::find_if()
等带有lambda表达式的算法来代替原始循环。
#include <iostream> // to print the results
#include <vector> // to store the points and results
// only for convenience
#include <boost/foreach.hpp>
// Boost.Geometry headers
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/index/rtree.hpp>
// convenient namespaces
namespace bg = boost::geometry;
namespace bgi = boost::geometry::index;
// convenient typedefs
typedef bg::model::point<double, 2, bg::cs::cartesian> point_t;
typedef bg::model::box<point_t> box_t;
typedef bg::model::ring<point_t, true, false> ring_t; // clockwise, open
typedef bgi::rtree<point_t, bgi::linear<16> > rtree_t;
int main()
{
// prepare a container of points
std::vector<point_t> points;
points.push_back(point_t(0, 0));
points.push_back(point_t(1, 1));
points.push_back(point_t(2, 2));
points.push_back(point_t(3, 3));
// build a rtree
rtree_t rtree(points.begin(), points.end());
// prepare a triangle (not intersecting any point)
ring_t triangle;
bg::append(triangle, point_t(0.5, 0.6));
bg::append(triangle, point_t(0.5, 1.5));
bg::append(triangle, point_t(1.4, 1.5));
// create axis-aligned bounding box of a triangle
box_t box = bg::return_envelope<box_t>(triangle);
// using rtree::query()
// rather naiive approach since all points intersecting a geometry are returned
{
// check if at least 1 point intersecting a box was found
std::vector<point_t> result;
rtree.query(bgi::intersects(box), std::back_inserter(result));
bool test = !result.empty();
std::cout << test << std::endl;
}
{
// check if at least 1 point intersecting a triangle was found
std::vector<point_t> result;
rtree.query(bgi::intersects(triangle), std::back_inserter(result));
bool test = !result.empty();
std::cout << test << std::endl;
}
{
// check if at least 1 point intersecting a triangle was found
// similar to the above but should be faster since during a spatial query
// a box is checked and triangle only if needed
std::vector<point_t> result;
rtree.query(bgi::intersects(box), std::back_inserter(result));
bool test = false;
BOOST_FOREACH(point_t const& pt, result)
{
if ( bg::intersects(pt, triangle) )
{
test = true;
break;
}
}
std::cout << test << std::endl;
}
// using iterative queries - rtree::qbegin() and rtree::qend()
// the query is stopped when the first point is found
{
// check if at least 1 point intersecting a box was found
bool test = rtree.qbegin(bgi::intersects(box)) != rtree.qend();
std::cout << test << std::endl;
}
{
// check if at least 1 point intersecting a triangle was found
bool test = rtree.qbegin(bgi::intersects(triangle)) != rtree.qend();
std::cout << test << std::endl;
}
{
// check if at least 1 point intersecting a triangle was found
// this version should be faster than the above because a box is checked
// during the spatial query and triangle only if needed
bool test = false;
// for each Point intersecting a box
for ( rtree_t::const_query_iterator it = rtree.qbegin(bgi::intersects(box)) ;
it != rtree.qend() ;
++it )
{
// check if this Point also intersects a triangle
if ( bg::intersects(triangle, *it) )
{
test = true;
break;
}
}
std::cout << test << std::endl;
}
}