< Summary

Information
Class: GridForge.Spatial.SpatialAwareness
Assembly: GridForge
File(s): /home/runner/work/GridForge/GridForge/src/GridForge/Spatial/SpatialAwareness.cs
Line coverage
100%
Covered lines: 56
Uncovered lines: 0
Coverable lines: 56
Total lines: 122
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
IsPerpendicularNeighbor(...)100%11100%
IsDiagonalNeighbor(...)100%11100%
GetBoundaryRange(...)100%44100%
IsAxisFacingBoundary(...)100%44100%

File(s)

/home/runner/work/GridForge/GridForge/src/GridForge/Spatial/SpatialAwareness.cs

#LineLine coverage
 1using System;
 2using System.Linq;
 3using System.Runtime.CompilerServices;
 4
 5namespace GridForge.Spatial;
 6
 7/// <summary>
 8/// Provides global spatial awareness utilities to find neighboring grids or voxels
 9/// </summary>
 10public static class SpatialAwareness
 11{
 12    #region Fields & Properties
 13
 14    /// <summary>
 15    /// Predefined offsets for a 3x3x3 neighbor structure, excluding the center position.
 16    /// </summary>
 217    public static readonly (int x, int y, int z)[] DirectionOffsets = new (int x, int y, int z)[26]
 218    {
 219        (-1, 0, 0),
 220        (0, 0, -1),
 221        (1, 0, 0),
 222        (0, 0, 1),
 223        (0, -1, 0),
 224        (0, 1, 0),
 225        (-1, 0, -1),
 226        (-1, 0, 1),
 227        (1, 0, -1),
 228        (1, 0, 1),
 229        (-1, -1, 0),
 230        (0, -1, -1),
 231        (1, -1, 0),
 232        (0, -1, 1),
 233        (-1, 1, 0),
 234        (0, 1, -1),
 235        (1, 1, 0),
 236        (0, 1, 1),
 237        (-1, -1, -1),
 238        (-1, -1, 1),
 239        (1, -1, -1),
 240        (1, -1, 1),
 241        (-1, 1, -1),
 242        (-1, 1, 1),
 243        (1, 1, -1),
 244        (1, 1, 1)
 245    };
 46
 47    /// <summary>
 48    /// All 26 neighbor directions excluding None.
 49    /// </summary>
 250    public static readonly SpatialDirection[] AllDirections =
 251        Enum.GetValues(typeof(SpatialDirection))
 252            .Cast<SpatialDirection>()
 5453            .Where(d => d != SpatialDirection.None)
 254            .ToArray();
 55
 56    /// <summary>
 57    /// All 6 perpendicular neighbor directions excluding None.
 58    /// </summary>
 259    public static readonly SpatialDirection[] PerpendicularDirections
 260      = AllDirections
 261          .Where(IsPerpendicularNeighbor)
 262          .ToArray();
 63
 64    /// <summary>
 65    /// All 20 perpendicular neighbor directions excluding None.
 66    /// </summary>
 267    public static readonly SpatialDirection[] DiagonalDirections
 268      = AllDirections
 269          .Where(IsDiagonalNeighbor)
 270          .ToArray();
 71
 72    #endregion
 73
 74    #region Methods
 75
 76    /// <summary>True for pure axis-aligned directions.</summary>
 77    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 5278    public static bool IsPerpendicularNeighbor(SpatialDirection dir) => (int)dir < 6;
 79
 80    /// <summary>True for any diagonal step (multiple axes).</summary>
 81    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 5282    public static bool IsDiagonalNeighbor(SpatialDirection dir) => (int)dir >= 6;
 83
 84    /// <summary>
 85    /// Gets the boundary range for a given offset and size,
 86    /// returning (0, 0) for negative offsets, (size-1, size-1) for positive offsets,
 87    /// and (0, size-1) for zero offsets.
 88    /// </summary>
 89    /// <param name="offset">The offset value.</param>
 90    /// <param name="size">The size of the dimension.</param>
 91    /// <returns>A tuple representing the start and end of the boundary range.</returns>
 92    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 93    public static (int start, int end) GetBoundaryRange(int offset, int size)
 94    {
 11195        return offset switch
 11196        {
 2197            < 0 => (0, 0),
 3198            > 0 => (size - 1, size - 1),
 5999            _ => (0, size - 1)
 111100        };
 101    }
 102
 103    /// <summary>
 104    /// Determines if a coordinate is facing the boundary of an axis given an offset and size.
 105    /// </summary>
 106    /// <param name="coordinate">The coordinate value.</param>
 107    /// <param name="offset">The offset value.</param>
 108    /// <param name="size">The size of the dimension.</param>
 109    /// <returns>True if the coordinate is facing the boundary, otherwise false.</returns>
 110    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 111    public static bool IsAxisFacingBoundary(int coordinate, int offset, int size)
 112    {
 120113        return offset switch
 120114        {
 41115            < 0 => coordinate == 0,
 41116            > 0 => coordinate == size - 1,
 38117            _ => true
 120118        };
 119    }
 120
 121    #endregion
 122}