-
Notifications
You must be signed in to change notification settings - Fork 4
/
aabb_tree.h
329 lines (263 loc) · 7.43 KB
/
aabb_tree.h
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#pragma once
#include "aabb.h"
#include "collider.h"
#include "collision.h"
#include "growable_array.h"
#include "settings.h"
namespace muli
{
// You can use either Area() or Perimeter() as a surface area heuristic(SAH) function
inline float SurfaceArea(const AABB& aabb)
{
#if 0
return aabb.GetArea();
#else
return aabb.GetPerimeter();
#endif
}
typedef int32 NodeProxy;
typedef Collider Data;
class AABBTree
{
public:
static constexpr inline int32 nullNode = -1;
struct Node
{
bool IsLeaf() const
{
return child1 == nullNode;
}
AABB aabb;
NodeProxy parent;
NodeProxy child1;
NodeProxy child2;
NodeProxy next;
bool moved;
Data* data; // user data
};
AABBTree();
~AABBTree() noexcept;
AABBTree(const AABBTree&) = delete;
AABBTree& operator=(const AABBTree&) = delete;
AABBTree(AABBTree&&) noexcept;
AABBTree& operator=(AABBTree&&) noexcept;
void Reset();
NodeProxy CreateNode(Data* data, const AABB& aabb);
bool MoveNode(NodeProxy node, AABB aabb, const Vec2& displacement, bool forceMove);
void RemoveNode(NodeProxy node);
bool TestOverlap(NodeProxy nodeA, NodeProxy nodeB) const;
const AABB& GetAABB(NodeProxy node) const;
void ClearMoved(NodeProxy node) const;
bool WasMoved(NodeProxy node) const;
Data* GetData(NodeProxy node) const;
template <typename T>
void Traverse(T* callback) const;
template <typename T>
void Query(const Vec2& point, T* callback) const;
template <typename T>
void Query(const AABB& aabb, T* callback) const;
template <typename T>
void AABBCast(const AABBCastInput& input, T* callback) const;
void Traverse(std::function<void(const Node*)> callback) const;
void Query(const Vec2& point, std::function<bool(NodeProxy, Data*)> callback) const;
void Query(const AABB& aabb, std::function<bool(NodeProxy, Data*)> callback) const;
void AABBCast(const AABBCastInput& input, std::function<float(const AABBCastInput& input, Data* data)> callback) const;
float ComputeTreeCost() const;
void Rebuild();
private:
NodeProxy root;
Node* nodes;
int32 nodeCapacity;
int32 nodeCount;
NodeProxy freeList;
NodeProxy AllocateNode();
void FreeNode(NodeProxy node);
NodeProxy InsertLeaf(NodeProxy leaf);
void RemoveLeaf(NodeProxy leaf);
void Rotate(NodeProxy node);
void Swap(NodeProxy node1, NodeProxy node2);
};
inline bool AABBTree::TestOverlap(NodeProxy nodeA, NodeProxy nodeB) const
{
MuliAssert(0 <= nodeA && nodeA < nodeCapacity);
MuliAssert(0 <= nodeB && nodeB < nodeCapacity);
return nodes[nodeA].aabb.TestOverlap(nodes[nodeB].aabb);
}
inline const AABB& AABBTree::GetAABB(NodeProxy node) const
{
MuliAssert(0 <= node && node < nodeCapacity);
return nodes[node].aabb;
}
inline void AABBTree::ClearMoved(NodeProxy node) const
{
MuliAssert(0 <= node && node < nodeCapacity);
nodes[node].moved = false;
}
inline bool AABBTree::WasMoved(NodeProxy node) const
{
MuliAssert(0 <= node && node < nodeCapacity);
return nodes[node].moved;
}
inline Data* AABBTree::GetData(NodeProxy node) const
{
MuliAssert(0 <= node && node < nodeCapacity);
return nodes[node].data;
}
inline float AABBTree::ComputeTreeCost() const
{
float cost = 0.0f;
Traverse([&cost](const Node* node) -> void { cost += SurfaceArea(node->aabb); });
return cost;
}
template <typename T>
void AABBTree::Traverse(T* callback) const
{
if (root == nullNode)
{
return;
}
GrowableArray<NodeProxy, 256> stack;
stack.EmplaceBack(root);
while (stack.Count() != 0)
{
NodeProxy current = stack.PopBack();
if (nodes[current].IsLeaf() == false)
{
stack.EmplaceBack(nodes[current].child1);
stack.EmplaceBack(nodes[current].child2);
}
const Node* node = nodes + current;
callback->TraverseCallback(node);
}
}
template <typename T>
void AABBTree::Query(const Vec2& point, T* callback) const
{
if (root == nullNode)
{
return;
}
GrowableArray<NodeProxy, 256> stack;
stack.EmplaceBack(root);
while (stack.Count() != 0)
{
NodeProxy current = stack.PopBack();
if (nodes[current].aabb.TestPoint(point) == false)
{
continue;
}
if (nodes[current].IsLeaf())
{
bool proceed = callback->QueryCallback(current, nodes[current].data);
if (proceed == false)
{
return;
}
}
else
{
stack.EmplaceBack(nodes[current].child1);
stack.EmplaceBack(nodes[current].child2);
}
}
}
template <typename T>
void AABBTree::Query(const AABB& aabb, T* callback) const
{
if (root == nullNode)
{
return;
}
GrowableArray<NodeProxy, 256> stack;
stack.EmplaceBack(root);
while (stack.Count() != 0)
{
NodeProxy current = stack.PopBack();
if (nodes[current].aabb.TestOverlap(aabb) == false)
{
continue;
}
if (nodes[current].IsLeaf())
{
bool proceed = callback->QueryCallback(current, nodes[current].data);
if (proceed == false)
{
return;
}
}
else
{
stack.EmplaceBack(nodes[current].child1);
stack.EmplaceBack(nodes[current].child2);
}
}
}
template <typename T>
void AABBTree::AABBCast(const AABBCastInput& input, T* callback) const
{
const Vec2 p1 = input.from;
const Vec2 p2 = input.to;
const Vec2 halfExtents = input.halfExtents;
float maxFraction = input.maxFraction;
Vec2 d = p2 - p1;
float length = d.NormalizeSafe();
if (length == 0.0f)
{
return;
}
GrowableArray<NodeProxy, 256> stack;
stack.EmplaceBack(root);
while (stack.Count() > 0)
{
NodeProxy current = stack.PopBack();
if (current == nullNode)
{
continue;
}
const Node* node = nodes + current;
if (node->IsLeaf())
{
AABBCastInput subInput;
subInput.from = p1;
subInput.to = p2;
subInput.maxFraction = maxFraction;
subInput.halfExtents = halfExtents;
float newFraction = callback->AABBCastCallback(subInput, node->data);
if (newFraction == 0.0f)
{
return;
}
if (newFraction > 0.0f)
{
// Shorten the ray
maxFraction = newFraction;
}
}
else
{
// Ordered traversal
NodeProxy child1 = node->child1;
NodeProxy child2 = node->child2;
float dist1 = nodes[child1].aabb.RayCast(p1, p2, 0.0f, maxFraction, halfExtents);
float dist2 = nodes[child2].aabb.RayCast(p1, p2, 0.0f, maxFraction, halfExtents);
if (dist2 < dist1)
{
std::swap(dist1, dist2);
std::swap(child1, child2);
}
if (dist1 == max_value)
{
continue;
}
else
{
if (dist2 != max_value)
{
stack.EmplaceBack(child2);
}
stack.EmplaceBack(child1);
}
}
}
}
} // namespace muli