载入中...
搜索中...
未找到
BSPTree3D.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
4
5#include "spatial/Bounds.h"
6#include "spatial/QueryIds.h"
7
8#include <memory>
9#include <unordered_map>
10#include <vector>
11
12namespace eve::spatial {
13
19public:
21 BSPTree3D(float minX, float minY, float minZ, float maxX, float maxY, float maxZ,
22 int maxDepth = 12, int maxPerNode = 8);
24 ~BSPTree3D() = default;
25
26 BSPTree3D(const BSPTree3D &) = delete;
27 BSPTree3D &operator=(const BSPTree3D &) = delete;
28
30 void clear();
32 bool insert(int id, float minX, float minY, float minZ, float maxX, float maxY, float maxZ);
34 bool remove(int id);
36 bool update(int id, float minX, float minY, float minZ, float maxX, float maxY, float maxZ);
38 bool contains(int id) const;
40 int getCount() const { return static_cast<int>(items_.size()); }
41
43 int queryPoint(float x, float y, float z);
45 int queryAABB(float minX, float minY, float minZ, float maxX, float maxY, float maxZ);
47 int querySphere(float cx, float cy, float cz, float radius);
48
50 int getResultCount() const { return results_.getCount(); }
52 int getResultId(int index) const { return results_.getId(index); }
53
55 float getMinX() const { return rootBounds_.minX; }
57 float getMinY() const { return rootBounds_.minY; }
59 float getMinZ() const { return rootBounds_.minZ; }
61 float getMaxX() const { return rootBounds_.maxX; }
63 float getMaxY() const { return rootBounds_.maxY; }
65 float getMaxZ() const { return rootBounds_.maxZ; }
67 int getMaxDepth() const { return maxDepth_; }
69 int getMaxPerNode() const { return maxPerNode_; }
70
71private:
72 struct Node {
74 int depth = 0;
75 int axis = 0; // 0=X, 1=Y, 2=Z
76 float split = 0.f;
77 std::vector<int> itemIds;
78 std::unique_ptr<Node> left;
79 std::unique_ptr<Node> right;
80 bool isLeaf() const { return left == nullptr; }
81 };
82
83 void rebuild();
84 bool insertInto(Node &node, int id, const AABB3 &bounds);
85 void split(Node &node);
86 void collect(Node &node, const AABB3 *box, float cx, float cy, float cz, float radius,
87 bool useSphere);
88 Node *ensureRoot();
89
90 AABB3 rootBounds_;
91 int maxDepth_ = 12;
92 int maxPerNode_ = 8;
93 std::unique_ptr<Node> root_;
94 std::unordered_map<int, AABB3> items_;
95 QueryIds results_;
96};
97
98} // namespace eve::spatial
float y
Definition AnimClip.cpp:738
float x
Definition AnimClip.cpp:738
float z
Definition AnimClip.cpp:738
float cx
Definition CardTypes.cpp:33
float cy
Definition CardTypes.cpp:34
bool split
Definition CaveMesh.cpp:123
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
HexVec3 left
HexVec3 right
float radius
const RoadNode * node
uint32_t index
std::uint32_t depth
Binary space partition tree (kd-style AABB splits) for 3D culling. Alternating X/Y/Z splits at node m...
Definition BSPTree3D.h:18
float getMinY() const
Root/world minimum Y.
Definition BSPTree3D.h:57
BSPTree3D & operator=(const BSPTree3D &)=delete
float getMinX() const
Root/world minimum X.
Definition BSPTree3D.h:55
float getMinZ() const
Root/world minimum Z.
Definition BSPTree3D.h:59
BSPTree3D(const BSPTree3D &)=delete
float getMaxZ() const
Root/world maximum Z.
Definition BSPTree3D.h:65
float getMaxY() const
Root/world maximum Y.
Definition BSPTree3D.h:63
int getResultCount() const
Number of hits from the last query*.
Definition BSPTree3D.h:50
~BSPTree3D()=default
Releases tree nodes.
int getCount() const
Number of stored ids.
Definition BSPTree3D.h:40
int getResultId(int index) const
Hit id at dense index from the last query*, or -1.
Definition BSPTree3D.h:52
int getMaxPerNode() const
Item capacity before a node splits.
Definition BSPTree3D.h:69
float getMaxX() const
Root/world maximum X.
Definition BSPTree3D.h:61
int getMaxDepth() const
Maximum subdivision depth.
Definition BSPTree3D.h:67
Axis-aligned 3D bounding box (min/max inclusive).
Definition Bounds.h:54
glm::vec4 bounds