- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
经过一些阅读后,我了解到层次结构遍历虽然可能在 boost rtree 中并未得到官方支持。我有几个不同的用例,我可以在没有层次结构遍历的情况下进行管理,但我不确定效率。因此,我正在寻求有关 boost rtree 如何在内部工作以及这些用例的最佳实践是什么的建议。
案例 1 - 我有 2 棵树,我想过滤这两棵树以查找与另一棵树中至少一个元素相交的元素。我有下面的代码可以执行此操作并提供我想要的结果:
std::set<int> results2;
std::vector<rtree3d_item> tempResults, results1;
std::vector<rtree3d_item>::iterator it;
tree1->query(bgi::satisfies(
[&](const rtree3d_item& item) {
tempResults.clear();
tree2->query(bgi::intersects(item.first), std::back_inserter(tempResults));
for (it = tempResults.begin(); it < tempResults.end(); it++)
{
results2.insert(it->second);
}
return tempResults.size() > 0;
}
), std::back_inserter(results1));
虽然我得到了我想要的结果,但该方法似乎不是最优的。我传递给 satisfies()
的 lambda 表达式对 tree1 中的每个叶节点运行一次。如果我可以遍历树的层次结构,我就可以测试父节点的大框并排除大块的 tree1 并使过程更加高效。几乎就像 tree1 是一个树结构是没有用的。
情况 2 - 我只想查询 rtree 以查找与球体相交的所有项目。我使用谓词 lambda 表达式来实现:
bgi::satisfies(
[=](const rtree3d_item &item) {
return bg::distance(item.first, cpt) < radius;
})
该表达式对内部 rtree 的每个叶节点运行一次,这又显得很浪费。如果我对父节点进行此测试,我可以一次排除多个叶节点。
似乎每当我测试 rtree 满足条件时我都会遇到同样的情况 - (如果边界框不满足条件,其中包含的任何边界框也将不满足条件)。我的意思是,如果我要一直测试所有叶节点,那么拥有“树”结构有什么意义呢?为什么 boost 官方不支持遍历树的层次结构的方法?
我找到了一个资源,其中讨论了不受支持的非官方方式 ( link ) 来做这件事,但我想尝试官方的、受支持的方式来优化我的代码。
仅供引用,我在 C++ 方面的经验有限,因此欢迎提供所有建议,谢谢!
最佳答案
Why doesn't boost officially support ways to traverse the hierarchy of the tree ?
猜测:
他们使用友好的 API 实现高级原语。在文档化的界面中不包括较低级别可以更灵活地迭代这些界面的设计,而不会给库的用户带来麻烦。因此,最终结果将严格来说是更好、更底层的接口(interface),一旦稳定下来就可以记录下来。
遍历的语义将与平衡/结构化策略密切相关。这意味着在所有情况下都很难理解/记录遍历顺序的含义,这可能是错误的来源。没有将其记录在案向用户发出信号(他们可以使用它,但后果自负)
同意。我会说你最好在第一棵树上做一个 BFS,然后查询与第二棵树的交集。这样,您可以快速删除树(子树)中不感兴趣的“部分”。
基于代码 linked by the library dev here ,我想出了一个粗略的、最小的访问者:
namespace rtree = bgi::detail::rtree;
template <typename Predicate, typename Value, typename Options, typename Box, typename Allocators>
struct BFSQuery : public rtree::visitor<Value, typename Options::parameters_type, Box, Allocators, typename Options::node_tag, true>::type
{
typedef typename rtree::internal_node<Value, typename Options::parameters_type, Box, Allocators, typename Options::node_tag>::type internal_node;
typedef typename rtree::leaf<Value, typename Options::parameters_type, Box, Allocators, typename Options::node_tag>::type leaf;
inline BFSQuery(Predicate const& p) : pr(p) {}
inline void operator()(internal_node const& n) {
for (auto&& [bounds, node] : rtree::elements(n))
if (pr(bounds))
rtree::apply_visitor(*this, *node);
}
inline void operator()(leaf const& n) {
for (auto& item : rtree::elements(n))
if (pr(item)) results.insert(&item);
}
Predicate const& pr;
std::set<Value const*> results;
};
Note: one random choice was to deduplicate results using a std::set, which has the side-effect that results will be in unspecified order ("address order" if you will).
使用这个访问者,算法本身可以非常简单:
template <typename TreeA, typename TreeB, typename F>
void tree_insersects(TreeA const& a, TreeB const& b, F action) {
using V = rtree::utilities::view<TreeA>;
V av(a);
auto const pred = [&b](auto const& bounds) {
return bgi::qbegin(b, bgi::intersects(bounds)) != bgi::qend(b);
};
BFSQuery<
decltype(pred),
typename V::value_type,
typename V::options_type,
typename V::box_type,
typename V::allocators_type
> vis(pred);
av.apply_visitor(vis);
auto tr = av.translator();
for (auto* hit : vis.results)
action(tr(*hit));
}
注意尽可能通用。
使用它:
int main() {
using Box = bg::model::box<bg::model::d2::point_xy<int> >;
// generate some boxes with nesting
bgi::rtree<Box, bgi::rstar<5>> a;
for (auto [k,l] : { std::pair(0, 1), std::pair(-1, 2) }) {
std::generate_n(bgi::insert_iterator(a), 10,
[k,l,i=1]() mutable { Box b{ {i+k,i+k}, {i+l,i+l} }; i+=2; return b; });
}
// another simple tree to intersect with
bgi::rtree<Box, bgi::quadratic<16> > b;
b.insert({ {9,9}, {12,12} });
b.insert({ {-9,-9}, {1,2} });
Demo::tree_insersects(a, b, [](auto& value) {
std::cout << "intersects: " << bg::dsv(value) << "\n";
});
}
打印(顺序可能不同):
intersects: ((1, 1), (2, 2))
intersects: ((0, 0), (3, 3))
intersects: ((11, 11), (12, 12))
intersects: ((10, 10), (13, 13))
intersects: ((12, 12), (15, 15))
intersects: ((6, 6), (9, 9))
intersects: ((8, 8), (11, 11))
intersects: ((9, 9), (10, 10))
我认为您可以使用标准查询谓词实现此目的:
for (auto& value : boost::make_iterator_range(bgi::qbegin(a, bgi::intersects(sphere)), {})) {
std::cout << "intersects: " << bg::dsv(value) << "\n";
}
后代的tree_insersects
算法
#include <boost/geometry/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/index/rtree.hpp>
#include <boost/geometry/index/detail/utilities.hpp>
#include <boost/geometry/index/predicates.hpp>
#include <iostream>
namespace bg = boost::geometry;
namespace bgi = bg::index;
namespace Demo {
namespace rtree = bgi::detail::rtree;
template <typename Predicate, typename Value, typename Options, typename Box, typename Allocators>
struct BFSQuery : public rtree::visitor<Value, typename Options::parameters_type, Box, Allocators, typename Options::node_tag, true>::type
{
typedef typename rtree::internal_node<Value, typename Options::parameters_type, Box, Allocators, typename Options::node_tag>::type internal_node;
typedef typename rtree::leaf<Value, typename Options::parameters_type, Box, Allocators, typename Options::node_tag>::type leaf;
inline BFSQuery(Predicate const& p) : pr(p) {}
inline void operator()(internal_node const& n) {
for (auto&& [bounds, node] : rtree::elements(n)) {
if (pr(bounds))
rtree::apply_visitor(*this, *node);
}
}
inline void operator()(leaf const& n) {
for (auto& item : rtree::elements(n))
if (pr(item)) results.insert(&item);
}
Predicate const& pr;
std::set<Value const*> results;
};
template <typename TreeA, typename TreeB, typename F> void tree_insersects(TreeA const& a, TreeB const& b, F action) {
using V = rtree::utilities::view<TreeA>;
V av(a);
auto const pred = [&b](auto const& bounds) {
return bgi::qbegin(b, bgi::intersects(bounds)) != bgi::qend(b);
};
BFSQuery<
decltype(pred),
typename V::value_type,
typename V::options_type,
typename V::box_type,
typename V::allocators_type
> vis(pred);
av.apply_visitor(vis);
auto tr = av.translator();
for (auto* hit : vis.results)
action(tr(*hit));
}
}
int main() {
using Box = bg::model::box<bg::model::d2::point_xy<int> >;
// generate some boxes with nesting
bgi::rtree<Box, bgi::rstar<5>> a;
for (auto [k,l] : { std::pair(0, 1), std::pair(-1, 2) }) {
std::generate_n(bgi::insert_iterator(a), 10,
[k,l,i=1]() mutable { Box b{ {i+k,i+k}, {i+l,i+l} }; i+=2; return b; });
}
// another simple tree to intersect with
bgi::rtree<Box, bgi::quadratic<16> > b;
b.insert({ {9,9}, {12,12} });
b.insert({ {-9,-9}, {1,2} });
Demo::tree_insersects(a, b, [](auto& value) {
std::cout << "intersects: " << bg::dsv(value) << "\n";
});
}
关于c++ - 我是否需要遍历 boost rtree 的层次结构以达到最大效率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57613782/
“用 Haskell 进行函数式思考”中的练习之一是使用融合定律使程序更加高效。我在尝试复制答案时遇到了一些麻烦。 部分计算要求您将 maximum (xs++ map (x+) xs) 转换为 ma
我正在尝试获得 R 中最大/最小的可表示数字。 输入“.Machine”后 我有: $double.xmin [1] 2.225074e-308 $double.xmax [1] 1.797693e+
有没有办法更改浏览器验证消息 请检查所附图片。 我目前正在使用 wooCommerce 目前它显示小于或等于 X 个数字,我想更改为请求超过 X 个项目的报价。 请多多指教 最佳答案 您需要使用oni
我正在尝试将解决方案从 Excel 求解器复制到 R 中,但不知道从哪里开始。 问题: 每小时选择 5 个选项(5 行),以最大化“分数”的总和,而无需在多个小时内选择同一组 2 次。 换句话说: 最
Haskell 中是否有这样的功能: max_of_type :: (Num a) => a 所以: max_of_type :: Int == 2 ^ 31 - 1 // for example,
我有这两个表示时间范围(秒)的输入字段,我需要这样设置,以便“from/min”字段不能高于“to/max”,反之亦然。 到目前为止我得到了这个: jQuery(document).ready(fun
我有一个看起来像这样的表: http://sqlfiddle.com/#!9/152d2/1/0 CREATE TABLE Table1 ( id int, value decimal(10,
我会尝试尽可能简单地解释它: 首先是一些带有虚拟数据的数据库结构。 结构 tb_spec_fk feature value ----------------- 1 1 1
我有两个表。 表 1: +---------+---------+ | Lead_ID | Deal_ID | +---------+---------+ | 2323 | null |
我的数据库中有一个字段可以包含数字,例如8.00 或范围编号,例如8.00 - 10.00。 如果您将每个数字作为单独的数字,我需要从表中获取 MIN() 和 MAX()。例如当范围为 8.00 -
max(float('nan'), 1) 计算结果为 nan max(1, float('nan')) 计算结果为 1 这是预期的行为吗? 感谢您的回答。 max 在 iterable 为空时引发异常
我想问一下如何在 CSS 中创建一个页脚栏,它具有最小宽度(比如 650 像素),并且会根据窗口大小进行拉伸(stretch),但仅限于某个点(比如 1024 像素)。 我的意思是当窗口大小为例如 1
我尝试调整表格列宽(下一个链接上的“作者”列 http://deploy.jtalks.org/jcommune/branches/1?lang=en)。我已将最小/最大属性添加到 .author-c
在 C# 中,是否有用于将最小值和最大值存储为 double 值的内置类? 此处列出的要点 http://msdn.microsoft.com/en-us/library/system.windows
问题: 每个任务队列是否可以每秒处理超过 500 个任务? 每个 GAE 应用是否可以每秒处理超过 50,000 个任务? 详细信息: Task queue quota文档说: Push Queue
我想知道是否允许最大或最小堆树具有重复值?我试图仅通过在线资源查找与此相关的信息,但一直没有成功。 最佳答案 是的,他们可以。您可以在“算法简介”(Charles E. Leiserson、Cliff
首先,我是 .NET 开发人员,喜欢 C# 中的 LINQ 和扩展方法。 但是当我编写脚本时,我需要相当于 Enumerable extension methods 的东西 任何人都可以给我任何建议/
这是一个检查最大 malloc 大小的简单程序: #include std::size_t maxDataSize = 2097152000; //2000mb void MallocTest(vo
我想找到我的数据的最小值和最大值。 我的数据文件: 1 2 4 5 -3 -13 112 -3 55 42 42 而我的脚本: {min=max=$1} {if ($1max) {max=$1}
我想查询我的Elastic-Search以获取仅具有正值的最低价格价格。我的价格也可以为零和-1;所以我不希望我的最小聚合返回0或-1。我知道我应该向查询(或过滤器)添加脚本,但是我不知道如何。我当前
我是一名优秀的程序员,十分优秀!