-
Notifications
You must be signed in to change notification settings - Fork 20
/
bbtree.go
534 lines (445 loc) · 10.9 KB
/
bbtree.go
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
package cp
import "math"
type BBTreeVelocityFunc func(obj interface{}) Vector
type Node struct {
obj *Shape
bb BB
parent *Node
Children
Leaf
}
type Children struct {
a, b *Node
}
type Leaf struct {
stamp uint
pairs *Pair
}
type Pair struct {
a, b Thread
collisionId uint32
}
type Thread struct {
prev, next *Pair
leaf *Node
}
func (thread *Thread) Unlink() {
next := thread.next
prev := thread.prev
if next != nil {
if next.a.leaf == thread.leaf {
next.a.prev = prev
} else {
next.b.prev = prev
}
}
if prev != nil {
if prev.a.leaf == thread.leaf {
prev.a.next = next
} else {
prev.b.next = next
}
} else {
thread.leaf.pairs = next
}
}
type BBTree struct {
spatialIndex *SpatialIndex
velocityFunc BBTreeVelocityFunc
leaves *HashSet[*Shape, *Node]
root *Node
pooledNodes *Node
pooledPairs *Pair
stamp uint
}
func leafSetEql(obj *Shape, node *Node) bool {
return obj == node.obj
}
func NewBBTree(bbfunc SpatialIndexBB, staticIndex *SpatialIndex) *SpatialIndex {
bbtree := &BBTree{
leaves: NewHashSet(leafSetEql),
}
bbtree.spatialIndex = NewSpatialIndex(bbtree, bbfunc, staticIndex)
return bbtree.spatialIndex
}
func (tree *BBTree) Count() int {
return int(tree.leaves.Count())
}
func (tree *BBTree) Each(f SpatialIndexIterator) {
tree.leaves.Each(func(node *Node) {
f(node.obj)
})
}
func (tree *BBTree) Contains(obj *Shape, hashId HashValue) bool {
return tree.leaves.Find(hashId, obj) != nil
}
func (tree *BBTree) Insert(obj *Shape, hashId HashValue) {
leaf := tree.leaves.Insert(hashId, obj, func(obj *Shape) *Node {
return tree.NewLeaf(obj)
})
root := tree.root
tree.root = tree.SubtreeInsert(root, leaf)
leaf.stamp = tree.GetMasterTree().stamp
tree.LeafAddPairs(leaf)
tree.IncrementStamp()
}
func (tree *BBTree) IncrementStamp() {
dynamicTree := tree.spatialIndex.dynamicIndex.GetTree()
if dynamicTree != nil {
dynamicTree.stamp++
} else {
tree.stamp++
}
}
type MarkContext struct {
tree *BBTree
staticRoot *Node
f SpatialIndexQuery
data interface{}
}
func VoidQueryFunc(obj1 interface{}, obj2 *Shape, collisionId uint32, data interface{}) uint32 {
return collisionId
}
func (tree *BBTree) LeafAddPairs(leaf *Node) {
dynamicIndex := tree.spatialIndex.dynamicIndex
if dynamicIndex != nil {
dynamicRoot := dynamicIndex.GetRootIfTree()
if dynamicRoot != nil {
dynamicTree := dynamicIndex.GetTree()
context := &MarkContext{dynamicTree, nil, nil, nil}
dynamicRoot.MarkLeafQuery(leaf, true, context)
}
} else {
staticRoot := tree.spatialIndex.staticIndex.GetRootIfTree()
context := &MarkContext{tree, staticRoot, VoidQueryFunc, nil}
leaf.MarkLeaf(context)
}
}
func (leaf *Node) MarkLeaf(context *MarkContext) {
tree := context.tree
if leaf.stamp == tree.GetMasterTree().stamp {
staticRoot := context.staticRoot
if staticRoot != nil {
staticRoot.MarkLeafQuery(leaf, false, context)
}
for node := leaf; node.parent != nil; node = node.parent {
if node == node.parent.a {
node.parent.b.MarkLeafQuery(leaf, true, context)
} else {
node.parent.a.MarkLeafQuery(leaf, false, context)
}
}
} else {
pair := leaf.pairs
for pair != nil {
if leaf == pair.b.leaf {
pair.collisionId = context.f(pair.a.leaf.obj, leaf.obj, pair.collisionId, context.data)
pair = pair.b.next
} else {
pair = pair.a.next
}
}
}
}
func (subtree *Node) MarkLeafQuery(leaf *Node, left bool, context *MarkContext) {
if leaf.bb.Intersects(subtree.bb) {
if subtree.IsLeaf() {
if left {
context.tree.PairInsert(leaf, subtree)
} else {
if subtree.stamp < leaf.stamp {
context.tree.PairInsert(subtree, leaf)
}
context.f(leaf.obj, subtree.obj, 0, context.data)
}
} else {
subtree.a.MarkLeafQuery(leaf, left, context)
subtree.b.MarkLeafQuery(leaf, left, context)
}
}
}
func (tree *BBTree) PairInsert(a *Node, b *Node) {
nextA := a.pairs
nextB := b.pairs
pair := tree.PairFromPool()
pair.a = Thread{prev: nil, next: nextA, leaf: a}
pair.b = Thread{prev: nil, next: nextB, leaf: b}
pair.collisionId = 0
a.pairs = pair
b.pairs = pair
if nextA != nil {
if nextA.a.leaf == a {
nextA.a.prev = pair
} else {
nextA.b.prev = pair
}
}
if nextB != nil {
if nextB.a.leaf == b {
nextB.a.prev = pair
} else {
nextB.b.prev = pair
}
}
}
func (subtree *BBTree) PairFromPool() *Pair {
tree := subtree.GetMasterTree()
pair := tree.pooledPairs
if pair != nil {
tree.pooledPairs = pair.a.next
return pair
}
// Pool is exhausted make more
for i := 0; i < POOLED_BUFFER_SIZE; i++ {
tree.RecyclePair(&Pair{})
}
return &Pair{}
}
func (tree *BBTree) RecyclePair(pair *Pair) {
master := tree.GetMasterTree()
pair.a.next = master.pooledPairs
master.pooledPairs = pair
}
func (tree *BBTree) SubtreeInsert(subtree *Node, leaf *Node) *Node {
if subtree == nil {
return leaf
}
if subtree.IsLeaf() {
return tree.NewNode(leaf, subtree)
}
cost_a := subtree.b.bb.Area() + subtree.a.bb.MergedArea(leaf.bb)
cost_b := subtree.a.bb.Area() + subtree.b.bb.MergedArea(leaf.bb)
if cost_a == cost_b {
cost_a = subtree.a.bb.Proximity(leaf.bb)
cost_b = subtree.b.bb.Proximity(leaf.bb)
}
if cost_b < cost_a {
NodeSetB(subtree, tree.SubtreeInsert(subtree.b, leaf))
} else {
NodeSetA(subtree, tree.SubtreeInsert(subtree.a, leaf))
}
subtree.bb = subtree.bb.Merge(leaf.bb)
return subtree
}
func (tree *BBTree) SubtreeRemove(subtree *Node, leaf *Node) *Node {
if leaf == subtree {
return nil
}
parent := leaf.parent
if parent == subtree {
other := subtree.Other(leaf)
other.parent = subtree.parent
tree.RecycleNode(subtree)
return other
}
tree.ReplaceChild(parent.parent, parent, parent.Other(leaf))
return subtree
}
func (tree *BBTree) ReplaceChild(parent, child, value *Node) {
if parent.a == child {
tree.RecycleNode(parent.a)
NodeSetA(parent, value)
} else {
tree.RecycleNode(parent.b)
NodeSetB(parent, value)
}
for node := parent; node != nil; node = node.parent {
node.bb = node.a.bb.Merge(node.b.bb)
}
}
func (node *Node) Other(child *Node) *Node {
if node.a == child {
return node.b
}
return node.a
}
func (node *Node) IsLeaf() bool {
return node.obj != nil
}
func (tree *BBTree) Remove(obj *Shape, hashId HashValue) {
leaf := tree.leaves.Remove(hashId, obj)
tree.root = tree.SubtreeRemove(tree.root, leaf)
tree.PairsClear(leaf)
tree.RecycleNode(leaf)
}
func (tree *BBTree) Reindex() {
panic("implement me")
}
func (tree *BBTree) ReindexObject(obj *Shape, hashId HashValue) {
leaf := tree.leaves.Find(hashId, obj)
if leaf != nil {
if tree.LeafUpdate(leaf) {
tree.LeafAddPairs(leaf)
}
tree.IncrementStamp()
}
}
func (tree *BBTree) ReindexQuery(f SpatialIndexQuery, data interface{}) {
if tree.root == nil {
return
}
// LeafUpdate() may modify tree->root. Don't cache it.
tree.leaves.Each(func(leaf *Node) {
tree.LeafUpdate(leaf)
})
staticIndex := tree.spatialIndex.staticIndex
var staticRoot *Node
if staticIndex != nil {
staticRoot = staticIndex.class.(*BBTree).root
}
context := &MarkContext{tree, staticRoot, f, data}
tree.root.MarkSubtree(context)
if staticIndex != nil && staticRoot == nil {
tree.spatialIndex.CollideStatic(staticIndex, f, data)
}
tree.IncrementStamp()
}
func (subtree *Node) MarkSubtree(context *MarkContext) {
if subtree.IsLeaf() {
subtree.MarkLeaf(context)
} else {
subtree.a.MarkSubtree(context)
subtree.b.MarkSubtree(context)
}
}
func (tree *BBTree) LeafUpdate(leaf *Node) bool {
root := tree.root
bb := tree.spatialIndex.bbfunc(leaf.obj)
if !leaf.bb.Contains(bb) {
leaf.bb = tree.GetBB(leaf.obj)
root = tree.SubtreeRemove(root, leaf)
tree.root = tree.SubtreeInsert(root, leaf)
tree.PairsClear(leaf)
leaf.stamp = tree.GetMasterTree().stamp
return true
}
return false
}
func (tree *BBTree) PairsClear(leaf *Node) {
pair := leaf.pairs
leaf.pairs = nil
for pair != nil {
if pair.a.leaf == leaf {
next := pair.a.next
pair.b.Unlink()
tree.RecyclePair(pair)
pair = next
} else {
next := pair.b.next
pair.a.Unlink()
tree.RecyclePair(pair)
pair = next
}
}
}
func (tree *BBTree) Query(obj interface{}, bb BB, f SpatialIndexQuery, data interface{}) {
if tree.root != nil {
tree.root.SubtreeQuery(obj, bb, f, data)
}
}
func (subtree *Node) SubtreeQuery(obj interface{}, bb BB, query SpatialIndexQuery, data interface{}) {
if subtree.bb.Intersects(bb) {
if subtree.IsLeaf() {
query(obj, subtree.obj, 0, data)
} else {
subtree.a.SubtreeQuery(obj, bb, query, data)
subtree.b.SubtreeQuery(obj, bb, query, data)
}
}
}
func (subtree *Node) SubtreeSegmentQuery(obj interface{}, a, b Vector, t_exit float64, f SpatialIndexSegmentQuery, data interface{}) float64 {
if subtree.IsLeaf() {
return f(obj, subtree.obj, data)
}
tA := subtree.a.bb.SegmentQuery(a, b)
tB := subtree.b.bb.SegmentQuery(a, b)
if tA < tB {
if tA < t_exit {
t_exit = math.Min(t_exit, subtree.a.SubtreeSegmentQuery(obj, a, b, t_exit, f, data))
}
if tB < t_exit {
t_exit = math.Min(t_exit, subtree.b.SubtreeSegmentQuery(obj, a, b, t_exit, f, data))
}
} else {
if tB < t_exit {
t_exit = math.Min(t_exit, subtree.b.SubtreeSegmentQuery(obj, a, b, t_exit, f, data))
}
if tA < t_exit {
t_exit = math.Min(t_exit, subtree.a.SubtreeSegmentQuery(obj, a, b, t_exit, f, data))
}
}
return t_exit
}
func (tree *BBTree) SegmentQuery(obj interface{}, a, b Vector, t_exit float64, f SpatialIndexSegmentQuery, data interface{}) {
root := tree.root
if root != nil {
root.SubtreeSegmentQuery(obj, a, b, t_exit, f, data)
}
}
func (tree *BBTree) GetBB(obj *Shape) BB {
bb := tree.spatialIndex.bbfunc(obj)
if tree.velocityFunc != nil {
coef := 0.1
x := (bb.R - bb.L) * coef
y := (bb.T - bb.B) * coef
v := tree.velocityFunc(obj).Mult(0.1)
return BB{
bb.L + math.Min(-x, v.X),
bb.B + math.Min(-y, v.Y),
bb.R + math.Max(x, v.X),
bb.T + math.Max(y, v.Y),
}
}
return bb
}
func (tree *BBTree) NewNode(a, b *Node) *Node {
node := tree.NodeFromPool()
node.obj = nil
node.bb = a.bb.Merge(b.bb)
node.parent = nil
NodeSetA(node, a)
NodeSetB(node, b)
return node
}
func NodeSetA(node, value *Node) {
node.a = value
value.parent = node
}
func NodeSetB(node, value *Node) {
node.b = value
value.parent = node
}
func (tree *BBTree) NewLeaf(obj *Shape) *Node {
node := tree.NodeFromPool()
node.obj = obj
node.bb = tree.GetBB(obj)
node.parent = nil
node.stamp = 0
node.pairs = nil
return node
}
func (tree *BBTree) NodeFromPool() *Node {
node := tree.pooledNodes
if node != nil {
tree.pooledNodes = node.parent
return node
}
// Pool is exhausted make more
for i := 0; i < POOLED_BUFFER_SIZE; i++ {
tree.RecycleNode(&Node{})
}
return &Node{
parent: tree.pooledNodes,
}
}
func (tree *BBTree) RecycleNode(node *Node) {
node.parent = tree.pooledNodes
tree.pooledNodes = node
}
func (tree *BBTree) GetMasterTree() *BBTree {
dynamicTree := tree.spatialIndex.dynamicIndex.GetTree()
if dynamicTree != nil {
return dynamicTree
}
return tree
}