< Summary

Information
Class: GridForge.Grids.Topology.GridCellPrism
Assembly: GridForge
File(s): /home/runner/work/GridForge/GridForge/src/GridForge/Grids/Topology/GridCellPrism.cs
Line coverage
100%
Covered lines: 60
Uncovered lines: 0
Coverable lines: 60
Total lines: 167
Line coverage: 100%
Branch coverage
100%
Covered branches: 51
Total branches: 51
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%88100%
GetFootprintVertex(...)100%1919100%
CopyFootprintTo(...)100%44100%
Contains(...)100%1818100%
GetAabb()100%22100%

File(s)

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

#LineLine coverage
 1//=======================================================================
 2// GridCellPrism.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 FixedMathSharp.Geometry;
 12using GridForge.Spatial;
 13
 14namespace GridForge.Grids.Topology;
 15
 16/// <summary>
 17/// Describes one exact topology cell as an ordered convex XZ footprint and a closed vertical interval.
 18/// </summary>
 19/// <remarks>
 20/// Construction fails when a metric cannot be bisected exactly in the fixed-point scalar domain.
 21/// </remarks>
 22public readonly struct GridCellPrism
 23{
 24    private readonly Vector2d _vertex0;
 25    private readonly Vector2d _vertex1;
 26    private readonly Vector2d _vertex2;
 27    private readonly Vector2d _vertex3;
 28    private readonly Vector2d _vertex4;
 29    private readonly Vector2d _vertex5;
 30
 31    /// <summary>
 32    /// The exact runtime cell identity represented by this prism.
 33    /// </summary>
 34    public WorldVoxelIndex Cell { get; }
 35
 36    /// <summary>
 37    /// The topology that produced the footprint.
 38    /// </summary>
 39    public GridTopologyKind TopologyKind { get; }
 40
 41    /// <summary>
 42    /// The world-space cell center.
 43    /// </summary>
 44    public Vector3d Center { get; }
 45
 46    /// <summary>
 47    /// The inclusive lower Y bound.
 48    /// </summary>
 49    public Fixed64 VerticalMin { get; }
 50
 51    /// <summary>
 52    /// The inclusive upper Y bound.
 53    /// </summary>
 54    public Fixed64 VerticalMax { get; }
 55
 56    /// <summary>
 57    /// The largest radius that can be inset from every horizontal footprint edge.
 58    /// </summary>
 59    public Fixed64 PlanarInradius { get; }
 60
 61    /// <summary>
 62    /// The number of boundary-ordered footprint vertices.
 63    /// </summary>
 64    public int FootprintVertexCount { get; }
 65
 66    internal GridCellPrism(
 67        WorldVoxelIndex cell,
 68        GridTopologyKind topologyKind,
 69        Vector3d center,
 70        Fixed64 verticalMin,
 71        Fixed64 verticalMax,
 72        Fixed64 planarInradius,
 73        ReadOnlySpan<Vector2d> footprint)
 74    {
 2812075        if (footprint.Length != 4 && footprint.Length != 6)
 176            throw new ArgumentOutOfRangeException(nameof(footprint));
 77
 2811978        Cell = cell;
 2811979        TopologyKind = topologyKind;
 2811980        Center = center;
 2811981        VerticalMin = verticalMin;
 2811982        VerticalMax = verticalMax;
 2811983        PlanarInradius = planarInradius;
 2811984        FootprintVertexCount = footprint.Length;
 2811985        _vertex0 = footprint[0];
 2811986        _vertex1 = footprint[1];
 2811987        _vertex2 = footprint[2];
 2811988        _vertex3 = footprint[3];
 2811989        _vertex4 = footprint.Length > 4 ? footprint[4] : default;
 2811990        _vertex5 = footprint.Length > 5 ? footprint[5] : default;
 2811991    }
 92
 93    /// <summary>
 94    /// Gets one boundary-ordered XZ footprint vertex.
 95    /// </summary>
 96    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 24807497    public Vector2d GetFootprintVertex(int index) => index switch
 24807498    {
 10929299        0 when FootprintVertexCount > 0 => _vertex0,
 124006100        1 when FootprintVertexCount > 1 => _vertex1,
 128988101        2 when FootprintVertexCount > 2 => _vertex2,
 123836102        3 when FootprintVertexCount > 3 => _vertex3,
 5013103        4 when FootprintVertexCount > 4 => _vertex4,
 5010104        5 when FootprintVertexCount > 5 => _vertex5,
 2105        _ => throw new ArgumentOutOfRangeException(nameof(index))
 248074106    };
 107
 108    /// <summary>
 109    /// Copies the boundary-ordered XZ footprint into caller-owned storage.
 110    /// </summary>
 111    public void CopyFootprintTo(Span<Vector2d> destination)
 112    {
 6135113        if (destination.Length < FootprintVertexCount)
 1114            throw new ArgumentException("The destination is smaller than the footprint.", nameof(destination));
 115
 63228116        for (int i = 0; i < FootprintVertexCount; i++)
 25480117            destination[i] = GetFootprintVertex(i);
 6134118    }
 119
 120    /// <summary>
 121    /// Determines whether a world-space point lies inside or on this closed prism.
 122    /// </summary>
 123    public bool Contains(Vector3d point)
 124    {
 29824125        if (FootprintVertexCount is not 4 and not 6
 29824126            || point.Y < VerticalMin
 29824127            || point.Y > VerticalMax)
 13128            return false;
 129
 130        // Rectangular construction stores exact opposite corners in this order.
 29811131        if (TopologyKind == GridTopologyKind.RectangularPrism)
 29703132            return point.X >= _vertex0.X && point.X <= _vertex2.X
 29703133                && point.Z >= _vertex0.Y && point.Z <= _vertex2.Y;
 134
 108135        Span<Vector2d> offsets = stackalloc Vector2d[6];
 108136        Vector2d origin = new(Center.X, Center.Z);
 1512137        for (int i = 0; i < FootprintVertexCount; i++)
 648138            offsets[i] = GetFootprintVertex(i) - origin;
 139
 108140        return FixedConvex2dRelations.ContainsPoint(
 108141            new Vector2d(point.X, point.Z),
 108142            origin,
 108143            offsets[..FootprintVertexCount]);
 144    }
 145
 146    internal TopologyVoxelAabb GetAabb()
 147    {
 9869148        Vector2d first = _vertex0;
 9869149        Fixed64 minX = first.X;
 9869150        Fixed64 maxX = first.X;
 9869151        Fixed64 minZ = first.Y;
 9869152        Fixed64 maxZ = first.Y;
 153
 82014154        for (int i = 1; i < FootprintVertexCount; i++)
 155        {
 31138156            Vector2d vertex = GetFootprintVertex(i);
 31138157            minX = FixedMath.Min(minX, vertex.X);
 31138158            maxX = FixedMath.Max(maxX, vertex.X);
 31138159            minZ = FixedMath.Min(minZ, vertex.Y);
 31138160            maxZ = FixedMath.Max(maxZ, vertex.Y);
 161        }
 162
 9869163        return new TopologyVoxelAabb(
 9869164            new Vector3d(minX, VerticalMin, minZ),
 9869165            new Vector3d(maxX, VerticalMax, maxZ));
 166    }
 167}