< Summary

Information
Class: GridForge.Utility.GridTraversalState
Assembly: GridForge
File(s): /home/runner/work/GridForge/GridForge/src/GridForge/Utility/GridTraversalState.cs
Line coverage
100%
Covered lines: 47
Uncovered lines: 0
Coverable lines: 47
Total lines: 122
Line coverage: 100%
Branch coverage
100%
Covered branches: 30
Total branches: 30
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
TryVisitUnique(...)100%44100%
GetCellEdge(...)100%11100%
GetCellEdge(...)100%88100%
TryResolveGrid(...)100%1818100%

File(s)

/home/runner/work/GridForge/GridForge/src/GridForge/Utility/GridTraversalState.cs

#LineLine coverage
 1//=======================================================================
 2// GridTraversalState.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 System;
 9using System.Runtime.CompilerServices;
 10using FixedMathSharp;
 11using GridForge.Grids;
 12using GridForge.Grids.Topology;
 13using GridForge.Spatial;
 14using SwiftCollections;
 15
 16namespace GridForge.Utility;
 17
 18/// <summary>
 19/// Tracks per-grid traversal padding while suppressing duplicate voxel visits.
 20/// </summary>
 21public struct GridTraversalState
 22{
 23    private readonly GridWorld _world;
 24    private readonly GridTraversalPaddingMode _paddingMode;
 25    private ushort _currentGridIndex;
 26    private long _currentGridSpawnToken;
 27    private VoxelGrid? _currentGrid;
 28    private Fixed64 _cellEdge;
 29    private bool _hasCachedGrid;
 30
 31    /// <summary>
 32    /// Initializes traversal state for one world and padding mode.
 33    /// </summary>
 34    public GridTraversalState(GridWorld world, GridTraversalPaddingMode paddingMode)
 35    {
 104536        _world = world;
 104537        _paddingMode = paddingMode;
 104538        _currentGridIndex = 0;
 104539        _currentGridSpawnToken = 0;
 104540        _currentGrid = null;
 104541        _cellEdge = Fixed64.Zero;
 104542        _hasCachedGrid = false;
 104543    }
 44
 45    /// <summary>
 46    /// Visits a voxel only once and returns the selected cell-edge measurement for its grid.
 47    /// </summary>
 48    /// <returns>True when the voxel belongs to an active grid generation and was not already visited; otherwise false.<
 49    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 50    public bool TryVisitUnique(Voxel voxel, SwiftHashSet<WorldVoxelIndex> visited, out Fixed64 cellEdge)
 51    {
 208952        cellEdge = Fixed64.Zero;
 208953        WorldVoxelIndex voxelIndex = voxel.WorldIndex;
 208954        if (!visited.Add(voxelIndex))
 104255            return false;
 56
 104757        if (!TryResolveGrid(voxelIndex, out VoxelGrid? grid))
 58        {
 159            visited.Remove(voxelIndex);
 160            return false;
 61        }
 62
 104663        cellEdge = GetCellEdge(grid!);
 104664        return true;
 65    }
 66
 67    /// <summary>
 68    /// Gets the selected cell-edge measurement for a voxel's grid, caching repeated grid lookups.
 69    /// </summary>
 70    /// <exception cref="InvalidOperationException">The voxel does not belong to an active grid generation in this trave
 71    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 72    public Fixed64 GetCellEdge(Voxel voxel)
 73    {
 274        bool isCurrent = TryResolveGrid(voxel.WorldIndex, out VoxelGrid? grid);
 275        ChronicleGuard.ThrowIfTrue(
 276            !isCurrent,
 277            nameof(voxel),
 278            "The voxel does not belong to an active grid generation in this traversal's world.");
 79
 180        return GetCellEdge(grid!);
 81    }
 82
 83    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 84    private Fixed64 GetCellEdge(VoxelGrid grid)
 85    {
 104786        if (_hasCachedGrid
 104787            && grid.GridIndex == _currentGridIndex
 104788            && grid.SpawnToken == _currentGridSpawnToken)
 89        {
 190            return _cellEdge;
 91        }
 92
 104693        _currentGridIndex = grid.GridIndex;
 104694        _currentGridSpawnToken = grid.SpawnToken;
 104695        _currentGrid = grid;
 104696        _hasCachedGrid = true;
 104697        _cellEdge = _paddingMode == GridTraversalPaddingMode.PlanarMaxCellEdge
 104698            ? GridTopologyMetricUtility.GetPlanarMaxCellEdge(grid)
 104699            : GridTopologyMetricUtility.GetMaxCellEdge(grid);
 1046100        return _cellEdge;
 101    }
 102
 103    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 104    private bool TryResolveGrid(WorldVoxelIndex voxelIndex, out VoxelGrid? grid)
 105    {
 1049106        grid = _currentGrid;
 1049107        if (_hasCachedGrid
 1049108            && voxelIndex.WorldSpawnToken == _world.SpawnToken
 1049109            && voxelIndex.GridIndex == _currentGridIndex
 1049110            && voxelIndex.GridSpawnToken == _currentGridSpawnToken
 1049111            && grid != null
 1049112            && grid.IsActive
 1049113            && ReferenceEquals(grid.World, _world)
 1049114            && grid.GridIndex == _currentGridIndex
 1049115            && grid.SpawnToken == _currentGridSpawnToken)
 116        {
 1117            return true;
 118        }
 119
 1048120        return _world.TryGetGrid(voxelIndex, out grid);
 121    }
 122}