< Summary

Information
Class: GridForge.Grids.Topology.HexPrismTopology
Assembly: GridForge
File(s): /home/runner/work/GridForge/GridForge/src/GridForge/Grids/Topology/HexPrismTopology.cs
Line coverage
100%
Covered lines: 149
Uncovered lines: 0
Coverable lines: 149
Total lines: 327
Line coverage: 100%
Branch coverage
100%
Covered branches: 70
Total branches: 70
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Kind()100%11100%
get_OverlapTolerance()100%11100%
get_MaxCellEdge()100%11100%
get_NeighborSlotCount()100%11100%
.ctor(...)100%11100%
CalculateDimensions(...)100%11100%
NormalizeBounds(...)100%44100%
IsInBounds(...)100%11100%
TryGetVoxelIndex(...)100%88100%
GetClosestVoxelIndex(...)100%11100%
GetWorldPosition(...)100%11100%
GetWorldOffset(...)100%11100%
GetNeighborOffset(...)100%11100%
TryGetNeighborSlotFromWorldDelta(...)100%88100%
IsFacingBoundary(...)100%44100%
GetBoundaryRange(...)100%11100%
FloorToGrid(...)100%11100%
CeilToGrid(...)100%11100%
SnapToScanCell(...)100%11100%
ClampToGrid(...)100%11100%
GetMaxIndex(...)100%88100%
IsInsideCoarseBounds(...)100%1010100%
FloorToLayerOrigin(...)100%11100%
CeilToLayerOrigin(...)100%11100%
TryGetNeighborSlot(...)100%44100%
NormalizePlanarOffset(...)100%2424100%

File(s)

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

