< Summary

Information
Class: Gravitas.Colliders.ColliderGeometryPolicy
Assembly: Gravitas
File(s): /home/runner/work/Gravitas/Gravitas/src/Gravitas/Colliders/Geometry/ColliderGeometryPolicy.cs
Line coverage
100%
Covered lines: 45
Uncovered lines: 0
Coverable lines: 45
Total lines: 104
Line coverage: 100%
Branch coverage
100%
Covered branches: 22
Total branches: 22
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
ValidateReach(...)100%22100%
ValidateReach(...)100%11100%
ValidateFiniteAxisReach(...)100%11100%
ValidateWorldPolygonReach(...)100%44100%
Add(...)100%22100%
Magnitude(...)100%22100%
Magnitude(...)100%22100%
Attachment(...)100%44100%
Attachment(...)100%11100%
RotatedReach(...)100%66100%
Capsule(...)100%11100%
FiniteAxis(...)100%11100%
Mixed(...)100%11100%
HalfCeiling(...)100%11100%

File(s)

/home/runner/work/Gravitas/Gravitas/src/Gravitas/Colliders/Geometry/ColliderGeometryPolicy.cs

#LineLine coverage
 1//=======================================================================
 2// ColliderGeometryPolicy.cs
 3//=======================================================================
 4// MIT License, Copyright (c) 2026-present David Oravsky (mrdav30)
 5// See LICENSE file in the project root for full license information.
 6//=======================================================================
 7
 8using System;
 9using FixedMathSharp;
 10using FixedMathSharp.Geometry;
 11
 12namespace Gravitas.Colliders;
 13
 14/// <summary>Defines the supported effective geometry envelope, independently of world position.</summary>
 15internal static class ColliderGeometryPolicy
 16{
 217    internal static readonly Fixed64 MaximumGeometryReach = (Fixed64)4096;
 18
 19    internal static void ValidateReach(Fixed64 reach, string parameterName) =>
 22986720        ChronicleGuard.ThrowIfArgument(reach < Fixed64.Zero || reach > MaximumGeometryReach,
 22986721            parameterName, "Effective geometry reach must not exceed 4,096 world units.");
 22
 23    internal static void ValidateReach(Vector2d envelope, string parameterName) =>
 321224        ChronicleGuard.ThrowIfArgument(Vector2d.CompareMagnitudeSquared(
 321225                envelope, new Vector2d(MaximumGeometryReach, Fixed64.Zero)) > 0,
 321226            parameterName, "Effective geometry reach must not exceed 4,096 world units.");
 27
 28    internal static void ValidateFiniteAxisReach(Fixed64 radius, Fixed64 height, string parameterName) =>
 55029        ChronicleGuard.ThrowIfArgument(Fixed64.CompareMagnitudeSquared(
 55030                radius, HalfCeiling(height), radius, Fixed64.Zero,
 55031                MaximumGeometryReach, Fixed64.Zero, Fixed64.Zero, Fixed64.Zero) > 0,
 55032            parameterName, "Effective finite-axis geometry reach must not exceed 4,096 world units.");
 33
 34    internal static void ValidateWorldPolygonReach(ReadOnlySpan<Vector2d> vertices)
 35    {
 6836        Signed192 sumX = default, sumY = default;
 30237        for (int i = 0; i < vertices.Length; i++)
 38        {
 11739            sumX = WideArithmetic.AddSigned192(sumX, Signed192.Raw(vertices[i].X));
 11740            sumY = WideArithmetic.AddSigned192(sumY, Signed192.Raw(vertices[i].Y));
 41        }
 42        // Retain n*v - sum(v): a rounded mean can hide an excess at the cutoff.
 43        // Span counts keep these numerators below 95 bits and their squared sum below 191.
 3444        Fixed64 countRaw = Fixed64.FromRaw(vertices.Length);
 3445        Signed192 limit = Fixed64.GetExactRawProduct(MaximumGeometryReach, countRaw);
 3446        Signed320 limitSquared = WideArithmetic.MultiplySigned192(limit, limit);
 24247        for (int i = 0; i < vertices.Length; i++)
 48        {
 10249            Signed192 x = WideArithmetic.SubtractSigned192(
 10250                Fixed64.GetExactRawProduct(vertices[i].X, countRaw), sumX);
 10251            Signed192 y = WideArithmetic.SubtractSigned192(
 10252                Fixed64.GetExactRawProduct(vertices[i].Y, countRaw), sumY);
 10253            ChronicleGuard.ThrowIfArgument(WideArithmetic.CompareMagnitude(
 10254                    WideArithmetic.AddProducts(x, x, y, y), limitSquared) > 0,
 10255                nameof(vertices), "Effective polygon geometry reach must not exceed 4,096 world units.");
 56        }
 1957    }
 58
 59    internal static Fixed64 Add(Fixed64 attachment, Fixed64 intrinsic) =>
 64842060        Fixed64.TryAdd(attachment, intrinsic, out Fixed64 reach) ? reach : Fixed64.MaxValue;
 61
 62    internal static Fixed64 Magnitude(Vector3d vector) =>
 2513963        vector.TryGetMagnitudeCeiling(out Fixed64 reach) ? reach : Fixed64.MaxValue;
 64
 65    internal static Fixed64 Magnitude(Vector2d vector) =>
 9067866        vector.TryGetMagnitudeCeiling(out Fixed64 reach) ? reach : Fixed64.MaxValue;
 67
 68    internal static Fixed64 Attachment(Vector3d offset, Vector3d scale) =>
 22181769        offset == Vector3d.Zero ? Fixed64.Zero
 22181770            : WideGeometry.TryGetScaledMagnitudeCeiling(offset, scale, out Fixed64 reach)
 22181771                ? reach : Fixed64.MaxValue;
 72
 73    internal static Fixed64 Attachment(Vector2d offset, Vector2d scale) =>
 4104974        Attachment(new Vector3d(offset.X, offset.Y, Fixed64.Zero),
 4104975            new Vector3d(scale.X, scale.Y, Fixed64.One));
 76
 77    internal static Fixed64 RotatedReach(Fixed64 reach, bool planar)
 78    {
 33990679        if (reach == Fixed64.Zero)
 21330880            return Fixed64.Zero;
 81
 82        // The rational 3D basis preserves norm; final component rounding adds
 83        // less than one raw unit to length. Planar sin/cos has operator norm
 84        // at most 1 + 2e; its scalar product rounding needs another half raw.
 12659885        if (planar)
 2327686            reach = Fixed64.MultiplyDivide(reach,
 2327687                Fixed64.One + Fixed64.Two * FixedMath.CanonicalSinCosErrorBound,
 2327688                Fixed64.One, out _);
 12659889        return Add(reach, Fixed64.FromRaw(planar ? 2 : 1));
 90    }
 91
 92    // Dividing raw lengths outward preserves the half-raw endpoint of an odd core/height.
 93    internal static Fixed64 Capsule(Fixed64 radius, Fixed64 coreLength) =>
 3170994        Add(radius, HalfCeiling(coreLength));
 95
 96    internal static Fixed64 FiniteAxis(Fixed64 radius, Fixed64 height) =>
 208297        Magnitude(new Vector3d(radius, HalfCeiling(height), radius));
 98
 99    internal static Fixed64 Mixed(Fixed64 planarReach, Fixed64 halfThickness) =>
 22852100        Magnitude(new Vector2d(planarReach, halfThickness));
 101
 102    private static Fixed64 HalfCeiling(Fixed64 length) =>
 34341103        Fixed64.FromRaw((length.m_rawValue >> 1) + (length.m_rawValue & 1));
 104}