-
Notifications
You must be signed in to change notification settings - Fork 2
/
quadtreenode.go
237 lines (185 loc) · 4.52 KB
/
quadtreenode.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
package cirno
import (
"fmt"
)
// represents a single node in quad tree
// for a certain subarea.
type quadTreeNode struct {
tree *quadTree
parent *quadTreeNode
northEast *quadTreeNode
northWest *quadTreeNode
southWest *quadTreeNode
southEast *quadTreeNode
boundary *aabb
shapes Shapes
level int
}
// add adds all the shapes covered by node area
// in the set of node shapes.
func (node *quadTreeNode) add(shapes Shapes) error {
if shapes == nil {
return fmt.Errorf("the set of shapes is nil")
}
for shape := range shapes {
overlapped, err := node.boundary.collidesShape(shape)
if err != nil {
return err
}
if overlapped {
node.shapes.Insert(shape)
shape.addNodes(node)
}
}
return nil
}
// remove removes all the shapes from the set that
// have the node in their domains.
func (node *quadTreeNode) remove(shapes Shapes) error {
if shapes == nil {
return fmt.Errorf("the set of shapes is nil")
}
for shape := range shapes {
node.shapes.Remove(shape)
shape.removeNodes(node)
}
return nil
}
// clear removes all the shapes from the node
// removes the node from the shapes' domains.
func (node *quadTreeNode) clear() {
for shape := range node.shapes {
shape.removeNodes(node)
}
node.shapes = Shapes{}
}
// split subdivides the node area into four subareas
// and creates new nodes for the subareas; reassigns
// shapes to the subnodes.
func (node *quadTreeNode) split() error {
nextLevel := node.level + 1
// Compute the center of the original boundary
// the other points to form new boundaries.
center := node.boundary.center()
northPoint := NewVector(center.X, node.boundary.max.Y)
southPoint := NewVector(center.X, node.boundary.min.Y)
westPoint := NewVector(node.boundary.min.X, center.Y)
eastPoint := NewVector(node.boundary.max.X, center.Y)
// Create new nodes.
northEastBoundary, err := newAABB(center, node.boundary.max)
if err != nil {
return err
}
node.northEast = &quadTreeNode{
tree: node.tree,
parent: node,
boundary: northEastBoundary,
level: nextLevel,
shapes: Shapes{},
}
northWestBoundary, err := newAABB(westPoint, northPoint)
if err != nil {
return err
}
node.northWest = &quadTreeNode{
tree: node.tree,
parent: node,
boundary: northWestBoundary,
level: nextLevel,
shapes: Shapes{},
}
southEastBoundary, err := newAABB(southPoint, eastPoint)
if err != nil {
return err
}
node.southEast = &quadTreeNode{
tree: node.tree,
parent: node,
boundary: southEastBoundary,
level: nextLevel,
shapes: Shapes{},
}
southWestBoundary, err := newAABB(node.boundary.min, center)
if err != nil {
return err
}
node.southWest = &quadTreeNode{
tree: node.tree,
parent: node,
boundary: southWestBoundary,
level: nextLevel,
shapes: Shapes{},
}
// Redistribute shapes between subnodes.
node.northEast.add(node.shapes)
node.northWest.add(node.shapes)
node.southEast.add(node.shapes)
node.southWest.add(node.shapes)
node.clear()
// Remove the current node from tree leaves.
err = node.tree.removeLeaf(node)
if err != nil {
return err
}
// Add its children to tree leaves.
err = node.tree.addLeaf(node.northEast)
if err != nil {
return err
}
err = node.tree.addLeaf(node.northWest)
if err != nil {
return err
}
err = node.tree.addLeaf(node.southEast)
if err != nil {
return err
}
err = node.tree.addLeaf(node.southWest)
return err
}
// assemble adds all the children shapes to the parent
// and removes children.
func (node *quadTreeNode) assemble() error {
// Add all the shapes in the parent node.
node.add(node.northWest.shapes)
node.add(node.northEast.shapes)
node.add(node.southWest.shapes)
node.add(node.southEast.shapes)
// Clear all the child nodes.
node.northWest.clear()
node.northEast.clear()
node.southWest.clear()
node.southEast.clear()
// Remove all the children from leaves.
err := node.tree.removeLeaf(node.northWest)
if err != nil {
return err
}
err = node.tree.removeLeaf(node.northEast)
if err != nil {
return err
}
err = node.tree.removeLeaf(node.southWest)
if err != nil {
return err
}
err = node.tree.removeLeaf(node.southEast)
if err != nil {
return err
}
// Get rid of child nodes.
node.northWest.parent = nil
node.northEast.parent = nil
node.southWest.parent = nil
node.southEast.parent = nil
node.northWest = nil
node.northEast = nil
node.southWest = nil
node.southEast = nil
// Add the parent node to the tree leaves.
err = node.tree.addLeaf(node)
if err != nil {
return err
}
return nil
}