【问题标题】:Boost geometry rtree find iterator for exact match of boxBoost geometry rtree find iterator for the exact match of box
【发布时间】:2019-05-18 21:37:35
【问题描述】:

将新框插入 rtree 时,我想首先检查树中是否已经存在相同的框。如果是,我只想获取该值,否则我需要插入一个新值。最好(即最有效)的方法是什么?

我可以调用nearest(box,1),然后检查是否相等:

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point.hpp>
#include <boost/geometry/geometries/box.hpp>
#include <boost/geometry/index/rtree.hpp>

namespace bg = boost::geometry;
namespace bgi = boost::geometry::index;

typedef bg::model::point<double, 1, bg::cs::cartesian> TPoint;
typedef bg::model::box<TPoint> TBox;
typedef std::pair<TBox, void*> TPair;
typedef bgi::rtree<TPair, bgi::quadratic<16>> TTree;

TTree::const_query_iterator findExact(const TTree& tree, const TBox& box)
{
    auto it = rtree.qbegin(bgi::nearest(box, 1));
    if(it == rtree.qend() || !bg::equals(it->first, box))
        return rtree.qend();
    return it;
}

这真的是执行此查询的最佳(即最高性能)方式吗?

【问题讨论】:

    标签: c++ boost boost-geometry


    【解决方案1】:

    这不是安全的方法。我可以很容易地想象一种可能行不通的情况:

    插入新框之前带有 2 个框的 Rtree 状态:

    (0,2) --- (1,2)
      |    b    |
    (0,1) --- (1,1)
      |    a    |
    (0,0) --- (1,0)
    

    所以我们有 2 个盒子 ab。现在,当您尝试插入与a 框具有相同几何形状的新输入框时,您调用nearest prediacte 1 作为结果数。输入几何和a 之间的distance 为0,但对于distance(input,b) 也为0。 nearest 仅限退回一盒 - 哪一盒?你不知道,如果它返回b 框,你将a 的副本插入Rtree。

    安全方法可以如下:

    1. 获取新的输入框
    2. 计算其质心
    3. 从 rtree 中获取所有包含输入质心的框
    4. 遍历返回的框并在pair上调用equals函数(来自rtee的框,输入框)

    为此,您可以使用contains predicate,它使用boost::geometry::within 方法。作为contains/within 的输入,您传递了点-质心,如果它被编译器丢弃(我不确定它是否可以取点),您可以将点质心扩展到小盒子中进行编译。

    【讨论】:

    • 啊;我没有意识到最近的盒子是这样工作的,但我想这是有道理的。
    猜你喜欢
    • 1970-01-01
    • 2021-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-03
    相关资源
    最近更新 更多