-
Notifications
You must be signed in to change notification settings - Fork 0
/
quadtree.go
337 lines (277 loc) · 5.48 KB
/
quadtree.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
package quadtree
type QuadTree struct {
Root Node
}
//Create a new tree with the given bounds
func NewTree(bounds Rect) QuadTree {
return QuadTree{Root: Node{
Rect: bounds,
}}
}
//Intersect all Values inside the query rectangle
func (q *QuadTree) Intersect(query Rect) []*Value {
results := q.Root.retrieve(query)
filtered := results[:0]
for _, r := range results {
if query.contains(r.Point) {
filtered = append(filtered, r)
}
}
return filtered
}
//Insert new Value into Quadtree
func (q *QuadTree) Insert(value *Value) {
q.Root.insert(value)
}
//delete Value from Quadtree (expensive!)
func (q *QuadTree) Delete(value *Value) {
q.Root.delete(value)
}
//Return the number of all nodes which contains a non nil Value
func (q *QuadTree) Size() int {
return q.Root.size()
}
//Return the depth of the tree
func (q *QuadTree) Depth() int {
return q.Root.depth()
}
//Return all bounding rects from nodes
func (q *QuadTree) GetNodeRects() []Rect {
return q.Root.getNodeRect()
}
//Clear the entire quadtree
func (q *QuadTree) Clear() {
q.Root.clear()
q.Root.Nodes = nil
}
type Point struct {
X float64
Y float64
}
type Rect struct {
Point
Height float64
Width float64
}
func (r *Rect) contains(p Point) bool {
if p.X < r.X || p.Y < r.Y {
return false
}
if p.X > r.X+r.Width || p.Y > r.Y+r.Height {
return false
}
return true
}
func NewRect(x, y, width, height float64) Rect {
if width < 0 {
width = -width
x -= width
}
if height < 0 {
height = -height
y -= height
}
return Rect{
Point: Point{
X: x,
Y: y,
},
Height: height,
Width: width,
}
}
type Node struct {
Rect
Nodes map[float64]*Node
Level int
Value *Value
}
type Value struct {
Point
Data interface{}
}
const topRight = 0
const topLeft = 1
const bottomLeft = 2
const bottomRight = 3
func (n *Node) getIndex(p Point) float64 {
x := p.X
y := p.Y
vX := n.X + n.Width/2
vY := n.Y + n.Height/2
if x <= vX && y <= vY {
return topLeft
}
if x > vX && y <= vY {
return topRight
}
if x <= vX && y > vY {
return bottomLeft
}
if x > vX && y > vY {
return bottomRight
}
return -1
}
func (n *Node) queryIndexes(r Rect) []float64 {
var indexes []float64
verticalMidpoint := n.X + (n.Width / 2)
horizontalMidpoint := n.Y + (n.Height / 2)
startIsNorth := r.Y < horizontalMidpoint
startIsWest := r.X < verticalMidpoint
endIsEast := r.X+r.Width > verticalMidpoint
endIsSouth := r.Y+r.Height > horizontalMidpoint
//top-right quad
if startIsNorth && endIsEast {
indexes = append(indexes, topRight)
}
//top-left quad
if startIsWest && startIsNorth {
indexes = append(indexes, topLeft)
}
//bottom-left quad
if startIsWest && endIsSouth {
indexes = append(indexes, bottomLeft)
}
//bottom-right quad
if endIsEast && endIsSouth {
indexes = append(indexes, bottomRight)
}
return indexes
}
func (n *Node) insert(value *Value) {
if len(n.Nodes) > 0 {
index := n.getIndex(value.Point)
if index != -1 {
into := n.Nodes[index]
into.insert(value)
}
return
}
n.Value = value
n.split()
}
func (n *Node) delete(value *Value) bool {
if n.Value == value {
return true
}
if n.Nodes == nil || len(n.Nodes) == 0 {
return false
} else {
for _, subNode := range n.Nodes {
if subNode.delete(value) {
//Return all elements from subNodes, query with subNodes bounds
values := subNode.retrieve(subNode.Rect)
//Crop tree
subNode.clear()
for _, v := range values {
//Prevent from reinserting
if v != value {
n.insert(v)
}
}
}
}
}
return false
}
func (n *Node) retrieve(query Rect) []*Value {
results := make([]*Value, 0)
if n.Value != nil {
results = append(results, n.Value)
}
idx := n.queryIndexes(query)
for _, id := range idx {
subNode, ok := n.Nodes[id]
if ok {
results = append(results, subNode.retrieve(query)...)
}
}
return results
}
func (n *Node) split() {
nextLevel := n.Level + 1
subWidth := n.Width / 2
subHeight := n.Height / 2
x := n.X
y := n.Y
n.Nodes = make(map[float64]*Node)
//top right node
n.Nodes[topRight] = &Node{
Rect: NewRect(x+subWidth, y, subWidth, subHeight),
Nodes: nil,
Level: nextLevel,
Value: nil,
}
//top left node
n.Nodes[topLeft] = &Node{
Rect: NewRect(x, y, subWidth, subHeight),
Nodes: nil,
Level: nextLevel,
Value: nil,
}
//bottom left node
n.Nodes[bottomLeft] = &Node{
Rect: NewRect(x, y+subHeight, subWidth, subHeight),
Nodes: nil,
Level: nextLevel,
Value: nil,
}
//bottom right node
n.Nodes[bottomRight] = &Node{
Rect: NewRect(x+subWidth, y+subHeight, subWidth, subHeight),
Nodes: nil,
Level: nextLevel,
Value: nil,
}
}
func (n *Node) size() int {
i := 0
if n.Value != nil {
i++
}
if n.Nodes == nil || len(n.Nodes) == 0 {
return i
}
for _, subNode := range n.Nodes {
i += subNode.size()
}
return i
}
func (n *Node) getNodeRect() []Rect {
nodes := make([]Rect, 0)
if n.Nodes == nil || len(n.Nodes) == 0 {
return nodes
} else {
for _, subNode := range n.Nodes {
nodes = append(nodes, subNode.getNodeRect()...)
nodes = append(nodes, subNode.Rect)
}
}
return nodes
}
func (n *Node) clear() {
n.Value = nil
if n.Nodes == nil || len(n.Nodes) == 0 {
return
} else {
for _, subNode := range n.Nodes {
subNode.clear()
}
n.Nodes = nil
}
}
func (n *Node) depth() (level int) {
level = n.Level
if n.Nodes == nil || len(n.Nodes) == 0 {
return level
} else {
for _, subNode := range n.Nodes {
subLevel := subNode.depth()
if subLevel > level {
level = subLevel
}
}
}
return
}