如何有效地修剪和合并四叉树中的节点?
How can I efficiently prune and merge nodes in a quadtree?
我正在使用四叉树来有效地收集某个2D区域中的粒子。粒子会随着时间的推移而褪色并最终消失,所以我需要在它们死亡时将它们从四叉树中移除。
我面临的问题是,如何最好地修剪四叉树以删除空节点,并在节点不再有足够的粒子来证明拆分的合理性时合并节点。
我的第一个想法是递归遍历树,检查每个子节点。如果所有 4 个节点都是空的,那么我会用一个标志将其传递到调用堆栈(true 表示空,如果不是,则为 false(,否则,我将检查每个子节点中的粒子数量,如果它们都小于MAX_PER_NODE,则将所有粒子拉入父节点并删除子节点。
也许像下面这样(在我写这篇文章时,这个伪代码实际上是意识流,所以对它持保留态度(:
class QuadTreeNode
{
public bool prune();
public void deleteChildren(); //just assume it does what you expect...
private:
std::vector<ParticleType> particles;
std::vector<QuadTreeNode*> children; // size <= 4
};
bool QuadTreeNode::prune()
{
bool allChildrenEmpty = true;
for(QuadTreeNode & n : children)
{
allChildrenEmpty &= n.prune();
}
//Merging could be handled by returning the number of particles instead of true/false
//If the total number of particles falls below the max per bucket, pull the particles into the
//current node and delete the children...
if(allChildrenEmpty) this.deleteChildren();
return allChildrenEmpty && (!this.particles.size());
}
然而,这似乎是低效的,因为我觉得这需要我触摸树中的每个节点(尽管我想这比访问每个粒子要好(。
有没有更好的方法来实现这种修剪和合并操作?
假设您没有受到严重的 RAM 约束,您可以为每个节点添加一个权重计数器,表示其本身的粒子总数及其十进制。 每当你插入或删除一个粒子时,你都会更新它,这实际上并不昂贵,因为你无论如何都要将这些节点拉到L1中。
然后,当您删除时,您还可以检查重量是否
相关文章:
- 在我的四叉树实现中遇到问题
- 如何有效地修剪和合并四叉树中的节点?
- 如何在C++的四叉树中存储 bmp 文件?
- 如何提高四叉树代码的性能以防止程序冻结
- 细分四叉树而不带新运算符
- 如何在UE4 / c++中可视化我的四叉树
- 四叉树递归检索
- 查询四叉树会导致溢出
- 如何有效地将游戏对象存储在四叉树中
- 如何将四叉树/八叉树用于块世界
- 用于碰撞检测实现的四叉树
- 四叉树和递归构造函数堆栈溢出
- 在基于四叉树的LOD地形上寻找示例
- 改进我的四叉树设计
- 不能建造四叉树吗?
- 尝试在游戏中使用四叉树进行碰撞检测
- 四叉树遍历
- 四叉树更新对象失败
- 如何在没有递归的情况下清除四叉树(也许使用队列?).
- c++中任何好的范围查询库(使用K-D树,四叉树或r语言 - 树)