载入中...
搜索中...
未找到
BSPTree2D.cpp
浏览该文件的文档.
1#include "spatial/BSPTree2D.h"
2#include "common/Exception.h"
3#include "spatial/BSPTree3D.h"
4
5// BSPTree2D.cpp
6namespace eve::spatial {
7
8BSPTree2D::BSPTree2D(float minX, float minY, float maxX, float maxY, int maxDepth, int maxPerNode)
9 : rootBounds_(makeAABB2(minX, minY, maxX, maxY)),
10 maxDepth_(std::max(1, maxDepth)),
11 maxPerNode_(std::max(1, maxPerNode)) {
12 if (!rootBounds_.valid() || rootBounds_.width() <= 0.f || rootBounds_.height() <= 0.f) {
13 throw Exception("BSPTree2D: bounds must have positive area");
14 }
15 ensureRoot();
16}
17
18BSPTree2D::Node *BSPTree2D::ensureRoot() {
19 if (!root_) {
20 root_ = std::make_unique<Node>();
21 root_->bounds = rootBounds_;
22 root_->depth = 0;
23 root_->axis = 0;
24 }
25 return root_.get();
26}
27
29 items_.clear();
30 results_.clear();
31 root_.reset();
32 ensureRoot();
33}
34
35bool BSPTree2D::contains(int id) const { return items_.find(id) != items_.end(); }
36
37bool BSPTree2D::insert(int id, float minX, float minY, float maxX, float maxY) {
38 AABB2 b = makeAABB2(minX, minY, maxX, maxY);
39 if (!b.valid()) return false;
40 if (contains(id)) remove(id);
41 items_[id] = b;
42 if (!insertInto(*ensureRoot(), id, b)) {
43 ensureRoot()->itemIds.push_back(id);
44 }
45 return true;
46}
47
48bool BSPTree2D::remove(int id) {
49 if (!contains(id)) return false;
50 items_.erase(id);
51 rebuild();
52 return true;
53}
54
55bool BSPTree2D::update(int id, float minX, float minY, float maxX, float maxY) {
56 if (!contains(id)) return false;
57 return insert(id, minX, minY, maxX, maxY);
58}
59
60void BSPTree2D::rebuild() {
61 results_.clear();
62 root_.reset();
63 ensureRoot();
64 for (const auto &kv : items_) {
65 if (!insertInto(*root_, kv.first, kv.second)) {
66 root_->itemIds.push_back(kv.first);
67 }
68 }
69}
70
71bool BSPTree2D::insertInto(Node &node, int id, const AABB2 &bounds) {
72 if (!node.bounds.containsAABB(bounds) && node.depth == 0) {
73 if (!node.bounds.intersectsAABB(bounds)) return false;
74 } else if (node.depth > 0 && !node.bounds.containsAABB(bounds)) {
75 return false;
76 }
77
78 if (node.isLeaf()) {
79 node.itemIds.push_back(id);
80 if (static_cast<int>(node.itemIds.size()) > maxPerNode_ && node.depth < maxDepth_) {
81 split(node);
82 }
83 return true;
84 }
85
86 // Fully on one side of the split plane?
87 if (node.axis == 0) {
88 if (bounds.maxX <= node.split && node.left) {
89 return insertInto(*node.left, id, bounds);
90 }
91 if (bounds.minX >= node.split && node.right) {
92 return insertInto(*node.right, id, bounds);
93 }
94 } else {
95 if (bounds.maxY <= node.split && node.left) {
96 return insertInto(*node.left, id, bounds);
97 }
98 if (bounds.minY >= node.split && node.right) {
99 return insertInto(*node.right, id, bounds);
100 }
101 }
102 node.itemIds.push_back(id);
103 return true;
104}
105
106void BSPTree2D::split(Node &node) {
107 node.axis = node.depth % 2;
108 node.split = (node.axis == 0) ? node.bounds.centerX() : node.bounds.centerY();
109
110 node.left = std::make_unique<Node>();
111 node.right = std::make_unique<Node>();
112 node.left->depth = node.depth + 1;
113 node.right->depth = node.depth + 1;
114 node.left->axis = node.left->depth % 2;
115 node.right->axis = node.right->depth % 2;
116
117 if (node.axis == 0) {
118 node.left->bounds = makeAABB2(node.bounds.minX, node.bounds.minY, node.split, node.bounds.maxY);
119 node.right->bounds = makeAABB2(node.split, node.bounds.minY, node.bounds.maxX, node.bounds.maxY);
120 } else {
121 node.left->bounds = makeAABB2(node.bounds.minX, node.bounds.minY, node.bounds.maxX, node.split);
122 node.right->bounds = makeAABB2(node.bounds.minX, node.split, node.bounds.maxX, node.bounds.maxY);
123 }
124
125 std::vector<int> remain;
126 remain.reserve(node.itemIds.size());
127 for (int id : node.itemIds) {
128 auto it = items_.find(id);
129 if (it == items_.end()) continue;
130 const AABB2 &b = it->second;
131 if (node.axis == 0) {
132 if (b.maxX <= node.split) {
133 node.left->itemIds.push_back(id);
134 } else if (b.minX >= node.split) {
135 node.right->itemIds.push_back(id);
136 } else {
137 remain.push_back(id);
138 }
139 } else {
140 if (b.maxY <= node.split) {
141 node.left->itemIds.push_back(id);
142 } else if (b.minY >= node.split) {
143 node.right->itemIds.push_back(id);
144 } else {
145 remain.push_back(id);
146 }
147 }
148 }
149 node.itemIds.swap(remain);
150}
151
152void BSPTree2D::collect(Node &node, const AABB2 *rect, float cx, float cy, float radius, bool useCircle) {
153 if (rect && !node.bounds.intersectsAABB(*rect)) return;
154 if (useCircle && !node.bounds.intersectsCircle(cx, cy, radius)) return;
155
156 for (int id : node.itemIds) {
157 auto it = items_.find(id);
158 if (it == items_.end()) continue;
159 const AABB2 &b = it->second;
160 bool hit = false;
161 if (rect) {
162 hit = b.intersectsAABB(*rect);
163 } else if (useCircle) {
164 hit = b.intersectsCircle(cx, cy, radius);
165 } else {
166 hit = b.containsPoint(cx, cy);
167 }
168 if (hit) results_.addUnique(id);
169 }
170
171 if (!node.isLeaf()) {
172 if (node.left) collect(*node.left, rect, cx, cy, radius, useCircle);
173 if (node.right) collect(*node.right, rect, cx, cy, radius, useCircle);
174 }
175}
176
177int BSPTree2D::queryPoint(float x, float y) {
178 results_.clear();
179 collect(*ensureRoot(), nullptr, x, y, 0.f, false);
180 return results_.getCount();
181}
182
183int BSPTree2D::queryRect(float minX, float minY, float maxX, float maxY) {
184 results_.clear();
185 AABB2 rect = makeAABB2(minX, minY, maxX, maxY);
186 collect(*ensureRoot(), &rect, 0.f, 0.f, 0.f, false);
187 return results_.getCount();
188}
189
190int BSPTree2D::queryCircle(float cx, float cy, float radius) {
191 results_.clear();
192 if (radius < 0.f) radius = 0.f;
193 collect(*ensureRoot(), nullptr, cx, cy, radius, true);
194 return results_.getCount();
195}
196
197} // namespace eve::spatial
198
199// BSPTree3D.cpp
200namespace eve::spatial {
201
202BSPTree3D::BSPTree3D(float minX, float minY, float minZ, float maxX, float maxY, float maxZ, int maxDepth,
203 int maxPerNode)
204 : rootBounds_(makeAABB3(minX, minY, minZ, maxX, maxY, maxZ)),
205 maxDepth_(std::max(1, maxDepth)),
206 maxPerNode_(std::max(1, maxPerNode)) {
207 if (!rootBounds_.valid() || rootBounds_.width() <= 0.f || rootBounds_.height() <= 0.f ||
208 rootBounds_.depth() <= 0.f) {
209 throw Exception("BSPTree3D: bounds must have positive volume");
210 }
211 ensureRoot();
212}
213
214BSPTree3D::Node *BSPTree3D::ensureRoot() {
215 if (!root_) {
216 root_ = std::make_unique<Node>();
217 root_->bounds = rootBounds_;
218 root_->depth = 0;
219 root_->axis = 0;
220 }
221 return root_.get();
222}
223
225 items_.clear();
226 results_.clear();
227 root_.reset();
228 ensureRoot();
229}
230
231bool BSPTree3D::contains(int id) const { return items_.find(id) != items_.end(); }
232
233bool BSPTree3D::insert(int id, float minX, float minY, float minZ, float maxX, float maxY, float maxZ) {
234 AABB3 b = makeAABB3(minX, minY, minZ, maxX, maxY, maxZ);
235 if (!b.valid()) return false;
236 if (contains(id)) remove(id);
237 items_[id] = b;
238 if (!insertInto(*ensureRoot(), id, b)) {
239 ensureRoot()->itemIds.push_back(id);
240 }
241 return true;
242}
243
244bool BSPTree3D::remove(int id) {
245 if (!contains(id)) return false;
246 items_.erase(id);
247 rebuild();
248 return true;
249}
250
251bool BSPTree3D::update(int id, float minX, float minY, float minZ, float maxX, float maxY, float maxZ) {
252 if (!contains(id)) return false;
253 return insert(id, minX, minY, minZ, maxX, maxY, maxZ);
254}
255
256void BSPTree3D::rebuild() {
257 results_.clear();
258 root_.reset();
259 ensureRoot();
260 for (const auto &kv : items_) {
261 if (!insertInto(*root_, kv.first, kv.second)) {
262 root_->itemIds.push_back(kv.first);
263 }
264 }
265}
266
267bool BSPTree3D::insertInto(Node &node, int id, const AABB3 &bounds) {
268 if (!node.bounds.containsAABB(bounds) && node.depth == 0) {
269 if (!node.bounds.intersectsAABB(bounds)) return false;
270 } else if (node.depth > 0 && !node.bounds.containsAABB(bounds)) {
271 return false;
272 }
273
274 if (node.isLeaf()) {
275 node.itemIds.push_back(id);
276 if (static_cast<int>(node.itemIds.size()) > maxPerNode_ && node.depth < maxDepth_) {
277 split(node);
278 }
279 return true;
280 }
281
282 float bMin = 0.f, bMax = 0.f;
283 if (node.axis == 0) {
284 bMin = bounds.minX;
285 bMax = bounds.maxX;
286 } else if (node.axis == 1) {
287 bMin = bounds.minY;
288 bMax = bounds.maxY;
289 } else {
290 bMin = bounds.minZ;
291 bMax = bounds.maxZ;
292 }
293
294 if (bMax <= node.split && node.left) {
295 return insertInto(*node.left, id, bounds);
296 }
297 if (bMin >= node.split && node.right) {
298 return insertInto(*node.right, id, bounds);
299 }
300 node.itemIds.push_back(id);
301 return true;
302}
303
304void BSPTree3D::split(Node &node) {
305 node.axis = node.depth % 3;
306 if (node.axis == 0) {
307 node.split = node.bounds.centerX();
308 } else if (node.axis == 1) {
309 node.split = node.bounds.centerY();
310 } else {
311 node.split = node.bounds.centerZ();
312 }
313
314 node.left = std::make_unique<Node>();
315 node.right = std::make_unique<Node>();
316 node.left->depth = node.depth + 1;
317 node.right->depth = node.depth + 1;
318 node.left->axis = node.left->depth % 3;
319 node.right->axis = node.right->depth % 3;
320
321 const AABB3 &b = node.bounds;
322 if (node.axis == 0) {
323 node.left->bounds = makeAABB3(b.minX, b.minY, b.minZ, node.split, b.maxY, b.maxZ);
324 node.right->bounds = makeAABB3(node.split, b.minY, b.minZ, b.maxX, b.maxY, b.maxZ);
325 } else if (node.axis == 1) {
326 node.left->bounds = makeAABB3(b.minX, b.minY, b.minZ, b.maxX, node.split, b.maxZ);
327 node.right->bounds = makeAABB3(b.minX, node.split, b.minZ, b.maxX, b.maxY, b.maxZ);
328 } else {
329 node.left->bounds = makeAABB3(b.minX, b.minY, b.minZ, b.maxX, b.maxY, node.split);
330 node.right->bounds = makeAABB3(b.minX, b.minY, node.split, b.maxX, b.maxY, b.maxZ);
331 }
332
333 std::vector<int> remain;
334 remain.reserve(node.itemIds.size());
335 for (int id : node.itemIds) {
336 auto it = items_.find(id);
337 if (it == items_.end()) continue;
338 const AABB3 &ib = it->second;
339 float iMin = 0.f, iMax = 0.f;
340 if (node.axis == 0) {
341 iMin = ib.minX;
342 iMax = ib.maxX;
343 } else if (node.axis == 1) {
344 iMin = ib.minY;
345 iMax = ib.maxY;
346 } else {
347 iMin = ib.minZ;
348 iMax = ib.maxZ;
349 }
350 if (iMax <= node.split) {
351 node.left->itemIds.push_back(id);
352 } else if (iMin >= node.split) {
353 node.right->itemIds.push_back(id);
354 } else {
355 remain.push_back(id);
356 }
357 }
358 node.itemIds.swap(remain);
359}
360
361void BSPTree3D::collect(Node &node, const AABB3 *box, float cx, float cy, float cz, float radius, bool useSphere) {
362 if (box && !node.bounds.intersectsAABB(*box)) return;
363 if (useSphere && !node.bounds.intersectsSphere(cx, cy, cz, radius)) return;
364
365 for (int id : node.itemIds) {
366 auto it = items_.find(id);
367 if (it == items_.end()) continue;
368 const AABB3 &b = it->second;
369 bool hit = false;
370 if (box) {
371 hit = b.intersectsAABB(*box);
372 } else if (useSphere) {
373 hit = b.intersectsSphere(cx, cy, cz, radius);
374 } else {
375 hit = b.containsPoint(cx, cy, cz);
376 }
377 if (hit) results_.addUnique(id);
378 }
379
380 if (!node.isLeaf()) {
381 if (node.left) collect(*node.left, box, cx, cy, cz, radius, useSphere);
382 if (node.right) collect(*node.right, box, cx, cy, cz, radius, useSphere);
383 }
384}
385
386int BSPTree3D::queryPoint(float x, float y, float z) {
387 results_.clear();
388 collect(*ensureRoot(), nullptr, x, y, z, 0.f, false);
389 return results_.getCount();
390}
391
392int BSPTree3D::queryAABB(float minX, float minY, float minZ, float maxX, float maxY, float maxZ) {
393 results_.clear();
394 AABB3 box = makeAABB3(minX, minY, minZ, maxX, maxY, maxZ);
395 collect(*ensureRoot(), &box, 0.f, 0.f, 0.f, 0.f, false);
396 return results_.getCount();
397}
398
399int BSPTree3D::querySphere(float cx, float cy, float cz, float radius) {
400 results_.clear();
401 if (radius < 0.f) radius = 0.f;
402 collect(*ensureRoot(), nullptr, cx, cy, cz, radius, true);
403 return results_.getCount();
404}
405
406} // namespace eve::spatial
double z
double y
double x
float cx
Definition CardTypes.cpp:36
float cy
Definition CardTypes.cpp:37
std::string id
MeleePoint3 b
Definition MeleeHit.cpp:41
double radius
Definition MeleeHit.cpp:42
bool hit
const RoadNode * node
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
void clear()
Removes all stored entries.
Definition BSPTree2D.cpp:28
bool update(int id, float minX, float minY, float maxX, float maxY)
Moves an existing item to a new AABB; false if unknown or out of bounds.
Definition BSPTree2D.cpp:55
BSPTree2D(float minX, float minY, float maxX, float maxY, int maxDepth=12, int maxPerNode=8)
Creates a 2D BSP/kd tree covering the given bounds.
Definition BSPTree2D.cpp:8
int queryPoint(float x, float y)
Finds items overlapping a point; fills the result buffer.
bool contains(int id) const
True if the id is currently stored.
Definition BSPTree2D.cpp:35
int queryRect(float minX, float minY, float maxX, float maxY)
Finds items overlapping an AABB; fills the result buffer.
int queryCircle(float cx, float cy, float radius)
Finds items overlapping a circle; fills the result buffer.
bool remove(int id)
Removes an item by id; false if unknown.
Definition BSPTree2D.cpp:48
bool insert(int id, float minX, float minY, float maxX, float maxY)
Inserts an item AABB; false if out of bounds or id already present.
Definition BSPTree2D.cpp:37
bool contains(int id) const
True if the id is currently stored.
bool remove(int id)
Removes an item by id; false if unknown.
void clear()
Removes all stored entries.
int queryAABB(float minX, float minY, float minZ, float maxX, float maxY, float maxZ)
Finds items overlapping an AABB; fills the result buffer.
int querySphere(float cx, float cy, float cz, float radius)
Finds items overlapping a sphere; fills the result buffer.
BSPTree3D(float minX, float minY, float minZ, float maxX, float maxY, float maxZ, int maxDepth=12, int maxPerNode=8)
Creates a 3D BSP/kd tree covering the given bounds.
int queryPoint(float x, float y, float z)
Finds items overlapping a point; fills the result buffer.
bool update(int id, float minX, float minY, float minZ, float maxX, float maxY, float maxZ)
Moves an existing item to a new AABB; false if unknown or out of bounds.
bool insert(int id, float minX, float minY, float minZ, float maxX, float maxY, float maxZ)
Inserts an item AABB; false if out of bounds or id already present.
void clear()
Removes all stored entries.
Definition QueryIds.h:14
int getCount() const
Number of stored ids.
Definition QueryIds.h:28
void addUnique(int id)
Appends id if not already present.
Definition QueryIds.h:17
AABB2 makeAABB2(float minX, float minY, float maxX, float maxY)
Builds a normalized 2D AABB (swaps inverted mins/maxes).
Definition Bounds.h:108
AABB3 makeAABB3(float minX, float minY, float minZ, float maxX, float maxY, float maxZ)
Builds a normalized 3D AABB (swaps inverted mins/maxes).
Definition Bounds.h:115
Axis-aligned 2D bounding box (min/max inclusive).
Definition Bounds.h:10
bool valid() const
True when min <= max on every axis.
Definition Bounds.h:26
float width() const
Extent along X (max - min).
Definition Bounds.h:17
float height() const
Extent along Y (max - min).
Definition Bounds.h:19
Axis-aligned 3D bounding box (min/max inclusive).
Definition Bounds.h:54
float height() const
Extent along Y (max - min).
Definition Bounds.h:65
bool valid() const
True when min <= max on every axis.
Definition Bounds.h:76
float width() const
Extent along X (max - min).
Definition Bounds.h:63
float depth() const
Extent along Z (max - min).
Definition Bounds.h:67
glm::vec4 bounds