#LineLine coverage
 1//=======================================================================
 2// HexPrismTopology.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.Runtime.CompilerServices;
 9using FixedMathSharp;
 10using GridForge.Spatial;
 11
 12namespace GridForge.Grids.Topology;
 13
 14internal sealed class HexPrismTopology : IGridTopology
 15{
 122816    public GridTopologyKind Kind => GridTopologyKind.HexPrism;
 17
 18    public GridTopologyMetrics Metrics { get; }
 19
 15420    public Fixed64 OverlapTolerance => MaxCellEdge * Fixed64.Half;
 21
 32122    public Fixed64 MaxCellEdge => Metrics.LargestHexEdge;
 23
 177024    public int NeighborSlotCount => HexDirectionUtility.Offsets.Length;
 25
 428326    public HexPrismTopology(GridTopologyMetrics metrics)
 27    {
 428328        Metrics = GridTopologyMetrics.Normalize(GridTopologyKind.HexPrism, metrics);
 428329    }
 30
 31    public GridDimensions CalculateDimensions(Vector3d boundsMin, Vector3d boundsMax)
 32    {
 442333        VoxelIndex maxIndex = GetMaxIndex(boundsMin, boundsMax);
 442334        int height = ((boundsMax.Y - boundsMin.Y) / Metrics.LayerHeight).FloorToInt() + 1;
 442335        return new GridDimensions(maxIndex.x + 1, height, maxIndex.z + 1);
 36    }
 37
 38    public (Vector3d min, Vector3d max) NormalizeBounds(Vector3d min, Vector3d max, Fixed64? padding = null)
 39    {
 428140        Fixed64 fixedPadding = padding.HasValue && padding.Value > Fixed64.Zero
 428141            ? padding.Value
 428142            : Fixed64.Zero;
 43
 428144        min -= fixedPadding;
 428145        max += fixedPadding;
 46
 428147        Fixed64 yMin = FloorToLayerOrigin(min.Y);
 428148        Fixed64 yMax = CeilToLayerOrigin(max.Y);
 428149        Vector3d normalizedMin = new(min.X, yMin, min.Z);
 50
 428151        VoxelIndex maxIndex = GetMaxIndex(normalizedMin, max);
 428152        int maxY = ((yMax - yMin) / Metrics.LayerHeight).FloorToInt();
 428153        Vector3d normalizedMax = normalizedMin + HexCoordinateUtility.AxialToWorldOffset(new VoxelIndex(maxIndex.x, maxY
 54
 428155        return (normalizedMin, normalizedMax);
 56    }
 57
 58    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 59    public bool IsInBounds(
 60        Vector3d boundsMin,
 61        Vector3d boundsMax,
 62        int width,
 63        int height,
 64        int length,
 65        Vector3d position) =>
 2466        TryGetVoxelIndex(boundsMin, boundsMax, width, height, length, position, out _);
 67
 68    public bool TryGetVoxelIndex(
 69        Vector3d boundsMin,
 70        Vector3d boundsMax,
 71        int width,
 72        int height,
 73        int length,
 74        Vector3d position,
 75        out VoxelIndex result)
 76    {
 7877        result = default;
 78
 7879        if (!IsInsideCoarseBounds(boundsMin, boundsMax, position))
 580            return false;
 81
 7382        HexCoordinateUtility.WorldOffsetToAxial(
 7383            position.X - boundsMin.X,
 7384            position.Z - boundsMin.Z,
 7385            Metrics,
 7386            out Fixed64 q,
 7387            out Fixed64 r);
 88
 7389        Fixed64 layer = (position.Y - boundsMin.Y) / Metrics.LayerHeight;
 7390        VoxelIndex rounded = HexCoordinateUtility.RoundAxial(q, layer, r);
 7391        if ((uint)rounded.x >= (uint)width
 7392            || (uint)rounded.y >= (uint)height
 7393            || (uint)rounded.z >= (uint)length)
 94        {
 795            return false;
 96        }
 97
 6698        result = rounded;
 6699        return true;
 100    }
 101
 102    public VoxelIndex GetClosestVoxelIndex(
 103        Vector3d boundsMin,
 104        int width,
 105        int height,
 106        int length,
 107        Vector3d position)
 108    {
 116109        HexCoordinateUtility.WorldOffsetToAxial(
 116110            position.X - boundsMin.X,
 116111            position.Z - boundsMin.Z,
 116112            Metrics,
 116113            out Fixed64 q,
 116114            out Fixed64 r);
 116115        HexCoordinateUtility.RoundCube(q, r, out int roundedQ, out int roundedR);
 116
 116117        int roundedY = ((position.Y - boundsMin.Y) / Metrics.LayerHeight).RoundToInt();
 116118        return new VoxelIndex(
 116119            FixedMath.Clamp(roundedQ, 0, width - 1),
 116120            FixedMath.Clamp(roundedY, 0, height - 1),
 116121            FixedMath.Clamp(roundedR, 0, length - 1));
 122    }
 123
 124    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 125    public Vector3d GetWorldPosition(Vector3d boundsMin, VoxelIndex index) =>
 209485126        boundsMin + HexCoordinateUtility.AxialToWorldOffset(index, Metrics);
 127
 128    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 129    public Vector3d GetWorldOffset((int x, int y, int z) offset) =>
 767130        HexCoordinateUtility.AxialToWorldOffset(new VoxelIndex(offset.x, offset.y, offset.z), Metrics);
 131
 1660132    public VoxelIndex GetNeighborOffset(int slot) => HexDirectionUtility.Offsets[slot];
 133
 134    public bool TryGetNeighborSlotFromWorldDelta(Vector3d worldDelta, out int slot)
 135    {
 42136        HexCoordinateUtility.WorldOffsetToAxial(
 42137            worldDelta.X,
 42138            worldDelta.Z,
 42139            Metrics,
 42140            out Fixed64 q,
 42141            out Fixed64 r);
 42142        HexCoordinateUtility.RoundCube(q, r, out int roundedQ, out int roundedR);
 143
 42144        int yDirection = worldDelta.Y.Sign();
 42145        if (yDirection != 0)
 146        {
 15147            if (roundedQ == 0 && roundedR == 0)
 9148                return TryGetNeighborSlot(new VoxelIndex(0, yDirection, 0), out slot);
 149
 6150            VoxelIndex planarOffset = NormalizePlanarOffset(roundedQ, roundedR);
 6151            if (planarOffset == default)
 152            {
 1153                slot = -1;
 1154                return false;
 155            }
 156
 5157            return TryGetNeighborSlot(new VoxelIndex(planarOffset.x, yDirection, planarOffset.z), out slot);
 158        }
 159
 27160        return TryGetNeighborSlot(NormalizePlanarOffset(roundedQ, roundedR), out slot);
 161    }
 162
 163    public bool IsFacingBoundary(VoxelIndex voxelIndex, int slot, int width, int height, int length)
 164    {
 4165        VoxelIndex offset = HexDirectionUtility.Offsets[slot];
 4166        return RectangularDirectionUtility.IsAxisFacingBoundary(voxelIndex.x, offset.x, width)
 4167            && RectangularDirectionUtility.IsAxisFacingBoundary(voxelIndex.y, offset.y, height)
 4168            && RectangularDirectionUtility.IsAxisFacingBoundary(voxelIndex.z, offset.z, length);
 169    }
 170
 171    public void GetBoundaryRange(
 172        int slot,
 173        int width,
 174        int height,
 175        int length,
 176        out int xStart,
 177        out int xEnd,
 178        out int yStart,
 179        out int yEnd,
 180        out int zStart,
 181        out int zEnd)
 182    {
 1183        VoxelIndex offset = HexDirectionUtility.Offsets[slot];
 1184        (xStart, xEnd) = RectangularDirectionUtility.GetBoundaryRange(offset.x, width);
 1185        (yStart, yEnd) = RectangularDirectionUtility.GetBoundaryRange(offset.y, height);
 1186        (zStart, zEnd) = RectangularDirectionUtility.GetBoundaryRange(offset.z, length);
 1187    }
 188
 189    public Vector3d FloorToGrid(
 190        Vector3d boundsMin,
 191        Vector3d boundsMax,
 192        int width,
 193        int height,
 194        int length,
 195        Vector3d position)
 196    {
 25197        VoxelIndex index = ClampToGrid(boundsMin, width, height, length, position);
 25198        return GetWorldPosition(boundsMin, index);
 199    }
 200
 201    public Vector3d CeilToGrid(
 202        Vector3d boundsMin,
 203        Vector3d boundsMax,
 204        int width,
 205        int height,
 206        int length,
 207        Vector3d position)
 208    {
 7209        VoxelIndex index = ClampToGrid(boundsMin, width, height, length, position);
 7210        return GetWorldPosition(boundsMin, index);
 211    }
 212
 213    public (int x, int y, int z) SnapToScanCell(Vector3d boundsMin, Vector3d position, int scanCellSize)
 214    {
 7215        HexCoordinateUtility.WorldOffsetToAxial(
 7216            position.X - boundsMin.X,
 7217            position.Z - boundsMin.Z,
 7218            Metrics,
 7219            out Fixed64 q,
 7220            out Fixed64 r);
 221
 7222        Fixed64 layer = (position.Y - boundsMin.Y) / Metrics.LayerHeight;
 7223        VoxelIndex index = HexCoordinateUtility.RoundAxial(q, layer, r);
 7224        return (index.x / scanCellSize, index.y / scanCellSize, index.z / scanCellSize);
 225    }
 226
 227    private VoxelIndex ClampToGrid(
 228        Vector3d boundsMin,
 229        int width,
 230        int height,
 231        int length,
 232        Vector3d position)
 233    {
 32234        HexCoordinateUtility.WorldOffsetToAxial(
 32235            position.X - boundsMin.X,
 32236            position.Z - boundsMin.Z,
 32237            Metrics,
 32238            out Fixed64 q,
 32239            out Fixed64 r);
 240
 32241        Fixed64 layer = (position.Y - boundsMin.Y) / Metrics.LayerHeight;
 32242        VoxelIndex rounded = HexCoordinateUtility.RoundAxial(q, layer, r);
 243
 32244        return new VoxelIndex(
 32245            FixedMath.Clamp(rounded.x, 0, width - 1),
 32246            FixedMath.Clamp(rounded.y, 0, height - 1),
 32247            FixedMath.Clamp(rounded.z, 0, length - 1));
 248    }
 249
 250    private VoxelIndex GetMaxIndex(Vector3d boundsMin, Vector3d boundsMax)
 251    {
 8704252        HexCoordinateUtility.WorldOffsetToAxial(
 8704253            boundsMax.X - boundsMin.X,
 8704254            boundsMax.Z - boundsMin.Z,
 8704255            Metrics,
 8704256            out Fixed64 qMax,
 8704257            out Fixed64 rMax);
 258
 259        // An odd raw width at an odd coupled index rounds its half-raw center to even.
 260        // Its continuous inverse can sit on either side of the integer address.
 261        // Preserve exact authored corners before applying outward rounding.
 8704262        HexCoordinateUtility.RoundCube(qMax, rMax, out int roundedQ, out int roundedR);
 8704263        VoxelIndex rounded = new VoxelIndex(roundedQ, 0, roundedR);
 8704264        Vector3d projected = HexCoordinateUtility.AxialToWorldOffset(rounded, Metrics);
 8704265        if (roundedQ >= 0 && roundedR >= 0
 8704266            && projected.X == boundsMax.X - boundsMin.X
 8704267            && projected.Z == boundsMax.Z - boundsMin.Z)
 268        {
 7691269            return rounded;
 270        }
 271
 1013272        int maxQ = HexCoordinateUtility.CeilToIntWithTolerance(FixedMath.Max(qMax, Fixed64.Zero));
 1013273        int maxR = HexCoordinateUtility.CeilToIntWithTolerance(FixedMath.Max(rMax, Fixed64.Zero));
 274
 1013275        return new VoxelIndex(
 1013276            maxQ,
 1013277            0,
 1013278            maxR);
 279    }
 280
 281    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 282    private bool IsInsideCoarseBounds(Vector3d boundsMin, Vector3d boundsMax, Vector3d position) =>
 78283        boundsMin.X <= position.X && position.X <= boundsMax.X
 78284        && boundsMin.Y <= position.Y && position.Y <= boundsMax.Y
 78285        && boundsMin.Z <= position.Z && position.Z <= boundsMax.Z;
 286
 287    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 288    private Fixed64 FloorToLayerOrigin(Fixed64 coordinate) =>
 4281289        (coordinate / Metrics.LayerHeight).FloorToInt() * Metrics.LayerHeight;
 290
 291    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 292    private Fixed64 CeilToLayerOrigin(Fixed64 coordinate) =>
 4281293        (coordinate / Metrics.LayerHeight).CeilToInt() * Metrics.LayerHeight;
 294
 295    private static bool TryGetNeighborSlot(VoxelIndex neighborOffset, out int slot)
 296    {
 726297        for (int i = 0; i < HexDirectionUtility.Offsets.Length; i++)
 298        {
 356299            if (HexDirectionUtility.Offsets[i] == neighborOffset)
 300            {
 34301                slot = i;
 34302                return true;
 303            }
 304        }
 305
 7306        slot = -1;
 7307        return false;
 308    }
 309
 310    private static VoxelIndex NormalizePlanarOffset(int q, int r)
 311    {
 33312        if (q > 0 && r == 0)
 13313            return new VoxelIndex(1, 0, 0);
 20314        if (q > 0 && r < 0)
 1315            return new VoxelIndex(1, 0, -1);
 19316        if (q == 0 && r < 0)
 1317            return new VoxelIndex(0, 0, -1);
 18318        if (q < 0 && r == 0)
 7319            return new VoxelIndex(-1, 0, 0);
 11320        if (q < 0 && r > 0)
 2321            return new VoxelIndex(-1, 0, 1);
 9322        if (q == 0 && r > 0)
 1323            return new VoxelIndex(0, 0, 1);
 324
 8325        return default;
 326    }
 327}

Methods/Properties

get_Kind()
get_OverlapTolerance()
get_MaxCellEdge()
get_NeighborSlotCount()
.ctor(GridForge.Grids.Topology.GridTopologyMetrics)
CalculateDimensions(FixedMathSharp.Vector3d,FixedMathSharp.Vector3d)
NormalizeBounds(FixedMathSharp.Vector3d,FixedMathSharp.Vector3d,System.Nullable`1<FixedMathSharp.Fixed64>)
IsInBounds(FixedMathSharp.Vector3d,FixedMathSharp.Vector3d,System.Int32,System.Int32,System.Int32,FixedMathSharp.Vector3d)
TryGetVoxelIndex(FixedMathSharp.Vector3d,FixedMathSharp.Vector3d,System.Int32,System.Int32,System.Int32,FixedMathSharp.Vector3d,GridForge.Spatial.VoxelIndex&)
GetClosestVoxelIndex(FixedMathSharp.Vector3d,System.Int32,System.Int32,System.Int32,FixedMathSharp.Vector3d)
GetWorldPosition(FixedMathSharp.Vector3d,GridForge.Spatial.VoxelIndex)
GetWorldOffset(System.ValueTuple`3<System.Int32,System.Int32,System.Int32>)
GetNeighborOffset(System.Int32)
TryGetNeighborSlotFromWorldDelta(FixedMathSharp.Vector3d,System.Int32&)
IsFacingBoundary(GridForge.Spatial.VoxelIndex,System.Int32,System.Int32,System.Int32,System.Int32)
GetBoundaryRange(System.Int32,System.Int32,System.Int32,System.Int32,System.Int32&,System.Int32&,System.Int32&,System.Int32&,System.Int32&,System.Int32&)
FloorToGrid(FixedMathSharp.Vector3d,FixedMathSharp.Vector3d,System.Int32,System.Int32,System.Int32,FixedMathSharp.Vector3d)
CeilToGrid(FixedMathSharp.Vector3d,FixedMathSharp.Vector3d,System.Int32,System.Int32,System.Int32,FixedMathSharp.Vector3d)
SnapToScanCell(FixedMathSharp.Vector3d,FixedMathSharp.Vector3d,System.Int32)
ClampToGrid(FixedMathSharp.Vector3d,System.Int32,System.Int32,System.Int32,FixedMathSharp.Vector3d)
GetMaxIndex(FixedMathSharp.Vector3d,FixedMathSharp.Vector3d)
IsInsideCoarseBounds(FixedMathSharp.Vector3d,FixedMathSharp.Vector3d,FixedMathSharp.Vector3d)
FloorToLayerOrigin(FixedMathSharp.Fixed64)
CeilToLayerOrigin(FixedMathSharp.Fixed64)
TryGetNeighborSlot(GridForge.Spatial.VoxelIndex,System.Int32&)
NormalizePlanarOffset(System.Int32,System.Int32)