-
Notifications
You must be signed in to change notification settings - Fork 2
/
quadtree.go
309 lines (240 loc) · 6.55 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
package cirno
import (
"fmt"
"github.com/golang-collections/collections/queue"
)
// quadTree is an implementation of
// quad tree data structure to subdivide
// space to detect collisions.
type quadTree struct {
root *quadTreeNode
maxLevel int
nodeCapacity int
leaves map[*quadTreeNode]none
}
// addLeaf adds the quad tree node in the list of quad tree leaves.
func (tree *quadTree) addLeaf(node *quadTreeNode) error {
if tree.containsLeaf(node) {
center := node.boundary.center()
return fmt.Errorf("The leaf {%f, %f} already exists",
center.X, center.Y)
}
if node.northWest != nil {
center := node.boundary.center()
return fmt.Errorf("The node {%f, %f} cannot be a leaf",
center.X, center.Y)
}
tree.leaves[node] = none{}
return nil
}
// removeLeaf removes the specified quad tree node from the list
// of quad tree leaves.
func (tree *quadTree) removeLeaf(node *quadTreeNode) error {
if !tree.containsLeaf(node) {
center := node.boundary.center()
return fmt.Errorf("The leaf {%f, %f} doesn't exist",
center.X, center.Y)
}
delete(tree.leaves, node)
return nil
}
// containsLeaf returns true if the quad tree contains
// the given node among the leaves, and false otherwise.
func (tree *quadTree) containsLeaf(node *quadTreeNode) bool {
_, exists := tree.leaves[node]
return exists
}
// insert inserts the given shape into appropriate nodes
// and returns them.
func (tree *quadTree) insert(shape Shape) ([]*quadTreeNode, error) {
if shape == nil {
return nil, fmt.Errorf("the shape cannot be nil")
}
if !tree.root.boundary.containsPoint(shape.Center()) {
return nil, fmt.Errorf("the shape is out of bounds")
}
nodes := []*quadTreeNode{}
nodeQueue := queue.New()
nodeQueue.Enqueue(tree.root)
for nodeQueue.Len() > 0 {
node := nodeQueue.Dequeue().(*quadTreeNode)
// If the shape is not covered by the node area,
// skip it to the next node.
inBounds, err := node.boundary.collidesShape(shape)
if err != nil {
return nil, err
}
if !inBounds {
continue
}
// If the node is not a leaf,
// skip it.
if node.northWest != nil {
nodeQueue.Enqueue(node.northEast)
nodeQueue.Enqueue(node.northWest)
nodeQueue.Enqueue(node.southEast)
nodeQueue.Enqueue(node.southWest)
continue
}
// If the node limit is not exceeded,
// add the shape in the list of shapes
// covered by the node area.
if len(node.shapes) < node.tree.nodeCapacity ||
node.level >= node.tree.maxLevel {
node.shapes.Insert(shape)
nodes = append(nodes, node)
} else {
// Split the node into four subareas
// and add the subnodes in the queue.
err := node.split()
if err != nil {
return nil, err
}
nodeQueue.Enqueue(node.northEast)
nodeQueue.Enqueue(node.northWest)
nodeQueue.Enqueue(node.southEast)
nodeQueue.Enqueue(node.southWest)
}
}
// Add nodes to the shape's domain.
shape.addNodes(nodes...)
return nodes, nil
}
// search returns all the nodes containing the given shape.
func (tree *quadTree) search(shape Shape) ([]*quadTreeNode, error) {
if shape == nil {
return nil, fmt.Errorf("the shape is nil")
}
if !tree.root.boundary.containsPoint(shape.Center()) {
return nil, fmt.Errorf("the shape is out of bounds")
}
nodes := []*quadTreeNode{}
nodeQueue := queue.New()
nodeQueue.Enqueue(tree.root)
for nodeQueue.Len() > 0 {
node := nodeQueue.Dequeue().(*quadTreeNode)
// If the shape is not covered by the node area,
// skip it to the next node.
inBounds, err := node.boundary.collidesShape(shape)
if err != nil {
return nil, err
}
if !inBounds {
continue
}
exists, err := node.shapes.Contains(shape)
if err != nil {
return nil, err
}
if node.northWest == nil && exists {
nodes = append(nodes, node)
} else {
nodeQueue.Enqueue(node.northEast)
nodeQueue.Enqueue(node.northWest)
nodeQueue.Enqueue(node.southEast)
nodeQueue.Enqueue(node.southWest)
}
}
return nodes, nil
}
// remove removes the specified shape from the quad tree.
func (tree *quadTree) remove(shape Shape) error {
if shape == nil {
return fmt.Errorf("the shape cannot be nil")
}
if !tree.root.boundary.containsPoint(shape.Center()) {
return fmt.Errorf("the shape is out of bounds")
}
for _, node := range shape.nodes() {
node.shapes.Remove(shape)
}
// Remove all the nodes
// from the shape's domain.
shape.clearNodes()
return nil
}
// redistribute removes all the unrequired leafs
// and subtrees containing them.
func (tree *quadTree) redistribute() error {
nodeQueue := queue.New()
for leaf := range tree.leaves {
nodeQueue.Enqueue(leaf)
}
for nodeQueue.Len() > 0 {
node := nodeQueue.Dequeue().(*quadTreeNode)
if node.parent == nil {
continue
}
parent := node.parent
// Check if all the child nodes are leafs.
if tree.containsLeaf(parent.northWest) &&
tree.containsLeaf(parent.northEast) &&
tree.containsLeaf(parent.southWest) &&
tree.containsLeaf(parent.southEast) {
shapes := Shapes{}
shapes.Merge(parent.northWest.shapes)
shapes.Merge(parent.northEast.shapes)
shapes.Merge(parent.southWest.shapes)
shapes.Merge(parent.southEast.shapes)
if len(shapes) <= tree.nodeCapacity {
err := parent.assemble()
if err != nil {
return err
}
nodeQueue.Enqueue(parent)
}
}
}
return nil
}
// clear removes all the shapes from the quad tree.
func (tree *quadTree) clear() error {
// Remove all the nodes from
// shapes' domains.
for node := range tree.leaves {
for shape := range node.shapes {
shape.clearNodes()
}
}
tree.root = &quadTreeNode{
tree: tree,
boundary: tree.root.boundary,
level: 0,
shapes: Shapes{},
}
tree.leaves = map[*quadTreeNode]none{}
if err := tree.addLeaf(tree.root); err != nil {
return err
}
return nil
}
// shapeGroups returns the dictionary of shapes grouped
// by their nodes in the quad tree.
func (tree *quadTree) shapeGroups() map[*quadTreeNode]Shapes {
shapes := map[*quadTreeNode]Shapes{}
for node := range tree.leaves {
shapes[node] = node.shapes.Copy()
}
return shapes
}
// newQuadTree creates a new empty quad tree.
func newQuadTree(boundary *aabb, maxLevel, nodeCapacity int) (*quadTree, error) {
if maxLevel < 1 {
return nil, fmt.Errorf("max depth must be greater or equal to 1")
}
tree := new(quadTree)
tree.maxLevel = maxLevel
tree.nodeCapacity = nodeCapacity
tree.leaves = map[*quadTreeNode]none{}
tree.root = &quadTreeNode{
tree: tree,
parent: nil,
boundary: boundary,
level: 0,
shapes: Shapes{},
}
if err := tree.addLeaf(tree.root); err != nil {
return nil, err
}
return tree, nil
}