如何检查boost r-tree中的任何2d点是否在给定的三角形中
How to check if any 2d point in boost r-tree is in a given triangle?
如何检查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;
}
}
相关文章:
- 检查 2D 网格的某个元素是否与另一个元素共享对角线、水平线或垂直线
- 是否可以在并行区域中为共享 2D 数组创建选定元素的线程本地副本?(共享,私有,障碍:OPenMP)
- 在 C++ 中搜索 2D 数组以查看一行/列是否与另一行/列相同
- 此方法是否不适合在 std::unordered_map 中使用 2D 坐标作为键
- 如何检查 2D 数组在 c++ 中是否按升序排列?
- C++,2D std::vector,我是否需要显式保留和推送空矢量
- 是否有任何打印2D阵列C 的快捷方式
- 用“ strcpy”在2D数组中删除(字符)的一行(字符)时是否存在任何错误
- C 检查2D矢量是否遇到了障碍
- 如何检查2D数组中是否存在所有请求的值
- 查找数字是否单独存在于 2D 网格中
- OPENCV是否有一个软件包将3D点转换为2D自上而下的视图
- 如何检查2D阵列中的字符是否出现多次?C
- 矢量阵列是否形成2D向量
- C++向量的 2D 数组是否适合跟踪 2D 数组的动态域值?
- 是否可以使用 1D 索引访问 C++ 2D int 数组
- 我是否必须求助于循环,以比较C 中的两个2D阵列
- 英特尔IPP卷积已弃用--是否有其他IPP 2D卷积方法
- 我是否正确地为 2D 数组添加了行与列
- Eigen3 或其他矩阵库 2D 矩阵是否可以通过共享内存从不同的进程更新