< Summary

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

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_SmallestRectangularEdge()100%11100%
get_LargestRectangularEdge()100%11100%
get_LargestHexEdge()100%11100%
Rectangular(...)100%11100%
Rectangular(...)100%11100%
Hex(...)100%11100%
Normalize(...)100%22100%
IsValid(...)100%1010100%
ResolvePositive(...)100%22100%
GetHashCode()100%11100%
Equals(...)100%88100%
Equals(...)100%22100%
op_Equality(...)100%11100%
op_Inequality(...)100%11100%

File(s)

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

#LineLine coverage
 1//=======================================================================
 2// GridTopologyMetrics.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 MemoryPack;
 10using SwiftCollections.Utility;
 11using System;
 12using System.Runtime.CompilerServices;
 13using System.Text.Json.Serialization;
 14
 15namespace GridForge.Grids.Topology;
 16
 17/// <summary>
 18/// Stores deterministic cell geometry for a grid topology.
 19/// </summary>
 20[Serializable]
 21[MemoryPackable]
 22public readonly partial struct GridTopologyMetrics : IEquatable<GridTopologyMetrics>
 23{
 24    /// <summary>
 25    /// Horizontal center-to-corner radius for hex-prism cells. Rectangular-prism grids leave this as zero.
 26    /// </summary>
 27    [JsonInclude]
 28    [MemoryPackInclude]
 29    public readonly Fixed64 CellRadius;
 30
 31    /// <summary>
 32    /// Rectangular-prism cell width along world X.
 33    /// </summary>
 34    [JsonInclude]
 35    [MemoryPackInclude]
 36    public readonly Fixed64 CellWidth;
 37
 38    /// <summary>
 39    /// Vertical layer height along world Y for rectangular-prism and hex-prism grids.
 40    /// </summary>
 41    [JsonInclude]
 42    [MemoryPackInclude]
 43    public readonly Fixed64 LayerHeight;
 44
 45    /// <summary>
 46    /// Rectangular-prism cell length along world Z.
 47    /// </summary>
 48    [JsonInclude]
 49    [MemoryPackInclude]
 50    public readonly Fixed64 CellLength;
 51
 52    /// <summary>
 53    /// Hex-prism horizontal projection orientation. Rectangular-prism grids ignore this value.
 54    /// The orientation affects fixed-point world XZ projection only; it is not an engine rendering setting.
 55    /// </summary>
 56    [JsonInclude]
 57    [MemoryPackInclude]
 58    public readonly HexOrientation HexOrientation;
 59
 60    /// <summary>
 61    /// Initializes deterministic topology metrics.
 62    /// </summary>
 63    [JsonConstructor]
 64    public GridTopologyMetrics(
 65        Fixed64 cellRadius,
 66        Fixed64 cellWidth,
 67        Fixed64 layerHeight,
 68        Fixed64 cellLength,
 69        HexOrientation hexOrientation = HexOrientation.PointyTop)
 70    {
 90071        CellRadius = cellRadius;
 90072        CellWidth = cellWidth;
 90073        LayerHeight = layerHeight;
 90074        CellLength = cellLength;
 90075        HexOrientation = hexOrientation;
 90076    }
 77
 78    internal Fixed64 SmallestRectangularEdge
 79    {
 80        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 81        get
 82        {
 6783            Fixed64 smallest = FixedMath.Min(CellWidth, LayerHeight);
 6784            return FixedMath.Min(smallest, CellLength);
 85        }
 86    }
 87
 88    internal Fixed64 LargestRectangularEdge
 89    {
 90        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 91        get
 92        {
 38793            Fixed64 largest = FixedMath.Max(CellWidth, LayerHeight);
 38794            return FixedMath.Max(largest, CellLength);
 95        }
 96    }
 97
 98    internal Fixed64 LargestHexEdge
 99    {
 100        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 101        get
 102        {
 83103            Fixed64 horizontalDiameter = CellRadius * new Fixed64(2);
 83104            return FixedMath.Max(horizontalDiameter, LayerHeight);
 105        }
 106    }
 107
 108    /// <summary>
 109    /// Creates cubic rectangular-prism metrics.
 110    /// </summary>
 111    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 112    public static GridTopologyMetrics Rectangular(Fixed64 cellSize) =>
 29113         Rectangular(cellSize, cellSize, cellSize);
 114
 115    /// <summary>
 116    /// Creates rectangular-prism metrics with independent X, Y, and Z cell edges.
 117    /// </summary>
 118    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 119    public static GridTopologyMetrics Rectangular(
 120        Fixed64 cellWidth,
 121        Fixed64 layerHeight,
 738122        Fixed64 cellLength) => new(Fixed64.Zero,
 738123            ResolvePositive(cellWidth),
 738124            ResolvePositive(layerHeight),
 738125            ResolvePositive(cellLength),
 738126            HexOrientation.PointyTop);
 127
 128    /// <summary>
 129    /// Creates hex-prism metrics with the supplied horizontal radius, vertical layer height, and orientation.
 130    /// </summary>
 131    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 132    public static GridTopologyMetrics Hex(
 133        Fixed64 cellRadius,
 134        Fixed64 layerHeight,
 157135        HexOrientation hexOrientation = HexOrientation.PointyTop) => new(
 157136            cellRadius,
 157137            Fixed64.Zero,
 157138            layerHeight,
 157139            Fixed64.Zero,
 157140            hexOrientation);
 141
 142    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 143    internal static GridTopologyMetrics Normalize(
 144        GridTopologyKind topologyKind,
 773145        GridTopologyMetrics metrics) => topologyKind == GridTopologyKind.RectangularPrism
 773146            ? Rectangular(metrics.CellWidth, metrics.LayerHeight, metrics.CellLength)
 773147            : Hex(metrics.CellRadius, metrics.LayerHeight, metrics.HexOrientation);
 148
 149    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 150    internal static bool IsValid(
 151        GridTopologyKind topologyKind,
 439152        GridTopologyMetrics metrics) => topologyKind switch
 439153        {
 373154            GridTopologyKind.RectangularPrism => metrics.CellWidth > Fixed64.Zero
 373155                && metrics.LayerHeight > Fixed64.Zero
 373156                && metrics.CellLength > Fixed64.Zero,
 65157            GridTopologyKind.HexPrism => metrics.CellRadius > Fixed64.Zero
 65158                && metrics.LayerHeight > Fixed64.Zero,
 1159            _ => false
 439160        };
 161
 162    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 163    private static Fixed64 ResolvePositive(Fixed64 value) =>
 2214164        value > Fixed64.Zero ? value : Fixed64.One;
 165
 166    /// <inheritdoc/>
 167    public override readonly int GetHashCode()
 168    {
 840169        int hash = SwiftHashTools.CombineHashCodes(CellRadius.GetHashCode(), CellWidth.GetHashCode());
 840170        hash = SwiftHashTools.CombineHashCodes(hash, LayerHeight.GetHashCode());
 840171        hash = SwiftHashTools.CombineHashCodes(hash, CellLength.GetHashCode());
 840172        return SwiftHashTools.CombineHashCodes(hash, HexOrientation.GetHashCode());
 173    }
 174
 175    /// <inheritdoc/>
 176    public readonly bool Equals(GridTopologyMetrics other)
 177    {
 941178        return CellRadius == other.CellRadius
 941179            && CellWidth == other.CellWidth
 941180            && LayerHeight == other.LayerHeight
 941181            && CellLength == other.CellLength
 941182            && HexOrientation == other.HexOrientation;
 183    }
 184
 185    /// <inheritdoc/>
 3186    public override readonly bool Equals(object? obj) => obj is GridTopologyMetrics other && Equals(other);
 187
 188    /// <inheritdoc/>
 888189    public static bool operator ==(GridTopologyMetrics left, GridTopologyMetrics right) => left.Equals(right);
 190
 191    /// <inheritdoc/>
 2192    public static bool operator !=(GridTopologyMetrics left, GridTopologyMetrics right) => !left.Equals(right);
 193}