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