< Summary

Information
Class: GridForge.Utility.GridTraversalState
Assembly: GridForge
File(s): /home/runner/work/GridForge/GridForge/src/GridForge/Utility/GridTraversal.cs
Line coverage
95%
Covered lines: 19
Uncovered lines: 1
Coverable lines: 20
Total lines: 144
Line coverage: 95%
Branch coverage
75%
Covered branches: 6
Total branches: 8
Branch coverage: 75%
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%22100%
GetCellEdge(...)66.66%6688.88%

File(s)

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

#LineLine coverage
 1//=======================================================================
 2// GridTraversal.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.Grids;
 10using GridForge.Grids.Topology;
 11using GridForge.Spatial;
 12using SwiftCollections;
 13using System.Runtime.CompilerServices;
 14
 15namespace GridForge.Utility;
 16
 17/// <summary>
 18/// Selects which topology edge measurement a grid traversal should use for padding.
 19/// </summary>
 20public enum GridTraversalPaddingMode
 21{
 22    /// <summary>
 23    /// Use the largest three-dimensional cell edge.
 24    /// </summary>
 25    MaxCellEdge,
 26
 27    /// <summary>
 28    /// Use the largest X/Z-plane cell edge.
 29    /// </summary>
 30    PlanarMaxCellEdge
 31}
 32
 33/// <summary>
 34/// Tracks per-grid traversal padding while suppressing duplicate voxel visits.
 35/// </summary>
 36public struct GridTraversalState
 37{
 38    private readonly GridWorld _world;
 39    private readonly GridTraversalPaddingMode _paddingMode;
 40    private ushort _currentGridIndex;
 41    private Fixed64 _cellEdge;
 42    private bool _hasCachedGrid;
 43
 44    /// <summary>
 45    /// Initializes traversal state for one world and padding mode.
 46    /// </summary>
 47    public GridTraversalState(GridWorld world, GridTraversalPaddingMode paddingMode)
 48    {
 249        _world = world;
 250        _paddingMode = paddingMode;
 251        _currentGridIndex = 0;
 252        _cellEdge = Fixed64.Zero;
 253        _hasCachedGrid = false;
 254    }
 55
 56    /// <summary>
 57    /// Visits a voxel only once and returns the selected cell-edge measurement for its grid.
 58    /// </summary>
 59    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 60    public bool TryVisitUnique(Voxel voxel, SwiftHashSet<int> visited, out Fixed64 cellEdge)
 61    {
 362        cellEdge = Fixed64.Zero;
 363        if (!visited.Add(voxel.SpawnToken))
 164            return false;
 65
 266        cellEdge = GetCellEdge(voxel);
 267        return true;
 68    }
 69
 70    /// <summary>
 71    /// Gets the selected cell-edge measurement for a voxel's grid, caching repeated grid lookups.
 72    /// </summary>
 73    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 74    public Fixed64 GetCellEdge(Voxel voxel)
 75    {
 276        if (_hasCachedGrid && voxel.GridIndex == _currentGridIndex)
 077            return _cellEdge;
 78
 279        _currentGridIndex = voxel.GridIndex;
 280        _hasCachedGrid = true;
 281        VoxelGrid grid = _world.ActiveGrids[_currentGridIndex];
 282        _cellEdge = _paddingMode == GridTraversalPaddingMode.PlanarMaxCellEdge
 283            ? GridTopologyMetricUtility.GetPlanarMaxCellEdge(grid)
 284            : GridTopologyMetricUtility.GetMaxCellEdge(grid);
 285        return _cellEdge;
 86    }
 87}
 88
 89/// <summary>
 90/// Provides deterministic helpers for duplicate-safe voxel traversal.
 91/// </summary>
 92public static class GridTraversal
 93{
 94    /// <summary>
 95    /// Gets a voxel partition once per voxel spawn token.
 96    /// </summary>
 97    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 98    public static bool TryGetUniquePartition<TPartition>(
 99        Voxel voxel,
 100        SwiftHashSet<int> visited,
 101        out TPartition? partition)
 102        where TPartition : class, IVoxelPartition
 103    {
 104        partition = null;
 105        return visited.Add(voxel.SpawnToken)
 106            && voxel.TryGetPartition(out partition);
 107    }
 108
 109    /// <summary>
 110    /// Tests whether a 3D world position lies inside bounds expanded by half of a cell edge.
 111    /// </summary>
 112    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 113    public static bool IsWorldPositionInPaddedBounds(
 114        Vector3d min,
 115        Vector3d max,
 116        Fixed64 cellEdge,
 117        Vector3d worldPosition)
 118    {
 119        Fixed64 padding = cellEdge * Fixed64.Half;
 120        return worldPosition.X >= min.X - padding
 121            && worldPosition.X <= max.X + padding
 122            && worldPosition.Y >= min.Y - padding
 123            && worldPosition.Y <= max.Y + padding
 124            && worldPosition.Z >= min.Z - padding
 125            && worldPosition.Z <= max.Z + padding;
 126    }
 127
 128    /// <summary>
 129    /// Tests whether a world position's X/Z projection lies inside bounds expanded by half of a cell edge.
 130    /// </summary>
 131    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 132    public static bool IsPlanarPositionInPaddedBounds(
 133        Vector2d min,
 134        Vector2d max,
 135        Fixed64 cellEdge,
 136        Vector3d worldPosition)
 137    {
 138        Fixed64 padding = cellEdge * Fixed64.Half;
 139        return worldPosition.X >= min.X - padding
 140            && worldPosition.X <= max.X + padding
 141            && worldPosition.Z >= min.Y - padding
 142            && worldPosition.Z <= max.Y + padding;
 143    }
 144}