< Summary

Information
Class: FixedMathSharp.Geometry.WideRationalBasis3d
Assembly: FixedMathSharp
File(s): /home/runner/work/FixedMathSharp/FixedMathSharp/src/FixedMathSharp/Geometry/Wide/Common/WideRationalBasis3d.cs
Line coverage
100%
Covered lines: 147
Uncovered lines: 0
Coverable lines: 147
Total lines: 224
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
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%
.ctor(...)100%11100%
CreateRelative(...)100%11100%
ScaledIdentity(...)100%11100%
GetAxis(...)100%44100%
GetComposedScaledCoordinateNumerator(...)100%11100%
Double(...)100%11100%

File(s)

/home/runner/work/FixedMathSharp/FixedMathSharp/src/FixedMathSharp/Geometry/Wide/Common/WideRationalBasis3d.cs

#LineLine coverage
 1//=======================================================================
 2// WideRationalBasis3d.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.Runtime.CompilerServices;
 9
 10namespace FixedMathSharp.Geometry;
 11
 12/// <summary>
 13/// Preserves a quaternion-derived rotation basis as exact rational
 14/// numerators over one shared denominator.
 15/// </summary>
 16internal readonly struct WideRationalBasis3d
 17{
 18    internal readonly Signed192 Denominator;
 19    internal readonly Signed192 Xx;
 20    internal readonly Signed192 Xy;
 21    internal readonly Signed192 Xz;
 22    internal readonly Signed192 Yx;
 23    internal readonly Signed192 Yy;
 24    internal readonly Signed192 Yz;
 25    internal readonly Signed192 Zx;
 26    internal readonly Signed192 Zy;
 27    internal readonly Signed192 Zz;
 28
 29    internal WideRationalBasis3d(FixedQuaternion orientation)
 30    {
 17832431        if (orientation == FixedQuaternion.Identity)
 32        {
 33            // Identity needs no quaternion-norm scale. Keeping the exact
 34            // unit basis avoids artificial powers in later geometry products.
 3854835            this = default;
 3854836            Denominator = Xx = Yy = Zz = Signed192.Signed(1);
 3854837            return;
 38        }
 39
 13977640        Signed192 xx = Fixed64.GetExactRawProduct(
 13977641            orientation.X,
 13977642            orientation.X);
 13977643        Signed192 yy = Fixed64.GetExactRawProduct(
 13977644            orientation.Y,
 13977645            orientation.Y);
 13977646        Signed192 zz = Fixed64.GetExactRawProduct(
 13977647            orientation.Z,
 13977648            orientation.Z);
 13977649        Signed192 ww = Fixed64.GetExactRawProduct(
 13977650            orientation.W,
 13977651            orientation.W);
 13977652        Signed192 xy = Fixed64.GetExactRawProduct(
 13977653            orientation.X,
 13977654            orientation.Y);
 13977655        Signed192 xz = Fixed64.GetExactRawProduct(
 13977656            orientation.X,
 13977657            orientation.Z);
 13977658        Signed192 xw = Fixed64.GetExactRawProduct(
 13977659            orientation.X,
 13977660            orientation.W);
 13977661        Signed192 yz = Fixed64.GetExactRawProduct(
 13977662            orientation.Y,
 13977663            orientation.Z);
 13977664        Signed192 yw = Fixed64.GetExactRawProduct(
 13977665            orientation.Y,
 13977666            orientation.W);
 13977667        Signed192 zw = Fixed64.GetExactRawProduct(
 13977668            orientation.Z,
 13977669            orientation.W);
 13977670        Denominator = WideArithmetic.AddSigned192(
 13977671            WideArithmetic.AddSigned192(xx, yy),
 13977672            WideArithmetic.AddSigned192(zz, ww));
 73
 13977674        Xx = WideArithmetic.AddSigned192(
 13977675            WideArithmetic.SubtractSigned192(
 13977676                WideArithmetic.SubtractSigned192(xx, yy),
 13977677                zz),
 13977678            ww);
 13977679        Xy = Double(WideArithmetic.AddSigned192(xy, zw));
 13977680        Xz = Double(WideArithmetic.SubtractSigned192(xz, yw));
 13977681        Yx = Double(WideArithmetic.SubtractSigned192(xy, zw));
 13977682        Yy = WideArithmetic.AddSigned192(
 13977683            WideArithmetic.SubtractSigned192(
 13977684                WideArithmetic.SubtractSigned192(yy, xx),
 13977685                zz),
 13977686            ww);
 13977687        Yz = Double(WideArithmetic.AddSigned192(yz, xw));
 13977688        Zx = Double(WideArithmetic.AddSigned192(xz, yw));
 13977689        Zy = Double(WideArithmetic.SubtractSigned192(yz, xw));
 13977690        Zz = WideArithmetic.AddSigned192(
 13977691            WideArithmetic.SubtractSigned192(
 13977692                WideArithmetic.SubtractSigned192(zz, xx),
 13977693                yy),
 13977694            ww);
 13977695    }
 96
 97    private WideRationalBasis3d(
 98        Signed192 denominator,
 99        Signed192 xx,
 100        Signed192 xy,
 101        Signed192 xz,
 102        Signed192 yx,
 103        Signed192 yy,
 104        Signed192 yz,
 105        Signed192 zx,
 106        Signed192 zy,
 107        Signed192 zz)
 108    {
 13710109        Denominator = denominator;
 13710110        Xx = xx;
 13710111        Xy = xy;
 13710112        Xz = xz;
 13710113        Yx = yx;
 13710114        Yy = yy;
 13710115        Yz = yz;
 13710116        Zx = zx;
 13710117        Zy = zy;
 13710118        Zz = zz;
 13710119    }
 120
 121    internal static WideRationalBasis3d CreateRelative(
 122        in WideRationalBasis3d target,
 123        in WideRationalBasis3d source) =>
 124        // Normalized quaternion-basis products and their three-term sums fit
 125        // Signed192, including the unreduced shared denominator.
 13595126        new(
 13595127            Signed192.NarrowProven(
 13595128                WideArithmetic.MultiplySigned192(
 13595129                    target.Denominator,
 13595130                    source.Denominator)),
 13595131            Signed192.NarrowProven(WideArithmetic.GetDotProduct3D(
 13595132                target.Xx, target.Xy, target.Xz,
 13595133                source.Xx, source.Xy, source.Xz)),
 13595134            Signed192.NarrowProven(WideArithmetic.GetDotProduct3D(
 13595135                target.Yx, target.Yy, target.Yz,
 13595136                source.Xx, source.Xy, source.Xz)),
 13595137            Signed192.NarrowProven(WideArithmetic.GetDotProduct3D(
 13595138                target.Zx, target.Zy, target.Zz,
 13595139                source.Xx, source.Xy, source.Xz)),
 13595140            Signed192.NarrowProven(WideArithmetic.GetDotProduct3D(
 13595141                target.Xx, target.Xy, target.Xz,
 13595142                source.Yx, source.Yy, source.Yz)),
 13595143            Signed192.NarrowProven(WideArithmetic.GetDotProduct3D(
 13595144                target.Yx, target.Yy, target.Yz,
 13595145                source.Yx, source.Yy, source.Yz)),
 13595146            Signed192.NarrowProven(WideArithmetic.GetDotProduct3D(
 13595147                target.Zx, target.Zy, target.Zz,
 13595148                source.Yx, source.Yy, source.Yz)),
 13595149            Signed192.NarrowProven(WideArithmetic.GetDotProduct3D(
 13595150                target.Xx, target.Xy, target.Xz,
 13595151                source.Zx, source.Zy, source.Zz)),
 13595152            Signed192.NarrowProven(WideArithmetic.GetDotProduct3D(
 13595153                target.Yx, target.Yy, target.Yz,
 13595154                source.Zx, source.Zy, source.Zz)),
 13595155            Signed192.NarrowProven(WideArithmetic.GetDotProduct3D(
 13595156                target.Zx, target.Zy, target.Zz,
 13595157                source.Zx, source.Zy, source.Zz)));
 158
 159    /// <summary>Retains the identity mapping at a caller's exact positive coordinate scale.</summary>
 160    internal static WideRationalBasis3d ScaledIdentity(Signed192 denominator) =>
 115161        new(denominator, denominator, default, default, default, denominator, default, default, default, denominator);
 162
 163    internal WideAxis3 GetAxis(int index) =>
 354164        index switch
 354165        {
 128166            0 => new WideAxis3(
 128167                Signed320.ExtendValue(Xx),
 128168                Signed320.ExtendValue(Xy),
 128169                Signed320.ExtendValue(Xz)),
 109170            1 => new WideAxis3(
 109171                Signed320.ExtendValue(Yx),
 109172                Signed320.ExtendValue(Yy),
 109173                Signed320.ExtendValue(Yz)),
 117174            _ => new WideAxis3(
 117175                Signed320.ExtendValue(Zx),
 117176                Signed320.ExtendValue(Zy),
 117177                Signed320.ExtendValue(Zz)),
 354178        };
 179
 180    internal static Signed576 GetComposedScaledCoordinateNumerator(
 181        Fixed64 outerLocalPoint,
 182        Fixed64 outerScale,
 183        Signed192 axisX,
 184        Signed192 axisY,
 185        Signed192 axisZ,
 186        Signed192 rotationDenominator,
 187        Fixed64 innerFrameOffset,
 188        Fixed64 innerFrameScale,
 189        Vector3d innerLocalDisplacement)
 190    {
 201191        Signed576 outerScaled = WideArithmetic.MultiplySigned320(
 201192            WideArithmetic.MultiplySigned192(
 201193                Signed192.Raw(outerLocalPoint),
 201194                Signed192.Raw(outerScale)),
 201195            Signed320.ExtendValue(rotationDenominator));
 201196        Signed576 innerScaled = WideArithmetic.MultiplySigned320(
 201197            WideArithmetic.MultiplySigned192(
 201198                Signed192.Raw(innerFrameOffset),
 201199                Signed192.Raw(innerFrameScale)),
 201200            Signed320.ExtendValue(rotationDenominator));
 201201        Signed320 displacement = WideArithmetic.AddSigned320(
 201202            WideArithmetic.AddSigned320(
 201203                WideArithmetic.MultiplySigned192(
 201204                    Signed192.Raw(innerLocalDisplacement.X),
 201205                    axisX),
 201206                WideArithmetic.MultiplySigned192(
 201207                    Signed192.Raw(innerLocalDisplacement.Y),
 201208                    axisY)),
 201209            WideArithmetic.MultiplySigned192(
 201210                Signed192.Raw(innerLocalDisplacement.Z),
 201211                axisZ));
 201212        return WideArithmetic.AddSigned576(
 201213            WideArithmetic.AddSigned576(
 201214                outerScaled,
 201215                innerScaled),
 201216            WideArithmetic.MultiplySigned320(
 201217                displacement,
 201218                Signed320.ExtendValue(Signed192.One)));
 219    }
 220
 221    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 222    private static Signed192 Double(Signed192 value) =>
 838656223        WideArithmetic.AddSigned192(value, value);
 224}