载入中...
搜索中...
未找到
BSPTree2D.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 BSPTree2D(float minX, float minY, float maxX, float maxY, int maxDepth = 12,
22 int maxPerNode = 8);
24 ~BSPTree2D() = default;
25
26 BSPTree2D(const BSPTree2D &) = delete;
27 BSPTree2D &operator=(const BSPTree2D &) = delete;
28
30 void clear();
32 bool insert(int id, float minX, float minY, float maxX, float maxY);
34 bool remove(int id);
36 bool update(int id, float minX, float minY, float maxX, float maxY);
38 bool contains(int id) const;
40 int getCount() const { return static_cast<int>(items_.size()); }
41
43 int queryPoint(float x, float y);
45 int queryRect(float minX, float minY, float maxX, float maxY);
47 int queryCircle(float cx, float cy, 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 getMaxX() const { return rootBounds_.maxX; }
61 float getMaxY() const { return rootBounds_.maxY; }
63 int getMaxDepth() const { return maxDepth_; }
65 int getMaxPerNode() const { return maxPerNode_; }
66
67private:
68 struct Node {
70 int depth = 0;
71 int axis = 0; // 0=X, 1=Y
72 float split = 0.f;
73 std::vector<int> itemIds;
74 std::unique_ptr<Node> left;
75 std::unique_ptr<Node> right;
76 bool isLeaf() const { return left == nullptr; }
77 };
78
79 void rebuild();
80 bool insertInto(Node &node, int id, const AABB2 &bounds);
81 void split(Node &node);
82 void collect(Node &node, const AABB2 *rect, float cx, float cy, float radius, bool useCircle);
83 Node *ensureRoot();
84
85 AABB2 rootBounds_;
86 int maxDepth_ = 12;
87 int maxPerNode_ = 8;
88 std::unique_ptr<Node> root_;
89 std::unordered_map<int, AABB2> items_;
90 QueryIds results_;
91};
92
93} // namespace eve::spatial
float y
Definition AnimClip.cpp:738
float x
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 2D culling. Alternating X/Y splits at node mid...
Definition BSPTree2D.h:18
~BSPTree2D()=default
Releases tree nodes.
float getMinX() const
Root/world minimum X.
Definition BSPTree2D.h:55
int getResultId(int index) const
Hit id at dense index from the last query*, or -1.
Definition BSPTree2D.h:52
int getCount() const
Number of stored ids.
Definition BSPTree2D.h:40
float getMaxY() const
Root/world maximum Y.
Definition BSPTree2D.h:61
BSPTree2D & operator=(const BSPTree2D &)=delete
int getMaxDepth() const
Maximum subdivision depth.
Definition BSPTree2D.h:63
float getMaxX() const
Root/world maximum X.
Definition BSPTree2D.h:59
int getMaxPerNode() const
Item capacity before a node splits.
Definition BSPTree2D.h:65
int getResultCount() const
Number of hits from the last query*.
Definition BSPTree2D.h:50
BSPTree2D(const BSPTree2D &)=delete
float getMinY() const
Root/world minimum Y.
Definition BSPTree2D.h:57
Axis-aligned 2D bounding box (min/max inclusive).
Definition Bounds.h:10
glm::vec4 bounds