载入中...
搜索中...
未找到
BSPTree3D.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 BSPTree3D {
17public:
18 BSPTree3D(float minX, float minY, float minZ, float maxX, float maxY, float maxZ,
19 int maxDepth = 12, int maxPerNode = 8);
20 ~BSPTree3D() = default;
21
22 BSPTree3D(const BSPTree3D &) = delete;
23 BSPTree3D &operator=(const BSPTree3D &) = delete;
24
25 void clear();
26 bool insert(int id, float minX, float minY, float minZ, float maxX, float maxY, float maxZ);
27 bool remove(int id);
28 bool update(int id, float minX, float minY, float minZ, float maxX, float maxY, float maxZ);
29 bool contains(int id) const;
30 int getCount() const { return static_cast<int>(items_.size()); }
31
32 int queryPoint(float x, float y, float z);
33 int queryAABB(float minX, float minY, float minZ, float maxX, float maxY, float maxZ);
34 int querySphere(float cx, float cy, float cz, float radius);
35
36 int getResultCount() const { return results_.getCount(); }
37 int getResultId(int index) const { return results_.getId(index); }
38
39 float getMinX() const { return rootBounds_.minX; }
40 float getMinY() const { return rootBounds_.minY; }
41 float getMinZ() const { return rootBounds_.minZ; }
42 float getMaxX() const { return rootBounds_.maxX; }
43 float getMaxY() const { return rootBounds_.maxY; }
44 float getMaxZ() const { return rootBounds_.maxZ; }
45 int getMaxDepth() const { return maxDepth_; }
46 int getMaxPerNode() const { return maxPerNode_; }
47
48private:
49 struct Node {
50 AABB3 bounds;
51 int depth = 0;
52 int axis = 0; // 0=X, 1=Y, 2=Z
53 float split = 0.f;
54 std::vector<int> itemIds;
55 std::unique_ptr<Node> left;
56 std::unique_ptr<Node> right;
57 bool isLeaf() const { return left == nullptr; }
58 };
59
60 void rebuild();
61 bool insertInto(Node &node, int id, const AABB3 &bounds);
62 void split(Node &node);
63 void collect(Node &node, const AABB3 *box, float cx, float cy, float cz, float radius,
64 bool useSphere);
65 Node *ensureRoot();
66
67 AABB3 rootBounds_;
68 int maxDepth_ = 12;
69 int maxPerNode_ = 8;
70 std::unique_ptr<Node> root_;
71 std::unordered_map<int, AABB3> items_;
72 QueryIds results_;
73};
74
75} // 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
Binary space partition tree (kd-style AABB splits) for 3D culling. Alternating X/Y/Z splits at node m...
Definition BSPTree3D.h:16
float getMinY() const
Definition BSPTree3D.h:40
BSPTree3D & operator=(const BSPTree3D &)=delete
bool contains(int id) const
Definition BSPTree3D.cpp:36
float getMinX() const
Definition BSPTree3D.h:39
float getMinZ() const
Definition BSPTree3D.h:41
int queryAABB(float minX, float minY, float minZ, float maxX, float maxY, float maxZ)
BSPTree3D(const BSPTree3D &)=delete
float getMaxZ() const
Definition BSPTree3D.h:44
float getMaxY() const
Definition BSPTree3D.h:43
int getResultCount() const
Definition BSPTree3D.h:36
int querySphere(float cx, float cy, float cz, float radius)
int queryPoint(float x, float y, float z)
int getResultId(int index) const
Definition BSPTree3D.h:37
int getMaxPerNode() const
Definition BSPTree3D.h:46
bool update(int id, float minX, float minY, float minZ, float maxX, float maxY, float maxZ)
Definition BSPTree3D.cpp:57
bool insert(int id, float minX, float minY, float minZ, float maxX, float maxY, float maxZ)
Definition BSPTree3D.cpp:38
float getMaxX() const
Definition BSPTree3D.h:42
int getMaxDepth() const
Definition BSPTree3D.h:45
int getId(int index) const
Definition QueryIds.h:26
int getCount() const
Definition QueryIds.h:24