载入中...
搜索中...
未找到
Octree.h
浏览该文件的文档.
1#pragma once
2
3#include "spatial/Bounds.h"
4#include "spatial/QueryIds.h"
5
6#include <memory>
7#include <unordered_map>
8#include <vector>
9
10namespace eve::spatial {
11
16class Octree {
17public:
19 Octree(float minX, float minY, float minZ, float maxX, float maxY, float maxZ,
20 int maxDepth = 8, int maxPerNode = 8);
21 ~Octree() = default;
22
23 Octree(const Octree &) = delete;
24 Octree &operator=(const Octree &) = delete;
25
27 void clear();
29 bool insert(int id, float minX, float minY, float minZ, float maxX, float maxY, float maxZ);
31 bool remove(int id);
32 bool update(int id, float minX, float minY, float minZ, float maxX, float maxY, float maxZ);
34 bool contains(int id) const;
35 int getCount() const { return static_cast<int>(items_.size()); }
36
38 int queryPoint(float x, float y, float z);
39 int queryAABB(float minX, float minY, float minZ, float maxX, float maxY, float maxZ);
40 int querySphere(float cx, float cy, float cz, float radius);
41
43 int getResultCount() const { return results_.getCount(); }
44 int getResultId(int index) const { return results_.getId(index); }
45
47 float getMinX() const { return rootBounds_.minX; }
48 float getMinY() const { return rootBounds_.minY; }
49 float getMinZ() const { return rootBounds_.minZ; }
50 float getMaxX() const { return rootBounds_.maxX; }
51 float getMaxY() const { return rootBounds_.maxY; }
52 float getMaxZ() const { return rootBounds_.maxZ; }
53 int getMaxDepth() const { return maxDepth_; }
54 int getMaxPerNode() const { return maxPerNode_; }
55
56private:
57 struct Node {
58 AABB3 bounds;
59 int depth = 0;
60 std::vector<int> itemIds;
61 std::unique_ptr<Node> children[8];
62 bool isLeaf() const { return children[0] == nullptr; }
63 };
64
65 void rebuild();
66 bool insertInto(Node &node, int id, const AABB3 &bounds);
67 void split(Node &node);
68 void collect(Node &node, const AABB3 *box, float cx, float cy, float cz, float radius,
69 bool useSphere);
70 Node *ensureRoot();
71
72 AABB3 rootBounds_;
73 int maxDepth_ = 8;
74 int maxPerNode_ = 8;
75 std::unique_ptr<Node> root_;
76 std::unordered_map<int, AABB3> items_;
77 QueryIds results_;
78};
79
80} // namespace eve::spatial
float cx
Definition CardTypes.cpp:31
float cy
Definition CardTypes.cpp:32
int y
Definition Grass.cpp:135
int z
Definition Grass.cpp:135
int x
Definition Grass.cpp:135
float depth
int children
Definition TreeMesh.cpp:177
Region octree for 3D AABB broad-phase / scene culling. Same storage rules as QuadTree (smallest fully...
Definition Octree.h:16
Octree(const Octree &)=delete
bool insert(int id, float minX, float minY, float minZ, float maxX, float maxY, float maxZ)
插入一个 AABB 对象;false 表示越界或已存在。
Definition Octree.cpp:37
float getMaxZ() const
Definition Octree.h:52
int queryPoint(float x, float y, float z)
查询:点 / AABB / 球体,命中写入结果缓冲区。
Definition Octree.cpp:161
int getMaxPerNode() const
Definition Octree.h:54
int queryAABB(float minX, float minY, float minZ, float maxX, float maxY, float maxZ)
Definition Octree.cpp:167
bool update(int id, float minX, float minY, float minZ, float maxX, float maxY, float maxZ)
Definition Octree.cpp:56
int querySphere(float cx, float cy, float cz, float radius)
Definition Octree.cpp:174
float getMinZ() const
Definition Octree.h:49
float getMinX() const
根包围盒与分裂参数。
Definition Octree.h:47
float getMaxY() const
Definition Octree.h:51
Octree & operator=(const Octree &)=delete
bool contains(int id) const
对象是否存在。
Definition Octree.cpp:35
void clear()
清空全部对象。
Definition Octree.cpp:28
int getResultCount() const
最近一次查询的结果。
Definition Octree.h:43
int getMaxDepth() const
Definition Octree.h:53
float getMaxX() const
Definition Octree.h:50
int getResultId(int index) const
Definition Octree.h:44
float getMinY() const
Definition Octree.h:48
int getCount() const
Definition Octree.h:35
bool remove(int id)
移除 / 更新一个对象。
Definition Octree.cpp:49
int getId(int index) const
Definition QueryIds.h:26
int getCount() const
Definition QueryIds.h:24