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