< Summary

Information
Class: GridForge.Grids.Topology.RectangularPrismTopology
Assembly: GridForge
File(s): /home/runner/work/GridForge/GridForge/src/GridForge/Grids/Topology/RectangularPrismTopology.cs
Line coverage
100%
Covered lines: 79
Uncovered lines: 0
Coverable lines: 79
Total lines: 212
Line coverage: 100%
Branch coverage
100%
Covered branches: 26
Total branches: 26
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%1010100%
IsInBounds(...)100%1010100%
TryGetVoxelIndex(...)100%22100%
GetClosestVoxelIndex(...)100%11100%
GetWorldPosition(...)100%11100%
GetWorldOffset(...)100%11100%
GetNeighborOffset(...)100%11100%
TryGetNeighborSlotFromWorldDelta(...)100%11100%
IsFacingBoundary(...)100%44100%
GetBoundaryRange(...)100%11100%
FloorToGrid(...)100%11100%
CeilToGrid(...)100%11100%
SnapToScanCell(...)100%11100%
FloorToCellOrigin(...)100%11100%
CeilToCellOrigin(...)100%11100%
FloorToCellOrigin(...)100%11100%
CeilToCellOrigin(...)100%11100%
GetClosestAxisIndex(...)100%11100%

File(s)

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

