| | | 1 | | //======================================================================= |
| | | 2 | | // SparseVoxelGridStorage.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 FixedMathSharp; |
| | | 9 | | using GridForge.Spatial; |
| | | 10 | | using SwiftCollections; |
| | | 11 | | using SwiftCollections.Query; |
| | | 12 | | using System; |
| | | 13 | | using System.Buffers; |
| | | 14 | | using System.Collections.Generic; |
| | | 15 | | using System.Runtime.CompilerServices; |
| | | 16 | | |
| | | 17 | | namespace GridForge.Grids.Storage; |
| | | 18 | | |
| | | 19 | | internal sealed class SparseVoxelGridStorage : IVoxelGridStorage |
| | | 20 | | { |
| | | 21 | | public GridStorageKind Kind |
| | | 22 | | { |
| | | 23 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 260 | 24 | | get => GridStorageKind.Sparse; |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | public int ConfiguredVoxelCount { get; private set; } |
| | | 28 | | |
| | | 29 | | public SwiftSparseMap<ScanCell>? ScanCells { get; private set; } |
| | | 30 | | |
| | | 31 | | private SwiftSparseMap<SparseVoxelBlock>? _blocks; |
| | | 32 | | private Voxel[]? _voxels; |
| | | 33 | | private int _scanCellSize; |
| | | 34 | | private int _scanWidth; |
| | | 35 | | private int _scanHeight; |
| | | 36 | | private int _scanLength; |
| | | 37 | | private int _scanLayerSize; |
| | | 38 | | private SwiftFixedBVH<Voxel>? _closestVoxelTree; |
| | | 39 | | private int[]? _closestQueryStack; |
| | | 40 | | |
| | | 41 | | public void Initialize(VoxelGrid grid, VoxelIndex[] configuredVoxels) |
| | | 42 | | { |
| | 64 | 43 | | _scanCellSize = grid.ScanCellSize; |
| | 64 | 44 | | _scanWidth = grid.ScanWidth; |
| | 64 | 45 | | _scanHeight = grid.ScanHeight; |
| | 64 | 46 | | _scanLength = grid.ScanLength; |
| | 64 | 47 | | _scanLayerSize = grid.ScanWidth * grid.ScanHeight; |
| | | 48 | | |
| | 64 | 49 | | ConfiguredVoxelCount = configuredVoxels.Length; |
| | 64 | 50 | | if (ConfiguredVoxelCount == 0) |
| | 12 | 51 | | return; |
| | | 52 | | |
| | 52 | 53 | | SwiftDictionary<int, int> blockCapacities = Pools.SparseVoxelBlockCapacityPool.Rent(); |
| | | 54 | | try |
| | | 55 | | { |
| | 52 | 56 | | CountConfiguredVoxelsPerBlock(grid, configuredVoxels, blockCapacities); |
| | 52 | 57 | | ScanCells = Pools.ScanCellMapPool.Rent(); |
| | 52 | 58 | | _blocks = Pools.SparseVoxelBlockMapPool.Rent(); |
| | 52 | 59 | | _voxels = ArrayPool<Voxel>.Shared.Rent(ConfiguredVoxelCount); |
| | 52 | 60 | | _closestVoxelTree = new SwiftFixedBVH<Voxel>(GetClosestVoxelTreeCapacity(ConfiguredVoxelCount)); |
| | | 61 | | |
| | 270 | 62 | | for (int i = 0; i < configuredVoxels.Length; i++) |
| | | 63 | | { |
| | 83 | 64 | | VoxelIndex index = configuredVoxels[i]; |
| | 83 | 65 | | int cellKey = grid.GetScanCellKey(index); |
| | | 66 | | |
| | 83 | 67 | | if (!_blocks.TryGetValue(cellKey, out SparseVoxelBlock? block)) |
| | | 68 | | { |
| | 61 | 69 | | block = Pools.SparseVoxelBlockPool.Rent(); |
| | 61 | 70 | | block.Initialize(grid, cellKey, blockCapacities[cellKey]); |
| | 61 | 71 | | _blocks.Add(cellKey, block); |
| | 61 | 72 | | ScanCells.Add(cellKey, block.ScanCell!); |
| | | 73 | | } |
| | | 74 | | |
| | 83 | 75 | | Voxel voxel = block.AddPreparedVoxel(grid, index); |
| | 83 | 76 | | _voxels[i] = voxel; |
| | 83 | 77 | | AddVoxelToClosestTree(voxel, ConfiguredVoxelCount); |
| | | 78 | | } |
| | 52 | 79 | | } |
| | | 80 | | finally |
| | | 81 | | { |
| | 52 | 82 | | Pools.SparseVoxelBlockCapacityPool.Release(blockCapacities); |
| | 52 | 83 | | } |
| | 52 | 84 | | } |
| | | 85 | | |
| | | 86 | | public void Reset(VoxelGrid grid) |
| | | 87 | | { |
| | 65 | 88 | | ReleaseBlocks(grid); |
| | 65 | 89 | | ReleaseScanCells(); |
| | 65 | 90 | | ReleaseVoxelCache(); |
| | 65 | 91 | | ReleaseClosestVoxelTree(); |
| | | 92 | | |
| | 65 | 93 | | ConfiguredVoxelCount = 0; |
| | 65 | 94 | | } |
| | | 95 | | |
| | | 96 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 97 | | public bool TryGetVoxel(int x, int y, int z, out Voxel? result) |
| | | 98 | | { |
| | 170 | 99 | | result = null; |
| | | 100 | | |
| | 170 | 101 | | if (_blocks == null) |
| | 11 | 102 | | return false; |
| | | 103 | | |
| | 159 | 104 | | VoxelIndex index = new(x, y, z); |
| | 159 | 105 | | int cellKey = GetScanCellKey(x, y, z); |
| | 159 | 106 | | return cellKey >= 0 |
| | 159 | 107 | | && _blocks.TryGetValue(cellKey, out SparseVoxelBlock? block) |
| | 159 | 108 | | && block.TryGetVoxel(index, out result); |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | public bool TryGetClosestVoxel( |
| | | 112 | | VoxelGrid grid, |
| | | 113 | | VoxelIndex closestIndex, |
| | | 114 | | Vector3d position, |
| | | 115 | | out Voxel? result, |
| | | 116 | | out Fixed64 distanceSquared) |
| | | 117 | | { |
| | 12 | 118 | | result = null; |
| | 12 | 119 | | distanceSquared = Fixed64.MaxValue; |
| | | 120 | | |
| | 12 | 121 | | if (_voxels == null || ConfiguredVoxelCount == 0) |
| | 1 | 122 | | return false; |
| | | 123 | | |
| | 11 | 124 | | if (TryGetVoxel(closestIndex.x, closestIndex.y, closestIndex.z, out result)) |
| | | 125 | | { |
| | 2 | 126 | | distanceSquared = (result!.WorldPosition - position).MagnitudeSquared; |
| | 2 | 127 | | return true; |
| | | 128 | | } |
| | | 129 | | |
| | 9 | 130 | | return TryGetClosestVoxelFromTree(position, out result, out distanceSquared); |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 134 | | public bool TryGetScanCell(int key, out ScanCell? result) |
| | | 135 | | { |
| | 15 | 136 | | result = null; |
| | 15 | 137 | | return ScanCells?.TryGetValue(key, out result) == true; |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | public IEnumerable<Voxel> EnumerateVoxels() |
| | | 141 | | { |
| | 9 | 142 | | if (_voxels == null) |
| | 5 | 143 | | yield break; |
| | | 144 | | |
| | 102 | 145 | | for (int i = 0; i < ConfiguredVoxelCount; i++) |
| | 47 | 146 | | yield return _voxels[i]; |
| | 4 | 147 | | } |
| | | 148 | | |
| | | 149 | | public void VisitVoxels<TVisitor>(ref TVisitor visitor) |
| | | 150 | | where TVisitor : struct, IVoxelStorageVisitor |
| | | 151 | | { |
| | 37 | 152 | | if (_voxels == null) |
| | 0 | 153 | | return; |
| | | 154 | | |
| | 358 | 155 | | for (int i = 0; i < ConfiguredVoxelCount; i++) |
| | | 156 | | { |
| | 142 | 157 | | if (!visitor.Visit(_voxels[i])) |
| | 0 | 158 | | return; |
| | | 159 | | } |
| | 37 | 160 | | } |
| | | 161 | | |
| | | 162 | | public bool TryAddVoxel(VoxelGrid grid, VoxelIndex index, out Voxel? voxel) |
| | | 163 | | { |
| | 36 | 164 | | voxel = null; |
| | 36 | 165 | | int cellKey = grid.GetScanCellKey(index); |
| | 36 | 166 | | if (cellKey < 0) |
| | 1 | 167 | | return false; |
| | | 168 | | |
| | 35 | 169 | | EnsureStorageMaps(); |
| | | 170 | | |
| | 35 | 171 | | if (!_blocks!.TryGetValue(cellKey, out SparseVoxelBlock? block)) |
| | | 172 | | { |
| | 8 | 173 | | block = Pools.SparseVoxelBlockPool.Rent(); |
| | 8 | 174 | | block.Initialize(grid, cellKey, capacity: 1); |
| | 8 | 175 | | _blocks.Add(cellKey, block); |
| | 8 | 176 | | ScanCells!.Add(cellKey, block.ScanCell!); |
| | | 177 | | } |
| | | 178 | | |
| | 35 | 179 | | if (!block!.TryAddVoxel(grid, index, out voxel)) |
| | 2 | 180 | | return false; |
| | | 181 | | |
| | 33 | 182 | | AddVoxelToCache(voxel!); |
| | 33 | 183 | | AddVoxelToClosestTree(voxel!, ConfiguredVoxelCount + 1); |
| | 33 | 184 | | ConfiguredVoxelCount++; |
| | 33 | 185 | | return true; |
| | | 186 | | } |
| | | 187 | | |
| | | 188 | | public bool TryRemoveVoxel(VoxelGrid grid, VoxelIndex index, out Voxel? voxel) |
| | | 189 | | { |
| | 18 | 190 | | voxel = null; |
| | 18 | 191 | | if (_blocks == null) |
| | 1 | 192 | | return false; |
| | | 193 | | |
| | 17 | 194 | | int cellKey = grid.GetScanCellKey(index); |
| | 17 | 195 | | if (cellKey < 0) |
| | 1 | 196 | | return false; |
| | | 197 | | |
| | 16 | 198 | | if (!_blocks.TryGetValue(cellKey, out SparseVoxelBlock? block)) |
| | 1 | 199 | | return false; |
| | | 200 | | |
| | 15 | 201 | | if (!block!.TryRemoveVoxel(grid, index, out voxel)) |
| | 2 | 202 | | return false; |
| | | 203 | | |
| | 13 | 204 | | RemoveVoxelFromCache(index); |
| | 13 | 205 | | RemoveVoxelFromClosestTree(voxel!); |
| | 13 | 206 | | ConfiguredVoxelCount--; |
| | | 207 | | |
| | 13 | 208 | | if (block.Count == 0) |
| | 7 | 209 | | ReleaseBlock(grid, cellKey, block); |
| | | 210 | | |
| | 13 | 211 | | ReleaseEmptyStorageMapsIfNeeded(); |
| | 13 | 212 | | return true; |
| | | 213 | | } |
| | | 214 | | |
| | | 215 | | public void AddVoxelsInIndexRange( |
| | | 216 | | VoxelIndex min, |
| | | 217 | | VoxelIndex max, |
| | | 218 | | SwiftList<Voxel> results, |
| | | 219 | | SwiftHashSet<Voxel> redundancy) |
| | | 220 | | { |
| | 15 | 221 | | if (_blocks == null || _scanCellSize <= 0) |
| | 4 | 222 | | return; |
| | | 223 | | |
| | 11 | 224 | | int scanXMin = min.x / _scanCellSize; |
| | 11 | 225 | | int scanYMin = min.y / _scanCellSize; |
| | 11 | 226 | | int scanZMin = min.z / _scanCellSize; |
| | 11 | 227 | | int scanXMax = max.x / _scanCellSize; |
| | 11 | 228 | | int scanYMax = max.y / _scanCellSize; |
| | 11 | 229 | | int scanZMax = max.z / _scanCellSize; |
| | | 230 | | |
| | 48 | 231 | | for (int scanX = scanXMin; scanX <= scanXMax; scanX++) |
| | | 232 | | { |
| | 52 | 233 | | for (int scanY = scanYMin; scanY <= scanYMax; scanY++) |
| | | 234 | | { |
| | 52 | 235 | | for (int scanZ = scanZMin; scanZ <= scanZMax; scanZ++) |
| | | 236 | | { |
| | 13 | 237 | | int cellKey = GetScanCellKeyFromScanCoordinates(scanX, scanY, scanZ); |
| | 13 | 238 | | if (cellKey >= 0 && _blocks.TryGetValue(cellKey, out SparseVoxelBlock? block)) |
| | 10 | 239 | | block.AddVoxelsInIndexRange(min, max, results, redundancy); |
| | | 240 | | } |
| | | 241 | | } |
| | | 242 | | } |
| | 11 | 243 | | } |
| | | 244 | | |
| | | 245 | | public void AddScanCellsInRange( |
| | | 246 | | VoxelGrid _, |
| | | 247 | | int xMin, |
| | | 248 | | int yMin, |
| | | 249 | | int zMin, |
| | | 250 | | int xMax, |
| | | 251 | | int yMax, |
| | | 252 | | int zMax, |
| | | 253 | | SwiftList<ScanCell> results, |
| | | 254 | | SwiftHashSet<ScanCell> redundancy) |
| | | 255 | | { |
| | 7 | 256 | | if (_blocks == null) |
| | 1 | 257 | | return; |
| | | 258 | | |
| | 34 | 259 | | for (int x = xMin; x <= xMax; x++) |
| | | 260 | | { |
| | 44 | 261 | | for (int y = yMin; y <= yMax; y++) |
| | | 262 | | { |
| | 72 | 263 | | for (int z = zMin; z <= zMax; z++) |
| | | 264 | | { |
| | 25 | 265 | | int cellKey = GetScanCellKeyFromScanCoordinates(x, y, z); |
| | 25 | 266 | | if (cellKey >= 0 |
| | 25 | 267 | | && _blocks.TryGetValue(cellKey, out SparseVoxelBlock? block) |
| | 25 | 268 | | && block.ScanCell != null |
| | 25 | 269 | | && redundancy.Add(block.ScanCell)) |
| | | 270 | | { |
| | 7 | 271 | | results.Add(block.ScanCell); |
| | | 272 | | } |
| | | 273 | | } |
| | | 274 | | } |
| | | 275 | | } |
| | 6 | 276 | | } |
| | | 277 | | |
| | | 278 | | private static void CountConfiguredVoxelsPerBlock( |
| | | 279 | | VoxelGrid grid, |
| | | 280 | | VoxelIndex[] configuredVoxels, |
| | | 281 | | SwiftDictionary<int, int> result) |
| | | 282 | | { |
| | 52 | 283 | | result.EnsureCapacity(configuredVoxels.Length); |
| | 270 | 284 | | for (int i = 0; i < configuredVoxels.Length; i++) |
| | | 285 | | { |
| | 83 | 286 | | int key = grid.GetScanCellKey(configuredVoxels[i]); |
| | 83 | 287 | | if (result.TryGetValue(key, out int count)) |
| | 22 | 288 | | result[key] = count + 1; |
| | | 289 | | else |
| | 61 | 290 | | result.Add(key, 1); |
| | | 291 | | } |
| | 52 | 292 | | } |
| | | 293 | | |
| | | 294 | | private void EnsureStorageMaps() |
| | | 295 | | { |
| | 35 | 296 | | ScanCells ??= Pools.ScanCellMapPool.Rent(); |
| | 35 | 297 | | _blocks ??= Pools.SparseVoxelBlockMapPool.Rent(); |
| | 35 | 298 | | } |
| | | 299 | | |
| | | 300 | | private void AddVoxelToCache(Voxel voxel) |
| | | 301 | | { |
| | 33 | 302 | | EnsureVoxelCacheCapacity(ConfiguredVoxelCount + 1); |
| | 33 | 303 | | TryFindVoxelCacheIndex(voxel.Index, out int insertIndex); |
| | | 304 | | |
| | 33 | 305 | | if (insertIndex < ConfiguredVoxelCount) |
| | 11 | 306 | | Array.Copy(_voxels!, insertIndex, _voxels!, insertIndex + 1, ConfiguredVoxelCount - insertIndex); |
| | | 307 | | |
| | 33 | 308 | | _voxels![insertIndex] = voxel; |
| | 33 | 309 | | } |
| | | 310 | | |
| | | 311 | | private void AddVoxelToClosestTree(Voxel voxel, int targetVoxelCount) |
| | | 312 | | { |
| | 116 | 313 | | _closestVoxelTree ??= new SwiftFixedBVH<Voxel>(GetClosestVoxelTreeCapacity(targetVoxelCount)); |
| | 116 | 314 | | _closestVoxelTree.EnsureCapacity(GetClosestVoxelTreeCapacity(targetVoxelCount)); |
| | 116 | 315 | | _closestVoxelTree.Insert(voxel, CreateVoxelPointBounds(voxel)); |
| | 116 | 316 | | EnsureClosestQueryStackCapacity(_closestVoxelTree.NodePool.Length); |
| | 116 | 317 | | } |
| | | 318 | | |
| | | 319 | | private void RemoveVoxelFromClosestTree(Voxel voxel) |
| | | 320 | | { |
| | 13 | 321 | | _closestVoxelTree!.Remove(voxel); |
| | 13 | 322 | | if (_closestVoxelTree.Count == 0) |
| | 7 | 323 | | ReleaseClosestVoxelTree(); |
| | 13 | 324 | | } |
| | | 325 | | |
| | | 326 | | private void RemoveVoxelFromCache(VoxelIndex index) |
| | | 327 | | { |
| | 13 | 328 | | Voxel[] voxels = _voxels!; |
| | 13 | 329 | | TryFindVoxelCacheIndex(index, out int voxelArrayIndex); |
| | | 330 | | |
| | 13 | 331 | | int moveCount = ConfiguredVoxelCount - voxelArrayIndex - 1; |
| | 13 | 332 | | if (moveCount > 0) |
| | 1 | 333 | | Array.Copy(voxels, voxelArrayIndex + 1, voxels, voxelArrayIndex, moveCount); |
| | | 334 | | |
| | 13 | 335 | | voxels[ConfiguredVoxelCount - 1] = null!; |
| | 13 | 336 | | if (ConfiguredVoxelCount == 1) |
| | | 337 | | { |
| | 7 | 338 | | ArrayPool<Voxel>.Shared.Return(voxels, clearArray: true); |
| | 7 | 339 | | _voxels = null; |
| | | 340 | | } |
| | 13 | 341 | | } |
| | | 342 | | |
| | | 343 | | private void EnsureVoxelCacheCapacity(int minCapacity) |
| | | 344 | | { |
| | 33 | 345 | | if (_voxels != null && _voxels.Length >= minCapacity) |
| | 24 | 346 | | return; |
| | | 347 | | |
| | 9 | 348 | | int capacity = _voxels == null |
| | 9 | 349 | | ? minCapacity |
| | 9 | 350 | | : Math.Max(minCapacity, _voxels.Length << 1); |
| | 9 | 351 | | Voxel[] replacement = ArrayPool<Voxel>.Shared.Rent(capacity); |
| | | 352 | | |
| | 9 | 353 | | if (_voxels != null) |
| | | 354 | | { |
| | 1 | 355 | | Array.Copy(_voxels, replacement, ConfiguredVoxelCount); |
| | 1 | 356 | | ArrayPool<Voxel>.Shared.Return(_voxels, clearArray: true); |
| | | 357 | | } |
| | | 358 | | |
| | 9 | 359 | | _voxels = replacement; |
| | 9 | 360 | | } |
| | | 361 | | |
| | | 362 | | private bool TryFindVoxelCacheIndex(VoxelIndex index, out int voxelArrayIndex) |
| | | 363 | | { |
| | 46 | 364 | | voxelArrayIndex = 0; |
| | 46 | 365 | | Voxel[] voxels = _voxels!; |
| | | 366 | | |
| | 46 | 367 | | int min = 0; |
| | 46 | 368 | | int max = ConfiguredVoxelCount - 1; |
| | 144 | 369 | | while (min <= max) |
| | | 370 | | { |
| | 101 | 371 | | int mid = min + ((max - min) >> 1); |
| | 101 | 372 | | int compare = voxels[mid].Index.CompareTo(index); |
| | 101 | 373 | | if (compare == 0) |
| | | 374 | | { |
| | 3 | 375 | | voxelArrayIndex = mid; |
| | 3 | 376 | | return true; |
| | | 377 | | } |
| | | 378 | | |
| | 98 | 379 | | if (compare < 0) |
| | 83 | 380 | | min = mid + 1; |
| | | 381 | | else |
| | 15 | 382 | | max = mid - 1; |
| | | 383 | | } |
| | | 384 | | |
| | 43 | 385 | | voxelArrayIndex = min; |
| | 43 | 386 | | return false; |
| | | 387 | | } |
| | | 388 | | |
| | | 389 | | private bool TryGetClosestVoxelFromTree( |
| | | 390 | | Vector3d position, |
| | | 391 | | out Voxel? result, |
| | | 392 | | out Fixed64 distanceSquared) |
| | | 393 | | { |
| | 9 | 394 | | result = null; |
| | 9 | 395 | | distanceSquared = Fixed64.MaxValue; |
| | | 396 | | |
| | 9 | 397 | | int rootNodeIndex = _closestVoxelTree!.RootNodeIndex; |
| | | 398 | | |
| | 9 | 399 | | SwiftBVHNode<Voxel, FixedBoundVolume>[] nodes = _closestVoxelTree.NodePool; |
| | 9 | 400 | | EnsureClosestQueryStackCapacity(nodes.Length); |
| | | 401 | | |
| | 9 | 402 | | int[] stack = _closestQueryStack!; |
| | 9 | 403 | | int stackCount = 0; |
| | 9 | 404 | | stack[stackCount++] = rootNodeIndex; |
| | | 405 | | |
| | 30 | 406 | | while (stackCount > 0) |
| | | 407 | | { |
| | 21 | 408 | | int nodeIndex = stack[--stackCount]; |
| | 21 | 409 | | ref SwiftBVHNode<Voxel, FixedBoundVolume> node = ref nodes[nodeIndex]; |
| | 21 | 410 | | Fixed64 nodeDistanceSquared = GetDistanceSquaredToBounds(position, node.Bounds); |
| | 21 | 411 | | if (nodeDistanceSquared > distanceSquared) |
| | | 412 | | continue; |
| | | 413 | | |
| | 16 | 414 | | if (node.IsLeaf) |
| | | 415 | | { |
| | 10 | 416 | | Voxel candidate = node.Value; |
| | 10 | 417 | | Fixed64 candidateDistanceSquared = (candidate.WorldPosition - position).MagnitudeSquared; |
| | 10 | 418 | | if (IsBetterClosestVoxel(candidate, candidateDistanceSquared, result, distanceSquared)) |
| | | 419 | | { |
| | 9 | 420 | | result = candidate; |
| | 9 | 421 | | distanceSquared = candidateDistanceSquared; |
| | | 422 | | } |
| | | 423 | | |
| | 9 | 424 | | continue; |
| | | 425 | | } |
| | | 426 | | |
| | 6 | 427 | | PushClosestChildrenFirst(position, nodes, node, stack, ref stackCount, distanceSquared); |
| | | 428 | | } |
| | | 429 | | |
| | 9 | 430 | | return true; |
| | | 431 | | } |
| | | 432 | | |
| | | 433 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 434 | | private static bool IsBetterClosestVoxel( |
| | | 435 | | Voxel candidate, |
| | | 436 | | Fixed64 candidateDistanceSquared, |
| | | 437 | | Voxel? current, |
| | | 438 | | Fixed64 currentDistanceSquared) |
| | | 439 | | { |
| | 13 | 440 | | if (current == null) |
| | 10 | 441 | | return true; |
| | | 442 | | |
| | 3 | 443 | | if (candidateDistanceSquared != currentDistanceSquared) |
| | 1 | 444 | | return candidateDistanceSquared < currentDistanceSquared; |
| | | 445 | | |
| | 2 | 446 | | return candidate.Index.CompareTo(current.Index) < 0; |
| | | 447 | | } |
| | | 448 | | |
| | | 449 | | private static void PushClosestChildrenFirst( |
| | | 450 | | Vector3d position, |
| | | 451 | | SwiftBVHNode<Voxel, FixedBoundVolume>[] nodes, |
| | | 452 | | SwiftBVHNode<Voxel, FixedBoundVolume> node, |
| | | 453 | | int[] stack, |
| | | 454 | | ref int stackCount, |
| | | 455 | | Fixed64 bestDistanceSquared) |
| | | 456 | | { |
| | 6 | 457 | | int leftIndex = node.LeftChildIndex; |
| | 6 | 458 | | int rightIndex = node.RightChildIndex; |
| | | 459 | | |
| | 6 | 460 | | Fixed64 leftDistanceSquared = GetDistanceSquaredToBounds(position, nodes[leftIndex].Bounds); |
| | 6 | 461 | | Fixed64 rightDistanceSquared = GetDistanceSquaredToBounds(position, nodes[rightIndex].Bounds); |
| | | 462 | | |
| | 6 | 463 | | if (leftDistanceSquared <= rightDistanceSquared) |
| | | 464 | | { |
| | 5 | 465 | | PushChildIfWithinBest(rightIndex, rightDistanceSquared, stack, ref stackCount, bestDistanceSquared); |
| | 5 | 466 | | PushChildIfWithinBest(leftIndex, leftDistanceSquared, stack, ref stackCount, bestDistanceSquared); |
| | | 467 | | } |
| | | 468 | | else |
| | | 469 | | { |
| | 1 | 470 | | PushChildIfWithinBest(leftIndex, leftDistanceSquared, stack, ref stackCount, bestDistanceSquared); |
| | 1 | 471 | | PushChildIfWithinBest(rightIndex, rightDistanceSquared, stack, ref stackCount, bestDistanceSquared); |
| | | 472 | | } |
| | 1 | 473 | | } |
| | | 474 | | |
| | | 475 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 476 | | private static void PushChildIfWithinBest( |
| | | 477 | | int childIndex, |
| | | 478 | | Fixed64 childDistanceSquared, |
| | | 479 | | int[] stack, |
| | | 480 | | ref int stackCount, |
| | | 481 | | Fixed64 bestDistanceSquared) |
| | | 482 | | { |
| | 14 | 483 | | if (childDistanceSquared <= bestDistanceSquared) |
| | 13 | 484 | | stack[stackCount++] = childIndex; |
| | 14 | 485 | | } |
| | | 486 | | |
| | | 487 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 488 | | private static FixedBoundVolume CreateVoxelPointBounds(Voxel voxel) => |
| | 116 | 489 | | new(voxel.WorldPosition, voxel.WorldPosition); |
| | | 490 | | |
| | | 491 | | private static Fixed64 GetDistanceSquaredToBounds(Vector3d position, FixedBoundVolume bounds) |
| | | 492 | | { |
| | 33 | 493 | | Fixed64 x = GetAxisDistance(position.X, bounds.Min.X, bounds.Max.X); |
| | 33 | 494 | | Fixed64 y = GetAxisDistance(position.Y, bounds.Min.Y, bounds.Max.Y); |
| | 33 | 495 | | Fixed64 z = GetAxisDistance(position.Z, bounds.Min.Z, bounds.Max.Z); |
| | 33 | 496 | | return x * x + y * y + z * z; |
| | | 497 | | } |
| | | 498 | | |
| | | 499 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 500 | | private static Fixed64 GetAxisDistance(Fixed64 value, Fixed64 min, Fixed64 max) |
| | | 501 | | { |
| | 99 | 502 | | if (value < min) |
| | 21 | 503 | | return min - value; |
| | | 504 | | |
| | 78 | 505 | | return value > max ? value - max : Fixed64.Zero; |
| | | 506 | | } |
| | | 507 | | |
| | | 508 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 509 | | private static int GetClosestVoxelTreeCapacity(int voxelCapacity) |
| | | 510 | | { |
| | 179 | 511 | | if (voxelCapacity <= 1) |
| | 77 | 512 | | return 1; |
| | | 513 | | |
| | 102 | 514 | | return voxelCapacity > int.MaxValue / 2 |
| | 102 | 515 | | ? int.MaxValue |
| | 102 | 516 | | : voxelCapacity << 1; |
| | | 517 | | } |
| | | 518 | | |
| | | 519 | | private void EnsureClosestQueryStackCapacity(int minCapacity) |
| | | 520 | | { |
| | 125 | 521 | | if (_closestQueryStack != null && _closestQueryStack.Length >= minCapacity) |
| | 63 | 522 | | return; |
| | | 523 | | |
| | 62 | 524 | | int[] replacement = ArrayPool<int>.Shared.Rent(minCapacity); |
| | 62 | 525 | | if (_closestQueryStack != null) |
| | 2 | 526 | | ArrayPool<int>.Shared.Return(_closestQueryStack); |
| | | 527 | | |
| | 62 | 528 | | _closestQueryStack = replacement; |
| | 62 | 529 | | } |
| | | 530 | | |
| | | 531 | | private void ReleaseBlock(VoxelGrid grid, int cellKey, SparseVoxelBlock block) |
| | | 532 | | { |
| | 7 | 533 | | ScanCells!.Remove(cellKey); |
| | 7 | 534 | | _blocks!.Remove(cellKey); |
| | 7 | 535 | | block.Reset(grid); |
| | 7 | 536 | | Pools.SparseVoxelBlockPool.Release(block); |
| | 7 | 537 | | } |
| | | 538 | | |
| | | 539 | | private void ReleaseEmptyStorageMapsIfNeeded() |
| | | 540 | | { |
| | 13 | 541 | | if (ConfiguredVoxelCount != 0) |
| | 6 | 542 | | return; |
| | | 543 | | |
| | 7 | 544 | | Pools.SparseVoxelBlockMapPool.Release(_blocks!); |
| | 7 | 545 | | _blocks = null; |
| | | 546 | | |
| | 7 | 547 | | Pools.ScanCellMapPool.Release(ScanCells!); |
| | 7 | 548 | | ScanCells = null; |
| | 7 | 549 | | } |
| | | 550 | | |
| | | 551 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 552 | | private int GetScanCellKey(int x, int y, int z) |
| | | 553 | | { |
| | 159 | 554 | | int scanX = x / _scanCellSize; |
| | 159 | 555 | | int scanY = y / _scanCellSize; |
| | 159 | 556 | | int scanZ = z / _scanCellSize; |
| | | 557 | | |
| | 159 | 558 | | return GetScanCellKeyFromScanCoordinates(scanX, scanY, scanZ); |
| | | 559 | | } |
| | | 560 | | |
| | | 561 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 562 | | private int GetScanCellKeyFromScanCoordinates(int scanX, int scanY, int scanZ) |
| | | 563 | | { |
| | 197 | 564 | | if ((uint)scanX >= (uint)_scanWidth |
| | 197 | 565 | | || (uint)scanY >= (uint)_scanHeight |
| | 197 | 566 | | || (uint)scanZ >= (uint)_scanLength) |
| | | 567 | | { |
| | 4 | 568 | | return -1; |
| | | 569 | | } |
| | | 570 | | |
| | 193 | 571 | | return scanX + scanY * _scanWidth + scanZ * _scanLayerSize; |
| | | 572 | | } |
| | | 573 | | |
| | | 574 | | private void ReleaseBlocks(VoxelGrid grid) |
| | | 575 | | { |
| | 65 | 576 | | if (_blocks == null) |
| | 12 | 577 | | return; |
| | | 578 | | |
| | 53 | 579 | | Span<SparseVoxelBlock> blocks = _blocks.Values; |
| | 230 | 580 | | for (int i = 0; i < blocks.Length; i++) |
| | | 581 | | { |
| | 62 | 582 | | SparseVoxelBlock block = blocks[i]; |
| | 62 | 583 | | block.Reset(grid); |
| | 62 | 584 | | Pools.SparseVoxelBlockPool.Release(block); |
| | | 585 | | } |
| | | 586 | | |
| | 53 | 587 | | Pools.SparseVoxelBlockMapPool.Release(_blocks); |
| | 53 | 588 | | _blocks = null; |
| | 53 | 589 | | } |
| | | 590 | | |
| | | 591 | | private void ReleaseScanCells() |
| | | 592 | | { |
| | 65 | 593 | | if (ScanCells == null) |
| | 12 | 594 | | return; |
| | | 595 | | |
| | 53 | 596 | | Pools.ScanCellMapPool.Release(ScanCells); |
| | 53 | 597 | | ScanCells = null; |
| | 53 | 598 | | } |
| | | 599 | | |
| | | 600 | | private void ReleaseVoxelCache() |
| | | 601 | | { |
| | 65 | 602 | | if (_voxels != null) |
| | 53 | 603 | | ArrayPool<Voxel>.Shared.Return(_voxels, clearArray: true); |
| | | 604 | | |
| | 65 | 605 | | _voxels = null; |
| | 65 | 606 | | _scanCellSize = 0; |
| | 65 | 607 | | _scanWidth = 0; |
| | 65 | 608 | | _scanHeight = 0; |
| | 65 | 609 | | _scanLength = 0; |
| | 65 | 610 | | _scanLayerSize = 0; |
| | 65 | 611 | | } |
| | | 612 | | |
| | | 613 | | private void ReleaseClosestVoxelTree() |
| | | 614 | | { |
| | 72 | 615 | | _closestVoxelTree?.Clear(); |
| | 72 | 616 | | _closestVoxelTree = null; |
| | | 617 | | |
| | 72 | 618 | | if (_closestQueryStack != null) |
| | 60 | 619 | | ArrayPool<int>.Shared.Return(_closestQueryStack); |
| | | 620 | | |
| | 72 | 621 | | _closestQueryStack = null; |
| | 72 | 622 | | } |
| | | 623 | | } |