| | | 1 | | //======================================================================= |
| | | 2 | | // SparseVoxelBlock.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 GridForge.Spatial; |
| | | 9 | | using SwiftCollections; |
| | | 10 | | using System; |
| | | 11 | | using System.Buffers; |
| | | 12 | | using System.Runtime.CompilerServices; |
| | | 13 | | |
| | | 14 | | namespace GridForge.Grids.Storage; |
| | | 15 | | |
| | | 16 | | internal sealed class SparseVoxelBlock |
| | | 17 | | { |
| | | 18 | | public int CellKey { get; private set; } = -1; |
| | | 19 | | |
| | | 20 | | public ScanCell? ScanCell { get; private set; } |
| | | 21 | | |
| | 15 | 22 | | public int Count => _count; |
| | | 23 | | |
| | | 24 | | private Voxel[]? _voxels; |
| | | 25 | | private int _count; |
| | | 26 | | |
| | | 27 | | public void Initialize(VoxelGrid grid, int cellKey, int capacity) |
| | | 28 | | { |
| | 70 | 29 | | CellKey = cellKey; |
| | 70 | 30 | | ScanCell = Pools.ScanCellPool.Rent(); |
| | 70 | 31 | | ScanCell.Initialize(grid.World!, grid.GridIndex, cellKey); |
| | 70 | 32 | | _voxels = capacity > 0 |
| | 70 | 33 | | ? ArrayPool<Voxel>.Shared.Rent(capacity) |
| | 70 | 34 | | : null; |
| | 70 | 35 | | _count = 0; |
| | 70 | 36 | | } |
| | | 37 | | |
| | | 38 | | public Voxel AddPreparedVoxel(VoxelGrid grid, VoxelIndex index) |
| | | 39 | | { |
| | | 40 | | // GridWorld validates prepared sparse input as sorted and deduplicated before storage initialization. |
| | 83 | 41 | | EnsureCapacity(_count + 1); |
| | 83 | 42 | | Voxel voxel = CreateVoxel(grid, index); |
| | 83 | 43 | | _voxels![_count++] = voxel; |
| | 83 | 44 | | return voxel; |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | public bool TryAddVoxel(VoxelGrid grid, VoxelIndex index, out Voxel? voxel) |
| | | 48 | | { |
| | 37 | 49 | | voxel = null; |
| | 37 | 50 | | if (TryFindVoxelArrayIndex(index, out int insertIndex)) |
| | 2 | 51 | | return false; |
| | | 52 | | |
| | 35 | 53 | | EnsureCapacity(_count + 1); |
| | 35 | 54 | | voxel = CreateVoxel(grid, index); |
| | | 55 | | |
| | 35 | 56 | | if (insertIndex < _count) |
| | 10 | 57 | | Array.Copy(_voxels!, insertIndex, _voxels!, insertIndex + 1, _count - insertIndex); |
| | | 58 | | |
| | 35 | 59 | | _voxels![insertIndex] = voxel; |
| | 35 | 60 | | _count++; |
| | 35 | 61 | | return true; |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | public bool TryRemoveVoxel(VoxelGrid grid, VoxelIndex index, out Voxel? voxel) |
| | | 65 | | { |
| | 16 | 66 | | voxel = null; |
| | 16 | 67 | | if (!TryFindVoxelArrayIndex(index, out int voxelArrayIndex)) |
| | 3 | 68 | | return false; |
| | | 69 | | |
| | 13 | 70 | | voxel = _voxels![voxelArrayIndex]; |
| | 13 | 71 | | int moveCount = _count - voxelArrayIndex - 1; |
| | 13 | 72 | | if (moveCount > 0) |
| | 1 | 73 | | Array.Copy(_voxels, voxelArrayIndex + 1, _voxels, voxelArrayIndex, moveCount); |
| | | 74 | | |
| | 13 | 75 | | _voxels[--_count] = null!; |
| | 13 | 76 | | voxel.Reset(grid); |
| | 13 | 77 | | Pools.VoxelPool.Release(voxel); |
| | 13 | 78 | | return true; |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | public bool TryGetVoxel(VoxelIndex index, out Voxel? result) |
| | | 82 | | { |
| | 150 | 83 | | result = null; |
| | 150 | 84 | | if (!TryFindVoxelArrayIndex(index, out int voxelArrayIndex)) |
| | 61 | 85 | | return false; |
| | | 86 | | |
| | 89 | 87 | | result = _voxels![voxelArrayIndex]; |
| | 89 | 88 | | return result.IsAllocated; |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | private bool TryFindVoxelArrayIndex(VoxelIndex index, out int voxelArrayIndex) |
| | | 92 | | { |
| | 203 | 93 | | voxelArrayIndex = 0; |
| | 203 | 94 | | if (_voxels == null) |
| | 3 | 95 | | return false; |
| | | 96 | | |
| | 200 | 97 | | int min = 0; |
| | 200 | 98 | | int max = _count - 1; |
| | 395 | 99 | | while (min <= max) |
| | | 100 | | { |
| | 299 | 101 | | int mid = min + ((max - min) >> 1); |
| | 299 | 102 | | Voxel voxel = _voxels[mid]; |
| | 299 | 103 | | int compare = voxel.Index.CompareTo(index); |
| | | 104 | | |
| | 299 | 105 | | if (compare == 0) |
| | | 106 | | { |
| | 104 | 107 | | voxelArrayIndex = mid; |
| | 104 | 108 | | return true; |
| | | 109 | | } |
| | | 110 | | |
| | 195 | 111 | | if (compare < 0) |
| | 143 | 112 | | min = mid + 1; |
| | | 113 | | else |
| | 52 | 114 | | max = mid - 1; |
| | | 115 | | } |
| | | 116 | | |
| | 96 | 117 | | voxelArrayIndex = min; |
| | 96 | 118 | | return false; |
| | | 119 | | } |
| | | 120 | | |
| | | 121 | | private void EnsureCapacity(int minCapacity) |
| | | 122 | | { |
| | 118 | 123 | | if (_voxels != null && _voxels.Length >= minCapacity) |
| | 116 | 124 | | return; |
| | | 125 | | |
| | 2 | 126 | | int capacity = _voxels == null |
| | 2 | 127 | | ? minCapacity |
| | 2 | 128 | | : Math.Max(minCapacity, _voxels.Length << 1); |
| | 2 | 129 | | Voxel[] replacement = ArrayPool<Voxel>.Shared.Rent(capacity); |
| | | 130 | | |
| | 2 | 131 | | if (_voxels != null) |
| | | 132 | | { |
| | 1 | 133 | | Array.Copy(_voxels, replacement, _count); |
| | 1 | 134 | | ArrayPool<Voxel>.Shared.Return(_voxels, clearArray: true); |
| | | 135 | | } |
| | | 136 | | |
| | 2 | 137 | | _voxels = replacement; |
| | 2 | 138 | | } |
| | | 139 | | |
| | | 140 | | private Voxel CreateVoxel(VoxelGrid grid, VoxelIndex index) |
| | | 141 | | { |
| | 118 | 142 | | Voxel voxel = Pools.VoxelPool.Rent(); |
| | 118 | 143 | | voxel.Initialize( |
| | 118 | 144 | | new WorldVoxelIndex(grid.World!.SpawnToken, grid.GridIndex, grid.SpawnToken, index), |
| | 118 | 145 | | grid.GetWorldPosition(index), |
| | 118 | 146 | | CellKey, |
| | 118 | 147 | | grid.IsOnBoundary(index), |
| | 118 | 148 | | grid.Version); |
| | | 149 | | |
| | 118 | 150 | | return voxel; |
| | | 151 | | } |
| | | 152 | | |
| | | 153 | | public void AddVoxelsInIndexRange( |
| | | 154 | | VoxelIndex min, |
| | | 155 | | VoxelIndex max, |
| | | 156 | | SwiftList<Voxel> results, |
| | | 157 | | SwiftHashSet<Voxel> redundancy) |
| | | 158 | | { |
| | 11 | 159 | | if (_voxels == null) |
| | 1 | 160 | | return; |
| | | 161 | | |
| | 46 | 162 | | for (int i = 0; i < _count; i++) |
| | | 163 | | { |
| | 13 | 164 | | Voxel voxel = _voxels[i]; |
| | 13 | 165 | | VoxelIndex index = voxel.Index; |
| | 13 | 166 | | if (IsIndexInRange(index, min, max) && redundancy.Add(voxel)) |
| | 10 | 167 | | results.Add(voxel); |
| | | 168 | | } |
| | 10 | 169 | | } |
| | | 170 | | |
| | | 171 | | public void Reset(VoxelGrid grid) |
| | | 172 | | { |
| | 71 | 173 | | if (_voxels != null) |
| | | 174 | | { |
| | 350 | 175 | | for (int i = 0; i < _count; i++) |
| | | 176 | | { |
| | 105 | 177 | | Voxel voxel = _voxels[i]; |
| | 105 | 178 | | voxel.Reset(grid); |
| | 105 | 179 | | Pools.VoxelPool.Release(voxel); |
| | | 180 | | } |
| | | 181 | | |
| | 70 | 182 | | ArrayPool<Voxel>.Shared.Return(_voxels, clearArray: true); |
| | | 183 | | } |
| | | 184 | | |
| | 71 | 185 | | if (ScanCell != null) |
| | 70 | 186 | | Pools.ScanCellPool.Release(ScanCell); |
| | | 187 | | |
| | 71 | 188 | | Clear(); |
| | 71 | 189 | | } |
| | | 190 | | |
| | | 191 | | public void Clear() |
| | | 192 | | { |
| | 140 | 193 | | CellKey = -1; |
| | 140 | 194 | | ScanCell = null; |
| | 140 | 195 | | _voxels = null; |
| | 140 | 196 | | _count = 0; |
| | 140 | 197 | | } |
| | | 198 | | |
| | | 199 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 200 | | private static bool IsIndexInRange(VoxelIndex index, VoxelIndex min, VoxelIndex max) => |
| | 13 | 201 | | index.x >= min.x && index.x <= max.x |
| | 13 | 202 | | && index.y >= min.y && index.y <= max.y |
| | 13 | 203 | | && index.z >= min.z && index.z <= max.z; |
| | | 204 | | } |