| | | 1 | | //======================================================================= |
| | | 2 | | // SwiftBVH.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 System; |
| | | 9 | | using System.Collections.Generic; |
| | | 10 | | using System.Runtime.CompilerServices; |
| | | 11 | | using SwiftCollections.Diagnostics; |
| | | 12 | | using SwiftCollections.Utility; |
| | | 13 | | |
| | | 14 | | namespace SwiftCollections.Query; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Represents a Bounding Volume Hierarchy (BVH) optimized for spatial queries. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <remarks> |
| | | 20 | | /// <para> |
| | | 21 | | /// This class is not thread-safe. Concurrent access from multiple threads must be |
| | | 22 | | /// serialized externally (e.g., with a lock or by limiting access to a single thread). |
| | | 23 | | /// </para> |
| | | 24 | | /// </remarks> |
| | | 25 | | public class SwiftBVH<TKey, TVolume> |
| | | 26 | | where TKey : notnull |
| | | 27 | | where TVolume : struct, IBoundVolume<TVolume> |
| | | 28 | | { |
| | | 29 | | #region Static & Constants |
| | | 30 | | |
| | | 31 | | private const string _diagnosticSource = nameof(SwiftBVH<TKey, TVolume>); |
| | | 32 | | |
| | | 33 | | #endregion |
| | | 34 | | |
| | | 35 | | #region Fields |
| | | 36 | | |
| | | 37 | | private SwiftBVHNode<TKey, TVolume>[] _nodePool; |
| | | 38 | | private int _peakIndex; |
| | | 39 | | private int _leafCount; |
| | | 40 | | |
| | | 41 | | private readonly QueryKeyIndexMap<TKey> _keyToNodeIndex; |
| | 44 | 42 | | private readonly QueryTraversalScratch _queryScratch = new(); |
| | | 43 | | |
| | 44 | 44 | | private readonly SwiftIntStack _freeIndices = new(); |
| | | 45 | | |
| | | 46 | | private int _rootNodeIndex; |
| | | 47 | | |
| | | 48 | | #endregion |
| | | 49 | | |
| | | 50 | | #region Constructor |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Initializes a new instance of the <see cref="SwiftBVH{TKey, TVolume}"/> class with the specified capacity. |
| | | 54 | | /// </summary> |
| | 44 | 55 | | public SwiftBVH(int capacity) |
| | | 56 | | { |
| | 44 | 57 | | capacity = SwiftHashTools.NextPowerOfTwo(capacity); |
| | 44 | 58 | | _nodePool = new SwiftBVHNode<TKey, TVolume>[capacity].Populate(() => |
| | 44 | 59 | | new SwiftBVHNode<TKey, TVolume>() { ParentIndex = -1, LeftChildIndex = -1, RightChildIndex = -1 }); |
| | 44 | 60 | | _keyToNodeIndex = new QueryKeyIndexMap<TKey>(capacity, MatchesEntryKey, IsLeafNode, GetNodeValue); |
| | | 61 | | |
| | 44 | 62 | | _rootNodeIndex = -1; |
| | | 63 | | |
| | 44 | 64 | | _freeIndices = new SwiftIntStack(SwiftIntStack.DefaultCapacity); |
| | 44 | 65 | | } |
| | | 66 | | |
| | | 67 | | #endregion |
| | | 68 | | |
| | | 69 | | #region Properties |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Gets the underlying pool of nodes used in the BVH. |
| | | 73 | | /// </summary> |
| | | 74 | | /// <remarks> |
| | | 75 | | /// Prefer BVH APIs. Direct structural mutation must preserve tree invariants; invalid edits may fail fast. |
| | | 76 | | /// </remarks> |
| | 8053 | 77 | | public SwiftBVHNode<TKey, TVolume>[] NodePool => _nodePool; |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Gets the root node of the BVH. |
| | | 81 | | /// </summary> |
| | 6 | 82 | | public SwiftBVHNode<TKey, TVolume> RootNode => _rootNodeIndex >= 0 && _nodePool[_rootNodeIndex].IsAllocated |
| | 6 | 83 | | ? _nodePool[_rootNodeIndex] |
| | 6 | 84 | | : SwiftBVHNode<TKey, TVolume>.Default; |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Gets the index of the root node in the BVH. |
| | | 88 | | /// </summary> |
| | 204 | 89 | | public int RootNodeIndex => _rootNodeIndex; |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Gets the total number of leaf nodes in the BVH. |
| | | 93 | | /// </summary> |
| | 6 | 94 | | public int Count => _leafCount; |
| | | 95 | | |
| | | 96 | | #endregion |
| | | 97 | | |
| | | 98 | | #region Collection Manipulation |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Allocates a new node with the specified value, bounds, and leaf status. |
| | | 102 | | /// Reuses indices from the freelist when available. |
| | | 103 | | /// </summary> |
| | | 104 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 105 | | private int AllocateNode(TKey value, TVolume bounds, bool isLeaf) |
| | | 106 | | { |
| | | 107 | | int index; |
| | | 108 | | |
| | | 109 | | // Check if there are any reusable indices in the freelist |
| | 26442 | 110 | | if (_freeIndices.Count > 0) |
| | 4 | 111 | | index = _freeIndices.Pop(); // Reuse an available index |
| | | 112 | | else |
| | | 113 | | { |
| | 26438 | 114 | | if (_peakIndex + 1 >= _nodePool.Length) |
| | 27 | 115 | | Resize(_nodePool.Length * 2); |
| | | 116 | | |
| | | 117 | | // Allocate a new index if freelist is empty |
| | 26438 | 118 | | index = _peakIndex++; |
| | | 119 | | } |
| | | 120 | | |
| | 26442 | 121 | | ref SwiftBVHNode<TKey, TVolume> node = ref _nodePool[index]; |
| | 26442 | 122 | | node.Reset(); // Explicit reset |
| | 26442 | 123 | | node.MyIndex = index; |
| | 26442 | 124 | | node.Value = value; |
| | 26442 | 125 | | node.Bounds = bounds; |
| | | 126 | | |
| | 26442 | 127 | | if (isLeaf) |
| | | 128 | | { |
| | 13241 | 129 | | node.IsLeaf = isLeaf; |
| | 13241 | 130 | | node.SubtreeSize = 1; |
| | 13241 | 131 | | _leafCount++; |
| | | 132 | | } |
| | | 133 | | |
| | 26442 | 134 | | node.IsAllocated = true; |
| | | 135 | | |
| | 26442 | 136 | | return index; |
| | | 137 | | } |
| | | 138 | | |
| | | 139 | | /// <summary> |
| | | 140 | | /// Inserts a bounding volume with an associated value into the BVH. |
| | | 141 | | /// Ensures tree balance and updates hash buckets. |
| | | 142 | | /// </summary> |
| | | 143 | | public bool Insert(TKey value, TVolume bounds) |
| | | 144 | | { |
| | 13241 | 145 | | int newNodeIndex = AllocateNode(value, bounds, true); // Allocate new node as a leaf |
| | 13241 | 146 | | _rootNodeIndex = InsertIntoTree(_rootNodeIndex, newNodeIndex); |
| | 13241 | 147 | | InsertIntoBuckets(value, newNodeIndex); |
| | 13241 | 148 | | return true; |
| | | 149 | | } |
| | | 150 | | |
| | | 151 | | /// <summary> |
| | | 152 | | /// Inserts a node into the tree while maintaining tree balance. |
| | | 153 | | /// Adjusts parent-child relationships as necessary. |
| | | 154 | | /// </summary> |
| | | 155 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 156 | | private int InsertIntoTree(int parentNodeIndex, int newNodeIndex) |
| | | 157 | | { |
| | 151880 | 158 | | if (parentNodeIndex < 0 || !_nodePool[parentNodeIndex].IsAllocated) |
| | 40 | 159 | | return newNodeIndex; |
| | | 160 | | |
| | 151840 | 161 | | if (_nodePool[parentNodeIndex].IsLeaf) |
| | 13201 | 162 | | return CreateParentForLeaves(parentNodeIndex, newNodeIndex); |
| | | 163 | | |
| | 138639 | 164 | | InsertIntoBestChild(parentNodeIndex, newNodeIndex); |
| | 138639 | 165 | | RefreshParentNode(parentNodeIndex); |
| | 138639 | 166 | | return parentNodeIndex; |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | private int CreateParentForLeaves(int existingLeafIndex, int newLeafIndex) |
| | | 170 | | { |
| | 13201 | 171 | | TVolume combinedBounds = _nodePool[existingLeafIndex].Bounds.Union(_nodePool[newLeafIndex].Bounds); |
| | 13201 | 172 | | int oldParentIndex = _nodePool[existingLeafIndex].ParentIndex; |
| | 13201 | 173 | | int newParentIndex = AllocateNode(default!, combinedBounds, false); |
| | | 174 | | |
| | 13201 | 175 | | ref SwiftBVHNode<TKey, TVolume> newParentNode = ref _nodePool[newParentIndex]; |
| | 13201 | 176 | | newParentNode.ParentIndex = oldParentIndex; |
| | 13201 | 177 | | newParentNode.LeftChildIndex = existingLeafIndex; |
| | 13201 | 178 | | newParentNode.RightChildIndex = newLeafIndex; |
| | 13201 | 179 | | newParentNode.SubtreeSize = 1 + _nodePool[existingLeafIndex].SubtreeSize + _nodePool[newLeafIndex].SubtreeSize; |
| | | 180 | | |
| | 13201 | 181 | | _nodePool[existingLeafIndex].ParentIndex = newParentIndex; |
| | 13201 | 182 | | _nodePool[newLeafIndex].ParentIndex = newParentIndex; |
| | | 183 | | |
| | 13201 | 184 | | return newParentIndex; |
| | | 185 | | } |
| | | 186 | | |
| | | 187 | | private void InsertIntoBestChild(int parentNodeIndex, int newNodeIndex) |
| | | 188 | | { |
| | 138639 | 189 | | ref SwiftBVHNode<TKey, TVolume> parentNode = ref _nodePool[parentNodeIndex]; |
| | 138639 | 190 | | if (ShouldInsertIntoLeftChild(parentNodeIndex, newNodeIndex)) |
| | 70202 | 191 | | parentNode.LeftChildIndex = InsertIntoTree(parentNode.LeftChildIndex, newNodeIndex); |
| | | 192 | | else |
| | 68437 | 193 | | parentNode.RightChildIndex = InsertIntoTree(parentNode.RightChildIndex, newNodeIndex); |
| | 68437 | 194 | | } |
| | | 195 | | |
| | | 196 | | private bool ShouldInsertIntoLeftChild(int parentNodeIndex, int newNodeIndex) |
| | | 197 | | { |
| | 138639 | 198 | | SwiftBVHNode<TKey, TVolume> parentNode = _nodePool[parentNodeIndex]; |
| | 138639 | 199 | | SwiftBVHNode<TKey, TVolume> leftChild = _nodePool[parentNode.LeftChildIndex]; |
| | 138639 | 200 | | SwiftBVHNode<TKey, TVolume> rightChild = _nodePool[parentNode.RightChildIndex]; |
| | 138639 | 201 | | int leftSize = GetSubtreeSize(leftChild); |
| | 138639 | 202 | | int rightSize = GetSubtreeSize(rightChild); |
| | | 203 | | |
| | 138639 | 204 | | if (IsSeverelyUnbalanced(leftSize, rightSize)) |
| | 12627 | 205 | | return leftSize <= rightSize; |
| | | 206 | | |
| | 126012 | 207 | | return ShouldInsertIntoLowerCostChild( |
| | 126012 | 208 | | leftChild, |
| | 126012 | 209 | | rightChild, |
| | 126012 | 210 | | leftSize, |
| | 126012 | 211 | | rightSize, |
| | 126012 | 212 | | _nodePool[newNodeIndex].Bounds); |
| | | 213 | | } |
| | | 214 | | |
| | | 215 | | private static bool IsSeverelyUnbalanced(int leftSize, int rightSize) |
| | | 216 | | { |
| | 138639 | 217 | | int maxSize = Math.Max(leftSize, rightSize); |
| | 138639 | 218 | | int minSize = Math.Min(leftSize, rightSize); |
| | 138639 | 219 | | return maxSize > minSize * 2; |
| | | 220 | | } |
| | | 221 | | |
| | | 222 | | private static bool ShouldInsertIntoLowerCostChild( |
| | | 223 | | SwiftBVHNode<TKey, TVolume> leftChild, |
| | | 224 | | SwiftBVHNode<TKey, TVolume> rightChild, |
| | | 225 | | int leftSize, |
| | | 226 | | int rightSize, |
| | | 227 | | TVolume newBounds) |
| | | 228 | | { |
| | 126012 | 229 | | long leftCost = leftChild.Bounds.GetCost(newBounds); |
| | 126012 | 230 | | long rightCost = rightChild.Bounds.GetCost(newBounds); |
| | | 231 | | |
| | 126012 | 232 | | if (leftCost == rightCost) |
| | 5700 | 233 | | return leftSize <= rightSize; |
| | | 234 | | |
| | 120312 | 235 | | return leftCost < rightCost; |
| | | 236 | | } |
| | | 237 | | |
| | | 238 | | private void RefreshParentNode(int parentNodeIndex) |
| | | 239 | | { |
| | 139128 | 240 | | ref SwiftBVHNode<TKey, TVolume> parentNode = ref _nodePool[parentNodeIndex]; |
| | 139128 | 241 | | SwiftBVHNode<TKey, TVolume> leftChild = _nodePool[parentNode.LeftChildIndex]; |
| | 139128 | 242 | | SwiftBVHNode<TKey, TVolume> rightChild = _nodePool[parentNode.RightChildIndex]; |
| | | 243 | | |
| | 139128 | 244 | | parentNode.Bounds = GetCombinedBounds(leftChild, rightChild); |
| | 139128 | 245 | | parentNode.SubtreeSize = 1 + GetSubtreeSize(leftChild) + GetSubtreeSize(rightChild); |
| | 139128 | 246 | | } |
| | | 247 | | |
| | | 248 | | private static int GetSubtreeSize(SwiftBVHNode<TKey, TVolume> node) |
| | | 249 | | { |
| | 555534 | 250 | | return node.SubtreeSize; |
| | | 251 | | } |
| | | 252 | | |
| | | 253 | | /// <summary> |
| | | 254 | | /// Inserts a value into the hash bucket for fast lookup. |
| | | 255 | | /// Handles collisions with linear probing. |
| | | 256 | | /// </summary> |
| | | 257 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 258 | | private void InsertIntoBuckets(TKey value, int nodeIndex) |
| | | 259 | | { |
| | 13241 | 260 | | _keyToNodeIndex.Insert(value, nodeIndex); |
| | 13241 | 261 | | } |
| | | 262 | | |
| | | 263 | | /// <summary> |
| | | 264 | | /// Updates the bounding volume of a node and propagates changes up the tree. |
| | | 265 | | /// Ensures consistency in parent bounds and subtree sizes. |
| | | 266 | | /// </summary> |
| | | 267 | | public void UpdateEntryBounds(TKey value, TVolume newBounds) |
| | | 268 | | { |
| | 8 | 269 | | SwiftThrowHelper.ThrowIfNullGeneric(value, nameof(value)); |
| | | 270 | | |
| | 8 | 271 | | int index = _keyToNodeIndex.Find(value); |
| | 9 | 272 | | if (index == -1) return; |
| | | 273 | | |
| | 7 | 274 | | ref SwiftBVHNode<TKey, TVolume> node = ref _nodePool[index]; |
| | 8 | 275 | | if (!node.IsAllocated) return; // Skip update if node has been removed |
| | | 276 | | |
| | 6 | 277 | | TVolume oldBounds = node.Bounds; |
| | 6 | 278 | | if (oldBounds.BoundsEquals(newBounds)) |
| | 1 | 279 | | return; // Skip unnecessary updates |
| | | 280 | | |
| | 5 | 281 | | node.Bounds = newBounds; |
| | | 282 | | |
| | | 283 | | // Propagate changes up the tree |
| | 5 | 284 | | int parentIndex = node.ParentIndex; |
| | 9 | 285 | | while (parentIndex != -1) |
| | | 286 | | { |
| | 5 | 287 | | ref SwiftBVHNode<TKey, TVolume> parent = ref _nodePool[parentIndex]; |
| | 5 | 288 | | SwiftBVHNode<TKey, TVolume> leftChild = _nodePool[parent.LeftChildIndex]; |
| | 5 | 289 | | SwiftBVHNode<TKey, TVolume> rightChild = _nodePool[parent.RightChildIndex]; |
| | | 290 | | |
| | 5 | 291 | | TVolume newParentBounds = GetCombinedBounds(leftChild, rightChild); |
| | 5 | 292 | | if (parent.Bounds.BoundsEquals(newParentBounds)) |
| | | 293 | | break; // No further updates needed |
| | | 294 | | |
| | 4 | 295 | | parent.Bounds = newParentBounds; |
| | 4 | 296 | | parentIndex = parent.ParentIndex; |
| | | 297 | | } |
| | 5 | 298 | | } |
| | | 299 | | |
| | | 300 | | /// <summary> |
| | | 301 | | /// Removes a value and its associated bounding volume from the BVH. |
| | | 302 | | /// Updates tree structure and clears hash bucket entries. |
| | | 303 | | /// </summary> |
| | | 304 | | public bool Remove(TKey value) |
| | | 305 | | { |
| | 135 | 306 | | SwiftThrowHelper.ThrowIfNullGeneric(value, nameof(value)); |
| | | 307 | | |
| | 135 | 308 | | int nodeIndex = _keyToNodeIndex.Find(value); |
| | 137 | 309 | | if (nodeIndex == -1) return false; |
| | | 310 | | |
| | | 311 | | // If the node is the root and the only node, reset the BVH |
| | 133 | 312 | | if (nodeIndex == RootNodeIndex && _leafCount == 1) |
| | | 313 | | { |
| | 2 | 314 | | Clear(); |
| | 2 | 315 | | return true; |
| | | 316 | | } |
| | | 317 | | |
| | 131 | 318 | | RemoveFromBuckets(value); // Ensure the bucket is cleared before further operations |
| | | 319 | | |
| | | 320 | | // Remove node and update tree structure |
| | 131 | 321 | | RemoveFromTree(nodeIndex); |
| | | 322 | | |
| | 131 | 323 | | return true; |
| | | 324 | | } |
| | | 325 | | |
| | | 326 | | /// <summary> |
| | | 327 | | /// Removes an entry from the hash buckets, resolving collisions as necessary. |
| | | 328 | | /// </summary> |
| | | 329 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 330 | | private void RemoveFromBuckets(TKey value) |
| | | 331 | | { |
| | 131 | 332 | | _keyToNodeIndex.Remove(value); |
| | 131 | 333 | | } |
| | | 334 | | |
| | | 335 | | /// <summary> |
| | | 336 | | /// Removes a leaf node from the tree, collapses its parent, and propagates |
| | | 337 | | /// bound and subtree-size updates upward. Every internal node is guaranteed |
| | | 338 | | /// to have exactly two children after this operation completes. |
| | | 339 | | /// </summary> |
| | | 340 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 341 | | private void RemoveFromTree(int nodeIndex) |
| | | 342 | | { |
| | 131 | 343 | | int parentIndex = _nodePool[nodeIndex].ParentIndex; |
| | | 344 | | |
| | 131 | 345 | | int siblingIndex = ReleaseLeafAndParent(nodeIndex, parentIndex, out int grandParentIndex); |
| | 131 | 346 | | PromoteSiblingToGrandParent(siblingIndex, parentIndex, grandParentIndex); |
| | 131 | 347 | | if (grandParentIndex != -1) |
| | 125 | 348 | | RefreshAncestors(grandParentIndex); |
| | 131 | 349 | | } |
| | | 350 | | |
| | | 351 | | private int ReleaseLeafAndParent(int nodeIndex, int parentIndex, out int grandParentIndex) |
| | | 352 | | { |
| | 131 | 353 | | ref SwiftBVHNode<TKey, TVolume> parent = ref _nodePool[parentIndex]; |
| | 131 | 354 | | int siblingIndex = parent.LeftChildIndex == nodeIndex |
| | 131 | 355 | | ? parent.RightChildIndex |
| | 131 | 356 | | : parent.LeftChildIndex; |
| | 131 | 357 | | grandParentIndex = parent.ParentIndex; |
| | | 358 | | |
| | | 359 | | // Push parent before the leaf so that the leaf index sits on top of the |
| | | 360 | | // freelist stack and is reused first by the next allocation. |
| | 131 | 361 | | parent.Reset(); |
| | 131 | 362 | | _freeIndices.Push(parentIndex); |
| | | 363 | | |
| | 131 | 364 | | _leafCount--; |
| | 131 | 365 | | _nodePool[nodeIndex].Reset(); |
| | 131 | 366 | | _freeIndices.Push(nodeIndex); |
| | | 367 | | |
| | 131 | 368 | | return siblingIndex; |
| | | 369 | | } |
| | | 370 | | |
| | | 371 | | private void PromoteSiblingToGrandParent(int siblingIndex, int parentIndex, int grandParentIndex) |
| | | 372 | | { |
| | 131 | 373 | | _nodePool[siblingIndex].ParentIndex = grandParentIndex; |
| | | 374 | | |
| | 131 | 375 | | if (grandParentIndex == -1) |
| | | 376 | | { |
| | 6 | 377 | | _rootNodeIndex = siblingIndex; |
| | 6 | 378 | | return; |
| | | 379 | | } |
| | | 380 | | |
| | 125 | 381 | | ref SwiftBVHNode<TKey, TVolume> grandParent = ref _nodePool[grandParentIndex]; |
| | 125 | 382 | | if (grandParent.LeftChildIndex == parentIndex) |
| | 65 | 383 | | grandParent.LeftChildIndex = siblingIndex; |
| | | 384 | | else |
| | 60 | 385 | | grandParent.RightChildIndex = siblingIndex; |
| | 60 | 386 | | } |
| | | 387 | | |
| | | 388 | | private void RefreshAncestors(int current) |
| | | 389 | | { |
| | 614 | 390 | | while (current != -1) |
| | | 391 | | { |
| | 489 | 392 | | RefreshParentNode(current); |
| | 489 | 393 | | current = _nodePool[current].ParentIndex; |
| | | 394 | | } |
| | 125 | 395 | | } |
| | | 396 | | |
| | | 397 | | #endregion |
| | | 398 | | |
| | | 399 | | #region Capacity Management |
| | | 400 | | |
| | | 401 | | /// <summary> |
| | | 402 | | /// Ensures the BVH has sufficient capacity, resizing the node pool and buckets if needed. |
| | | 403 | | /// </summary> |
| | | 404 | | public void EnsureCapacity(int capacity) |
| | | 405 | | { |
| | 2 | 406 | | capacity = SwiftHashTools.NextPowerOfTwo(capacity); |
| | 2 | 407 | | if (capacity > _nodePool.Length) |
| | 1 | 408 | | Resize(capacity); |
| | 2 | 409 | | } |
| | | 410 | | |
| | | 411 | | /// <summary> |
| | | 412 | | /// Resizes the internal node pool to accommodate additional nodes. |
| | | 413 | | /// Preserves existing nodes and reinitializes the expanded capacity. |
| | | 414 | | /// </summary> |
| | | 415 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 416 | | private void Resize(int newSize) |
| | | 417 | | { |
| | 28 | 418 | | SwiftBVHNode<TKey, TVolume>[] newArray = new SwiftBVHNode<TKey, TVolume>[newSize]; |
| | 28 | 419 | | Array.Copy(_nodePool, 0, newArray, 0, _peakIndex); |
| | | 420 | | |
| | 70450 | 421 | | for (int i = _peakIndex; i < newSize; i++) |
| | 35197 | 422 | | newArray[i].Reset(); // set default index lookup values |
| | | 423 | | |
| | 28 | 424 | | _nodePool = newArray; |
| | | 425 | | |
| | 28 | 426 | | ResizeBuckets(newSize); |
| | 28 | 427 | | SwiftCollectionDiagnostics.Shared.Info($"Resized BVH storage to {newSize} nodes.", _diagnosticSource); |
| | 28 | 428 | | } |
| | | 429 | | |
| | | 430 | | /// <summary> |
| | | 431 | | /// Resizes and rehashes the hash buckets to maintain lookup efficiency. |
| | | 432 | | /// Rehashes existing nodes after resizing. |
| | | 433 | | /// </summary> |
| | | 434 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 435 | | private void ResizeBuckets(int newSize) |
| | | 436 | | { |
| | 28 | 437 | | _keyToNodeIndex.ResizeAndRehash(newSize, _peakIndex); |
| | 28 | 438 | | } |
| | | 439 | | |
| | | 440 | | #endregion |
| | | 441 | | |
| | | 442 | | #region Utility Methods |
| | | 443 | | |
| | | 444 | | /// <summary> |
| | | 445 | | /// Gets the combined bounding volume of two child nodes. |
| | | 446 | | /// Handles cases where one or both children are missing. |
| | | 447 | | /// </summary> |
| | | 448 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 449 | | private static TVolume GetCombinedBounds(SwiftBVHNode<TKey, TVolume> leftChild, SwiftBVHNode<TKey, TVolume> rightChi |
| | | 450 | | { |
| | 139133 | 451 | | return leftChild.Bounds.Union(rightChild.Bounds); |
| | | 452 | | } |
| | | 453 | | |
| | | 454 | | /// <summary> |
| | | 455 | | /// Queries the BVH for values whose bounding volumes intersect with the specified volume. |
| | | 456 | | /// Uses a stack-based approach for efficient traversal. |
| | | 457 | | /// </summary> |
| | | 458 | | public void Query(TVolume queryBounds, ICollection<TKey> results) |
| | | 459 | | { |
| | 30 | 460 | | SwiftThrowHelper.ThrowIfNull(results, nameof(results)); |
| | | 461 | | |
| | 33 | 462 | | if (RootNodeIndex == -1) return; |
| | | 463 | | |
| | 27 | 464 | | SwiftIntStack nodeStack = _queryScratch.RentIntStack(_peakIndex + 1); |
| | 27 | 465 | | nodeStack.Push(RootNodeIndex); |
| | | 466 | | |
| | 21080 | 467 | | while (nodeStack.Count > 0) |
| | | 468 | | { |
| | 21055 | 469 | | int index = nodeStack.Pop(); |
| | 21055 | 470 | | QueryNode(index, queryBounds, results, nodeStack); |
| | | 471 | | } |
| | 25 | 472 | | } |
| | | 473 | | |
| | | 474 | | private void QueryNode(int index, TVolume queryBounds, ICollection<TKey> results, SwiftIntStack nodeStack) |
| | | 475 | | { |
| | 21055 | 476 | | ref SwiftBVHNode<TKey, TVolume> node = ref _nodePool[index]; |
| | 21055 | 477 | | ThrowIfQueryNodeIsUnallocated(index, node); |
| | | 478 | | |
| | 21053 | 479 | | if (!queryBounds.Intersects(node.Bounds)) |
| | 307 | 480 | | return; |
| | | 481 | | |
| | 20746 | 482 | | if (node.IsLeaf) |
| | | 483 | | { |
| | 10232 | 484 | | results.Add(node.Value); |
| | 10232 | 485 | | return; |
| | | 486 | | } |
| | | 487 | | |
| | 10514 | 488 | | PushChildNodes(node, nodeStack); |
| | 10514 | 489 | | } |
| | | 490 | | |
| | | 491 | | private static void PushChildNodes(SwiftBVHNode<TKey, TVolume> node, SwiftIntStack nodeStack) |
| | | 492 | | { |
| | 10514 | 493 | | nodeStack.Push(node.LeftChildIndex); |
| | 10514 | 494 | | nodeStack.Push(node.RightChildIndex); |
| | 10514 | 495 | | } |
| | | 496 | | |
| | | 497 | | private static void ThrowIfQueryNodeIsUnallocated(int index, SwiftBVHNode<TKey, TVolume> node) |
| | | 498 | | { |
| | 21055 | 499 | | if (node.IsAllocated) |
| | 21053 | 500 | | return; |
| | | 501 | | |
| | 2 | 502 | | SwiftCollectionDiagnostics.Shared.Error($"Encountered an unallocated node at index {index} during query traversa |
| | 2 | 503 | | throw new InvalidOperationException($"Encountered an unallocated node at index {index} during query traversal.") |
| | | 504 | | } |
| | | 505 | | |
| | | 506 | | /// <summary> |
| | | 507 | | /// Finds the index of a node by its value in the BVH using hash buckets. |
| | | 508 | | /// Returns -1 if the value is not found. |
| | | 509 | | /// </summary> |
| | | 510 | | public int FindEntry(TKey value) |
| | | 511 | | { |
| | 12 | 512 | | SwiftThrowHelper.ThrowIfNullGeneric(value, nameof(value)); |
| | 12 | 513 | | return _keyToNodeIndex.Find(value); |
| | | 514 | | } |
| | | 515 | | |
| | | 516 | | /// <summary> |
| | | 517 | | /// Clears the BVH, resetting all nodes, buckets, and metadata. |
| | | 518 | | /// </summary> |
| | | 519 | | public void Clear() |
| | | 520 | | { |
| | 5 | 521 | | if (RootNodeIndex == -1) return; |
| | | 522 | | |
| | 208 | 523 | | for (int i = 0; i < _peakIndex; i++) |
| | 101 | 524 | | _nodePool[i].Reset(); |
| | | 525 | | |
| | 3 | 526 | | _keyToNodeIndex.Clear(); |
| | | 527 | | |
| | 3 | 528 | | _freeIndices.Reset(); |
| | | 529 | | |
| | 3 | 530 | | _leafCount = 0; |
| | 3 | 531 | | _peakIndex = 0; |
| | 3 | 532 | | _rootNodeIndex = -1; |
| | 3 | 533 | | } |
| | | 534 | | |
| | | 535 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 18530 | 536 | | private TKey GetNodeValue(int index) => _nodePool[index].Value; |
| | | 537 | | |
| | | 538 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 35135 | 539 | | private bool IsLeafNode(int index) => _nodePool[index].IsLeaf; |
| | | 540 | | |
| | | 541 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 542 | | private bool MatchesEntryKey(int index, TKey value) |
| | | 543 | | { |
| | 286 | 544 | | return _nodePool[index].IsLeaf && EqualityComparer<TKey>.Default.Equals(_nodePool[index].Value, value); |
| | | 545 | | } |
| | | 546 | | |
| | | 547 | | #endregion |
| | | 548 | | } |