< Summary

Information
Class: GridForge.Grids.Storage.DenseVoxelGridStorage
Assembly: GridForge
File(s): /home/runner/work/GridForge/GridForge/src/GridForge/Grids/Storage/DenseVoxelGridStorage.cs
Line coverage
98%
Covered lines: 97
Uncovered lines: 1
Coverable lines: 98
Total lines: 253
Line coverage: 98.9%
Branch coverage
97%
Covered branches: 66
Total branches: 68
Branch coverage: 97%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Kind()100%11100%
Initialize(...)100%11100%
Reset(...)100%11100%
TryGetVoxel(...)100%11100%
TryGetClosestVoxel(...)100%11100%
TryGetScanCell(...)100%22100%
EnumerateVoxels()100%88100%
VisitVoxels(...)90%101087.5%
AddVoxelsInIndexRange(...)100%1010100%
AddScanCellsInRange(...)92.85%1414100%
GenerateScanCells(...)100%66100%
GenerateVoxels(...)100%66100%
ReleaseVoxels(...)100%88100%
ReleaseScanCells()100%44100%

File(s)

/home/runner/work/GridForge/GridForge/src/GridForge/Grids/Storage/DenseVoxelGridStorage.cs

#LineLine coverage
 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
 8using FixedMathSharp;
 9using GridForge.Spatial;
 10using SwiftCollections;
 11using SwiftCollections.Dimensions;
 12using System.Collections.Generic;
 13using System.Runtime.CompilerServices;
 14
 15namespace GridForge.Grids.Storage;
 16
 17internal sealed class DenseVoxelGridStorage : IVoxelGridStorage
 18{
 19    public GridStorageKind Kind
 20    {
 21        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 860522        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    {
 34437        _width = grid.Width;
 34438        _height = grid.Height;
 34439        _length = grid.Length;
 34440        ConfiguredVoxelCount = grid.Size;
 41
 34442        GenerateScanCells(grid);
 34443        GenerateVoxels(grid);
 34444    }
 45
 46    public void Reset(VoxelGrid grid)
 47    {
 34548        ReleaseVoxels(grid);
 34549        ReleaseScanCells();
 50
 34551        ConfiguredVoxelCount = 0;
 34552        _width = 0;
 34553        _height = 0;
 34554        _length = 0;
 34555    }
 56
 57    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 58    public bool TryGetVoxel(int x, int y, int z, out Voxel? result)
 59    {
 122560        result = Voxels![x, y, z];
 122561        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    {
 1371        result = Voxels![closestIndex.x, closestIndex.y, closestIndex.z];
 1372        distanceSquared = (result!.WorldPosition - position).MagnitudeSquared;
 1373        return true;
 74    }
 75
 76    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 77    public bool TryGetScanCell(int key, out ScanCell? result)
 78    {
 60279        result = null;
 60280        return ScanCells?.TryGetValue(key, out result) == true;
 81    }
 82
 83    public IEnumerable<Voxel> EnumerateVoxels()
 84    {
 285        if (Voxels == null)
 186            yield break;
 87
 688        for (int x = 0; x < _width; x++)
 89        {
 1290            for (int y = 0; y < _height; y++)
 91            {
 2492                for (int z = 0; z < _length; z++)
 93                {
 894                    yield return Voxels[x, y, z];
 95                }
 96            }
 97        }
 198    }
 99
 100    public void VisitVoxels<TVisitor>(ref TVisitor visitor)
 101        where TVisitor : struct, IVoxelStorageVisitor
 102    {
 49103        if (Voxels == null)
 0104            return;
 105
 1228106        for (int x = 0; x < _width; x++)
 107        {
 2336108            for (int y = 0; y < _height; y++)
 109            {
 18544110                for (int z = 0; z < _length; z++)
 111                {
 8671112                    if (!visitor.Visit(Voxels[x, y, z]))
 2113                        return;
 114                }
 115            }
 116        }
 47117    }
 118
 119    public void AddVoxelsInIndexRange(
 120        VoxelIndex min,
 121        VoxelIndex max,
 122        SwiftList<Voxel> results,
 123        SwiftHashSet<Voxel> redundancy)
 124    {
 198125        if (Voxels == null)
 1126            return;
 127
 1214128        for (int x = min.x; x <= max.x; x++)
 129        {
 1640130            for (int y = min.y; y <= max.y; y++)
 131            {
 3076132                for (int z = min.z; z <= max.z; z++)
 133                {
 1128134                    Voxel voxel = Voxels[x, y, z];
 1128135                    if (redundancy.Add(voxel))
 1124136                        results.Add(voxel);
 137                }
 138            }
 139        }
 197140    }
 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    {
 543153        if (ScanCells == null)
 1154            return;
 155
 3262156        for (int x = xMin; x <= xMax; x++)
 157        {
 4364158            for (int y = yMin; y <= yMax; y++)
 159            {
 6580160                for (int z = zMin; z <= zMax; z++)
 161                {
 2197162                    int scanCellKey = grid.GetScanCellKey(x, y, z);
 2197163                    if (scanCellKey >= 0
 2197164                        && ScanCells.TryGetValue(scanCellKey, out ScanCell? scanCell)
 2197165                        && redundancy.Add(scanCell))
 166                    {
 2195167                        results.Add(scanCell!);
 168                    }
 169                }
 170            }
 171        }
 542172    }
 173
 174    private void GenerateScanCells(VoxelGrid grid)
 175    {
 344176        ScanCells = Pools.ScanCellMapPool.Rent();
 177
 1812178        for (int x = 0; x < grid.ScanWidth; x++)
 179        {
 2308180            for (int y = 0; y < grid.ScanHeight; y++)
 181            {
 4718182                for (int z = 0; z < grid.ScanLength; z++)
 183                {
 1767184                    int cellKey = grid.GetScanCellKey(x, y, z);
 185
 1767186                    ScanCell scanCell = Pools.ScanCellPool.Rent();
 1767187                    scanCell.Initialize(grid.World!, grid.GridIndex, cellKey);
 1767188                    ScanCells.Add(cellKey, scanCell);
 189                }
 190            }
 191        }
 344192    }
 193
 194    private void GenerateVoxels(VoxelGrid grid)
 195    {
 344196        Voxels = new SwiftArray3D<Voxel>(_width, _height, _length);
 197
 5408198        for (int x = 0; x < _width; x++)
 199        {
 12548200            for (int y = 0; y < _height; y++)
 201            {
 199384202                for (int z = 0; z < _length; z++)
 203                {
 95778204                    VoxelIndex index = new(x, y, z);
 95778205                    Vector3d position = grid.GetWorldPosition(index);
 95778206                    Voxel voxel = Pools.VoxelPool.Rent();
 207
 95778208                    voxel.Initialize(
 95778209                        new WorldVoxelIndex(grid.World!.SpawnToken, grid.GridIndex, grid.SpawnToken, index),
 95778210                        position,
 95778211                        grid.GetScanCellKey(index),
 95778212                        grid.IsOnBoundary(index),
 95778213                        grid.Version);
 214
 95778215                    Voxels[x, y, z] = voxel;
 216                }
 217            }
 218        }
 344219    }
 220
 221    private void ReleaseVoxels(VoxelGrid grid)
 222    {
 345223        if (Voxels == null)
 1224            return;
 225
 5408226        for (int x = 0; x < _width; x++)
 227        {
 12548228            for (int y = 0; y < _height; y++)
 229            {
 199384230                for (int z = 0; z < _length; z++)
 231                {
 95778232                    Voxel voxel = Voxels[x, y, z];
 95778233                    voxel.Reset(grid);
 95778234                    Pools.VoxelPool.Release(voxel);
 235                }
 236            }
 237        }
 238
 344239        Voxels = null;
 344240    }
 241
 242    private void ReleaseScanCells()
 243    {
 345244        if (ScanCells == null)
 1245            return;
 246
 4222247        foreach (ScanCell cell in ScanCells.Values)
 1767248            Pools.ScanCellPool.Release(cell);
 249
 344250        Pools.ScanCellMapPool.Release(ScanCells);
 344251        ScanCells = null;
 344252    }
 253}