| | | 1 | | //======================================================================= |
| | | 2 | | // SwiftOctree.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024–present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using SwiftCollections.Diagnostics; |
| | | 9 | | using SwiftCollections.Utility; |
| | | 10 | | using System; |
| | | 11 | | using System.Collections.Generic; |
| | | 12 | | using System.Runtime.CompilerServices; |
| | | 13 | | |
| | | 14 | | namespace SwiftCollections.Query; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Represents a mutable octree that stores keyed bounding volumes within immutable world bounds. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <typeparam name="TKey">The key used to identify each stored entry.</typeparam> |
| | | 20 | | /// <typeparam name="TVolume">The volume type used for octree registration and queries.</typeparam> |
| | | 21 | | public class SwiftOctree<TKey, TVolume> |
| | | 22 | | where TKey : notnull |
| | | 23 | | where TVolume : struct, IBoundVolume<TVolume> |
| | | 24 | | { |
| | | 25 | | private const string _diagnosticSource = nameof(SwiftOctree<TKey, TVolume>); |
| | | 26 | | |
| | | 27 | | private readonly IOctreeBoundsPartitioner<TVolume> _boundsPartitioner; |
| | | 28 | | private readonly QueryKeyIndexMap<TKey> _keyToEntryIndex; |
| | | 29 | | private readonly SwiftIntStack _freeEntries; |
| | | 30 | | |
| | | 31 | | private OctreeEntry[] _entries; |
| | | 32 | | private OctreeNode _root; |
| | | 33 | | private int _peakCount; |
| | | 34 | | private int _count; |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Initializes a new instance of the <see cref="SwiftOctree{TKey, TVolume}"/> class. |
| | | 38 | | /// </summary> |
| | | 39 | | /// <param name="worldBounds">The immutable world bounds covered by the octree.</param> |
| | | 40 | | /// <param name="options">Subdivision options for the octree.</param> |
| | | 41 | | /// <param name="boundsPartitioner">The backend-owned partitioner that maps bounds into octants.</param> |
| | 41 | 42 | | public SwiftOctree(TVolume worldBounds, SwiftOctreeOptions options, IOctreeBoundsPartitioner<TVolume> boundsPartitio |
| | | 43 | | { |
| | 41 | 44 | | SwiftThrowHelper.ThrowIfNull(boundsPartitioner, nameof(boundsPartitioner)); |
| | | 45 | | |
| | 41 | 46 | | WorldBounds = worldBounds; |
| | 41 | 47 | | Options = options; |
| | 41 | 48 | | _boundsPartitioner = boundsPartitioner; |
| | | 49 | | |
| | 41 | 50 | | int capacity = SwiftHashTools.NextPowerOfTwo(Math.Max(4, options.NodeCapacity)); |
| | 41 | 51 | | _keyToEntryIndex = new QueryKeyIndexMap<TKey>(capacity); |
| | 41 | 52 | | _freeEntries = new SwiftIntStack(); |
| | 41 | 53 | | _entries = new OctreeEntry[capacity]; |
| | 41 | 54 | | _root = new OctreeNode(worldBounds, 0, null); |
| | 41 | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Gets the number of active entries stored in the octree. |
| | | 59 | | /// </summary> |
| | 19 | 60 | | public int Count => _count; |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Gets the immutable world bounds covered by this octree. |
| | | 64 | | /// </summary> |
| | | 65 | | public TVolume WorldBounds { get; } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Gets the subdivision options used by this octree. |
| | | 69 | | /// </summary> |
| | | 70 | | public SwiftOctreeOptions Options { get; } |
| | | 71 | | |
| | 4 | 72 | | internal int DebugNodeCount => CountNodes(_root); |
| | | 73 | | |
| | 4 | 74 | | internal int DebugMaxDepth => GetMaxDepth(_root); |
| | | 75 | | |
| | 12 | 76 | | internal bool DebugRootHasChildren => _root.HasChildren; |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Inserts a new entry or replaces the bounds of an existing key. |
| | | 80 | | /// </summary> |
| | | 81 | | /// <param name="key">The entry key.</param> |
| | | 82 | | /// <param name="bounds">The entry bounds.</param> |
| | | 83 | | /// <returns><c>true</c> when a new key was added; <c>false</c> when an existing key was replaced.</returns> |
| | | 84 | | public bool Insert(TKey key, TVolume bounds) |
| | | 85 | | { |
| | 99 | 86 | | SwiftThrowHelper.ThrowIfNull(key, nameof(key)); |
| | 99 | 87 | | EnsureWithinWorldBounds(bounds, nameof(bounds)); |
| | | 88 | | |
| | 86 | 89 | | int existingIndex = FindEntryIndex(key); |
| | 86 | 90 | | if (existingIndex >= 0) |
| | | 91 | | { |
| | 1 | 92 | | RelocateEntry(existingIndex, bounds); |
| | 1 | 93 | | return false; |
| | | 94 | | } |
| | | 95 | | |
| | 85 | 96 | | EnsureEntryCapacity(_count + 1); |
| | | 97 | | |
| | 85 | 98 | | int entryIndex = AllocateEntry(key, bounds); |
| | 85 | 99 | | _keyToEntryIndex.Insert(key, entryIndex); |
| | 85 | 100 | | InsertIntoNode(_root, entryIndex); |
| | 85 | 101 | | _count++; |
| | 85 | 102 | | return true; |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Removes an entry from the octree. |
| | | 107 | | /// </summary> |
| | | 108 | | /// <param name="key">The entry key.</param> |
| | | 109 | | /// <returns><c>true</c> when the key existed and was removed; otherwise, <c>false</c>.</returns> |
| | | 110 | | public bool Remove(TKey key) |
| | | 111 | | { |
| | 5 | 112 | | SwiftThrowHelper.ThrowIfNullGeneric(key, nameof(key)); |
| | | 113 | | |
| | 5 | 114 | | int entryIndex = FindEntryIndex(key); |
| | 5 | 115 | | if (entryIndex < 0) |
| | 1 | 116 | | return false; |
| | | 117 | | |
| | 4 | 118 | | OctreeNode node = _entries[entryIndex].Node!; |
| | 4 | 119 | | RemoveEntryFromNode(node, entryIndex); |
| | | 120 | | |
| | 4 | 121 | | _keyToEntryIndex.Remove(key, MatchesEntryKey, IsAllocatedEntry, GetEntryKey); |
| | 4 | 122 | | _entries[entryIndex].Reset(); |
| | 4 | 123 | | _freeEntries.Push(entryIndex); |
| | 4 | 124 | | _count--; |
| | | 125 | | |
| | 4 | 126 | | if (Options.EnableMergeOnRemove) |
| | 4 | 127 | | TryMergeUp(node); |
| | | 128 | | |
| | 4 | 129 | | return true; |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | /// <summary> |
| | | 133 | | /// Attempts to retrieve the bounds registered for the supplied key. |
| | | 134 | | /// </summary> |
| | | 135 | | public bool TryGetBounds(TKey key, out TVolume bounds) |
| | | 136 | | { |
| | 2 | 137 | | SwiftThrowHelper.ThrowIfNullGeneric(key, nameof(key)); |
| | | 138 | | |
| | 2 | 139 | | int entryIndex = FindEntryIndex(key); |
| | 2 | 140 | | if (entryIndex < 0) |
| | | 141 | | { |
| | 1 | 142 | | bounds = default; |
| | 1 | 143 | | return false; |
| | | 144 | | } |
| | | 145 | | |
| | 1 | 146 | | bounds = _entries[entryIndex].Bounds; |
| | 1 | 147 | | return true; |
| | | 148 | | } |
| | | 149 | | |
| | | 150 | | /// <summary> |
| | | 151 | | /// Updates the bounds for an existing entry. |
| | | 152 | | /// </summary> |
| | | 153 | | /// <param name="key">The entry key.</param> |
| | | 154 | | /// <param name="newBounds">The replacement bounds.</param> |
| | | 155 | | /// <returns><c>true</c> when the key existed; otherwise, <c>false</c>.</returns> |
| | | 156 | | public bool UpdateEntryBounds(TKey key, TVolume newBounds) |
| | | 157 | | { |
| | 6 | 158 | | SwiftThrowHelper.ThrowIfNullGeneric(key, nameof(key)); |
| | | 159 | | |
| | 6 | 160 | | int entryIndex = FindEntryIndex(key); |
| | 6 | 161 | | if (entryIndex < 0) |
| | 1 | 162 | | return false; |
| | | 163 | | |
| | 5 | 164 | | return RelocateEntry(entryIndex, newBounds); |
| | | 165 | | } |
| | | 166 | | |
| | | 167 | | /// <summary> |
| | | 168 | | /// Determines whether the octree contains the specified key. |
| | | 169 | | /// </summary> |
| | | 170 | | public bool Contains(TKey key) |
| | | 171 | | { |
| | 3 | 172 | | SwiftThrowHelper.ThrowIfNullGeneric(key, nameof(key)); |
| | 3 | 173 | | return FindEntryIndex(key) >= 0; |
| | | 174 | | } |
| | | 175 | | |
| | | 176 | | /// <summary> |
| | | 177 | | /// Queries the octree and returns only entries whose bounds intersect the supplied query volume. |
| | | 178 | | /// </summary> |
| | | 179 | | /// <param name="queryBounds">The bounds used to test for intersection.</param> |
| | | 180 | | /// <param name="results">The collection that receives intersecting keys.</param> |
| | | 181 | | public void Query(TVolume queryBounds, ICollection<TKey> results) |
| | | 182 | | { |
| | 36 | 183 | | SwiftThrowHelper.ThrowIfNull(results, nameof(results)); |
| | | 184 | | |
| | 36 | 185 | | if (_count == 0 || !_root.Bounds.Intersects(queryBounds)) |
| | 4 | 186 | | return; |
| | | 187 | | |
| | 32 | 188 | | QueryNode(_root, queryBounds, results); |
| | 32 | 189 | | } |
| | | 190 | | |
| | | 191 | | /// <summary> |
| | | 192 | | /// Removes all entries from the octree while preserving the configured world bounds. |
| | | 193 | | /// </summary> |
| | | 194 | | public void Clear() |
| | | 195 | | { |
| | 3 | 196 | | if (_count == 0) |
| | 1 | 197 | | return; |
| | | 198 | | |
| | 12 | 199 | | for (int i = 0; i < _peakCount; i++) |
| | 4 | 200 | | _entries[i].Reset(); |
| | | 201 | | |
| | 2 | 202 | | _keyToEntryIndex.Clear(); |
| | 2 | 203 | | _freeEntries.Reset(); |
| | 2 | 204 | | _peakCount = 0; |
| | 2 | 205 | | _count = 0; |
| | 2 | 206 | | _root = new OctreeNode(WorldBounds, 0, null); |
| | 2 | 207 | | } |
| | | 208 | | |
| | | 209 | | private void QueryNode(OctreeNode node, TVolume queryBounds, ICollection<TKey> results) |
| | | 210 | | { |
| | 117 | 211 | | AddIntersectingEntries(node, queryBounds, results); |
| | | 212 | | |
| | 117 | 213 | | if (node.HasChildren) |
| | 28 | 214 | | QueryIntersectingChildren(node, queryBounds, results); |
| | 117 | 215 | | } |
| | | 216 | | |
| | | 217 | | private void AddIntersectingEntries(OctreeNode node, TVolume queryBounds, ICollection<TKey> results) |
| | | 218 | | { |
| | 344 | 219 | | for (int i = 0; i < node.EntryIndices.Count; i++) |
| | | 220 | | { |
| | 55 | 221 | | int entryIndex = node.EntryIndices[i]; |
| | 55 | 222 | | ref OctreeEntry entry = ref _entries[entryIndex]; |
| | 55 | 223 | | if (entry.Bounds.Intersects(queryBounds)) |
| | 50 | 224 | | results.Add(entry.Key); |
| | | 225 | | } |
| | 117 | 226 | | } |
| | | 227 | | |
| | | 228 | | private void QueryIntersectingChildren(OctreeNode node, TVolume queryBounds, ICollection<TKey> results) |
| | | 229 | | { |
| | 28 | 230 | | OctreeNode[] children = node.Children!; |
| | 504 | 231 | | for (int i = 0; i < children.Length; i++) |
| | | 232 | | { |
| | 224 | 233 | | OctreeNode child = children[i]; |
| | 224 | 234 | | if (child.Bounds.Intersects(queryBounds)) |
| | 85 | 235 | | QueryNode(child, queryBounds, results); |
| | | 236 | | } |
| | 28 | 237 | | } |
| | | 238 | | |
| | | 239 | | private bool RelocateEntry(int entryIndex, TVolume newBounds) |
| | | 240 | | { |
| | 6 | 241 | | EnsureWithinWorldBounds(newBounds, nameof(newBounds)); |
| | | 242 | | |
| | 5 | 243 | | TVolume currentBounds = _entries[entryIndex].Bounds; |
| | 5 | 244 | | if (currentBounds.BoundsEquals(newBounds)) |
| | 1 | 245 | | return true; |
| | | 246 | | |
| | 4 | 247 | | OctreeNode oldNode = _entries[entryIndex].Node!; |
| | 4 | 248 | | RemoveEntryFromNode(oldNode, entryIndex); |
| | | 249 | | |
| | 4 | 250 | | _entries[entryIndex].Bounds = newBounds; |
| | 4 | 251 | | InsertIntoNode(_root, entryIndex); |
| | | 252 | | |
| | 4 | 253 | | if (Options.EnableMergeOnRemove) |
| | 4 | 254 | | TryMergeUp(oldNode); |
| | | 255 | | |
| | 4 | 256 | | return true; |
| | | 257 | | } |
| | | 258 | | |
| | | 259 | | private void InsertIntoNode(OctreeNode node, int entryIndex) |
| | | 260 | | { |
| | 153 | 261 | | if (node.HasChildren && _boundsPartitioner.TryGetContainingChildIndex(node.Bounds, _entries[entryIndex].Bounds, |
| | | 262 | | { |
| | 64 | 263 | | InsertIntoNode(node.Children![childIndex], entryIndex); |
| | 64 | 264 | | return; |
| | | 265 | | } |
| | | 266 | | |
| | 89 | 267 | | node.EntryIndices.Add(entryIndex); |
| | 89 | 268 | | _entries[entryIndex].Node = node; |
| | | 269 | | |
| | 89 | 270 | | if (ShouldSubdivide(node)) |
| | 15 | 271 | | Subdivide(node); |
| | 89 | 272 | | } |
| | | 273 | | |
| | | 274 | | private bool ShouldSubdivide(OctreeNode node) |
| | | 275 | | { |
| | 89 | 276 | | return !node.HasChildren && |
| | 89 | 277 | | node.Depth < Options.MaxDepth && |
| | 89 | 278 | | node.EntryIndices.Count > Options.NodeCapacity && |
| | 89 | 279 | | _boundsPartitioner.CanSubdivide(node.Bounds); |
| | | 280 | | } |
| | | 281 | | |
| | | 282 | | private void Subdivide(OctreeNode node) |
| | | 283 | | { |
| | 27 | 284 | | CreateChildNodes(node); |
| | 27 | 285 | | MoveContainedEntriesToChildren(node); |
| | 27 | 286 | | SubdivideOverflowingChildren(node); |
| | 27 | 287 | | } |
| | | 288 | | |
| | | 289 | | private void CreateChildNodes(OctreeNode node) |
| | | 290 | | { |
| | 27 | 291 | | node.Children = new OctreeNode[8]; |
| | 486 | 292 | | for (int i = 0; i < node.Children.Length; i++) |
| | 216 | 293 | | node.Children[i] = CreateChildNode(node, i); |
| | 27 | 294 | | } |
| | | 295 | | |
| | | 296 | | private void MoveContainedEntriesToChildren(OctreeNode node) |
| | | 297 | | { |
| | 27 | 298 | | OctreeNode[] children = node.Children!; |
| | 27 | 299 | | int entryIndex = 0; |
| | 98 | 300 | | while (entryIndex < node.EntryIndices.Count) |
| | | 301 | | { |
| | 71 | 302 | | int currentEntryIndex = node.EntryIndices[entryIndex]; |
| | 71 | 303 | | if (!_boundsPartitioner.TryGetContainingChildIndex(node.Bounds, _entries[currentEntryIndex].Bounds, out int |
| | | 304 | | { |
| | 1 | 305 | | entryIndex++; |
| | 1 | 306 | | continue; |
| | | 307 | | } |
| | | 308 | | |
| | 70 | 309 | | OctreeNode child = children[childIndex]; |
| | 70 | 310 | | child.EntryIndices.Add(currentEntryIndex); |
| | 70 | 311 | | _entries[currentEntryIndex].Node = child; |
| | 70 | 312 | | node.EntryIndices.RemoveAt(entryIndex); |
| | | 313 | | } |
| | 27 | 314 | | } |
| | | 315 | | |
| | | 316 | | private void SubdivideOverflowingChildren(OctreeNode node) |
| | | 317 | | { |
| | 27 | 318 | | OctreeNode[] children = node.Children!; |
| | 486 | 319 | | for (int i = 0; i < children.Length; i++) |
| | | 320 | | { |
| | 216 | 321 | | OctreeNode child = children[i]; |
| | 216 | 322 | | if (ChildShouldSubdivide(child)) |
| | 12 | 323 | | Subdivide(child); |
| | | 324 | | } |
| | 27 | 325 | | } |
| | | 326 | | |
| | | 327 | | private bool ChildShouldSubdivide(OctreeNode child) |
| | | 328 | | { |
| | 216 | 329 | | return child.EntryIndices.Count > Options.NodeCapacity && |
| | 216 | 330 | | child.Depth < Options.MaxDepth && |
| | 216 | 331 | | _boundsPartitioner.CanSubdivide(child.Bounds); |
| | | 332 | | } |
| | | 333 | | |
| | | 334 | | private OctreeNode CreateChildNode(OctreeNode parent, int childIndex) |
| | | 335 | | { |
| | 216 | 336 | | TVolume bounds = _boundsPartitioner.CreateChildBounds(parent.Bounds, childIndex); |
| | 216 | 337 | | return new OctreeNode(bounds, parent.Depth + 1, parent); |
| | | 338 | | } |
| | | 339 | | |
| | | 340 | | private void TryMergeUp(OctreeNode node) |
| | | 341 | | { |
| | 8 | 342 | | OctreeNode? current = node; |
| | 21 | 343 | | while (current != null) |
| | | 344 | | { |
| | 13 | 345 | | if (current.HasChildren && CanMerge(current)) |
| | 2 | 346 | | CollapseChildrenInto(current); |
| | | 347 | | |
| | 13 | 348 | | current = current.Parent ?? null; |
| | | 349 | | } |
| | 8 | 350 | | } |
| | | 351 | | |
| | | 352 | | private bool CanMerge(OctreeNode node) |
| | | 353 | | { |
| | 5 | 354 | | int totalEntries = node.EntryIndices.Count; |
| | 5 | 355 | | OctreeNode[] children = node.Children!; |
| | 44 | 356 | | for (int i = 0; i < children.Length; i++) |
| | | 357 | | { |
| | 20 | 358 | | OctreeNode child = children[i]; |
| | 20 | 359 | | if (child.HasChildren) |
| | 2 | 360 | | return false; |
| | | 361 | | |
| | 18 | 362 | | totalEntries += child.EntryIndices.Count; |
| | 18 | 363 | | if (totalEntries > Options.NodeCapacity) |
| | 1 | 364 | | return false; |
| | | 365 | | } |
| | | 366 | | |
| | 2 | 367 | | return true; |
| | | 368 | | } |
| | | 369 | | |
| | | 370 | | private void CollapseChildrenInto(OctreeNode node) |
| | | 371 | | { |
| | 2 | 372 | | OctreeNode[] children = node.Children!; |
| | 36 | 373 | | for (int i = 0; i < children.Length; i++) |
| | | 374 | | { |
| | 16 | 375 | | OctreeNode child = children[i]; |
| | 36 | 376 | | for (int j = 0; j < child.EntryIndices.Count; j++) |
| | | 377 | | { |
| | 2 | 378 | | int entryIndex = child.EntryIndices[j]; |
| | 2 | 379 | | node.EntryIndices.Add(entryIndex); |
| | 2 | 380 | | _entries[entryIndex].Node = node; |
| | | 381 | | } |
| | | 382 | | } |
| | | 383 | | |
| | 2 | 384 | | node.Children = null; |
| | 2 | 385 | | } |
| | | 386 | | |
| | | 387 | | private void RemoveEntryFromNode(OctreeNode node, int entryIndex) |
| | | 388 | | { |
| | 8 | 389 | | int index = node.EntryIndices.IndexOf(entryIndex); |
| | 8 | 390 | | node.EntryIndices.RemoveAt(index); |
| | 8 | 391 | | _entries[entryIndex].Node = null; |
| | 8 | 392 | | } |
| | | 393 | | |
| | | 394 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 395 | | private int AllocateEntry(TKey key, TVolume bounds) |
| | | 396 | | { |
| | | 397 | | int entryIndex; |
| | 85 | 398 | | if (_freeEntries.Count > 0) |
| | 1 | 399 | | entryIndex = _freeEntries.Pop(); |
| | | 400 | | else |
| | 84 | 401 | | entryIndex = _peakCount++; |
| | | 402 | | |
| | 85 | 403 | | _entries[entryIndex].Key = key; |
| | 85 | 404 | | _entries[entryIndex].Bounds = bounds; |
| | 85 | 405 | | _entries[entryIndex].Node = null; |
| | 85 | 406 | | _entries[entryIndex].IsAllocated = true; |
| | 85 | 407 | | return entryIndex; |
| | | 408 | | } |
| | | 409 | | |
| | | 410 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 411 | | private void EnsureEntryCapacity(int capacity) |
| | | 412 | | { |
| | 85 | 413 | | if (capacity <= _entries.Length) |
| | 78 | 414 | | return; |
| | | 415 | | |
| | 7 | 416 | | int newCapacity = SwiftHashTools.NextPowerOfTwo(capacity); |
| | 7 | 417 | | Array.Resize(ref _entries, newCapacity); |
| | 7 | 418 | | _keyToEntryIndex.ResizeAndRehash(newCapacity, _peakCount, IsAllocatedEntry, GetEntryKey); |
| | 7 | 419 | | SwiftCollectionDiagnostics.Shared.Info($"Resized octree entry storage to {newCapacity} entries.", _diagnosticSou |
| | 7 | 420 | | } |
| | | 421 | | |
| | | 422 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 423 | | private int FindEntryIndex(TKey key) |
| | | 424 | | { |
| | 102 | 425 | | return _keyToEntryIndex.Find(key, MatchesEntryKey); |
| | | 426 | | } |
| | | 427 | | |
| | | 428 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 429 | | private bool MatchesEntryKey(int index, TKey key) |
| | | 430 | | { |
| | 39 | 431 | | return EqualityComparer<TKey>.Default.Equals(_entries[index].Key, key); |
| | | 432 | | } |
| | | 433 | | |
| | | 434 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 32 | 435 | | private bool IsAllocatedEntry(int index) => _entries[index].IsAllocated; |
| | | 436 | | |
| | | 437 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 32 | 438 | | private TKey GetEntryKey(int index) => _entries[index].Key; |
| | | 439 | | |
| | | 440 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 441 | | private void EnsureWithinWorldBounds(TVolume bounds, string paramName) |
| | | 442 | | { |
| | 105 | 443 | | if (!_boundsPartitioner.ContainsBounds(WorldBounds, bounds)) |
| | 14 | 444 | | throw new ArgumentOutOfRangeException(paramName, "Bounds must be fully contained within the octree world bou |
| | 91 | 445 | | } |
| | | 446 | | |
| | | 447 | | private static int CountNodes(OctreeNode node) |
| | | 448 | | { |
| | 28 | 449 | | int count = 1; |
| | 28 | 450 | | if (!node.HasChildren) |
| | 25 | 451 | | return count; |
| | | 452 | | |
| | 3 | 453 | | OctreeNode[] children = node.Children!; |
| | 54 | 454 | | for (int i = 0; i < children.Length; i++) |
| | 24 | 455 | | count += CountNodes(children[i]); |
| | | 456 | | |
| | 3 | 457 | | return count; |
| | | 458 | | } |
| | | 459 | | |
| | | 460 | | private static int GetMaxDepth(OctreeNode node) |
| | | 461 | | { |
| | 76 | 462 | | int maxDepth = node.Depth; |
| | 76 | 463 | | if (!node.HasChildren) |
| | 67 | 464 | | return maxDepth; |
| | | 465 | | |
| | 9 | 466 | | OctreeNode[] children = node.Children!; |
| | 162 | 467 | | for (int i = 0; i < children.Length; i++) |
| | 72 | 468 | | maxDepth = Math.Max(maxDepth, GetMaxDepth(children[i])); |
| | | 469 | | |
| | 9 | 470 | | return maxDepth; |
| | | 471 | | } |
| | | 472 | | |
| | | 473 | | private sealed class OctreeNode |
| | | 474 | | { |
| | 259 | 475 | | public OctreeNode(TVolume bounds, int depth, OctreeNode? parent) |
| | | 476 | | { |
| | 259 | 477 | | Bounds = bounds; |
| | 259 | 478 | | Depth = depth; |
| | 259 | 479 | | Parent = parent; |
| | 259 | 480 | | EntryIndices = new SwiftList<int>(); |
| | 259 | 481 | | } |
| | | 482 | | |
| | | 483 | | public TVolume Bounds { get; } |
| | | 484 | | |
| | | 485 | | public int Depth { get; } |
| | | 486 | | |
| | | 487 | | public OctreeNode? Parent { get; } |
| | | 488 | | |
| | | 489 | | public SwiftList<int> EntryIndices { get; } |
| | | 490 | | |
| | | 491 | | public OctreeNode[]? Children { get; set; } |
| | | 492 | | |
| | 508 | 493 | | public bool HasChildren => Children != null; |
| | | 494 | | } |
| | | 495 | | |
| | | 496 | | private struct OctreeEntry |
| | | 497 | | { |
| | | 498 | | public TKey Key; |
| | | 499 | | public TVolume Bounds; |
| | | 500 | | public OctreeNode? Node; |
| | | 501 | | public bool IsAllocated; |
| | | 502 | | |
| | | 503 | | public void Reset() |
| | | 504 | | { |
| | 8 | 505 | | Key = default!; |
| | 8 | 506 | | Bounds = default; |
| | 8 | 507 | | Node = null; |
| | 8 | 508 | | IsAllocated = false; |
| | 8 | 509 | | } |
| | | 510 | | } |
| | | 511 | | } |