| | | 1 | | //======================================================================= |
| | | 2 | | // DenseVoxelGridStorage.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.Collections.Generic; |
| | | 9 | | using System.Diagnostics; |
| | | 10 | | using System.Runtime.CompilerServices; |
| | | 11 | | using FixedMathSharp; |
| | | 12 | | using GridForge.Spatial; |
| | | 13 | | using SwiftCollections; |
| | | 14 | | using SwiftCollections.Dimensions; |
| | | 15 | | |
| | | 16 | | namespace GridForge.Grids.Storage; |
| | | 17 | | |
| | | 18 | | internal sealed class DenseVoxelGridStorage : IVoxelGridStorage |
| | | 19 | | { |
| | | 20 | | public GridStorageKind Kind |
| | | 21 | | { |
| | | 22 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 8619 | 23 | | get => GridStorageKind.Dense; |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | public int ConfiguredVoxelCount { get; private set; } |
| | | 27 | | |
| | | 28 | | public SwiftSparseMap<ScanCell>? ScanCells { get; private set; } |
| | | 29 | | |
| | | 30 | | internal SwiftArray3D<Voxel>? Voxels { get; private set; } |
| | | 31 | | |
| | | 32 | | private int _width; |
| | | 33 | | private int _height; |
| | | 34 | | private int _length; |
| | | 35 | | |
| | | 36 | | public void Initialize(VoxelGrid grid) |
| | | 37 | | { |
| | 457 | 38 | | _width = grid.Width; |
| | 457 | 39 | | _height = grid.Height; |
| | 457 | 40 | | _length = grid.Length; |
| | 457 | 41 | | ConfiguredVoxelCount = grid.Size; |
| | | 42 | | |
| | 457 | 43 | | GenerateScanCells(grid); |
| | 457 | 44 | | GenerateVoxels(grid); |
| | 457 | 45 | | } |
| | | 46 | | |
| | | 47 | | public void Reset(VoxelGrid grid) |
| | | 48 | | { |
| | 458 | 49 | | ReleaseVoxels(grid); |
| | 458 | 50 | | ReleaseScanCells(); |
| | | 51 | | |
| | 458 | 52 | | ConfiguredVoxelCount = 0; |
| | 458 | 53 | | _width = 0; |
| | 458 | 54 | | _height = 0; |
| | 458 | 55 | | _length = 0; |
| | 458 | 56 | | } |
| | | 57 | | |
| | | 58 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 59 | | public bool TryGetVoxel(int x, int y, int z, out Voxel? result) |
| | | 60 | | { |
| | 1391 | 61 | | result = Voxels![x, y, z]; |
| | 1391 | 62 | | return result!.IsAllocated; |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | public bool TryGetClosestVoxel( |
| | | 66 | | VoxelGrid grid, |
| | | 67 | | VoxelIndex closestIndex, |
| | | 68 | | Vector3d position, |
| | | 69 | | out Voxel? result, |
| | | 70 | | out Fixed64 distanceSquared) |
| | | 71 | | { |
| | 13 | 72 | | result = Voxels![closestIndex.x, closestIndex.y, closestIndex.z]; |
| | 13 | 73 | | distanceSquared = (result!.WorldPosition - position).MagnitudeSquared; |
| | 13 | 74 | | return true; |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 78 | | public bool TryGetScanCell(int key, out ScanCell? result) |
| | | 79 | | { |
| | 950 | 80 | | result = null; |
| | 950 | 81 | | return ScanCells?.TryGetValue(key, out result) == true; |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | public IEnumerable<Voxel> EnumerateVoxels() |
| | | 85 | | { |
| | 3 | 86 | | if (Voxels == null) |
| | 1 | 87 | | yield break; |
| | | 88 | | |
| | 8 | 89 | | for (int x = 0; x < _width; x++) |
| | | 90 | | { |
| | 44 | 91 | | for (int y = 0; y < _height; y++) |
| | | 92 | | { |
| | 566 | 93 | | for (int z = 0; z < _length; z++) |
| | | 94 | | { |
| | 264 | 95 | | yield return Voxels[x, y, z]; |
| | | 96 | | } |
| | | 97 | | } |
| | | 98 | | } |
| | 1 | 99 | | } |
| | | 100 | | |
| | | 101 | | public void VisitVoxels<TVisitor>(ref TVisitor visitor) |
| | | 102 | | where TVisitor : struct, IVoxelStorageVisitor |
| | | 103 | | { |
| | 51 | 104 | | if (Voxels == null) |
| | 1 | 105 | | return; |
| | | 106 | | |
| | 1236 | 107 | | for (int x = 0; x < _width; x++) |
| | | 108 | | { |
| | 2360 | 109 | | for (int y = 0; y < _height; y++) |
| | | 110 | | { |
| | 18616 | 111 | | for (int z = 0; z < _length; z++) |
| | | 112 | | { |
| | 8698 | 113 | | if (!visitor.Visit(Voxels[x, y, z])) |
| | 2 | 114 | | return; |
| | | 115 | | } |
| | | 116 | | } |
| | | 117 | | } |
| | 48 | 118 | | } |
| | | 119 | | |
| | | 120 | | public void AddVoxelsInIndexRange( |
| | | 121 | | VoxelIndex min, |
| | | 122 | | VoxelIndex max, |
| | | 123 | | SwiftList<Voxel> results, |
| | | 124 | | SwiftHashSet<Voxel> redundancy) |
| | | 125 | | { |
| | 243 | 126 | | if (Voxels == null) |
| | 1 | 127 | | return; |
| | | 128 | | |
| | 1474 | 129 | | for (long x = min.x; x <= max.x; x++) |
| | | 130 | | { |
| | 1980 | 131 | | for (long y = min.y; y <= max.y; y++) |
| | | 132 | | { |
| | 3604 | 133 | | for (long z = min.z; z <= max.z; z++) |
| | | 134 | | { |
| | 1307 | 135 | | Voxel voxel = Voxels[(int)x, (int)y, (int)z]; |
| | 1307 | 136 | | if (redundancy.Add(voxel)) |
| | 1303 | 137 | | results.Add(voxel); |
| | | 138 | | } |
| | | 139 | | } |
| | | 140 | | } |
| | 242 | 141 | | } |
| | | 142 | | |
| | | 143 | | public void AddScanCellsInRange( |
| | | 144 | | VoxelGrid grid, |
| | | 145 | | int xMin, |
| | | 146 | | int yMin, |
| | | 147 | | int zMin, |
| | | 148 | | int xMax, |
| | | 149 | | int yMax, |
| | | 150 | | int zMax, |
| | | 151 | | SwiftList<ScanCell> results, |
| | | 152 | | SwiftHashSet<ScanCell> redundancy) |
| | | 153 | | { |
| | 578 | 154 | | if (ScanCells == null) |
| | 1 | 155 | | return; |
| | | 156 | | |
| | 3444 | 157 | | for (long x = xMin; x <= xMax; x++) |
| | | 158 | | { |
| | 4588 | 159 | | for (long y = yMin; y <= yMax; y++) |
| | | 160 | | { |
| | 6852 | 161 | | for (long z = zMin; z <= zMax; z++) |
| | | 162 | | { |
| | 2277 | 163 | | int scanCellKey = grid.GetScanCellKey( |
| | 2277 | 164 | | (int)x, |
| | 2277 | 165 | | (int)y, |
| | 2277 | 166 | | (int)z); |
| | 2277 | 167 | | if (scanCellKey >= 0) |
| | | 168 | | { |
| | 2275 | 169 | | bool found = ScanCells.TryGetValue(scanCellKey, out ScanCell? scanCell); |
| | | 170 | | Debug.Assert(found); |
| | 2275 | 171 | | if (redundancy.Add(scanCell!)) |
| | 2274 | 172 | | results.Add(scanCell!); |
| | | 173 | | } |
| | | 174 | | } |
| | | 175 | | } |
| | | 176 | | } |
| | 577 | 177 | | } |
| | | 178 | | |
| | | 179 | | private void GenerateScanCells(VoxelGrid grid) |
| | | 180 | | { |
| | 457 | 181 | | ScanCells = Pools.ScanCellMapPool.Rent(); |
| | | 182 | | |
| | 2296 | 183 | | for (int x = 0; x < grid.ScanWidth; x++) |
| | | 184 | | { |
| | 2894 | 185 | | for (int y = 0; y < grid.ScanHeight; y++) |
| | | 186 | | { |
| | 5578 | 187 | | for (int z = 0; z < grid.ScanLength; z++) |
| | | 188 | | { |
| | 2033 | 189 | | int cellKey = grid.GetScanCellKey(x, y, z); |
| | | 190 | | |
| | 2033 | 191 | | ScanCell scanCell = Pools.ScanCellPool.Rent(); |
| | 2033 | 192 | | scanCell.Initialize(grid, cellKey); |
| | 2033 | 193 | | ScanCells.Add(cellKey, scanCell); |
| | | 194 | | } |
| | | 195 | | } |
| | | 196 | | } |
| | 457 | 197 | | } |
| | | 198 | | |
| | | 199 | | private void GenerateVoxels(VoxelGrid grid) |
| | | 200 | | { |
| | 457 | 201 | | Voxels = new SwiftArray3D<Voxel>(_width, _height, _length); |
| | | 202 | | |
| | 6158 | 203 | | for (int x = 0; x < _width; x++) |
| | | 204 | | { |
| | 16560 | 205 | | for (int y = 0; y < _height; y++) |
| | | 206 | | { |
| | 255400 | 207 | | for (int z = 0; z < _length; z++) |
| | | 208 | | { |
| | 122042 | 209 | | VoxelIndex index = new(x, y, z); |
| | 122042 | 210 | | Vector3d position = grid.GetWorldPosition(index); |
| | 122042 | 211 | | Voxel voxel = Pools.VoxelPool.Rent(); |
| | | 212 | | |
| | 122042 | 213 | | voxel.Initialize( |
| | 122042 | 214 | | new WorldVoxelIndex(grid.World!.SpawnToken, grid.GridIndex, grid.SpawnToken, index), |
| | 122042 | 215 | | position, |
| | 122042 | 216 | | grid.GetScanCellKey(index), |
| | 122042 | 217 | | grid.IsOnBoundary(index), |
| | 122042 | 218 | | grid.Version); |
| | | 219 | | |
| | 122042 | 220 | | Voxels[x, y, z] = voxel; |
| | | 221 | | } |
| | | 222 | | } |
| | | 223 | | } |
| | 457 | 224 | | } |
| | | 225 | | |
| | | 226 | | private void ReleaseVoxels(VoxelGrid grid) |
| | | 227 | | { |
| | 458 | 228 | | if (Voxels == null) |
| | 1 | 229 | | return; |
| | | 230 | | |
| | 6158 | 231 | | for (int x = 0; x < _width; x++) |
| | | 232 | | { |
| | 16560 | 233 | | for (int y = 0; y < _height; y++) |
| | | 234 | | { |
| | 255400 | 235 | | for (int z = 0; z < _length; z++) |
| | | 236 | | { |
| | 122042 | 237 | | Voxel voxel = Voxels[x, y, z]; |
| | 122042 | 238 | | voxel.Reset(grid); |
| | 122042 | 239 | | Pools.VoxelPool.Release(voxel); |
| | | 240 | | } |
| | | 241 | | } |
| | | 242 | | } |
| | | 243 | | |
| | 457 | 244 | | Voxels = null; |
| | 457 | 245 | | } |
| | | 246 | | |
| | | 247 | | private void ReleaseScanCells() |
| | | 248 | | { |
| | 458 | 249 | | if (ScanCells == null) |
| | 1 | 250 | | return; |
| | | 251 | | |
| | 4980 | 252 | | foreach (ScanCell cell in ScanCells.Values) |
| | 2033 | 253 | | Pools.ScanCellPool.Release(cell); |
| | | 254 | | |
| | 457 | 255 | | Pools.ScanCellMapPool.Release(ScanCells); |
| | 457 | 256 | | ScanCells = null; |
| | 457 | 257 | | } |
| | | 258 | | } |