forked from 0000duck/VectorMath
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Octree.cs
562 lines (507 loc) · 14.5 KB
/
Octree.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
//The MIT License(MIT)
//Copyright(c) 2015 ChevyRay
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
using System;
using System.Collections.Generic;
using System.Linq;
namespace MatterHackers.VectorMath
{
/// <summary>
/// Used by the Octree to represent a rectangular area.
/// </summary>
public struct Bounds
{
public double MaxX;
public double MaxY;
public double MaxZ;
public double MinX;
public double MinY;
public double MinZ;
public Bounds(AxisAlignedBoundingBox axisAlignedBoundingBox) : this()
{
this.MinX = axisAlignedBoundingBox.minXYZ.X;
this.MinY = axisAlignedBoundingBox.minXYZ.Y;
this.MinZ = axisAlignedBoundingBox.minXYZ.Z;
this.MaxX = axisAlignedBoundingBox.maxXYZ.X;
this.MaxY = axisAlignedBoundingBox.maxXYZ.Y;
this.MaxZ = axisAlignedBoundingBox.maxXYZ.Z;
}
/// <summary>
/// Construct a new Octree.
/// </summary>
/// <param name="minX">Minimum x.</param>
/// <param name="minY">Minimum y.</param>
/// <param name="maxX">Max x.</param>
/// <param name="maxY">Max y.</param>
public Bounds(double minX, double minY, double minZ, double maxX, double maxY, double maxZ)
{
MinX = minX;
MinY = minY;
MinZ = minZ;
MaxX = maxX;
MaxY = maxY;
MaxZ = maxZ;
}
/// <summary>
/// Check if this Octree can completely contain another.
/// </summary>
public bool Contains(Bounds other)
{
return other.MinX >= MinX
&& other.MinY >= MinY
&& other.MinZ >= MinZ
&& other.MaxX <= MaxX
&& other.MaxY <= MaxY
&& other.MaxZ <= MaxZ;
}
/// <summary>
/// Check if this Octree contains the point.
/// </summary>
public bool Contains(double x, double y, double z)
{
return x > MinX
&& y > MinY
&& z > MinZ
&& x < MaxX
&& y < MaxY
&& z < MaxZ;
}
/// <summary>
/// Check if this Octree intersects with another.
/// </summary>
public bool Intersects(Bounds other)
{
return MinX <= other.MaxX
&& MinY <= other.MaxY
&& MinZ <= other.MaxZ
&& MaxX >= other.MinX
&& MaxY >= other.MinY
&& MaxZ >= other.MinZ;
}
/// <summary>
/// Set the Octree's position.
/// </summary>
/// <param name="minX">Minimum x.</param>
/// <param name="minY">Minimum y.</param>
/// <param name="maxX">Max x.</param>
/// <param name="maxY">Max y.</param>
public void Set(double minX, double minY, double minZ, double maxX, double maxY, double maxZ)
{
MinX = minX;
MinY = minY;
MinZ = minZ;
MaxX = maxX;
MaxY = maxY;
MaxZ = maxZ;
}
public void Expand(double amount)
{
MinX -= amount; MinY -= amount; MinZ -= amount;
MaxX += amount; MaxY += amount; MaxZ += amount;
}
}
/// <summary>
/// A Octree tree where leaf nodes contain a Octree and a unique instance of T.
/// For example, if you are developing a game, you might use Octree<GameObject>
/// for collisions, or Octree<int> if you just want to populate it with IDs.
/// </summary>
public class Octree<T>
{
internal Dictionary<T, Leaf> leafLookup = new Dictionary<T, Leaf>();
internal int splitCount;
private Branch root;
/// <summary>
/// Creates a new Octree.
/// </summary>
/// <param name="splitCount">How many leaves a branch can hold before it splits into sub-branches.</param>
/// <param name="region">The region that your Octree occupies, all inserted bounds should fit into this.</param>
public Octree(int splitCount, Bounds region)
{
this.splitCount = splitCount;
root = CreateBranch(this, null, region);
}
/// <summary>
/// Creates a new Octree.
/// </summary>
/// <param name="splitCount">How many leaves a branch can hold before it splits into sub-branches.</param>
/// <param name="minX">X position of the region.</param>
/// <param name="minY">Y position of the region.</param>
/// <param name="maxX">xSize of the region.</param>
/// <param name="maxY">ySize of the region.</param>
public Octree(int splitCount, double minX, double minY, double minZ, double maxX, double maxY, double maxZ)
: this(splitCount, new Bounds(minX, minY, minZ, maxX, maxY, maxZ))
{
}
public int Count { get; private set; }
/// <summary>
/// Clear the Octree. This will remove all leaves and branches. If you have a lot of moving objects,
/// you probably want to call Clear() every frame, and re-insert every object. Branches and leaves are pooled.
/// </summary>
public void Clear()
{
root.Clear();
root.Tree = this;
leafLookup.Clear();
}
/// <summary>
/// Count how many branches are in the Octree.
/// </summary>
public int CountBranches()
{
int count = CountBranches(root, 0);
return count;
}
/// <summary>
/// Find all other values whose areas are overlapping the specified value.
/// </summary>
/// <returns>True if any collisions were found.</returns>
/// <param name="value">The value to check collisions against.</param>
/// <param name="values">A list to populate with the results. If null, this function will create the list for you.</param>
public IEnumerable<T> FindCollisions(T value)
{
Leaf leaf;
if (leafLookup.TryGetValue(value, out leaf))
{
var branch = leaf.Branch;
//Add the leaf's siblings (prevent it from colliding with itself)
if (branch.Leaves.Count > 0)
{
for (int i = 0; i < branch.Leaves.Count; ++i)
{
if (leaf != branch.Leaves[i] && leaf.Bounds.Intersects(branch.Leaves[i].Bounds))
{
yield return branch.Leaves[i].Value;
}
}
}
//Add the branch's children
if (branch.Split)
{
for (int i = 0; i < 8; ++i)
{
if (branch.Branches[i] != null)
{
foreach (var child in branch.Branches[i].SearchBounds(leaf.Bounds))
{
yield return child;
}
}
}
}
//Add all leaves back to the root
branch = branch.Parent;
while (branch != null)
{
if (branch.Leaves.Count > 0)
{
for (int i = 0; i < branch.Leaves.Count; ++i)
{
if (leaf.Bounds.Intersects(branch.Leaves[i].Bounds))
{
yield return branch.Leaves[i].Value;
}
}
}
branch = branch.Parent;
}
}
}
public IEnumerable<T> AllObjects()
{
return root.AllObjects();
}
/// <summary>
/// Insert a new leaf node into the Octree.
/// </summary>
/// <param name="value">The leaf value.</param>
/// <param name="Bounds">The leaf size.</param>
public void Insert(T value, Bounds bounds)
{
Leaf leaf;
if (!leafLookup.TryGetValue(value, out leaf))
{
leaf = CreateLeaf(value, bounds);
leafLookup.Add(value, leaf);
}
root.Insert(leaf);
Count++;
}
/// <summary>
/// Insert a new leaf node into the Octree.
/// </summary>
/// <param name="value">The leaf value.</param>
/// <param name="x">X position of the leaf.</param>
/// <param name="y">Y position of the leaf.</param>
/// <param name="xSize">xSize of the leaf.</param>
/// <param name="ySize">ySize of the leaf.</param>
public void Insert(T value, double x, double y, double z, double xSize, double ySize, double zSize)
{
var bounds = new Bounds(x, y, z, x + xSize, y + ySize, z + zSize);
Insert(value, bounds);
}
public void Remove(T value)
{
Leaf leaf;
if (leafLookup.TryGetValue(value, out leaf))
{
root.Remove(leaf);
leafLookup.Remove(value);
Count--;
}
}
public IEnumerable<T> SearchBounds(Bounds bounds)
{
return root.SearchBounds(bounds);
}
/// <summary>
/// Find all values touching in the specified area.
/// </summary>
/// <returns>True if any values were found.</returns>
/// <param name="x">X position to search.</param>
/// <param name="y">Y position to search.</param>
/// <param name="xSize">xSize of the search area.</param>
/// <param name="ySize">ySize of the search area.</param>
/// <param name="values">A list to populate with the results. If null, this function will create the list for you.</param>
public IEnumerable<T> SearchBounds(double x, double y, double z, double xSize, double ySize, double zSize)
{
var bounds = new Bounds(x, y, z, x + xSize, y + ySize, z + zSize);
return SearchBounds(bounds);
}
/// <summary>
/// Find all values overlapping the specified point.
/// </summary>
/// <returns>True if any values were found.</returns>
/// <param name="x">The x coordinate.</param>
/// <param name="y">The y coordinate.</param>
/// <param name="values">A list to populate with the results. If null, this function will create the list for you.</param>
public IEnumerable<T> SearchPoint(double x, double y, double z)
{
return root.SearchPoint(x, y, z);
}
private static Branch CreateBranch(Octree<T> tree, Branch parent, Bounds bounds)
{
// ____________
// / / /
// / 6 / 7 / |
// /_____/_____/ |
// / / / |
// / 4 / 5 / |
// /_____/_____/ |
// | ____________|
// | / / /
// | / 2 / 3 /
// | /_____/_____/
// | / / /
// | / 0 / 1 /
// /_____/_____/
var branch = new Branch();
branch.Tree = tree;
branch.Parent = parent;
branch.Split = false;
double midX = bounds.MinX + (bounds.MaxX - bounds.MinX) / 2;
double midY = bounds.MinY + (bounds.MaxY - bounds.MinY) / 2;
double midZ = bounds.MinZ + (bounds.MaxZ - bounds.MinZ) / 2;
double[] xPos = new double[] { bounds.MinX, midX, midX, bounds.MaxX };
double[] yPos = new double[] { bounds.MinY, midY, midY, bounds.MaxY };
double[] zPos = new double[] { bounds.MinZ, midZ, midZ, bounds.MaxZ };
branch.Bounds[0].Set(xPos[0], yPos[0], zPos[0], xPos[1], yPos[1], zPos[1]);
branch.Bounds[1].Set(xPos[2], yPos[0], zPos[0], xPos[3], yPos[1], zPos[1]);
branch.Bounds[2].Set(xPos[0], yPos[2], zPos[0], xPos[1], yPos[3], zPos[1]);
branch.Bounds[3].Set(xPos[2], yPos[2], zPos[0], xPos[3], yPos[3], zPos[1]);
branch.Bounds[4].Set(xPos[0], yPos[0], zPos[2], xPos[1], yPos[1], zPos[3]);
branch.Bounds[5].Set(xPos[2], yPos[0], zPos[2], xPos[3], yPos[1], zPos[3]);
branch.Bounds[6].Set(xPos[0], yPos[2], zPos[2], xPos[1], yPos[3], zPos[3]);
branch.Bounds[7].Set(xPos[2], yPos[2], zPos[2], xPos[3], yPos[3], zPos[3]);
return branch;
}
private static Leaf CreateLeaf(T value, Bounds bounds)
{
var leaf = new Leaf();
leaf.Value = value;
leaf.Bounds = bounds;
return leaf;
}
private int CountBranches(Branch branch, int count)
{
++count;
if (branch.Split)
{
for (int i = 0; i < 8; ++i)
{
if (branch.Branches[i] != null)
{
CountBranches(branch.Branches[i], count);
}
}
}
return count;
}
internal class Branch
{
internal Bounds[] Bounds = new Bounds[8];
internal Branch[] Branches = new Branch[8];
internal List<Leaf> Leaves = new List<Leaf>();
internal Branch Parent;
internal bool Split;
internal Octree<T> Tree;
internal IEnumerable<T> AllObjects()
{
var items = new Stack<Branch>(new Branch[] { this });
while (items.Any())
{
Branch item = items.Pop();
if (item.Leaves.Count > 0)
{
for (int i = 0; i < item.Leaves.Count; ++i)
{
yield return item.Leaves[i].Value;
}
}
for (int i = 0; i < 8; ++i)
{
if (item.Branches[i] != null)
{
items.Push(item.Branches[i]);
}
}
}
}
internal void Clear()
{
Tree = null;
Parent = null;
Split = false;
for (int i = 0; i < 8; ++i)
{
Branches[i] = null;
}
Leaves.Clear();
}
internal void Insert(Leaf leaf)
{
//If this branch is already split
if (Split)
{
for (int i = 0; i < 8; ++i)
{
if (Bounds[i].Contains(leaf.Bounds))
{
if (Branches[i] == null)
{
Branches[i] = CreateBranch(Tree, this, Bounds[i]);
}
Branches[i].Insert(leaf);
return;
}
}
Leaves.Add(leaf);
leaf.Branch = this;
}
else
{
//Add the leaf to this node
Leaves.Add(leaf);
leaf.Branch = this;
//Once I have reached capacity, split the node
if (Leaves.Count >= Tree.splitCount)
{
var temp = new List<Leaf>();
temp.AddRange(Leaves);
Leaves.Clear();
Split = true;
for (int i = 0; i < temp.Count; ++i)
{
Insert(temp[i]);
}
}
}
}
internal void Remove(Leaf leaf)
{
if (Leaves.Contains(leaf))
{
Leaves.Remove(leaf);
}
else if (Split)
{
for (int i = 0; i < 8; ++i)
{
if (Bounds[i].Contains(leaf.Bounds)
&& Branches[i] != null)
{
Branches[i].Remove(leaf);
}
}
}
}
internal IEnumerable<T> SearchBounds(Bounds bounds)
{
var items = new Stack<Branch>(new Branch[] { this });
while (items.Any())
{
Branch item = items.Pop();
if (item.Leaves.Count > 0)
{
for (int i = 0; i < item.Leaves.Count; ++i)
{
if (bounds.Intersects(item.Leaves[i].Bounds))
{
yield return item.Leaves[i].Value;
}
}
}
for (int i = 0; i < 8; ++i)
{
if (item.Branches[i] != null)
{
items.Push(item.Branches[i]);
}
}
}
}
internal IEnumerable<T> SearchPoint(double x, double y, double z)
{
if (Leaves.Count > 0)
{
for (int i = 0; i < Leaves.Count; ++i)
{
if (Leaves[i].Bounds.Contains(x, y, z))
{
yield return Leaves[i].Value;
}
}
}
for (int i = 0; i < 8; ++i)
{
if (Branches[i] != null)
{
foreach (var child in Branches[i].SearchPoint(x, y, z))
{
yield return child;
}
}
}
}
}
internal class Leaf
{
internal Bounds Bounds;
internal Branch Branch;
internal T Value;
}
}
}