-
Notifications
You must be signed in to change notification settings - Fork 0
/
quadtree-v1.js
435 lines (345 loc) · 12.4 KB
/
quadtree-v1.js
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
class QuadTreeSector {
constructor(parent, x, y, width, height) {
this.bound = Boundary.FromXYSize(x, y, width, height);
let parentNotRoot = parent.hasOwnProperty('root');
// Save references to parent sector and root quadtree
this.parent = parentNotRoot ? parent:undefined;
this.root = parentNotRoot ? parent.root:parent;
this.depth = parentNotRoot ? parent.depth + 1:0;
this.entities = [];
this.sectors = [];
this._newEntities = [];
}
// Creates 4 new sectors based on a parent sector
static newQuad(parent) {
let bound = parent.bound;
let halfWidth = bound.width / 2;
let halfHeight = bound.height / 2;
// TL, TR, BL, BR
return [
new QuadTreeSector(parent, bound.minx, bound.miny, halfWidth, halfHeight),
new QuadTreeSector(parent, bound.minx + halfWidth, bound.miny, halfWidth, halfHeight),
new QuadTreeSector(parent, bound.minx, bound.miny + halfHeight, halfWidth, halfHeight),
new QuadTreeSector(parent, bound.minx + halfWidth, bound.miny + halfHeight, halfWidth, halfHeight),
];
}
// Check if this sector is directly before the last sectors in terms of depth
isPenultimate() {
// Tests if any child sectors have child sectors of their own
for(let child of this.sectors)
if(child.sectors.length) return false;
// No child sectors found; this sector is penultimate to the bottom-most sector
return true;
}
// Go up the branch and get all entities along the way
branchEntities() {
if(this.parent) return [...this.parent.branchEntities(), ...this.entities];
return this.entities;
}
// Get all entities below and in this sector
allEntities() {
return [...this.sectors.map(a => a.allEntities()).flat(), ...this.entities];
}
update() {
// Update children
for(let child of this.sectors) child.update();
this.collapse();
this.flagged = false;
}
// Load many entities
load(entities) {
// using .add() is more optimised than using .load() for a single entity
if(entities.length == 1) {
this.add(entities[0]);
return;
}
let rejects = [];
let potentialEntities = [];
// Add entities to this sector
for(let entity of entities) {
// Test if the given entity fits in this quadtree sector
if(this.bound.contains(entity.bound))
potentialEntities.push(entity);
else rejects.push(entity);
}
// Divide this sector
if(!this.sectors.length && this.depth < this.root.maxDepth && this.entities.length + potentialEntities.length > this.root.maxSize)
this.divide();
// Move entities into child sectors
if(this.sectors.length) {
entityLoop:
for(let entity of potentialEntities) {
// Test if the given entity fits in each child quadtree sector
for(let child of this.sectors) {
if(child.bound.contains(entity.bound)) {
// _newEntities is a cache for entities to be later added all at once via .load()
child._newEntities.push(entity);
continue entityLoop;
}
}
// If no child sector is viable, find all intersecting sectors
let intersections = this.allIntersections(entity);
for(let sector of intersections)
sector.entities.push(entity);
this.root.entities.push(entity);
entity.sectors.push(...intersections);
}
// Load viable entities into child sectors
for(let child of this.sectors) {
if(child._newEntities.length) {
child.load(child._newEntities);
child._newEntities.length = 0;
}
}
}
// If there is no potential overfill, simply put all entities into this sector
else {
potentialEntities.forEach(e => e.sectors.push(this));
this.entities.push(...potentialEntities);
this.root.entities.push(...potentialEntities);
}
// Rejects contain entities that cannot fit into this quadtree sector, but were included in the entities list
return rejects;
}
// Add a single entity
add(entity, force = false) {
// Reject entity if it does not fit inside of this sector
if(!force && !this.bound.contains(entity.bound)) return entity;
// Divide this sector
if(!this.sectors.length && this.depth < this.root.maxDepth && this.entities.length + 1 > this.root.maxSize) {
this.divide();
this.moveEntitiesDown();
}
// Test if the given entity fits in each child quadtree sector
for(let child of this.sectors) {
// If the child was successfully added into the child sector, return from the function
if(!child.add(entity)) return;
}
// If this sector has child sectors but the entity fit into none of them, find intersections instead
if(this.sectors.length) {
let intersections = this.allIntersections(entity);
for(let sector of intersections) {
sector.entities.push(entity);
}
entity.sectors.push(...intersections);
}
else {
this.root.entities.push(entity);
this.entities.push(entity);
entity.sectors.push(this);
}
}
remove(entity) {
let index = this.entities.indexOf(entity);
if(index != -1) this.entities.splice(index, 1);
}
// Gets every leaf sector that intersects with the given entity
allIntersections(entity) {
if(!this.sectors.length)
return [this];
let returnArray = [];
for(let child of this.sectors) {
if(child.bound.intersects(entity.bound))
returnArray.push(child.allIntersections(entity));
}
return returnArray.flat();
}
// When a new entity added via .add() causes a sector to have to divide, the existing entities
// must be moved to child sectors for optimum packing
moveEntitiesDown() {
let removalCache = [];
entityLoop:
for(let entity of this.entities) {
// Test if this entity can be added to child sectors
for(let child of this.sectors) {
if(child.bound.contains(entity.bound)) {
// Add to removal cache
removalCache.push(entity);
// Force add to child sector
child.add(entity, true);
break;
}
}
}
// Remove entities from current sector (.filter can be used here, but it is slightly slower than this impl.)
for(let entity of removalCache) {
entity.sectors.splice(entity.sectors.indexOf(this), 1);
this.remove(entity);
}
// Move all of the entities that could not be moved into child sectors into intersecting leaf sectors
for(let entity of this.entities) {
entity.sectors.forEach(sector => sector.remove(entity));
entity.sectors.length = 0;
let intersections = this.allIntersections(entity);
for(let sector of intersections)
sector.entities.push(entity);
entity.sectors.push(...intersections);
}
// Non-leaf sectors can't have entities
this.entities.length = 0;
}
// Check this entity for movement and move it to the correct node
recalculateEntities() {
let outOfBounds = [];
let sectorCache = [];
let removalCache = [];
entityLoop:
for(let entity of this.entities) {
// Do not need to recalculate the position of a static entity
if(entity.bound.static) continue;
// Test if entity is still encapsulated inside of this sector
if(this.bound.contains(entity.bound)) {
// Test if this entity can be added to child sectors
for(let child of this.sectors) {
if(child.bound.contains(entity.bound)) {
let cacheIndex = sectorCache.indexOf(child);
// Cache sector
if(cacheIndex == -1)
sectorCache.push(child);
// Cache entity
child._newEntities.push(entity);
// Add to removal cache
removalCache.push(entity);
continue entityLoop;
}
}
}
else {
// Remove entity from current sector
this.remove(entity);
// Travel up the branch of sectors and see if any sectors can contain this entity
let upwardSector = this;
while(upwardSector.parent !== undefined) {
upwardSector = upwardSector.parent;
// If a sector is found to be able to contain this entity, cache it to that sector
if(upwardSector.bound.contains(entity.bound)) {
let cacheIndex = sectorCache.indexOf(upwardSector);
// Add sector and entity to cache
if(cacheIndex == -1)
sectorCache.push(upwardSector);
upwardSector._newEntities.push(entity);
continue entityLoop;
}
}
outOfBounds.push(entity);
}
}
// Load each list of cached entities into each cached sector
for(let sector of sectorCache) {
sector.load(sector._newEntities);
sector._newEntities.length = 0;
}
// Remove entities from current sector (.filter can be used here, but it is slightly slower than this impl.)
for(let entity of removalCache)
this.remove(entity);
// Return any entities that are no longer a part of the quadtree due to being out of bounds
return outOfBounds;
}
// Giver this sector 4 child sectors
divide() {
this.sectors = QuadTreeSector.newQuad(this);
}
// Opposite of .divide()
collapse() {
if(!this.isPenultimate()) return;
// Sum up all entities (Eqiv. to: this.sectors.reduce((s, a) => s = a.entities.length, this.entities.length), but more readable)
let totalEntities = this.entities.length;
for(let child of this.sectors) totalEntities += child.entities.length;
// If this sector can contain all child entities, remove all children sectors
if(totalEntities <= this.root.maxSize) {
// Using .insert() is not required here because it is already known that every entity
// from each child node fits inside of this parent node
for(let child of this.sectors)
this.entities.push(...child.entities);
this.sectors.length = 0;
}
}
}
// TODO: Add line-intersecting entities to multiple sectors instead of just 1
class QuadTree {
constructor(x, y, width, height) {
this.bound = Boundary.FromXYSize(x, y, width, height);
this.maxSize = 5;
this.maxDepth = 5;
// Main storage of entities
this.entities = [];
// Top most quadtree level only has 1 rectangle
this.sector = new QuadTreeSector(this, x, y, width, height);
}
update() {
let outOfBounds = [];
entityLoop:
for(let entity of this.entities) {
if(entity.static) continue;
// If the entity is contained within 1 sector, there is no need to update it until it leaves
if(entity.sectors.length == 1 && entity.sectors[0].bound.contains(entity.bound)) continue;
// Check if the previously intersecting sectors now fully contain this entity...
let containerFound;
for(let sector of entity.sectors) {
if(sector.bound.intersects(entity.bound)) {
// If any of this entity's sectors can contain this entity, that means the entity
// is not intersecting any other sectors and can only be in 1 sector
if(sector.bound.contains(entity.bound)) {
containerFound = sector;
break;
}
}
// Remove this entity from sectors in which it is no longer intersecting
else
sector.remove(entity);
}
// ... If they do, there is no need to check for intersections
if(containerFound != undefined) {
entity.sectors = [containerFound];
continue;
}
// Recalculate the intersecting sectors for this entity
if(entity.sectors.length != 0 && this.recalculateSectors(entity)) continue;
// If no spatial partitioning solution was found for this entity, add it to the outOfBounds list
outOfBounds.push(entity);
entity.sectors.length = 0;
}
for(let entity of outOfBounds)
this.remove(entity);
// Update quadtree to collapse and divide sectors
this.sector.update();
entities[0].sectors.forEach(s => s.flagged = true);
}
recalculateSectors(entity) {
// Sort sectors by ascending depth to find the highest sector
entity.sectors.sort((a, b) => a.depth - b.depth);
let upwardSector = entity.sectors[0];
// Travel up the branch of sectors and see if any sectors can contain this entity
while(upwardSector.parent !== undefined) {
upwardSector = upwardSector.parent;
// If a sector is found to be able to contain this entity, find all leaf entities that intersect with it
if(upwardSector.bound.contains(entity.bound)) {
entity.sectors = upwardSector.allIntersections(entity);
// Add this entity to each sectors' list of entities
for(let sector of entity.sectors)
if(sector.entities.indexOf(entity) == -1)
sector.entities.push(entity);
return true;
}
}
}
// Add many entities at once
load(entities) {
this.sector.load(entities);
}
// Add a single entity
add(entity) {
this.sector.add(entity);
}
remove(entity) {
let index = this.entities.indexOf(entity);
if(index != -1) this.entities.splice(index, 1);
}
// Clear entire quadtree
clear() {
this.sector = new QuadTreeSector(this, this.bound.minx, this.bound.miny, this.bound.width, this.bound.height);
}
identity() {
return `quadtree[${this.maxSize}x${this.maxDepth}x${this.entities.length}]`;
}
}