-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQuadTreeNode.cs
564 lines (478 loc) · 14.1 KB
/
QuadTreeNode.cs
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
namespace QuadTreeSample
{
#region
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
#endregion
/// <summary>
/// Spatial/sparse quad tree implementation for C#.
/// This data structure optimizes storage of big condensed filled areas.
/// It's suboptimal for storing very sparse non-condensed data as overhead of having multiple subnodes is quite high.
/// It works similar to https://www.youtube.com/watch?v=NfjybO2PIq0 except it uses lazy initialization for subnodes.
/// Coding: Vladimir Kozlov, AtomicTorch Studio http://atomictorch.com
/// </summary>
public sealed class QuadTreeNode : IEnumerable<Vector2Int>
{
public readonly Vector2Int Position;
private readonly byte sizePowerOfTwo;
private QuadTreeNode subNodeBottomLeft;
private QuadTreeNode subNodeBottomRight;
private QuadTreeNode subNodeTopLeft;
private QuadTreeNode subNodeTopRight;
/// <param name="position">QuadTreeNode start position</param>
/// <param name="size">Size (will be rounded up to power of two; for example: 50->64, 200->256, 500->512, etc)</param>
public QuadTreeNode(Vector2Int position, ushort size)
{
if (size == 0)
{
throw new ArgumentOutOfRangeException(nameof(size), "Size must be > 0");
}
this.Position = position;
var canvasSizeRoundedUp = RoundUpToPowerOfTwo(size);
// calculate what the power of two corresponds to this size (take logarithm with base==2)
this.sizePowerOfTwo = (byte)Math.Log(canvasSizeRoundedUp, 2);
}
/// <param name="position">QuadTreeNode start position</param>
/// <param name="sizePowerOfTwo">Size as power of two</param>
private QuadTreeNode(Vector2Int position, byte sizePowerOfTwo)
{
this.Position = position;
this.sizePowerOfTwo = sizePowerOfTwo;
}
public enum SubNodeIndex : byte
{
BottomLeft = 0,
BottomRight = 1,
TopLeft = 2,
TopRight = 3
}
public bool HasSubNodes
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return !this.IsNodeFilled
&& (this.subNodeBottomLeft != null
|| this.subNodeBottomRight != null
|| this.subNodeTopLeft != null
|| this.subNodeTopRight != null);
}
}
/// <summary>
/// Returns true if the node is completely filled. If the node is filled, it cannot contain nodes.
/// </summary>
public bool IsNodeFilled { get; private set; }
public ushort Size => (ushort)(1 << this.sizePowerOfTwo);
public int SubNodesCount
{
get
{
var result = 0;
for (byte subNodeIndex = 0; subNodeIndex < 4; subNodeIndex++)
{
var subNode = this.GetSubNode((SubNodeIndex)subNodeIndex);
if (subNode != null)
{
result += 1 + subNode.SubNodesCount;
}
}
return result;
}
}
/// <summary>
/// Adds all stored positions in this quad tree node (and its subnodes) to the list.
/// </summary>
public void AddStoredPositions(IList<Vector2Int> list)
{
if (this.sizePowerOfTwo == 0)
{
// single-cell quad tree node - add self position
list.Add(this.Position);
return;
}
if (this.IsNodeFilled)
{
// filled node cannot have subnodes
// calculate and return all the positions stored in this node
var size = this.Size;
for (var x = 0; x < size; x++)
{
for (var y = 0; y < size; y++)
{
list.Add(new Vector2Int(this.Position.X + x, this.Position.Y + y));
}
}
return;
}
for (byte subNodeIndex = 0; subNodeIndex < 4; subNodeIndex++)
{
// add all positions stored in the subNode
var subNode = this.GetSubNode((SubNodeIndex)subNodeIndex);
subNode?.AddStoredPositions(list);
}
}
public IEnumerator<Vector2Int> GetEnumerator()
{
// We will not actually enumerate as it's very memory consuming (high overhead due to creation of enumerators).
// Instead we will create a new list and fill all the stored positions there recursively.
// TODO: it's better to use higher initial list capacity to avoid resizing of the inner array
var list = new List<Vector2Int>(capacity: 100);
this.AddStoredPositions(list);
return list.GetEnumerator();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public QuadTreeNode GetSubNode(SubNodeIndex subNodeIndex)
{
switch (subNodeIndex)
{
case SubNodeIndex.BottomLeft:
return this.subNodeBottomLeft;
case SubNodeIndex.BottomRight:
return this.subNodeBottomRight;
case SubNodeIndex.TopLeft:
return this.subNodeTopLeft;
case SubNodeIndex.TopRight:
return this.subNodeTopRight;
default:
throw new ArgumentOutOfRangeException(nameof(subNodeIndex), subNodeIndex, null);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsPositionFilled(Vector2Int position)
{
Debug.Assert(position.X >= this.Position.X);
Debug.Assert(position.Y >= this.Position.Y);
Debug.Assert(position.X < this.Position.X + this.Size);
Debug.Assert(position.Y < this.Position.Y + this.Size);
if (!this.HasSubNodes)
{
return this.IsNodeFilled;
}
var subNodeIndex = this.CalculateNodeIndex(position);
var subNode = this.GetSubNode(subNodeIndex);
return subNode != null && subNode.IsPositionFilled(position);
}
/// <summary>
/// Load (additive).
/// </summary>
public void Load(IReadOnlyList<QuadTreeNodeSnapshot> snapshots)
{
foreach (var snapshot in snapshots)
{
this.SetFilledPosition(snapshot.Position, snapshot.SizePowerOfTwo);
}
}
public void ResetFilledPosition(Vector2Int position)
{
if (this.sizePowerOfTwo == 0)
{
Debug.Assert(this.Position == position);
this.IsNodeFilled = false;
return;
}
if (!this.HasSubNodes)
{
if (!this.IsNodeFilled)
{
// no subnodes exists and this node is not filled, so nothing to reset
return;
}
// need to split this filled node on the filled subnodes
this.IsNodeFilled = false;
for (byte index = 0; index < 4; index++)
{
var node = this.CreateAndSetNode((SubNodeIndex)index);
node.IsNodeFilled = true;
}
}
// find subnode
var subNodeIndex = this.CalculateNodeIndex(position);
var subNode = this.GetSubNode(subNodeIndex);
if (subNode == null)
{
// not subnode exists - nothing to reset
return;
}
subNode.ResetFilledPosition(position);
this.TryConsolidateOnReset();
}
public IList<QuadTreeNodeSnapshot> Save()
{
var list = new List<QuadTreeNodeSnapshot>();
this.Save(list);
return list;
}
public void Save(IList<QuadTreeNodeSnapshot> snapshots)
{
if (this.IsNodeFilled)
{
snapshots.Add(new QuadTreeNodeSnapshot(this.Position, this.sizePowerOfTwo));
}
else
{
this.subNodeBottomLeft?.Save(snapshots);
this.subNodeBottomRight?.Save(snapshots);
this.subNodeTopLeft?.Save(snapshots);
this.subNodeTopRight?.Save(snapshots);
}
}
public void SetFilledPosition(Vector2Int position)
{
if (this.IsNodeFilled)
{
return;
}
if (this.sizePowerOfTwo == 0)
{
Debug.Assert(this.Position == position);
this.IsNodeFilled = true;
return;
}
var subNode = this.GetOrCreateSubNode(position);
subNode.SetFilledPosition(position);
this.TryConsolidateOnSet();
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
/// Round up to the next highest power of 2
/// http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
private static int RoundUpToPowerOfTwo(int v)
{
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
return v;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private SubNodeIndex CalculateNodeIndex(Vector2Int position)
{
var isLeftHalf = position.X < this.Position.X + this.Size / 2;
var isBottomHalf = position.Y < this.Position.Y + this.Size / 2;
if (isBottomHalf)
{
return isLeftHalf ? SubNodeIndex.BottomLeft : SubNodeIndex.BottomRight;
}
// top half
return isLeftHalf ? SubNodeIndex.TopLeft : SubNodeIndex.TopRight;
}
/// <summary>
/// Creates node for according subNodeIndex.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private QuadTreeNode CreateAndSetNode(SubNodeIndex subNodeIndex)
{
var x = this.Position.X;
var y = this.Position.Y;
var subSize = (ushort)(this.Size / 2);
var subSizePowerOfTwo = (byte)(this.sizePowerOfTwo - 1);
switch (subNodeIndex)
{
case SubNodeIndex.BottomLeft:
return this.subNodeBottomLeft = new QuadTreeNode(new Vector2Int(x, y), subSizePowerOfTwo);
case SubNodeIndex.BottomRight:
return this.subNodeBottomRight = new QuadTreeNode(new Vector2Int(x + subSize, y), subSizePowerOfTwo);
case SubNodeIndex.TopLeft:
return this.subNodeTopLeft = new QuadTreeNode(new Vector2Int(x, y + subSize), subSizePowerOfTwo);
case SubNodeIndex.TopRight:
return this.subNodeTopRight = new QuadTreeNode(new Vector2Int(x + subSize, y + subSize), subSizePowerOfTwo);
default:
throw new ArgumentOutOfRangeException(nameof(subNodeIndex), subNodeIndex, null);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void DestroySubNode(SubNodeIndex subNodeIndex)
{
switch (subNodeIndex)
{
case SubNodeIndex.BottomLeft:
this.subNodeBottomLeft = null;
break;
case SubNodeIndex.BottomRight:
this.subNodeBottomRight = null;
break;
case SubNodeIndex.TopLeft:
this.subNodeTopLeft = null;
break;
case SubNodeIndex.TopRight:
this.subNodeTopRight = null;
break;
default:
throw new ArgumentOutOfRangeException(nameof(subNodeIndex), subNodeIndex, null);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void DestroySubNodes()
{
this.subNodeBottomLeft = null;
this.subNodeBottomRight = null;
this.subNodeTopLeft = null;
this.subNodeTopRight = null;
}
/// <param name="checkSubnodesForConsolidation">
/// Optimization: this flag determines if we need to check subnodes for consolidation:
/// </param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private QuadTreeNode GetOrCreateSubNode(Vector2Int position)
{
// find subnode
var subNodeIndex = this.CalculateNodeIndex(position);
var subNode = this.GetSubNode(subNodeIndex)
?? this.CreateAndSetNode(subNodeIndex);
return subNode;
}
private void ResetFilledPosition(Vector2Int position, byte sizePowerOfTwo)
{
if (sizePowerOfTwo > this.sizePowerOfTwo)
{
throw new Exception(
"Size exceeded - this quadtree node is lower size than required: size (power of two) is "
+ this.sizePowerOfTwo + " and set filled position size is " + sizePowerOfTwo);
}
if (this.sizePowerOfTwo == sizePowerOfTwo)
{
Debug.Assert(this.Position == position);
this.IsNodeFilled = false;
return;
}
if (!this.HasSubNodes)
{
if (!this.IsNodeFilled)
{
// no subnodes exists and this node is not filled, so nothing to reset
return;
}
// need to split this filled node on the filled subnodes
this.IsNodeFilled = false;
for (byte index = 0; index < 4; index++)
{
var node = this.CreateAndSetNode((SubNodeIndex)index);
node.IsNodeFilled = true;
}
}
// find subnode
var subNodeIndex = this.CalculateNodeIndex(position);
var subNode = this.GetSubNode(subNodeIndex);
if (subNode == null)
{
// not subnode exists - nothing to reset
return;
}
subNode.ResetFilledPosition(position, sizePowerOfTwo);
this.TryConsolidateOnReset();
}
private void SetFilledPosition(Vector2Int position, byte sizePowerOfTwo)
{
if (this.IsNodeFilled)
{
return;
}
if (this.sizePowerOfTwo == sizePowerOfTwo)
{
Debug.Assert(this.Position == position);
if (this.IsNodeFilled)
{
return;
}
// consolidate and make filled
this.DestroySubNodes();
this.IsNodeFilled = true;
return;
}
if (this.sizePowerOfTwo == 0)
{
throw new Exception("Size mismatch!");
}
var subNode = this.GetOrCreateSubNode(position);
subNode.SetFilledPosition(position, sizePowerOfTwo);
this.TryConsolidateOnSet();
}
private void TryConsolidateOnReset()
{
// it doesn't make sense calling this method for filled node as it's already "consolidated"
Debug.Assert(!this.IsNodeFilled);
// check if all the nodes are not filled now
var isCanConsolidate = true;
for (byte subNodeIndex = 0; subNodeIndex < 4; subNodeIndex++)
{
var subNode = this.GetSubNode((SubNodeIndex)subNodeIndex);
if (subNode == null)
{
continue;
}
if (!subNode.IsNodeFilled
&& !subNode.HasSubNodes)
{
// destroy subnode because it's not used anymore
this.DestroySubNode((SubNodeIndex)subNodeIndex);
}
else
{
// used node found
isCanConsolidate = false;
}
}
if (isCanConsolidate)
{
// all nodes are not filled! we can merge them
this.DestroySubNodes();
}
}
/// <summary>
/// When all the subnodes are "filled" they must be consolidated.
/// </summary>
private void TryConsolidateOnSet()
{
// check if all the nodes are filled now
for (byte subNodeIndex = 0; subNodeIndex < 4; subNodeIndex++)
{
var n = this.GetSubNode((SubNodeIndex)subNodeIndex);
if (n == null
|| !n.IsNodeFilled)
{
return;
}
}
// all nodes are filled! we can merge them
this.DestroySubNodes();
this.IsNodeFilled = true;
}
public struct QuadTreeNodeSnapshot
{
public readonly Vector2Int Position;
public readonly byte SizePowerOfTwo;
public QuadTreeNodeSnapshot(Vector2Int position, byte sizePowerOfTwo)
{
this.Position = position;
this.SizePowerOfTwo = sizePowerOfTwo;
}
public bool Equals(QuadTreeNodeSnapshot other)
{
return this.Position.Equals(other.Position) && this.SizePowerOfTwo.Equals(other.SizePowerOfTwo);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}
return obj is QuadTreeNodeSnapshot && this.Equals((QuadTreeNodeSnapshot)obj);
}
public override int GetHashCode()
{
unchecked
{
return (this.Position.GetHashCode() * 397) ^ this.SizePowerOfTwo.GetHashCode();
}
}
}
}
}