< Summary

Information
Class: GridForge.Spatial.HexCoordinateUtility
Assembly: GridForge
File(s): /home/runner/work/GridForge/GridForge/src/GridForge/Spatial/HexCoordinateUtility.cs
Line coverage
100%
Covered lines: 44
Uncovered lines: 0
Coverable lines: 44
Total lines: 110
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
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%
AxialToWorldOffset(...)100%22100%
WorldOffsetToAxial(...)100%22100%
RoundAxial(...)100%11100%
RoundCube(...)100%66100%
CeilToIntWithTolerance(...)100%22100%
RoundToInt(...)100%11100%

File(s)

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

#LineLine coverage
 1//=======================================================================
 2// HexCoordinateUtility.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.Topology;
 12
 13namespace GridForge.Spatial;
 14
 15internal static class HexCoordinateUtility
 16{
 17    internal const long Sqrt3Raw = 7439101574L;
 18
 119    internal static readonly Fixed64 Sqrt3 = Fixed64.FromRaw(Sqrt3Raw);
 20
 121    private static readonly Fixed64 RawRoundingTolerance = Fixed64.FromRaw(4096);
 22
 23    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 24    internal static Vector3d AxialToWorldOffset(VoxelIndex index, GridTopologyMetrics metrics)
 25    {
 22760126        Fixed64 q = new(index.x);
 22760127        Fixed64 r = new(index.z);
 22760128        Fixed64 y = index.y * metrics.LayerHeight;
 29
 22760130        if (metrics.HexOrientation == HexOrientation.FlatTop)
 31        {
 11340132            return new Vector3d(
 11340133                metrics.CellRadius * Fixed64.Three * Fixed64.Half * q,
 11340134                y,
 11340135                metrics.CellRadius * Sqrt3 * (r + q * Fixed64.Half));
 36        }
 37
 11420038        return new Vector3d(
 11420039            metrics.CellRadius * Sqrt3 * (q + r * Fixed64.Half),
 11420040            y,
 11420041            metrics.CellRadius * Fixed64.Three * Fixed64.Half * r);
 42    }
 43
 44    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 45    internal static void WorldOffsetToAxial(
 46        Fixed64 worldX,
 47        Fixed64 worldZ,
 48        GridTopologyMetrics metrics,
 49        out Fixed64 q,
 50        out Fixed64 r)
 51    {
 52        // Invert the same quantized basis used by AxialToWorldOffset. Using the
 53        // ideal irrational inverse here introduces an error proportional to the
 54        // address, especially when a radius is only a few raw units.
 1278855        Fixed64 width = metrics.CellRadius * Sqrt3;
 1278856        Fixed64 rowStep = metrics.CellRadius * Fixed64.Three * Fixed64.Half;
 1278857        if (metrics.HexOrientation == HexOrientation.FlatTop)
 58        {
 616959            q = worldX / rowStep;
 616960            r = worldZ / width - q * Fixed64.Half;
 616961            return;
 62        }
 63
 661964        r = worldZ / rowStep;
 661965        q = worldX / width - r * Fixed64.Half;
 661966    }
 67
 68    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 69    internal static VoxelIndex RoundAxial(Fixed64 q, Fixed64 y, Fixed64 r)
 70    {
 328771        RoundCube(q, r, out int roundedQ, out int roundedR);
 328772        return new VoxelIndex(roundedQ, y.FloorToInt(), roundedR);
 73    }
 74
 75    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 76    internal static void RoundCube(Fixed64 q, Fixed64 r, out int roundedQ, out int roundedR)
 77    {
 1215178        Fixed64 s = -q - r;
 79
 1215180        int qCandidate = RoundToInt(q);
 1215181        int rCandidate = RoundToInt(r);
 1215182        int sCandidate = RoundToInt(s);
 83
 1215184        Fixed64 qDiff = (new Fixed64(qCandidate) - q).Abs();
 1215185        Fixed64 rDiff = (new Fixed64(rCandidate) - r).Abs();
 1215186        Fixed64 sDiff = (new Fixed64(sCandidate) - s).Abs();
 87
 1215188        if (qDiff > rDiff && qDiff > sDiff)
 30689            qCandidate = -rCandidate - sCandidate;
 1184590        else if (rDiff > sDiff)
 35191            rCandidate = -qCandidate - sCandidate;
 92
 1215193        roundedQ = qCandidate;
 1215194        roundedR = rCandidate;
 1215195    }
 96
 97    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 98    internal static int CeilToIntWithTolerance(Fixed64 value)
 99    {
 2027100        int rounded = RoundToInt(value);
 2027101        if ((new Fixed64(rounded) - value).Abs() <= RawRoundingTolerance)
 108102            return rounded;
 103
 1919104        return value.CeilToInt();
 105    }
 106
 107    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 108    private static int RoundToInt(Fixed64 value) =>
 38480109        (int)(FixedMath.Round(value, MidpointRounding.AwayFromZero).m_rawValue >> FixedMath.SHIFT_AMOUNT_I);
 110}