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