Debian Codegraph @

core/math/bvh_tree.h

raw symbols SCIP
1 /**************************************************************************/
2 /* bvh_tree.h */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #pragma once
32
33 // BVH Tree
34 // This is an implementation of a dynamic BVH with templated leaf size.
35 // This differs from most dynamic BVH in that it can handle more than 1 object
36 // in leaf nodes. This can make it far more efficient in certain circumstances.
37 // It also means that the splitting logic etc have to be completely different
38 // to a simpler tree.
39 // Note that MAX_CHILDREN should be fixed at 2 for now.
40
41 #include "core/math/aabb.h"
42 #include "core/math/bvh_abb.h"
43 #include "core/math/vector3.h"
44 #include "core/templates/local_vector.h"
45 #include "core/templates/pooled_list.h"
46
47 #include <climits>
48
49 #define BVHABB_CLASS BVH_ABB<BOUNDS, POINT>
50
51 // not sure if this is better yet so making optional
52 #define BVH_EXPAND_LEAF_AABBS
53
54 // never do these checks in release
55 #ifdef DEV_ENABLED
56 //#define BVH_VERBOSE
57 //#define BVH_VERBOSE_TREE
58 //#define BVH_VERBOSE_PAIRING
59 //#define BVH_VERBOSE_MOVES
60
61 //#define BVH_VERBOSE_FRAME
62 //#define BVH_CHECKS
63 //#define BVH_INTEGRITY_CHECKS
64 #endif
65
66 // debug only assert
67 #ifdef BVH_CHECKS
68 #define BVH_ASSERT(a) CRASH_COND((a) == false)
69 #else
70 #define BVH_ASSERT(a)
71 #endif
72
73 #ifdef BVH_VERBOSE
74 #define VERBOSE_PRINT print_line
75 #else
76 #define VERBOSE_PRINT(a)
77 #endif
78
79 // really just a namespace
80 struct BVHCommon {
81 // these could possibly also be the same constant,
82 // although this may be useful for debugging.
83 // or use zero for invalid and +1 based indices.
84 static const uint32_t INVALID = (0xffffffff);
85 static const uint32_t INACTIVE = (0xfffffffe);
86 };
87
88 // really a handle, can be anything
89 // note that zero is a valid reference for the BVH .. this may involve using
90 // a plus one based ID for clients that expect 0 to be invalid.
91 struct BVHHandle {
92 // conversion operator
93 operator uint32_t() const { return _data; }
94 void set(uint32_t p_value) { _data = p_value; }
95
96 uint32_t _data;
97
98 void set_invalid() { _data = BVHCommon::INVALID; }
99 bool is_invalid() const { return _data == BVHCommon::INVALID; }
100 uint32_t id() const { return _data; }
101 void set_id(uint32_t p_id) { _data = p_id; }
102
103 bool operator==(const BVHHandle &p_h) const { return _data == p_h._data; }
104 bool operator!=(const BVHHandle &p_h) const { return (*this == p_h) == false; }
105 };
106
107 // helper class to make iterative versions of recursive functions
108 template <typename T>
109 class BVH_IterativeInfo {
110 public:
111 constexpr static const size_t ALLOCA_STACK_SIZE = 128;
112
113 int32_t depth = 1;
114 int32_t threshold = ALLOCA_STACK_SIZE - 2;
115 T *stack;
116 //only used in rare occasions when you run out of alloca memory
117 // because tree is too unbalanced.
118 LocalVector<T> aux_stack;
119 int32_t get_alloca_stacksize() const { return ALLOCA_STACK_SIZE * sizeof(T); }
120
121 T *get_first() const {
122 return &stack[0];
123 }
124
125 // pop the last member of the stack, or return false
126 bool pop(T &r_value) {
127 if (!depth) {
128 return false;
129 }
130
131 depth--;
132 r_value = stack[depth];
133 return true;
134 }
135
136 // request new addition to stack
137 T *request() {
138 if (depth > threshold) {
139 if (aux_stack.is_empty()) {
140 aux_stack.resize(ALLOCA_STACK_SIZE * 2);
141 memcpy(aux_stack.ptr(), stack, get_alloca_stacksize());
142 } else {
143 aux_stack.resize(aux_stack.size() * 2);
144 }
145 stack = aux_stack.ptr();
146 threshold = aux_stack.size() - 2;
147 }
148 return &stack[depth++];
149 }
150 };
151
152 template <typename T>
153 class BVH_DummyPairTestFunction {
154 public:
155 static bool user_collision_check(T *p_a, T *p_b) {
156 // return false if no collision, decided by masks etc
157 return true;
158 }
159 };
160
161 template <typename T>
162 class BVH_DummyCullTestFunction {
163 public:
164 static bool user_cull_check(T *p_a, T *p_b) {
165 // return false if no collision
166 return true;
167 }
168 };
169
170 template <typename T, int NUM_TREES, int MAX_CHILDREN, int MAX_ITEMS, typename USER_PAIR_TEST_FUNCTION = BVH_DummyPairTestFunction<T>, typename USER_CULL_TEST_FUNCTION = BVH_DummyCullTestFunction<T>, bool USE_PAIRS = false, typename BOUNDS = AABB, typename POINT = Vector3>
171 class BVH_Tree {
172 friend class BVH;
173
174 #include "bvh_pair.inc"
175 #include "bvh_structs.inc"
176
177 public:
178 BVH_Tree() {
179 for (int n = 0; n < NUM_TREES; n++) {
180 _root_node_id[n] = BVHCommon::INVALID;
181 }
182
183 // disallow zero leaf ids
184 // (as these ids are stored as negative numbers in the node)
185 uint32_t dummy_leaf_id;
186 _leaves.request(dummy_leaf_id);
187
188 // In many cases you may want to change this default in the client code,
189 // or expose this value to the user.
190 // This default may make sense for a typically scaled 3d game, but maybe not for 2d on a pixel scale.
191 params_set_pairing_expansion(0.1);
192 }
193
194 private:
195 bool node_add_child(uint32_t p_node_id, uint32_t p_child_node_id) {
196 TNode &tnode = _nodes[p_node_id];
197 if (tnode.is_full_of_children()) {
198 return false;
199 }
200
201 tnode.children[tnode.num_children] = p_child_node_id;
202 tnode.num_children += 1;
203
204 // back link in the child to the parent
205 TNode &tnode_child = _nodes[p_child_node_id];
206 tnode_child.parent_id = p_node_id;
207
208 return true;
209 }
210
211 void node_replace_child(uint32_t p_parent_id, uint32_t p_old_child_id, uint32_t p_new_child_id) {
212 TNode &parent = _nodes[p_parent_id];
213 BVH_ASSERT(!parent.is_leaf());
214
215 int child_num = parent.find_child(p_old_child_id);
216 BVH_ASSERT(child_num != -1);
217 parent.children[child_num] = p_new_child_id;
218
219 TNode &new_child = _nodes[p_new_child_id];
220 new_child.parent_id = p_parent_id;
221 }
222
223 void node_remove_child(uint32_t p_parent_id, uint32_t p_child_id, uint32_t p_tree_id, bool p_prevent_sibling = false) {
224 TNode &parent = _nodes[p_parent_id];
225 BVH_ASSERT(!parent.is_leaf());
226
227 int child_num = parent.find_child(p_child_id);
228 BVH_ASSERT(child_num != -1);
229
230 parent.remove_child_internal(child_num);
231
232 // no need to keep back references for children at the moment
233
234 uint32_t sibling_id = 0; // always a node id, as tnode is never a leaf
235 bool sibling_present = false;
236
237 // if there are more children, or this is the root node, don't try and delete
238 if (parent.num_children > 1) {
239 return;
240 }
241
242 // if there is 1 sibling, it can be moved to be a child of the
243 if (parent.num_children == 1) {
244 // else there is now a redundant node with one child, which can be removed
245 sibling_id = parent.children[0];
246 sibling_present = true;
247 }
248
249 // now there may be no children in this node .. in which case it can be deleted
250 // remove node if empty
251 // remove link from parent
252 uint32_t grandparent_id = parent.parent_id;
253
254 // special case for root node
255 if (grandparent_id == BVHCommon::INVALID) {
256 if (sibling_present) {
257 // change the root node
258 change_root_node(sibling_id, p_tree_id);
259
260 // delete the old root node as no longer needed
261 node_free_node_and_leaf(p_parent_id);
262 }
263
264 return;
265 }
266
267 if (sibling_present) {
268 node_replace_child(grandparent_id, p_parent_id, sibling_id);
269 } else {
270 node_remove_child(grandparent_id, p_parent_id, p_tree_id, true);
271 }
272
273 // put the node on the free list to recycle
274 node_free_node_and_leaf(p_parent_id);
275 }
276
277 // A node can either be a node, or a node AND a leaf combo.
278 // Both must be deleted to prevent a leak.
279 void node_free_node_and_leaf(uint32_t p_node_id) {
280 TNode &node = _nodes[p_node_id];
281 if (node.is_leaf()) {
282 int leaf_id = node.get_leaf_id();
283 _leaves.free(leaf_id);
284 }
285
286 _nodes.free(p_node_id);
287 }
288
289 void change_root_node(uint32_t p_new_root_id, uint32_t p_tree_id) {
290 _root_node_id[p_tree_id] = p_new_root_id;
291 TNode &root = _nodes[p_new_root_id];
292
293 // mark no parent
294 root.parent_id = BVHCommon::INVALID;
295 }
296
297 void node_make_leaf(uint32_t p_node_id) {
298 uint32_t child_leaf_id;
299 TLeaf *child_leaf = _leaves.request(child_leaf_id);
300 child_leaf->clear();
301
302 // zero is reserved at startup, to prevent this id being used
303 // (as they are stored as negative values in the node, and zero is already taken)
304 BVH_ASSERT(child_leaf_id != 0);
305
306 TNode &node = _nodes[p_node_id];
307 node.neg_leaf_id = -(int)child_leaf_id;
308 }
309
310 void node_remove_item(uint32_t p_ref_id, uint32_t p_tree_id, BVHABB_CLASS *r_old_aabb = nullptr) {
311 // get the reference
312 ItemRef &ref = _refs[p_ref_id];
313 uint32_t owner_node_id = ref.tnode_id;
314
315 // debug draw special
316 // This may not be needed
317 if (owner_node_id == BVHCommon::INVALID) {
318 return;
319 }
320
321 TNode &tnode = _nodes[owner_node_id];
322 CRASH_COND(!tnode.is_leaf());
323
324 TLeaf &leaf = _node_get_leaf(tnode);
325
326 // if the aabb is not determining the corner size, then there is no need to refit!
327 // (optimization, as merging AABBs takes a lot of time)
328 const BVHABB_CLASS &old_aabb = leaf.get_aabb(ref.item_id);
329
330 // shrink a little to prevent using corner aabbs
331 // in order to miss the corners first we shrink by node_expansion
332 // (which is added to the overall bound of the leaf), then we also
333 // shrink by an epsilon, in order to miss out the very corner aabbs
334 // which are important in determining the bound. Any other aabb
335 // within this can be removed and not affect the overall bound.
336 BVHABB_CLASS node_bound = tnode.aabb;
337 node_bound.expand(-_node_expansion - 0.001f);
338 bool refit = true;
339
340 if (node_bound.is_other_within(old_aabb)) {
341 refit = false;
342 }
343
344 // record the old aabb if required (for incremental remove_and_reinsert)
345 if (r_old_aabb) {
346 *r_old_aabb = old_aabb;
347 }
348
349 leaf.remove_item_unordered(ref.item_id);
350
351 if (leaf.num_items) {
352 // the swapped item has to have its reference changed to, to point to the new item id
353 uint32_t swapped_ref_id = leaf.get_item_ref_id(ref.item_id);
354
355 ItemRef &swapped_ref = _refs[swapped_ref_id];
356
357 swapped_ref.item_id = ref.item_id;
358
359 // only have to refit if it is an edge item
360 // This is a VERY EXPENSIVE STEP
361 // we defer the refit updates until the update function is called once per frame
362 if (refit) {
363 leaf.set_dirty(true);
364 }
365 } else {
366 // remove node if empty
367 // remove link from parent
368 if (tnode.parent_id != BVHCommon::INVALID) {
369 // DANGER .. this can potentially end up with root node with 1 child ...
370 // we don't want this and must check for it
371
372 uint32_t parent_id = tnode.parent_id;
373
374 node_remove_child(parent_id, owner_node_id, p_tree_id);
375 refit_upward(parent_id);
376
377 // put the node on the free list to recycle
378 node_free_node_and_leaf(owner_node_id);
379 }
380
381 // else if no parent, it is the root node. Do not delete
382 }
383
384 ref.tnode_id = BVHCommon::INVALID;
385 ref.item_id = BVHCommon::INVALID; // unset
386 }
387
388 // returns true if needs refit of PARENT tree only, the node itself AABB is calculated
389 // within this routine
390 bool _node_add_item(uint32_t p_node_id, uint32_t p_ref_id, const BVHABB_CLASS &p_aabb) {
391 ItemRef &ref = _refs[p_ref_id];
392 ref.tnode_id = p_node_id;
393
394 TNode &node = _nodes[p_node_id];
395 BVH_ASSERT(node.is_leaf());
396 TLeaf &leaf = _node_get_leaf(node);
397
398 // optimization - we only need to do a refit
399 // if the added item is changing the AABB of the node.
400 // in most cases it won't.
401 bool needs_refit = true;
402
403 // expand bound now
404 BVHABB_CLASS expanded = p_aabb;
405 expanded.expand(_node_expansion);
406
407 // the bound will only be valid if there is an item in there already
408 if (leaf.num_items) {
409 if (node.aabb.is_other_within(expanded)) {
410 // no change to node AABBs
411 needs_refit = false;
412 } else {
413 node.aabb.merge(expanded);
414 }
415 } else {
416 // bound of the node = the new aabb
417 node.aabb = expanded;
418 }
419
420 ref.item_id = leaf.request_item();
421 BVH_ASSERT(ref.item_id != BVHCommon::INVALID);
422
423 // set the aabb of the new item
424 leaf.get_aabb(ref.item_id) = p_aabb;
425
426 // back reference on the item back to the item reference
427 leaf.get_item_ref_id(ref.item_id) = p_ref_id;
428
429 return needs_refit;
430 }
431
432 uint32_t _node_create_another_child(uint32_t p_node_id, const BVHABB_CLASS &p_aabb) {
433 uint32_t child_node_id;
434 TNode *child_node = _nodes.request(child_node_id);
435 child_node->clear();
436
437 // may not be necessary
438 child_node->aabb = p_aabb;
439
440 node_add_child(p_node_id, child_node_id);
441
442 return child_node_id;
443 }
444
445 #include "bvh_cull.inc"
446 #include "bvh_debug.inc"
447 #include "bvh_integrity.inc"
448 #include "bvh_logic.inc"
449 #include "bvh_misc.inc"
450 #include "bvh_public.inc"
451 #include "bvh_refit.inc"
452 #include "bvh_split.inc"
453 };
454
455 #undef VERBOSE_PRINT