< Summary

Information
Class: GridForge.Grids.Topology.TopologyVoxelRangeUtility
Assembly: GridForge
File(s): /home/runner/work/GridForge/GridForge/src/GridForge/Grids/Topology/TopologyVoxelRangeUtility.cs
Line coverage
100%
Covered lines: 88
Uncovered lines: 0
Coverable lines: 88
Total lines: 193
Line coverage: 100%
Branch coverage
100%
Covered branches: 16
Total branches: 16
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
TryGetCandidateRange(...)100%11100%
TryGetCandidateRange(...)100%44100%
TryGetPrismCandidateRange(...)100%22100%
TryGetRectangularCandidateRange(...)100%22100%
TryGetHexCandidateRange(...)100%22100%
TryClipBoundsToGrid(...)100%66100%
IncludeHexAxialCorner(...)100%11100%

File(s)

/home/runner/work/GridForge/GridForge/src/GridForge/Grids/Topology/TopologyVoxelRangeUtility.cs

#LineLine coverage
 1//=======================================================================
 2// TopologyVoxelRangeUtility.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.Diagnostics;
 9using System.Runtime.CompilerServices;
 10using FixedMathSharp;
 11using GridForge.Spatial;
 12
 13namespace GridForge.Grids.Topology;
 14
 15internal static class TopologyVoxelRangeUtility
 16{
 17    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 18    internal static bool TryGetCandidateRange(
 19        VoxelGrid grid,
 20        TopologyVoxelAabb bounds,
 21        out VoxelIndex minIndex,
 22        out VoxelIndex maxIndex) =>
 39823        TryGetCandidateRange(grid, bounds.Min, bounds.Max, out minIndex, out maxIndex);
 24
 25    internal static bool TryGetCandidateRange(
 26        VoxelGrid grid,
 27        Vector3d queryMin,
 28        Vector3d queryMax,
 29        out VoxelIndex minIndex,
 30        out VoxelIndex maxIndex)
 31    {
 114332        if (!grid.IsActive)
 33        {
 134            minIndex = default;
 135            maxIndex = default;
 136            return false;
 37        }
 38
 114239        return grid.Topology.Kind == GridTopologyKind.HexPrism
 114240            ? TryGetHexCandidateRange(grid, queryMin, queryMax, out minIndex, out maxIndex)
 114241            : TryGetRectangularCandidateRange(grid, queryMin, queryMax, out minIndex, out maxIndex);
 42    }
 43
 44    internal static bool TryGetPrismCandidateRange(
 45        VoxelGrid grid,
 46        Vector3d queryMin,
 47        Vector3d queryMax,
 48        out VoxelIndex minIndex,
 49        out VoxelIndex maxIndex)
 50    {
 40551        GridTopologyMetrics metrics = grid.Topology.Metrics;
 52        Vector3d rangeMin;
 53        Vector3d rangeMax;
 40554        if (grid.Topology.Kind == GridTopologyKind.RectangularPrism)
 55        {
 34356            Vector3d halfExtents = new(
 34357                metrics.CellWidth * Fixed64.Half,
 34358                metrics.LayerHeight * Fixed64.Half,
 34359                metrics.CellLength * Fixed64.Half);
 34360            rangeMin = queryMin - halfExtents;
 34361            rangeMax = queryMax + halfExtents;
 62        }
 63        else
 64        {
 6265            Fixed64 halfHeight = metrics.LayerHeight * Fixed64.Half;
 6266            rangeMin = new Vector3d(queryMin.X, queryMin.Y - halfHeight, queryMin.Z);
 6267            rangeMax = new Vector3d(queryMax.X, queryMax.Y + halfHeight, queryMax.Z);
 68        }
 69
 40570        return TryGetCandidateRange(grid, rangeMin, rangeMax, out minIndex, out maxIndex);
 71    }
 72
 73    private static bool TryGetRectangularCandidateRange(
 74        VoxelGrid grid,
 75        Vector3d queryMin,
 76        Vector3d queryMax,
 77        out VoxelIndex minIndex,
 78        out VoxelIndex maxIndex)
 79    {
 97080        minIndex = default;
 97081        maxIndex = default;
 82
 97083        (Vector3d snappedMin, Vector3d snappedMax) = grid.NormalizeBounds(queryMin, queryMax);
 97084        if (!TryClipBoundsToGrid(grid, snappedMin, snappedMax, out Vector3d clippedMin, out Vector3d clippedMax))
 585            return false;
 86
 96587        bool minResolved = grid.TryGetVoxelIndex(clippedMin, out minIndex);
 96588        bool maxResolved = grid.TryGetVoxelIndex(clippedMax, out maxIndex);
 89        Debug.Assert(minResolved && maxResolved);
 96590        return true;
 91    }
 92
 93    private static bool TryGetHexCandidateRange(
 94        VoxelGrid grid,
 95        Vector3d queryMin,
 96        Vector3d queryMax,
 97        out VoxelIndex minIndex,
 98        out VoxelIndex maxIndex)
 99    {
 172100        minIndex = default;
 172101        maxIndex = default;
 102
 172103        GridTopologyMetrics metrics = grid.Topology.Metrics;
 172104        Fixed64 horizontalExpansion = metrics.CellRadius;
 172105        Fixed64 layerHeight = metrics.LayerHeight;
 172106        Vector3d candidateMin = new(
 172107            queryMin.X - horizontalExpansion,
 172108            queryMin.Y,
 172109            queryMin.Z - horizontalExpansion);
 172110        Vector3d candidateMax = new(
 172111            queryMax.X + horizontalExpansion,
 172112            queryMax.Y,
 172113            queryMax.Z + horizontalExpansion);
 114
 172115        if (!TryClipBoundsToGrid(grid, candidateMin, candidateMax, out Vector3d clippedMin, out Vector3d clippedMax))
 4116            return false;
 117
 168118        Fixed64 qMin = Fixed64.MaxValue;
 168119        Fixed64 qMax = Fixed64.MinValue;
 168120        Fixed64 rMin = Fixed64.MaxValue;
 168121        Fixed64 rMax = Fixed64.MinValue;
 122
 168123        IncludeHexAxialCorner(grid.BoundsMin, metrics, clippedMin.X, clippedMin.Z, ref qMin, ref qMax, ref rMin, ref rMa
 168124        IncludeHexAxialCorner(grid.BoundsMin, metrics, clippedMin.X, clippedMax.Z, ref qMin, ref qMax, ref rMin, ref rMa
 168125        IncludeHexAxialCorner(grid.BoundsMin, metrics, clippedMax.X, clippedMin.Z, ref qMin, ref qMax, ref rMin, ref rMa
 168126        IncludeHexAxialCorner(grid.BoundsMin, metrics, clippedMax.X, clippedMax.Z, ref qMin, ref qMax, ref rMin, ref rMa
 127
 168128        int xMin = System.Math.Max(0, qMin.FloorToInt() - 1);
 168129        int xMax = System.Math.Min(grid.Width - 1, qMax.CeilToInt() + 1);
 168130        int zMin = System.Math.Max(0, rMin.FloorToInt() - 1);
 168131        int zMax = System.Math.Min(grid.Length - 1, rMax.CeilToInt() + 1);
 168132        int yMin = System.Math.Max(
 168133            0,
 168134            ((clippedMin.Y - grid.BoundsMin.Y) / layerHeight).FloorToInt());
 168135        int yMax = System.Math.Min(
 168136            grid.Height - 1,
 168137            ((clippedMax.Y - grid.BoundsMin.Y) / layerHeight).CeilToInt());
 138
 168139        minIndex = new VoxelIndex(xMin, yMin, zMin);
 168140        maxIndex = new VoxelIndex(xMax, yMax, zMax);
 168141        return true;
 142    }
 143
 144    internal static bool TryClipBoundsToGrid(
 145        VoxelGrid grid,
 146        Vector3d min,
 147        Vector3d max,
 148        out Vector3d clippedMin,
 149        out Vector3d clippedMax)
 150    {
 1768151        Fixed64 xMin = FixedMath.Max(min.X, grid.BoundsMin.X);
 1768152        Fixed64 yMin = FixedMath.Max(min.Y, grid.BoundsMin.Y);
 1768153        Fixed64 zMin = FixedMath.Max(min.Z, grid.BoundsMin.Z);
 1768154        Fixed64 xMax = FixedMath.Min(max.X, grid.BoundsMax.X);
 1768155        Fixed64 yMax = FixedMath.Min(max.Y, grid.BoundsMax.Y);
 1768156        Fixed64 zMax = FixedMath.Min(max.Z, grid.BoundsMax.Z);
 157
 1768158        if (xMin > xMax || yMin > yMax || zMin > zMax)
 159        {
 12160            clippedMin = default;
 12161            clippedMax = default;
 12162            return false;
 163        }
 164
 1756165        clippedMin = new Vector3d(xMin, yMin, zMin);
 1756166        clippedMax = new Vector3d(xMax, yMax, zMax);
 1756167        return true;
 168    }
 169
 170    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 171    private static void IncludeHexAxialCorner(
 172        Vector3d gridBoundsMin,
 173        GridTopologyMetrics metrics,
 174        Fixed64 x,
 175        Fixed64 z,
 176        ref Fixed64 qMin,
 177        ref Fixed64 qMax,
 178        ref Fixed64 rMin,
 179        ref Fixed64 rMax)
 180    {
 672181        HexCoordinateUtility.WorldOffsetToAxial(
 672182            x - gridBoundsMin.X,
 672183            z - gridBoundsMin.Z,
 672184            metrics,
 672185            out Fixed64 q,
 672186            out Fixed64 r);
 187
 672188        qMin = FixedMath.Min(qMin, q);
 672189        qMax = FixedMath.Max(qMax, q);
 672190        rMin = FixedMath.Min(rMin, r);
 672191        rMax = FixedMath.Max(rMax, r);
 672192    }
 193}