summaryrefslogtreecommitdiff
path: root/core/math/bvh_logic.inc
blob: d84c3f78300ffd0b5e65c66032d76f0a1940f1fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230

// for slow incremental optimization, we will periodically remove each
// item from the tree and reinsert, to give it a chance to find a better position
void _logic_item_remove_and_reinsert(uint32_t p_ref_id) {
	// get the reference
	ItemRef &ref = _refs[p_ref_id];

	// no need to optimize inactive items
	if (!ref.is_active()) {
		return;
	}

	// special case of debug draw
	if (ref.item_id == BVHCommon::INVALID) {
		return;
	}

	BVH_ASSERT(ref.tnode_id != BVHCommon::INVALID);

	// some overlay elaborate way to find out which tree the node is in!
	BVHHandle temp_handle;
	temp_handle.set_id(p_ref_id);
	_current_tree = _handle_get_tree_id(temp_handle);

	// remove and reinsert
	BVHABB_CLASS abb;
	node_remove_item(p_ref_id, &abb);

	// we must choose where to add to tree
	ref.tnode_id = _logic_choose_item_add_node(_root_node_id[_current_tree], abb);
	_node_add_item(ref.tnode_id, p_ref_id, abb);

	refit_upward_and_balance(ref.tnode_id);
}

// from randy gaul balance function
BVHABB_CLASS _logic_abb_merge(const BVHABB_CLASS &a, const BVHABB_CLASS &b) {
	BVHABB_CLASS c = a;
	c.merge(b);
	return c;
}

//--------------------------------------------------------------------------------------------------
/**
@file	q3DynamicAABBTree.h
@author	Randy Gaul
@date	10/10/2014
	Copyright (c) 2014 Randy Gaul http://www.randygaul.net
	This software is provided 'as-is', without any express or implied
	warranty. In no event will the authors be held liable for any damages
	arising from the use of this software.
	Permission is granted to anyone to use this software for any purpose,
	including commercial applications, and to alter it and redistribute it
	freely, subject to the following restrictions:
	  1. The origin of this software must not be misrepresented; you must not
	     claim that you wrote the original software. If you use this software
	     in a product, an acknowledgment in the product documentation would be
	     appreciated but is not required.
	  2. Altered source versions must be plainly marked as such, and must not
	     be misrepresented as being the original software.
	  3. This notice may not be removed or altered from any source distribution.
*/
//--------------------------------------------------------------------------------------------------

// This function is based on the 'Balance' function from Randy Gaul's qu3e
// https://github.com/RandyGaul/qu3e
// It is MODIFIED from qu3e version.
// This is the only function used (and _logic_abb_merge helper function).
int32_t _logic_balance(int32_t iA) {
	//	return iA; // uncomment this to bypass balance

	TNode *A = &_nodes[iA];

	if (A->is_leaf() || A->height == 1) {
		return iA;
	}

	/*      A
	      /   \
	     B     C
	    / \   / \
	   D   E F   G
	*/

	CRASH_COND(A->num_children != 2);
	int32_t iB = A->children[0];
	int32_t iC = A->children[1];
	TNode *B = &_nodes[iB];
	TNode *C = &_nodes[iC];

	int32_t balance = C->height - B->height;

	// C is higher, promote C
	if (balance > 1) {
		int32_t iF = C->children[0];
		int32_t iG = C->children[1];
		TNode *F = &_nodes[iF];
		TNode *G = &_nodes[iG];

		// grandParent point to C
		if (A->parent_id != BVHCommon::INVALID) {
			if (_nodes[A->parent_id].children[0] == iA) {
				_nodes[A->parent_id].children[0] = iC;

			} else {
				_nodes[A->parent_id].children[1] = iC;
			}
		} else {
			// check this .. seems dodgy
			change_root_node(iC);
		}

		// Swap A and C
		C->children[0] = iA;
		C->parent_id = A->parent_id;
		A->parent_id = iC;

		// Finish rotation
		if (F->height > G->height) {
			C->children[1] = iF;
			A->children[1] = iG;
			G->parent_id = iA;
			A->aabb = _logic_abb_merge(B->aabb, G->aabb);
			C->aabb = _logic_abb_merge(A->aabb, F->aabb);

			A->height = 1 + MAX(B->height, G->height);
			C->height = 1 + MAX(A->height, F->height);
		}

		else {
			C->children[1] = iG;
			A->children[1] = iF;
			F->parent_id = iA;
			A->aabb = _logic_abb_merge(B->aabb, F->aabb);
			C->aabb = _logic_abb_merge(A->aabb, G->aabb);

			A->height = 1 + MAX(B->height, F->height);
			C->height = 1 + MAX(A->height, G->height);
		}

		return iC;
	}

	// B is higher, promote B
	else if (balance < -1) {
		int32_t iD = B->children[0];
		int32_t iE = B->children[1];
		TNode *D = &_nodes[iD];
		TNode *E = &_nodes[iE];

		// grandParent point to B
		if (A->parent_id != BVHCommon::INVALID) {
			if (_nodes[A->parent_id].children[0] == iA) {
				_nodes[A->parent_id].children[0] = iB;
			} else {
				_nodes[A->parent_id].children[1] = iB;
			}
		}

		else {
			// check this .. seems dodgy
			change_root_node(iB);
		}

		// Swap A and B
		B->children[1] = iA;
		B->parent_id = A->parent_id;
		A->parent_id = iB;

		// Finish rotation
		if (D->height > E->height) {
			B->children[0] = iD;
			A->children[0] = iE;
			E->parent_id = iA;
			A->aabb = _logic_abb_merge(C->aabb, E->aabb);
			B->aabb = _logic_abb_merge(A->aabb, D->aabb);

			A->height = 1 + MAX(C->height, E->height);
			B->height = 1 + MAX(A->height, D->height);
		}

		else {
			B->children[0] = iE;
			A->children[0] = iD;
			D->parent_id = iA;
			A->aabb = _logic_abb_merge(C->aabb, D->aabb);
			B->aabb = _logic_abb_merge(A->aabb, E->aabb);

			A->height = 1 + MAX(C->height, D->height);
			B->height = 1 + MAX(A->height, E->height);
		}

		return iB;
	}

	return iA;
}

// either choose an existing node to add item to, or create a new node and return this
uint32_t _logic_choose_item_add_node(uint32_t p_node_id, const BVHABB_CLASS &p_aabb) {
	while (true) {
		BVH_ASSERT(p_node_id != BVHCommon::INVALID);
		TNode &tnode = _nodes[p_node_id];

		if (tnode.is_leaf()) {
			// if a leaf, and non full, use this to add to
			if (!node_is_leaf_full(tnode)) {
				return p_node_id;
			}

			// else split the leaf, and use one of the children to add to
			return split_leaf(p_node_id, p_aabb);
		}

		// this should not happen???
		// is still happening, need to debug and find circumstances. Is not that serious
		// but would be nice to prevent. I think it only happens with the root node.
		if (tnode.num_children == 1) {
			WARN_PRINT_ONCE("BVH::recursive_choose_item_add_node, node with 1 child, recovering");
			p_node_id = tnode.children[0];
		} else {
			BVH_ASSERT(tnode.num_children == 2);
			TNode &childA = _nodes[tnode.children[0]];
			TNode &childB = _nodes[tnode.children[1]];
			int which = p_aabb.select_by_proximity(childA.aabb, childB.aabb);

			p_node_id = tnode.children[which];
		}
	}
}