Debian Codegraph @

core/math/bvh_logic.inc

raw
1 // for slow incremental optimization, we will periodically remove each
2 // item from the tree and reinsert, to give it a chance to find a better position
3 void _logic_item_remove_and_reinsert(uint32_t p_ref_id) {
4 // get the reference
5 ItemRef &ref = _refs[p_ref_id];
6
7 // no need to optimize inactive items
8 if (!ref.is_active()) {
9 return;
10 }
11
12 // special case of debug draw
13 if (ref.item_id == BVHCommon::INVALID) {
14 return;
15 }
16
17 BVH_ASSERT(ref.tnode_id != BVHCommon::INVALID);
18
19 // some overlay elaborate way to find out which tree the node is in!
20 BVHHandle temp_handle;
21 temp_handle.set_id(p_ref_id);
22 uint32_t tree_id = _handle_get_tree_id(temp_handle);
23
24 // remove and reinsert
25 BVHABB_CLASS abb;
26 node_remove_item(p_ref_id, tree_id, &abb);
27
28 // we must choose where to add to tree
29 ref.tnode_id = _logic_choose_item_add_node(_root_node_id[tree_id], abb);
30 _node_add_item(ref.tnode_id, p_ref_id, abb);
31
32 refit_upward_and_balance(ref.tnode_id, tree_id);
33 }
34
35 // from randy gaul balance function
36 BVHABB_CLASS _logic_abb_merge(const BVHABB_CLASS &a, const BVHABB_CLASS &b) {
37 BVHABB_CLASS c = a;
38 c.merge(b);
39 return c;
40 }
41
42 //--------------------------------------------------------------------------------------------------
43 /**
44 * @file q3DynamicAABBTree.h
45 * @author Randy Gaul
46 * @date 10/10/2014
47 * Copyright (c) 2014 Randy Gaul http://www.randygaul.net
48 * This software is provided 'as-is', without any express or implied
49 * warranty. In no event will the authors be held liable for any damages
50 * arising from the use of this software.
51 * Permission is granted to anyone to use this software for any purpose,
52 * including commercial applications, and to alter it and redistribute it
53 * freely, subject to the following restrictions:
54 * 1. The origin of this software must not be misrepresented; you must not
55 * claim that you wrote the original software. If you use this software
56 * in a product, an acknowledgment in the product documentation would be
57 * appreciated but is not required.
58 * 2. Altered source versions must be plainly marked as such, and must not
59 * be misrepresented as being the original software.
60 * 3. This notice may not be removed or altered from any source distribution.
61 */
62 //--------------------------------------------------------------------------------------------------
63
64 // This function is based on the 'Balance' function from Randy Gaul's qu3e
65 // https://github.com/RandyGaul/qu3e
66 // It is MODIFIED from qu3e version.
67 // This is the only function used (and _logic_abb_merge helper function).
68 int32_t _logic_balance(int32_t iA, uint32_t p_tree_id) {
69 //return iA; // uncomment this to bypass balance
70
71 TNode *A = &_nodes[iA];
72
73 if (A->is_leaf() || A->height == 1) {
74 return iA;
75 }
76
77 /* A
78 * / \
79 * B C
80 * / \ / \
81 * D E F G
82 */
83
84 CRASH_COND(A->num_children != 2);
85 int32_t iB = A->children[0];
86 int32_t iC = A->children[1];
87 TNode *B = &_nodes[iB];
88 TNode *C = &_nodes[iC];
89
90 int32_t balance = C->height - B->height;
91
92 // C is higher, promote C
93 if (balance > 1) {
94 int32_t iF = C->children[0];
95 int32_t iG = C->children[1];
96 TNode *F = &_nodes[iF];
97 TNode *G = &_nodes[iG];
98
99 // grandParent point to C
100 if (A->parent_id != BVHCommon::INVALID) {
101 if (_nodes[A->parent_id].children[0] == iA) {
102 _nodes[A->parent_id].children[0] = iC;
103
104 } else {
105 _nodes[A->parent_id].children[1] = iC;
106 }
107 } else {
108 // check this .. seems dodgy
109 change_root_node(iC, p_tree_id);
110 }
111
112 // Swap A and C
113 C->children[0] = iA;
114 C->parent_id = A->parent_id;
115 A->parent_id = iC;
116
117 // Finish rotation
118 if (F->height > G->height) {
119 C->children[1] = iF;
120 A->children[1] = iG;
121 G->parent_id = iA;
122 A->aabb = _logic_abb_merge(B->aabb, G->aabb);
123 C->aabb = _logic_abb_merge(A->aabb, F->aabb);
124
125 A->height = 1 + MAX(B->height, G->height);
126 C->height = 1 + MAX(A->height, F->height);
127 }
128
129 else {
130 C->children[1] = iG;
131 A->children[1] = iF;
132 F->parent_id = iA;
133 A->aabb = _logic_abb_merge(B->aabb, F->aabb);
134 C->aabb = _logic_abb_merge(A->aabb, G->aabb);
135
136 A->height = 1 + MAX(B->height, F->height);
137 C->height = 1 + MAX(A->height, G->height);
138 }
139
140 return iC;
141 }
142
143 // B is higher, promote B
144 else if (balance < -1) {
145 int32_t iD = B->children[0];
146 int32_t iE = B->children[1];
147 TNode *D = &_nodes[iD];
148 TNode *E = &_nodes[iE];
149
150 // grandParent point to B
151 if (A->parent_id != BVHCommon::INVALID) {
152 if (_nodes[A->parent_id].children[0] == iA) {
153 _nodes[A->parent_id].children[0] = iB;
154 } else {
155 _nodes[A->parent_id].children[1] = iB;
156 }
157 }
158
159 else {
160 // check this .. seems dodgy
161 change_root_node(iB, p_tree_id);
162 }
163
164 // Swap A and B
165 B->children[1] = iA;
166 B->parent_id = A->parent_id;
167 A->parent_id = iB;
168
169 // Finish rotation
170 if (D->height > E->height) {
171 B->children[0] = iD;
172 A->children[0] = iE;
173 E->parent_id = iA;
174 A->aabb = _logic_abb_merge(C->aabb, E->aabb);
175 B->aabb = _logic_abb_merge(A->aabb, D->aabb);
176
177 A->height = 1 + MAX(C->height, E->height);
178 B->height = 1 + MAX(A->height, D->height);
179 }
180
181 else {
182 B->children[0] = iE;
183 A->children[0] = iD;
184 D->parent_id = iA;
185 A->aabb = _logic_abb_merge(C->aabb, D->aabb);
186 B->aabb = _logic_abb_merge(A->aabb, E->aabb);
187
188 A->height = 1 + MAX(C->height, D->height);
189 B->height = 1 + MAX(A->height, E->height);
190 }
191
192 return iB;
193 }
194
195 return iA;
196 }
197
198 // either choose an existing node to add item to, or create a new node and return this
199 uint32_t _logic_choose_item_add_node(uint32_t p_node_id, const BVHABB_CLASS &p_aabb) {
200 while (true) {
201 BVH_ASSERT(p_node_id != BVHCommon::INVALID);
202 TNode &tnode = _nodes[p_node_id];
203
204 if (tnode.is_leaf()) {
205 // if a leaf, and non full, use this to add to
206 if (!node_is_leaf_full(tnode)) {
207 return p_node_id;
208 }
209
210 // else split the leaf, and use one of the children to add to
211 return split_leaf(p_node_id, p_aabb);
212 }
213
214 // this should not happen???
215 // is still happening, need to debug and find circumstances. Is not that serious
216 // but would be nice to prevent. I think it only happens with the root node.
217 if (tnode.num_children == 1) {
218 WARN_PRINT_ONCE("BVH::recursive_choose_item_add_node, node with 1 child, recovering");
219 p_node_id = tnode.children[0];
220 } else {
221 BVH_ASSERT(tnode.num_children == 2);
222 TNode &childA = _nodes[tnode.children[0]];
223 TNode &childB = _nodes[tnode.children[1]];
224 int which = p_aabb.select_by_proximity(childA.aabb, childB.aabb);
225
226 p_node_id = tnode.children[which];
227 }
228 }
229 }