< Summary

Information
Class: GridForge.Grids.Topology.GridConvexPolygon2d
Assembly: GridForge
File(s): /home/runner/work/GridForge/GridForge/src/GridForge/Grids/Topology/GridConvexPolygon2d.cs
Line coverage
100%
Covered lines: 38
Uncovered lines: 0
Coverable lines: 38
Total lines: 98
Line coverage: 100%
Branch coverage
100%
Covered branches: 45
Total branches: 45
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%22100%
GetVertex(...)100%3737100%
CopyTo(...)100%44100%
GetOrDefault(...)100%22100%

File(s)

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

#LineLine coverage
 1//=======================================================================
 2// GridConvexPolygon2d.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;
 11
 12namespace GridForge.Grids.Topology;
 13
 14/// <summary>
 15/// Stores a boundary-ordered convex polygon produced by exact grid-cell contact clipping.
 16/// </summary>
 17public readonly struct GridConvexPolygon2d
 18{
 19    /// <summary>
 20    /// Maximum vertices in the intersection of two rectangular or hexagonal cell footprints.
 21    /// </summary>
 22    public const int MaxVertexCount = 12;
 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    private readonly Vector2d _vertex6;
 31    private readonly Vector2d _vertex7;
 32    private readonly Vector2d _vertex8;
 33    private readonly Vector2d _vertex9;
 34    private readonly Vector2d _vertex10;
 35    private readonly Vector2d _vertex11;
 36
 37    /// <summary>
 38    /// Number of boundary-ordered vertices in this polygon.
 39    /// </summary>
 40    public int VertexCount { get; }
 41
 42    internal GridConvexPolygon2d(ReadOnlySpan<Vector2d> vertices)
 43    {
 5444        if (vertices.Length > MaxVertexCount)
 145            throw new ArgumentOutOfRangeException(nameof(vertices));
 46
 5347        VertexCount = vertices.Length;
 5348        _vertex0 = GetOrDefault(vertices, 0);
 5349        _vertex1 = GetOrDefault(vertices, 1);
 5350        _vertex2 = GetOrDefault(vertices, 2);
 5351        _vertex3 = GetOrDefault(vertices, 3);
 5352        _vertex4 = GetOrDefault(vertices, 4);
 5353        _vertex5 = GetOrDefault(vertices, 5);
 5354        _vertex6 = GetOrDefault(vertices, 6);
 5355        _vertex7 = GetOrDefault(vertices, 7);
 5356        _vertex8 = GetOrDefault(vertices, 8);
 5357        _vertex9 = GetOrDefault(vertices, 9);
 5358        _vertex10 = GetOrDefault(vertices, 10);
 5359        _vertex11 = GetOrDefault(vertices, 11);
 5360    }
 61
 62    /// <summary>
 63    /// Gets one boundary-ordered polygon vertex.
 64    /// </summary>
 65    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 10866    public Vector2d GetVertex(int index) => index switch
 10867    {
 4668        0 when VertexCount > 0 => _vertex0,
 4669        1 when VertexCount > 1 => _vertex1,
 4670        2 when VertexCount > 2 => _vertex2,
 4671        3 when VertexCount > 3 => _vertex3,
 672        4 when VertexCount > 4 => _vertex4,
 673        5 when VertexCount > 5 => _vertex5,
 474        6 when VertexCount > 6 => _vertex6,
 275        7 when VertexCount > 7 => _vertex7,
 276        8 when VertexCount > 8 => _vertex8,
 277        9 when VertexCount > 9 => _vertex9,
 278        10 when VertexCount > 10 => _vertex10,
 479        11 when VertexCount > 11 => _vertex11,
 280        _ => throw new ArgumentOutOfRangeException(nameof(index))
 10881    };
 82
 83    /// <summary>
 84    /// Copies the boundary-ordered vertices into caller-owned storage.
 85    /// </summary>
 86    public void CopyTo(Span<Vector2d> destination)
 87    {
 2488        if (destination.Length < VertexCount)
 189            throw new ArgumentException("The destination is smaller than the polygon.", nameof(destination));
 90
 25491        for (int i = 0; i < VertexCount; i++)
 10492            destination[i] = GetVertex(i);
 2393    }
 94
 95    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 96    private static Vector2d GetOrDefault(ReadOnlySpan<Vector2d> vertices, int index) =>
 63697        index < vertices.Length ? vertices[index] : default;
 98}