-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
QuadTree.java
603 lines (563 loc) · 21.4 KB
/
QuadTree.java
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
package net.citizensnpcs.api.util.cuboid;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class QuadTree {
QuadNode root;
/**
* Adds the {@link QuadCuboid} to the target node and fixes up the children's list holder link
*
* @param node
* The target node
* @param cuboid
* The cuboid to add
*/
private void addAndFixListHolders(QuadNode node, QuadCuboid cuboid) {
node.cuboids.add(cuboid);
// This isn't our first cuboid, so no fix needed
if (node.cuboids.size() > 1) {
return;
}
// Descend the tree. When we find a node with no cuboids it needs a new
// list holder
Deque<QuadNode> todo = new ArrayDeque<QuadNode>();
todo.push(node);
QuadNode current;
do {
current = todo.pop();
for (QuadNode child : current.quads) {
if (child == null) {
continue;
}
// If the child isn't holding a list of cuboids itself
// (which would make it the list holder link for its children)
// Then we need to fix it's children as well
if (child.cuboids.size() == 0) {
todo.push(child);
}
child.nextListHolder = node;
}
} while (!todo.isEmpty());
}
private QuadNode ascendFirstSearch(QuadNode node, int x, int z) {
while (node != null && (node.x > x || node.z > z || (node.x + node.size) < x || (node.z + node.size) < z)) {
node = node.parent;
}
if (node == null)
return null;
return descendAndSearch(node, x, z);
}
private void beginTree(QuadCuboid cuboid) {
int size = 128;
int minSize = Math.abs(cuboid.lowCoords[0] - cuboid.highCoords[0]);
int minSizeB = Math.abs(cuboid.lowCoords[2] - cuboid.highCoords[2]);
if (minSize < minSizeB) {
minSize = minSizeB;
}
while (size < minSize) {
size = size << 1;
}
root = new QuadNode(cuboid.lowCoords[0], cuboid.lowCoords[2], size - 1, null);
}
/**
* Returns -1 for too small, 0 for minimal, 1 for larger than needed
* <p>
* Fit is based on the larger side. Means more tests but consumes an order of magnitude or less memory.
*
* @param node
* @param cuboid
* @return
*/
private int containerFit(QuadNode node, QuadCuboid cuboid) {
int minSizeA = Math.abs(cuboid.lowCoords[0] - cuboid.highCoords[0]);
int minSizeB = Math.abs(cuboid.lowCoords[2] - cuboid.highCoords[2]);
int fitSize;
if (minSizeA < minSizeB) {
fitSize = minSizeB;
} else {
fitSize = minSizeA;
}
if (node.size < fitSize) {
return -1;
} else if (node.size == 1 || (node.size >> 1) < fitSize) {
return 0;
} else {
return 1;
}
}
private QuadNode descendAndCreate(QuadNode start, QuadCuboid cuboid) {
QuadNode next = start;
while (containerFit(next, cuboid) > 0) {
int i = 0;
int nX = 0;
int nZ = 0;
int half = (next.size >> 1);
if (cuboid.lowCoords[0] > (next.x + half)) {
i++;
nX = half + 1;
}
if (cuboid.lowCoords[2] > (next.z + half)) {
i += 2;
nZ = half + 1;
}
if (next.quads[i] == null) {
next.quads[i] = new QuadNode(next.x + nX, next.z + nZ, half, next);
}
next = next.quads[i];
}
return next;
}
private QuadNode descendAndSearch(QuadNode node, int x, int z) {
QuadNode next = node;
while (next != null) {
node = next;
int half = node.size >> 1;
int i = 0;
if (x > (node.x + half)) {
i++;
}
if (z > (node.z + half)) {
i += 2;
}
next = node.quads[i];
}
return node;
}
private QuadNode descendNoCreate(QuadNode start, QuadCuboid cuboid) {
QuadNode next = start;
while (containerFit(next, cuboid) > 0) {
int i = 0;
int nX = 0;
int nZ = 0;
int half = (next.size >> 1);
if (cuboid.lowCoords[0] > (next.x + half)) {
i++;
nX = half + 1;
}
if (cuboid.lowCoords[2] > (next.z + half)) {
i += 2;
nZ = half + 1;
}
if (next.quads[i] == null) {
next = new QuadNode(next.x + nX, next.z + nZ, half, next);
} else {
next = next.quads[i];
}
}
return next;
}
public BookmarkedResult findOverlappingCuboids(int x, int y, int z) {
return relatedSearch(null, x, y, z);
}
public BookmarkedResult findOverlappingCuboidsFromBookmark(BookmarkedResult bookmark, int x, int y, int z) {
return relatedSearch(bookmark.bookmark, x, y, z);
}
/**
* Oftentimes a node will overlap with the neighbors of a node Since we always search for the next node based on the
* lower left we know that the left and bottom will not go over the edge, leaving only the top, right, and upper
* right possibilities need be regarded. Spits out a list of cuboids that are fit for "insertion" although we just
* use them for the search and actually attach the original cuboid. We also return the remainder shard if we
* generated any others. At the other end we only include a node if it's shard didn't re-shard. Keeps the tree
* search spaces minimal.
*/
private List<QuadCuboid> generateShards(QuadNode node, QuadCuboid cuboid) {
List<QuadCuboid> shards = new ArrayList<QuadCuboid>(4);
int top = node.z + node.size;
int right = node.x + node.size;
int tmp;
// find a shard above if it exists
if (top < cuboid.highCoords[2]) {
// Find out if it extends past the top only or the right and top
// Limit the "top" shard to only directly above the original node
if (right < cuboid.highCoords[0]) {
tmp = right;
} else {
tmp = cuboid.highCoords[0];
}
shards.add(new QuadCuboid(cuboid.lowCoords[0], 0, top + 1, tmp, 0, cuboid.highCoords[2]));
}
// Find a shard to the right
if (right < cuboid.highCoords[0]) {
// find if we extend past the top as well
// Limit the "right" shard to only directly right
if (top < cuboid.highCoords[2]) {
tmp = top;
} else {
tmp = cuboid.highCoords[2];
}
shards.add(new QuadCuboid(right + 1, 0, cuboid.lowCoords[2], cuboid.highCoords[0], 0, tmp));
}
// Check for a top right shard
if (right < cuboid.highCoords[0] && top < cuboid.highCoords[2]) {
shards.add(new QuadCuboid(right + 1, 0, top + 1, cuboid.highCoords[0], 0, cuboid.highCoords[2]));
}
// include the remainder as a shard if we generated any others
if (shards.size() > 0) {
shards.add(new QuadCuboid(cuboid.lowCoords[0], 0, cuboid.lowCoords[2], right, 0, top));
}
return shards;
}
public List<QuadCuboid> getAllOverlapsWith(QuadCuboid cuboid) {
if (root == null)
return Collections.emptyList();
// if this cuboid falls outside of the tree, we need to repot the tree
// to
// gain a wider perspective!
if (!nodeFullyContainsCuboid(root, cuboid)) {
repotTree(cuboid);
}
QuadNode node = root;
node = descendNoCreate(node, cuboid);
// Now that we have our target we potentially need to generate shards
// and
// target their nodes as well
List<QuadNode> targets = getAllTargetsNoCreate(node, cuboid);
Deque<QuadNode> children = new ArrayDeque<QuadNode>();
Set<QuadCuboid> cuboids = new HashSet<QuadCuboid>(256);
// Generous initial capacity for speed
QuadNode childTarget;
// Of note: adding all the cuboids to the set and then testing is faster
// than testing as we go and potentially getting out faster
// This is especially true when there is less likely to be an overlap
// anyway
for (QuadNode target : targets) {
// Drill down to the children nodes to get the smaller cuboids
// contained therein
children.add(target);
do {
childTarget = children.pop();
for (QuadNode child : childTarget.quads) {
if (child == null) {
continue;
}
children.push(child);
cuboids.addAll(child.cuboids);
}
} while (!children.isEmpty());
// Then ascend backup and add the ones there
while (target != null) {
cuboids.addAll(target.cuboids);
target = target.nextListHolder;
}
}
List<QuadCuboid> overlaps = new ArrayList<QuadCuboid>();
for (QuadCuboid pc : cuboids) {
if (cuboid.overlaps(pc)) {
overlaps.add(pc);
}
}
return overlaps;
}
// Finds all the nodes that a cuboid should reside in (handles sharding)
private List<QuadNode> getAllTargets(QuadNode initialNode, QuadCuboid cuboid) {
List<QuadNode> targets = new ArrayList<QuadNode>();
// Generate the initial shards
Deque<QuadCuboid> shards = new ArrayDeque<QuadCuboid>();
shards.addAll(generateShards(initialNode, cuboid));
QuadNode node;
while (!shards.isEmpty()) {
QuadCuboid shard = shards.pop();
node = descendAndCreate(root, shard);
List<QuadCuboid> newShards = generateShards(node, shard);
// If no shards were made then this is is the bounding node for this
// shard. Include it.
if (newShards.size() == 0) {
targets.add(node);
} else {
shards.addAll(newShards);
}
}
// If the initial shard attempt turns out to not have had
// to generate shards then we need to add the initial node
if (targets.size() == 0) {
targets.add(initialNode);
}
return targets;
}
// Finds all the nodes that a cuboid should reside in (handles sharding)
private List<QuadNode> getAllTargetsNoCreate(QuadNode initialNode, QuadCuboid cuboid) {
List<QuadNode> targets = new ArrayList<QuadNode>();
// Generate the initial shards
Deque<QuadCuboid> shards = new ArrayDeque<QuadCuboid>();
shards.addAll(generateShards(initialNode, cuboid));
QuadNode node;
while (!shards.isEmpty()) {
QuadCuboid shard = shards.pop();
node = descendNoCreate(root, shard);
List<QuadCuboid> newShards = generateShards(node, shard);
// If no shards were made then this is is the bounding node for this
// shard. Include it.
if (newShards.size() == 0) {
targets.add(node);
} else {
shards.addAll(newShards);
}
}
// If the initial shard attempt turns out to not have had
// to generate shards then we need to add the initial node
if (targets.size() == 0) {
targets.add(initialNode);
}
return targets;
}
private List<QuadCuboid> getMatchingCuboids(QuadNode target, int x, int y, int z) {
List<QuadCuboid> matches = new ArrayList<QuadCuboid>();
while (target != null) {
for (QuadCuboid potential : target.cuboids) {
if (potential.includesPoint(x, y, z)) {
matches.add(potential);
}
}
target = target.nextListHolder;
}
return matches;
}
public void insert(QuadCuboid cuboid) {
if (root == null) {
beginTree(cuboid);
}
// if this cuboid falls outside of the tree, we need to repot the tree
// to
// gain a wider perspective!
if (!nodeFullyContainsCuboid(root, cuboid)) {
repotTree(cuboid);
}
QuadNode node = root;
node = descendAndCreate(node, cuboid);
// Now that we have our target we potentially need to generate shards
// and
// target their nodes as well
List<QuadNode> targets = getAllTargets(node, cuboid);
// Add the cuboid everywhere it belongs
for (QuadNode target : targets) {
addAndFixListHolders(target, cuboid);
}
}
/**
* Attempts to insert the node ONLY if there are no overlaps with existing nodes
*
* @param cuboid
* cuboid to insert
* @return success or failure
*/
public boolean insertIfNoOverlaps(QuadCuboid cuboid) {
if (root == null) {
insert(cuboid);
return true;
}
// if this cuboid falls outside of the tree, we need to repot the tree
// to
// gain a wider perspective!
if (!nodeFullyContainsCuboid(root, cuboid)) {
repotTree(cuboid);
}
QuadNode node = root;
node = descendAndCreate(node, cuboid);
// Now that we have our target we potentially need to generate shards
// and target their nodes as well
List<QuadNode> targets = getAllTargets(node, cuboid);
Deque<QuadNode> children = new ArrayDeque<QuadNode>();
Set<QuadCuboid> cuboids = new HashSet<QuadCuboid>(256);
// Generous initial capacity for speed
QuadNode childTarget;
// Of note: adding all the cuboids to the set and then testing is faster
// than testing as we go and potentially getting out faster
// This is especially true when there is less likely to be an overlap
// anyway
for (QuadNode target : targets) {
// Drill down to the children nodes to get the smaller cuboids
// contained therein
children.add(target);
do {
childTarget = children.pop();
for (QuadNode child : childTarget.quads) {
if (child == null) {
continue;
}
children.push(child);
cuboids.addAll(child.cuboids);
}
} while (!children.isEmpty());
// Then ascend backup and add the ones there
while (target != null) {
cuboids.addAll(target.cuboids);
target = target.nextListHolder;
}
}
for (QuadCuboid pc : cuboids) {
if (cuboid.overlaps(pc)) {
for (QuadNode target : targets) {
if (target.cuboids.size() == 0) {
pruneTree(node);
}
}
return false;
}
}
// Add the cuboid everywhere it belongs
for (QuadNode target : targets) {
addAndFixListHolders(target, cuboid);
}
return true;
}
private boolean nodeFullyContainsCuboid(QuadNode node, QuadCuboid cuboid) {
return node.x <= cuboid.lowCoords[0] && node.z <= cuboid.lowCoords[2]
&& (node.x + node.size) >= cuboid.highCoords[0] && (node.z + node.size) >= cuboid.highCoords[2];
}
public boolean overlapsExisting(QuadCuboid cuboid) {
if (root == null) {
return false;
}
// if this cuboid falls outside of the tree, we need to repot the tree
// to
// gain a wider perspective!
if (!nodeFullyContainsCuboid(root, cuboid)) {
repotTree(cuboid);
}
QuadNode node = root;
node = descendNoCreate(node, cuboid);
// Now that we have our target we potentially need to generate shards
// and
// target their nodes as well
List<QuadNode> targets = getAllTargetsNoCreate(node, cuboid);
Deque<QuadNode> children = new ArrayDeque<QuadNode>();
Set<QuadCuboid> cuboids = new HashSet<QuadCuboid>(256);
// Generous initial capacity for speed
QuadNode childTarget;
// Of note: adding all the cuboids to the set and then testing is faster
// than testing as we go and potentially getting out faster
// This is especially true when there is less likely to be an overlap
// anyway
for (QuadNode target : targets) {
// Drill down to the children nodes to get the smaller cuboids
// contained therein
children.add(target);
do {
childTarget = children.pop();
for (QuadNode child : childTarget.quads) {
if (child == null) {
continue;
}
children.push(child);
cuboids.addAll(child.cuboids);
}
} while (!children.isEmpty());
// Then ascend backup and add the ones there
while (target != null) {
cuboids.addAll(target.cuboids);
target = target.nextListHolder;
}
}
for (QuadCuboid pc : cuboids) {
if (cuboid.overlaps(pc)) {
return true;
}
}
return false;
}
/**
* Removes any node from the tree that no longer serves a purpose, starting from the node given and moving up
*
* @param node
*/
private void pruneTree(QuadNode node) {
int i;
while (node.parent != null && node.quads[0] == null && node.quads[1] == null && node.quads[2] == null
&& node.quads[3] == null) {
i = 0;
if (node.x != node.parent.x)
i++;
if (node.z != node.parent.z)
i += 2;
node = node.parent;
node.quads[i] = null;
}
}
private BookmarkedResult relatedSearch(QuadNode bookmark, int x, int y, int z) {
if (bookmark == null)
bookmark = root;
QuadNode node = ascendFirstSearch(bookmark, x, z);
return new BookmarkedResult(node, getMatchingCuboids(node, x, y, z));
}
public void remove(QuadCuboid cuboid) {
// No root? No-Op!
if (root == null) {
return;
}
QuadNode node;
// Should not create any new nodes, but only if the cuboid is, in fact,
// in
// the tree
node = descendAndCreate(root, cuboid);
// Using the same algorithm that was used during creation will give us
// the
// same list of nodes to examine
List<QuadNode> targets = getAllTargets(node, cuboid);
for (QuadNode target : targets) {
removeAndFixListHolders(target, cuboid);
}
}
private void removeAndFixListHolders(QuadNode node, QuadCuboid cuboid) {
node.cuboids.remove(cuboid);
// This wasn't our only cuboid, so no fix needed
if (node.cuboids.size() > 0)
return;
// Descend the tree. When we find a node with no children we know it
// needs a new list holder
Deque<QuadNode> todo = new ArrayDeque<QuadNode>();
todo.push(node);
QuadNode current;
do {
current = todo.pop();
for (QuadNode child : current.quads) {
if (child == null) {
continue;
}
// If the child isn't holding a list of cuboids itself
// (which would make it the list holder link for its children)
// Then we need to fix its children as well
if (child.cuboids.size() == 0)
todo.push(child);
child.nextListHolder = node.nextListHolder;
}
} while (!todo.isEmpty());
pruneTree(node);
}
/**
* Grow the tree beyond the root in the direction of the target node
*
* @param cuboid
*/
private void repotTree(QuadCuboid cuboid) {
QuadNode oldRoot;
int i;
do {
oldRoot = root;
root = new QuadNode(oldRoot.x, oldRoot.z, (oldRoot.size << 1) + 1, null);
oldRoot.parent = root;
// Figure out the best direction to grow in (that is, which quadrant
// is the old root in the new root?)
// We start at lower left (quad 0)
i = 0;
// The target is left of us
if (cuboid.lowCoords[0] < root.x) {
i++;
root.x -= oldRoot.size + 1;
}
// The target is below us
if (cuboid.lowCoords[2] < root.z) {
i += 2;
root.z -= oldRoot.size + 1;
}
root.quads[i] = oldRoot;
} while (!nodeFullyContainsCuboid(root, cuboid));
}
public List<QuadCuboid> search(int x, int y, int z) {
QuadNode node = descendAndSearch(root, x, z);
return getMatchingCuboids(node, x, y, z);
}
}