forked from microsoft/microcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quadtree.ts
202 lines (173 loc) · 6.27 KB
/
quadtree.ts
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
namespace microcode {
export interface ITreeComp extends IKindable, IPlaceable, ISizable {}
class Node {
constructor(public bounds: Bounds, public comp: ITreeComp) {}
}
// QuadTree for spatial indexing of objects.
// https://en.wikipedia.org/wiki/Quadtree
export class QuadTree {
private quads: QuadTree[]
private nodes: Node[]
constructor(
public bounds: Bounds, // Max bounds of the indexed space.
public maxObjects = 3, // Max objects per level before split attempt.
public minDimension = 16 // Min size of a cell. Cannot split below this size.
) {
this.quads = []
this.nodes = []
}
public forEach(cb: (bounds: Bounds) => void) {
if (this.quads.length) {
for (let i = 0; i < this.quads.length; ++i) {
this.quads[i].forEach(cb)
}
} else {
cb(this.bounds)
}
}
private trySplit(): boolean {
if (this.quads.length) {
return false
}
const nextWidth = this.bounds.width >> 1
if (nextWidth < this.minDimension) {
return false
}
const nextHeight = this.bounds.height >> 1
if (nextHeight < this.minDimension) {
return false
}
const left = this.bounds.left
const top = this.bounds.top
// top-right
this.quads[0] = new QuadTree(
new Bounds({
left: left + nextWidth,
top: top,
width: nextWidth,
height: nextHeight,
}),
this.maxObjects,
this.minDimension
)
// top-left
this.quads[1] = new QuadTree(
new Bounds({
left: left,
top: top,
width: nextWidth,
height: nextHeight,
}),
this.maxObjects,
this.minDimension
)
// bottom-left
this.quads[2] = new QuadTree(
new Bounds({
left: left,
top: top + nextHeight,
width: nextWidth,
height: nextHeight,
}),
this.maxObjects,
this.minDimension
)
// bottom-right
this.quads[3] = new QuadTree(
new Bounds({
left: left + nextWidth,
top: top + nextHeight,
width: nextWidth,
height: nextHeight,
}),
this.maxObjects,
this.minDimension
)
return true
}
private getIndices(bounds: Bounds): number[] {
const indices: number[] = []
const vertMidpoint = this.bounds.left + (this.bounds.width >> 1)
const horzMidpoint = this.bounds.top + (this.bounds.height >> 1)
const startIsNorth = bounds.top < horzMidpoint
const startIsWest = bounds.left < vertMidpoint
const endIsEast = bounds.left + bounds.width > vertMidpoint
const endIsSouth = bounds.top + bounds.height > horzMidpoint
// top-right quad
if (startIsNorth && endIsEast) {
indices.push(0)
}
// top-left quad
if (startIsWest && startIsNorth) {
indices.push(1)
}
// bottom-left quad
if (startIsWest && endIsSouth) {
indices.push(2)
}
// bottom-right quad
if (endIsEast && endIsSouth) {
indices.push(3)
}
return indices
}
public insert(bounds: Bounds, comp: ITreeComp) {
// If we have subtrees, call insert on matching.
if (this.quads.length) {
const indices = this.getIndices(bounds)
for (let i = 0; i < indices.length; ++i) {
this.quads[indices[i]].insert(bounds, comp)
}
return
}
// Otherwise, store object here.
this.nodes.push(new Node(bounds, comp))
// maxObjects reached?
if (this.nodes.length > this.maxObjects) {
// Split if we don't already have subtrees.
if (this.trySplit()) {
// Add all objects to their corresponding subtree.
for (let i = 0; i < this.nodes.length; ++i) {
const node = this.nodes[i]
const indices = this.getIndices(node.bounds)
for (let k = 0; k < indices.length; ++k) {
this.quads[indices[k]].insert(
node.bounds,
node.comp
)
}
}
// Clean up this node.
this.nodes = []
}
}
}
/**
* Query for objects in rectangle.
* Note you will likely get objects outside the bounds. It depends on the quadtree resolution.
*/
public query(bounds: Bounds): ITreeComp[] {
let comps: ITreeComp[] = this.nodes.map(node => node.comp)
const indices = this.getIndices(bounds)
// If we have subtrees, query their objects.
if (this.quads.length) {
for (let i = 0; i < indices.length; ++i) {
comps = comps.concat(this.quads[indices[i]].query(bounds))
}
}
// Remove dups
comps = comps.filter((comp, index) => comps.indexOf(comp) >= index)
return comps
}
public clear() {
for (let i = 0; i < this.quads.length; ++i) {
this.quads[i].clear()
}
this.quads = []
this.nodes = []
}
public dbgDraw(color: number) {
this.forEach(bounds => bounds.dbgRect(color))
}
}
}