< Summary

Information
Class: Gravitas.Colliders.MeshMassIntegrals
Assembly: Gravitas
File(s): /home/runner/work/Gravitas/Gravitas/src/Gravitas/Colliders/MassProperties/MeshMassIntegrals.cs
Line coverage
100%
Covered lines: 42
Uncovered lines: 0
Coverable lines: 42
Total lines: 94
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
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%
FromSolid(...)100%44100%
FromShell(...)100%11100%
Project(...)100%44100%
Scale(...)100%11100%

File(s)

/home/runner/work/Gravitas/Gravitas/src/Gravitas/Colliders/MassProperties/MeshMassIntegrals.cs

#LineLine coverage
 1//=======================================================================
 2// MeshMassIntegrals.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 FixedMathSharp;
 9
 10namespace Gravitas.Colliders;
 11
 12/// <summary>Retains weighted first moments and inertia about a mesh reference before scalar projection.</summary>
 13internal readonly struct MeshMassIntegrals
 14{
 15    internal readonly ExactMassWeight Weight;
 16    internal readonly Vector3d Reference;
 17    // First moments use 60*Q192; inertia uses 60*Q224. These factors cancel
 18    // tetrahedron/shell divisors. Exact affine sums project once into these
 19    // guard fractions; final compound normalization retains that finite cache precision.
 20    internal readonly Signed576 FirstX, FirstY, FirstZ;
 21    internal readonly Signed576 Ixx, Iyy, Izz, Ixy, Ixz, Iyz;
 22
 23    private MeshMassIntegrals(ExactMassWeight weight, Vector3d reference,
 24        Signed576 firstX, Signed576 firstY, Signed576 firstZ,
 25        Signed576 ixx, Signed576 iyy, Signed576 izz,
 26        Signed576 ixy, Signed576 ixz, Signed576 iyz)
 27    {
 510428        Weight = weight;
 510429        Reference = reference;
 1531230        FirstX = firstX; FirstY = firstY; FirstZ = firstZ;
 1531231        Ixx = ixx; Iyy = iyy; Izz = izz;
 1531232        Ixy = ixy; Ixz = ixz; Iyz = iyz;
 510433    }
 34
 35    internal static MeshMassIntegrals FromSolid(Vector3d reference, Signed576 volume,
 36        Signed576 firstX, Signed576 firstY, Signed576 firstZ,
 37        Signed576 x2, Signed576 y2, Signed576 z2,
 38        Signed576 xy, Signed576 xz, Signed576 yz, int coordinateFractionBits = 0)
 39    {
 32640        int sign = volume.Sign;
 32641        Signed576 magnitude = sign < 0 ? WideArithmetic.SubtractSigned576(default, volume) : volume;
 32642        if (coordinateFractionBits != 0)
 43        {
 644            var exactWeight = new ExactMassWeight(Signed320.NarrowValue(Project(magnitude, 3 * coordinateFractionBits - 
 645            return new MeshMassIntegrals(exactWeight, reference,
 646                Project(Scale(firstX, 15 * sign, Signed192.Signed(1)), 4 * coordinateFractionBits - 64),
 647                Project(Scale(firstY, 15 * sign, Signed192.Signed(1)), 4 * coordinateFractionBits - 64),
 648                Project(Scale(firstZ, 15 * sign, Signed192.Signed(1)), 4 * coordinateFractionBits - 64),
 649                Project(Scale(WideArithmetic.AddSigned576(y2, z2), 6 * sign, Signed192.Signed(1)), 5 * coordinateFractio
 650                Project(Scale(WideArithmetic.AddSigned576(x2, z2), 6 * sign, Signed192.Signed(1)), 5 * coordinateFractio
 651                Project(Scale(WideArithmetic.AddSigned576(x2, y2), 6 * sign, Signed192.Signed(1)), 5 * coordinateFractio
 652                Project(Scale(xy, -3 * sign, Signed192.Signed(1)), 5 * coordinateFractionBits - 64),
 653                Project(Scale(xz, -3 * sign, Signed192.Signed(1)), 5 * coordinateFractionBits - 64),
 654                Project(Scale(yz, -3 * sign, Signed192.Signed(1)), 5 * coordinateFractionBits - 64));
 55        }
 32056        var weight = new ExactMassWeight(Signed320.NarrowValue(
 32057            WideArithmetic.MultiplySigned576(magnitude, Signed192.One)));
 32058        Signed192 qSquared = Signed192.NarrowValue(
 32059            WideArithmetic.MultiplySigned192(Signed192.One, Signed192.One));
 32060        return new MeshMassIntegrals(weight, reference,
 32061            Scale(firstX, 15 * sign, qSquared), Scale(firstY, 15 * sign, qSquared), Scale(firstZ, 15 * sign, qSquared),
 32062            Scale(WideArithmetic.AddSigned576(y2, z2), 6 * sign, qSquared),
 32063            Scale(WideArithmetic.AddSigned576(x2, z2), 6 * sign, qSquared),
 32064            Scale(WideArithmetic.AddSigned576(x2, y2), 6 * sign, qSquared),
 32065            Scale(xy, -3 * sign, qSquared), Scale(xz, -3 * sign, qSquared), Scale(yz, -3 * sign, qSquared));
 66    }
 67
 68    internal static MeshMassIntegrals FromShell(ExactMassWeight weight,
 69        Signed576 firstX, Signed576 firstY, Signed576 firstZ,
 70        Signed576 x2, Signed576 y2, Signed576 z2,
 71        Signed576 xy, Signed576 xz, Signed576 yz, int coordinateFractionBits = 0) =>
 477872        new(weight, Vector3d.Zero,
 477873            Project(Scale(firstX, 20, Signed192.One), coordinateFractionBits), Project(Scale(firstY, 20, Signed192.One),
 477874            Project(Scale(WideArithmetic.AddSigned576(y2, z2), 10, Signed192.One), 2 * coordinateFractionBits),
 477875            Project(Scale(WideArithmetic.AddSigned576(x2, z2), 10, Signed192.One), 2 * coordinateFractionBits),
 477876            Project(Scale(WideArithmetic.AddSigned576(x2, y2), 10, Signed192.One), 2 * coordinateFractionBits),
 477877            Project(Scale(xy, -5, Signed192.One), 2 * coordinateFractionBits), Project(Scale(xz, -5, Signed192.One), 2 *
 78
 79    // Public centroid/tensor projection uses unrounded sums. The retained runtime
 80    // cache has fixed Q192/Q224 guard precision, projected only after summation.
 81    internal static Signed576 Project(Signed576 value, int fractionBits)
 82    {
 8524583        if (fractionBits == 0) return value;
 87984        Signed576 denominator = Signed576.ExtendValue(Signed320.ExtendValue(Signed192.Signed(1)));
 1158685        for (int bit = 0; bit < fractionBits + 64; bit += 32)
 491486            denominator = WideArithmetic.MultiplySigned576(denominator, Signed192.One);
 87        // Admitted geometry and int-bounded accumulation keep projected moments
 88        // below 320 bits. The shared ratio owner rounds the complete value once.
 87989        return Signed576.ExtendValue(WideArithmetic.GetSignedRatioWith64FractionBits(value, denominator));
 90    }
 91
 92    private static Signed576 Scale(Signed576 value, long coefficient, Signed192 padding) =>
 4593693        WideArithmetic.MultiplySigned576(value, Signed192.Signed(coefficient), padding);
 94}