-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoroidalquadtree.go
More file actions
363 lines (327 loc) · 9.33 KB
/
Copy pathtoroidalquadtree.go
File metadata and controls
363 lines (327 loc) · 9.33 KB
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
// package toroidal contains types and functions related to toroidal quadtree data structures.
package toroquatre
import (
"fmt"
"sort"
"strings"
)
type Vector struct {
X, Y float64
}
type ToroidalQuadtree struct {
nodeLimit int
leafMap map[uint64]Vector
root *tree
}
type tree struct {
tqt *ToroidalQuadtree
// Bounding box for the tree.
topLeft Vector
bottomRight Vector
// If the tree has nodeLimit or fewer leaves, their IDs are here.
leaves []uint64
// Once the tree has more than nodeLimit leaves, it instead contains 4 subtrees.
// They are in quadrant order: Northwest, Northeast, Southwest, Southeast.
branches [4]*tree
// The total number of items within this part of the tree
count int
}
func medianLocation(ids []uint64, leafMap map[uint64]Vector) Vector {
sortedX := make([]float64, len(ids))
sortedY := make([]float64, len(ids))
for i, id := range ids {
sortedX[i] = leafMap[id].X
sortedY[i] = leafMap[id].Y
}
sort.Float64s(sortedX)
sort.Float64s(sortedY)
var result Vector
// Even length list: take the average of the two middle elements
if len(ids)%2 == 0 {
idx1 := len(ids) / 2
idx2 := idx1 - 1
result.X = (sortedX[idx1] + sortedX[idx2]) / 2
result.Y = (sortedY[idx1] + sortedY[idx2]) / 2
// Odd length list: take the middle element
} else {
idx := len(ids) / 2
result.X = sortedX[idx]
result.Y = sortedY[idx]
}
return result
}
func (t *tree) subdivide() {
// Subdivide on the median location within the region
median := medianLocation(t.leaves, t.tqt.leafMap)
// Set up the subregions
t.branches[0] = &tree{
tqt: t.tqt,
topLeft: t.topLeft,
bottomRight: median,
leaves: make([]uint64, 0, t.tqt.nodeLimit),
}
t.branches[1] = &tree{
tqt: t.tqt,
topLeft: Vector{X: median.X, Y: t.topLeft.Y},
bottomRight: Vector{X: t.bottomRight.X, Y: median.Y},
leaves: make([]uint64, 0, t.tqt.nodeLimit),
}
t.branches[2] = &tree{
tqt: t.tqt,
topLeft: Vector{X: t.topLeft.X, Y: median.Y},
bottomRight: Vector{X: median.X, Y: t.bottomRight.Y},
leaves: make([]uint64, 0, t.tqt.nodeLimit),
}
t.branches[3] = &tree{
tqt: t.tqt,
topLeft: median,
bottomRight: t.bottomRight,
leaves: make([]uint64, 0, t.tqt.nodeLimit),
}
// Move all the elements into their proper region
for _, id := range t.leaves {
loc := t.tqt.leafMap[id]
var destRegion int
switch {
case loc.X < median.X && loc.Y < median.Y:
destRegion = 0
case loc.X >= median.X && loc.Y < median.Y:
destRegion = 1
case loc.X < median.X && loc.Y >= median.Y:
destRegion = 2
default:
destRegion = 3
}
t.branches[destRegion].leaves = append(t.branches[destRegion].leaves, id)
t.branches[destRegion].count++
}
t.leaves = nil
}
func (t *tree) put(id uint64, p Vector) {
if t.branches[0] == nil {
t.leaves = append(t.leaves, id)
if len(t.leaves) > t.tqt.nodeLimit {
t.subdivide()
}
} else {
median := t.branches[0].bottomRight
var destRegion int
switch {
case p.X < median.X && p.Y < median.Y:
destRegion = 0
case p.X >= median.X && p.Y < median.Y:
destRegion = 1
case p.X < median.X && p.Y >= median.Y:
destRegion = 2
default:
destRegion = 3
}
t.branches[destRegion].put(id, p)
}
t.count++
}
func (t *tree) remove(id uint64, leafMap map[uint64]Vector) {
if len(t.leaves) != 0 {
// Find the element to remove, overwrite it with the last element, and subslice the leaves
for i := 0; i < len(t.leaves)-1; i++ {
if t.leaves[i] == id {
t.leaves[i] = t.leaves[len(t.leaves)-1]
break
}
}
t.leaves = t.leaves[:len(t.leaves)-1]
} else {
loc := leafMap[id]
// Delete from the proper subtree
median := t.branches[0].bottomRight
var destRegion int
switch {
case loc.X < median.X && loc.Y < median.Y:
destRegion = 0
case loc.X >= median.X && loc.Y < median.Y:
destRegion = 1
case loc.X < median.X && loc.Y >= median.Y:
destRegion = 2
default:
destRegion = 3
}
t.branches[destRegion].remove(id, leafMap)
}
t.count--
// N.B.: Do this after recursing on the child region so that we know it has been flattened
// before we merge it here.
// Wait to merge until we've gone 2 below the node limit, so a node can be efficiently
// moved around the tree without unnecessary merging and re-subdividing.
if t.count < t.tqt.nodeLimit-1 && t.branches[0] != nil {
// This region has shrunk enough that we should merge its children up
t.leaves = make([]uint64, 0, t.tqt.nodeLimit)
for i, child := range t.branches {
t.leaves = append(t.leaves, child.leaves...)
t.branches[i] = nil
}
}
delete(leafMap, id)
}
func isWithin(loc, topLeft, bottomRight Vector) bool {
if topLeft.X < bottomRight.X {
if loc.X < topLeft.X || loc.X >= bottomRight.X {
return false
}
} else {
if loc.X < topLeft.X && loc.X >= bottomRight.X {
return false
}
}
if topLeft.Y < bottomRight.Y {
if loc.Y < topLeft.Y || loc.Y >= bottomRight.Y {
return false
}
} else {
if loc.Y < topLeft.Y && loc.Y >= bottomRight.Y {
return false
}
}
return true
}
func regionWithin(rTopLeft, rBottomRight, topLeft, bottomRight Vector) bool {
if rTopLeft.X < rBottomRight.X {
if rTopLeft.X >= bottomRight.X || rBottomRight.X <= topLeft.X {
return false
}
} else {
if rTopLeft.X >= bottomRight.X && rBottomRight.X <= topLeft.X {
return false
}
}
if rTopLeft.Y < rBottomRight.Y {
if rTopLeft.Y >= bottomRight.Y || rBottomRight.Y <= topLeft.Y {
return false
}
} else {
if rTopLeft.Y >= bottomRight.Y && rBottomRight.Y <= topLeft.Y {
return false
}
}
return true
}
func (t *tree) find(topLeft, bottomRight Vector) []uint64 {
result := make([]uint64, 0, t.count)
if t.branches[0] == nil {
for _, id := range t.leaves {
if isWithin(t.tqt.leafMap[id], topLeft, bottomRight) {
result = append(result, id)
}
}
} else {
for _, region := range t.branches {
if regionWithin(topLeft, bottomRight, region.topLeft, region.bottomRight) {
result = append(result, region.find(topLeft, bottomRight)...)
}
}
}
return result
}
// New creates a new ToroidalQuadtree with the given width and height.
// nodeLimit is the maximum number of elements in a region before subdivision, and must be at least 4.
func New(nodeLimit int, width, height float64) (*ToroidalQuadtree, error) {
if nodeLimit < 4 {
return nil, fmt.Errorf("invalid node limit: must be at least 4")
}
result := ToroidalQuadtree{
nodeLimit: nodeLimit,
leafMap: make(map[uint64]Vector),
}
result.root = &tree{
tqt: &result,
topLeft: Vector{X: 0.0, Y: 0.0},
bottomRight: Vector{X: width, Y: height},
leaves: make([]uint64, 0, nodeLimit),
}
return &result, nil
}
// Width returns the width of the quadtree.
func (t *ToroidalQuadtree) Width() float64 {
return t.root.bottomRight.X
}
// Height returns the width of the quadtree.
func (t *ToroidalQuadtree) Height() float64 {
return t.root.bottomRight.Y
}
// Put sets the location of the element with the given ID.
// If the element is already within the quadtree, moves the element from its old location.
func (t *ToroidalQuadtree) Put(id uint64, p Vector) bool {
if p.X < 0.0 || p.Y < 0.0 || p.X >= t.root.bottomRight.X || p.Y >= t.root.bottomRight.Y {
return false
}
// If this ID is already in the tree, remove it.
if _, ok := t.leafMap[id]; ok {
t.Remove(id)
}
// Add the leaf to the leaf map and into the tree.
t.leafMap[id] = p
t.root.put(id, p)
return true
}
// Remove takes the element with the given ID out of the quadtree.
func (t *ToroidalQuadtree) Remove(id uint64) bool {
// If this ID is already not in the tree, return early.
if _, ok := t.leafMap[id]; !ok {
return false
}
t.root.remove(id, t.leafMap)
delete(t.leafMap, id)
return true
}
// Get returns the location of the element with the given ID.
func (t *ToroidalQuadtree) Get(id uint64) *Vector {
if loc, ok := t.leafMap[id]; ok {
return &loc
}
return nil
}
// Find returns all the elements by ID within the given region.
// NOTE: A region's location interval is closed on the left side and open on the right.
// The region ((0.0, 0.0), (1.0, 1.0)) contains the point (0.0, 0.0), but not (1.0, 1.0).
// Both corners of the selected region must be within the bounds of the quadtree.
func (t *ToroidalQuadtree) Find(topLeft, bottomRight Vector) []uint64 {
if topLeft.X < 0.0 || topLeft.Y < 0.0 || topLeft.X > t.root.bottomRight.X || topLeft.Y > t.root.bottomRight.Y {
return nil
}
if bottomRight.X < 0.0 || bottomRight.Y < 0.0 || bottomRight.X > t.root.bottomRight.X || bottomRight.Y > t.root.bottomRight.Y {
return nil
}
return t.root.find(topLeft, bottomRight)
}
// DebugString prints out the tree representation.
func (t *ToroidalQuadtree) DebugString() string {
var sb strings.Builder
type node struct {
level int
tree *tree
}
var q []node
q = append(q, node{0, t.root})
for len(q) > 0 {
next := q[0]
q = q[1:]
if next.tree.branches[0] != nil {
for _, subtree := range next.tree.branches {
q = append(q, node{next.level + 1, subtree})
}
}
for i := 0; i < next.level-2; i++ {
sb.WriteString(" ")
}
if next.level > 0 {
sb.WriteString("+-")
}
sb.WriteString(fmt.Sprintf("(%v), (%v)\n", next.tree.topLeft, next.tree.bottomRight))
for _, leaf := range next.tree.leaves {
for i := 0; i < next.level; i++ {
sb.WriteString(" ")
}
sb.WriteString(fmt.Sprintf("+-%v: (%v)\n", leaf, t.leafMap[leaf]))
}
}
return sb.String()
}