#LineLine coverage
 1//=======================================================================
 2// RectangularPrismTopology.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.Spatial;
 10using System.Runtime.CompilerServices;
 11
 12namespace GridForge.Grids.Topology;
 13
 14internal sealed class RectangularPrismTopology : IGridTopology
 15{
 138316    public GridTopologyKind Kind => GridTopologyKind.RectangularPrism;
 17
 18    public GridTopologyMetrics Metrics { get; }
 19
 6720    public Fixed64 OverlapTolerance => Metrics.SmallestRectangularEdge * Fixed64.Half;
 21
 38722    public Fixed64 MaxCellEdge => Metrics.LargestRectangularEdge;
 23
 57924    public int NeighborSlotCount => RectangularDirectionUtility.Offsets.Length;
 25
 36726    public RectangularPrismTopology(GridTopologyMetrics metrics)
 27    {
 36728        Metrics = GridTopologyMetrics.Normalize(GridTopologyKind.RectangularPrism, metrics);
 36729    }
 30
 31    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 32    public GridDimensions CalculateDimensions(Vector3d boundsMin, Vector3d boundsMax) =>
 71633        new(((boundsMax.X - boundsMin.X) / Metrics.CellWidth).FloorToInt() + 1,
 71634            ((boundsMax.Y - boundsMin.Y) / Metrics.LayerHeight).FloorToInt() + 1,
 71635            ((boundsMax.Z - boundsMin.Z) / Metrics.CellLength).FloorToInt() + 1);
 36
 37    public (Vector3d min, Vector3d max) NormalizeBounds(Vector3d min, Vector3d max, Fixed64? padding = null)
 38    {
 113439        Fixed64 fixedPadding = padding.HasValue && padding.Value > Fixed64.Zero
 113440            ? padding.Value
 113441            : Fixed64.Zero;
 42
 113443        min -= fixedPadding;
 113444        max += fixedPadding;
 45
 113446        Vector3d snapMin = FloorToCellOrigin(min);
 113447        Vector3d snapMax = CeilToCellOrigin(max);
 48
 113449        (snapMin.X, snapMax.X) = snapMin.X > snapMax.X ? (snapMax.X, snapMin.X) : (snapMin.X, snapMax.X);
 113450        (snapMin.Y, snapMax.Y) = snapMin.Y > snapMax.Y ? (snapMax.Y, snapMin.Y) : (snapMin.Y, snapMax.Y);
 113451        (snapMin.Z, snapMax.Z) = snapMin.Z > snapMax.Z ? (snapMax.Z, snapMin.Z) : (snapMin.Z, snapMax.Z);
 52
 113453        return (snapMin, snapMax);
 54    }
 55
 56    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 57    public bool IsInBounds(
 58        Vector3d boundsMin,
 59        Vector3d boundsMax,
 60        int width,
 61        int height,
 62        int length,
 63        Vector3d position) =>
 158764        boundsMin.X <= position.X && position.X <= boundsMax.X
 158765        && boundsMin.Y <= position.Y && position.Y <= boundsMax.Y
 158766        && boundsMin.Z <= position.Z && position.Z <= boundsMax.Z;
 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    {
 146477        result = default;
 146478        if (!IsInBounds(boundsMin, boundsMax, width, height, length, position))
 1579            return false;
 80
 144981        result = new VoxelIndex(
 144982            ((position.X - boundsMin.X) / Metrics.CellWidth).FloorToInt(),
 144983            ((position.Y - boundsMin.Y) / Metrics.LayerHeight).FloorToInt(),
 144984            ((position.Z - boundsMin.Z) / Metrics.CellLength).FloorToInt());
 144985        return true;
 86    }
 87
 88    public VoxelIndex GetClosestVoxelIndex(
 89        Vector3d boundsMin,
 90        int width,
 91        int height,
 92        int length,
 93        Vector3d position) =>
 2094        new(
 2095            GetClosestAxisIndex(position.X - boundsMin.X, Metrics.CellWidth, width),
 2096            GetClosestAxisIndex(position.Y - boundsMin.Y, Metrics.LayerHeight, height),
 2097            GetClosestAxisIndex(position.Z - boundsMin.Z, Metrics.CellLength, length));
 98
 99    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 100    public Vector3d GetWorldPosition(Vector3d boundsMin, VoxelIndex index) =>
 93576101         new(boundsMin.X + index.x * Metrics.CellWidth,
 93576102            boundsMin.Y + index.y * Metrics.LayerHeight,
 93576103            boundsMin.Z + index.z * Metrics.CellLength);
 104
 105    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 106    public Vector3d GetWorldOffset((int x, int y, int z) offset) =>
 73107         new(offset.x * Metrics.CellWidth,
 73108            offset.y * Metrics.LayerHeight,
 73109            offset.z * Metrics.CellLength);
 110
 111    public VoxelIndex GetNeighborOffset(int slot)
 112    {
 437113        (int x, int y, int z) offset = RectangularDirectionUtility.Offsets[slot];
 437114        return new VoxelIndex(offset.x, offset.y, offset.z);
 115    }
 116
 117    public bool TryGetNeighborSlotFromWorldDelta(Vector3d worldDelta, out int slot)
 118    {
 74119        RectangularDirection direction = RectangularDirectionUtility.GetDirectionFromOffset((
 74120            worldDelta.X.Sign(),
 74121            worldDelta.Y.Sign(),
 74122            worldDelta.Z.Sign()));
 74123        slot = (int)direction;
 74124        return direction != RectangularDirection.None;
 125    }
 126
 127    public bool IsFacingBoundary(VoxelIndex voxelIndex, int slot, int width, int height, int length)
 128    {
 54129        (int x, int y, int z) offset = RectangularDirectionUtility.Offsets[slot];
 54130        return RectangularDirectionUtility.IsAxisFacingBoundary(voxelIndex.x, offset.x, width)
 54131            && RectangularDirectionUtility.IsAxisFacingBoundary(voxelIndex.y, offset.y, height)
 54132            && RectangularDirectionUtility.IsAxisFacingBoundary(voxelIndex.z, offset.z, length);
 133    }
 134
 135    public void GetBoundaryRange(
 136        int slot,
 137        int width,
 138        int height,
 139        int length,
 140        out int xStart,
 141        out int xEnd,
 142        out int yStart,
 143        out int yEnd,
 144        out int zStart,
 145        out int zEnd)
 146    {
 1147        (int x, int y, int z) offset = RectangularDirectionUtility.Offsets[slot];
 1148        (xStart, xEnd) = RectangularDirectionUtility.GetBoundaryRange(offset.x, width);
 1149        (yStart, yEnd) = RectangularDirectionUtility.GetBoundaryRange(offset.y, height);
 1150        (zStart, zEnd) = RectangularDirectionUtility.GetBoundaryRange(offset.z, length);
 1151    }
 152
 153    public Vector3d FloorToGrid(
 154        Vector3d boundsMin,
 155        Vector3d boundsMax,
 156        int width,
 157        int height,
 158        int length,
 159        Vector3d position)
 160    {
 197161        return new Vector3d(
 197162            FixedMath.Clamp(((position.X - boundsMin.X) / Metrics.CellWidth).FloorToInt() * Metrics.CellWidth + boundsMi
 197163            FixedMath.Clamp(((position.Y - boundsMin.Y) / Metrics.LayerHeight).FloorToInt() * Metrics.LayerHeight + boun
 197164            FixedMath.Clamp(((position.Z - boundsMin.Z) / Metrics.CellLength).FloorToInt() * Metrics.CellLength + bounds
 165    }
 166
 167    public Vector3d CeilToGrid(
 168        Vector3d boundsMin,
 169        Vector3d boundsMax,
 170        int width,
 171        int height,
 172        int length,
 173        Vector3d position)
 174    {
 2175        return new Vector3d(
 2176            FixedMath.Clamp(((position.X - boundsMin.X) / Metrics.CellWidth).CeilToInt() * Metrics.CellWidth + boundsMin
 2177            FixedMath.Clamp(((position.Y - boundsMin.Y) / Metrics.LayerHeight).CeilToInt() * Metrics.LayerHeight + bound
 2178            FixedMath.Clamp(((position.Z - boundsMin.Z) / Metrics.CellLength).CeilToInt() * Metrics.CellLength + boundsM
 179    }
 180
 181    public (int x, int y, int z) SnapToScanCell(Vector3d boundsMin, Vector3d position, int scanCellSize)
 182    {
 1084183        return (((position.X - boundsMin.X) / Metrics.CellWidth).FloorToInt() / scanCellSize,
 1084184            ((position.Y - boundsMin.Y) / Metrics.LayerHeight).FloorToInt() / scanCellSize,
 1084185            ((position.Z - boundsMin.Z) / Metrics.CellLength).FloorToInt() / scanCellSize);
 186    }
 187
 188    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 189    private Vector3d FloorToCellOrigin(Vector3d position) =>
 1134190        new(FloorToCellOrigin(position.X, Metrics.CellWidth),
 1134191            FloorToCellOrigin(position.Y, Metrics.LayerHeight),
 1134192            FloorToCellOrigin(position.Z, Metrics.CellLength));
 193
 194    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 195    private Vector3d CeilToCellOrigin(Vector3d position) =>
 1134196        new(CeilToCellOrigin(position.X, Metrics.CellWidth),
 1134197            CeilToCellOrigin(position.Y, Metrics.LayerHeight),
 1134198            CeilToCellOrigin(position.Z, Metrics.CellLength));
 199
 200    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 201    private static Fixed64 FloorToCellOrigin(Fixed64 coordinate, Fixed64 cellSize) =>
 3402202        (coordinate / cellSize).FloorToInt() * cellSize;
 203
 204    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 205    private static Fixed64 CeilToCellOrigin(Fixed64 coordinate, Fixed64 cellSize) =>
 3402206        (coordinate / cellSize).CeilToInt() * cellSize;
 207
 208    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 209    private static int GetClosestAxisIndex(Fixed64 offset, Fixed64 cellSize, int size) =>
 60210        FixedMath.Clamp((offset / cellSize).RoundToInt(), 0, size - 1);
 211
 212}

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)
FloorToCellOrigin(FixedMathSharp.Vector3d)
CeilToCellOrigin(FixedMathSharp.Vector3d)
FloorToCellOrigin(FixedMathSharp.Fixed64,FixedMathSharp.Fixed64)
CeilToCellOrigin(FixedMathSharp.Fixed64,FixedMathSharp.Fixed64)
GetClosestAxisIndex(FixedMathSharp.Fixed64,FixedMathSharp.Fixed64,System.Int32)