< Summary

Information
Class: FixedMathSharp.Geometry.WidePointAnchor2d
Assembly: FixedMathSharp
File(s): /home/runner/work/FixedMathSharp/FixedMathSharp/src/FixedMathSharp/Geometry/Anchors/Wide/WidePointAnchor2d.cs
Line coverage
100%
Covered lines: 244
Uncovered lines: 0
Coverable lines: 244
Total lines: 352
Line coverage: 100%
Branch coverage
100%
Covered branches: 16
Total branches: 16
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
TryGetPoint(...)100%44100%
TryGetRelativeOffset(...)100%66100%
RepresentsSamePoint(...)100%22100%
TryGetLocalPointIn(...)100%44100%
CompareSquaredDistances(...)100%11100%
GetSquaredDistanceNumerator(...)100%11100%
GetExactRelativeOffsetRatio(...)100%11100%
GetExactCoordinates(...)100%11100%
GetExactAnchorDenominator()100%11100%

File(s)

/home/runner/work/FixedMathSharp/FixedMathSharp/src/FixedMathSharp/Geometry/Anchors/Wide/WidePointAnchor2d.cs

#LineLine coverage
 1//=======================================================================
 2// WidePointAnchor2d.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
 8namespace FixedMathSharp.Geometry;
 9
 10/// <summary>
 11/// Provides methods for working with 2D point anchors that use wide fixed-point arithmetic.
 12/// </summary>
 13internal static class WidePointAnchor2d
 14{
 15    internal static bool TryGetPoint(
 16        Vector2d origin,
 17        Vector2d localPoint,
 18        Vector2d localDisplacement,
 19        FixedPointAnchorTerm2d exactLocalTerm,
 20        Fixed64 angleInRadians,
 21        out Vector2d result)
 22    {
 33223        if (exactLocalTerm.IsZero)
 24        {
 32325            return WideVector2dTransform.TryTransformCompositePoint(
 32326                origin,
 32327                localPoint,
 32328                localDisplacement,
 32329                angleInRadians,
 32330                out result);
 31        }
 32
 933        Fixed64 cosine = FixedMath.Cos(angleInRadians);
 934        Fixed64 sine = FixedMath.Sin(angleInRadians);
 935        Signed320 denominator = GetExactAnchorDenominator();
 936        GetExactCoordinates(
 937            origin,
 938            localPoint,
 939            localDisplacement,
 940            exactLocalTerm,
 941            cosine,
 942            sine,
 943            out Signed320 xNumerator,
 944            out Signed320 yNumerator);
 945        bool representable = Fixed64.TryGetSignedRawRatio(
 946                Signed576.ExtendValue(xNumerator),
 947                Signed576.ExtendValue(denominator),
 948                out Fixed64 x)
 949            & Fixed64.TryGetSignedRawRatio(
 950                Signed576.ExtendValue(yNumerator),
 951                Signed576.ExtendValue(denominator),
 952                out Fixed64 y);
 953        result = representable ? new Vector2d(x, y) : default;
 954        return representable;
 55    }
 56
 57    internal static bool TryGetRelativeOffset(
 58        Vector2d firstOrigin,
 59        Vector2d firstLocalPoint,
 60        Vector2d firstLocalDisplacement,
 61        FixedPointAnchorTerm2d firstExactLocalTerm,
 62        Fixed64 firstAngleInRadians,
 63        Vector2d secondOrigin,
 64        Vector2d secondLocalPoint,
 65        Vector2d secondLocalDisplacement,
 66        FixedPointAnchorTerm2d secondExactLocalTerm,
 67        Fixed64 secondAngleInRadians,
 68        out Vector2d result)
 69    {
 4770        if (firstExactLocalTerm.IsZero
 4771            && secondExactLocalTerm.IsZero)
 72        {
 4273            return WideVector2dTransform.TryGetRelativeOffset(
 4274                firstOrigin,
 4275                firstLocalPoint,
 4276                firstLocalDisplacement,
 4277                firstAngleInRadians,
 4278                secondOrigin,
 4279                secondLocalPoint,
 4280                secondLocalDisplacement,
 4281                secondAngleInRadians,
 4282                out result);
 83        }
 84
 585        GetExactRelativeOffsetRatio(
 586            firstOrigin,
 587            firstLocalPoint,
 588            firstLocalDisplacement,
 589            firstExactLocalTerm,
 590            firstAngleInRadians,
 591            secondOrigin,
 592            secondLocalPoint,
 593            secondLocalDisplacement,
 594            secondExactLocalTerm,
 595            secondAngleInRadians,
 596            out Signed320 xNumerator,
 597            out Signed320 yNumerator,
 598            out Signed320 coordinateDenominator);
 599        Signed576 denominator =
 5100            Signed576.ExtendValue(coordinateDenominator);
 5101        bool representable = Fixed64.TryGetSignedRawRatio(
 5102                Signed576.ExtendValue(xNumerator),
 5103                denominator,
 5104                out Fixed64 x)
 5105            & Fixed64.TryGetSignedRawRatio(
 5106                Signed576.ExtendValue(yNumerator),
 5107                denominator,
 5108                out Fixed64 y);
 5109        result = representable ? new Vector2d(x, y) : default;
 5110        return representable;
 111    }
 112
 113    internal static bool RepresentsSamePoint(
 114        in FixedPointAnchor2d first,
 115        in FixedPointAnchor2d second)
 116    {
 35117        GetExactCoordinates(
 35118            first.Origin,
 35119            first.LocalPoint,
 35120            first.LocalDisplacement,
 35121            first.ExactLocalTerm,
 35122            FixedMath.Cos(first.Rotation),
 35123            FixedMath.Sin(first.Rotation),
 35124            out Signed320 firstX,
 35125            out Signed320 firstY);
 35126        GetExactCoordinates(
 35127            second.Origin,
 35128            second.LocalPoint,
 35129            second.LocalDisplacement,
 35130            second.ExactLocalTerm,
 35131            FixedMath.Cos(second.Rotation),
 35132            FixedMath.Sin(second.Rotation),
 35133            out Signed320 secondX,
 35134            out Signed320 secondY);
 35135        return firstX.Equals(secondX) && firstY.Equals(secondY);
 136    }
 137
 138    internal static bool TryGetLocalPointIn(
 139        Vector2d pointOrigin,
 140        Vector2d pointLocalPoint,
 141        Vector2d pointLocalDisplacement,
 142        FixedPointAnchorTerm2d exactLocalTerm,
 143        Fixed64 pointAngleInRadians,
 144        Vector2d frameOrigin,
 145        Fixed64 frameAngleInRadians,
 146        out Vector2d localPoint)
 147    {
 76148        if (exactLocalTerm.IsZero)
 149        {
 73150            return WideVector2dTransform.TryGetLocalPointIn(
 73151                pointOrigin,
 73152                pointLocalPoint,
 73153                pointLocalDisplacement,
 73154                pointAngleInRadians,
 73155                frameOrigin,
 73156                frameAngleInRadians,
 73157                out localPoint);
 158        }
 159
 3160        Fixed64 pointCosine = FixedMath.Cos(pointAngleInRadians);
 3161        Fixed64 pointSine = FixedMath.Sin(pointAngleInRadians);
 3162        Fixed64 frameCosine = FixedMath.Cos(frameAngleInRadians);
 3163        Fixed64 frameSine = FixedMath.Sin(frameAngleInRadians);
 3164        GetExactCoordinates(
 3165            pointOrigin,
 3166            pointLocalPoint,
 3167            pointLocalDisplacement,
 3168            exactLocalTerm,
 3169            pointCosine,
 3170            pointSine,
 3171            out Signed320 pointX,
 3172            out Signed320 pointY);
 3173        Signed320 denominator = GetExactAnchorDenominator();
 3174        Signed320 deltaX = WideArithmetic.SubtractSigned320(
 3175            pointX,
 3176            WideArithmetic.MultiplySigned192(
 3177                Signed192.Raw(frameOrigin.X),
 3178                Signed192.NarrowValue(denominator)));
 3179        Signed320 deltaY = WideArithmetic.SubtractSigned320(
 3180            pointY,
 3181            WideArithmetic.MultiplySigned192(
 3182                Signed192.Raw(frameOrigin.Y),
 3183                Signed192.NarrowValue(denominator)));
 3184        Signed576 xNumerator = WideArithmetic.AddSigned576(
 3185            WideArithmetic.MultiplySigned576(
 3186                Signed576.ExtendValue(deltaX),
 3187                Signed192.Raw(frameCosine)),
 3188            WideArithmetic.MultiplySigned576(
 3189                Signed576.ExtendValue(deltaY),
 3190                Signed192.Raw(frameSine)));
 3191        Signed576 yNumerator = WideArithmetic.SubtractSigned576(
 3192            WideArithmetic.MultiplySigned576(
 3193                Signed576.ExtendValue(deltaY),
 3194                Signed192.Raw(frameCosine)),
 3195            WideArithmetic.MultiplySigned576(
 3196                Signed576.ExtendValue(deltaX),
 3197                Signed192.Raw(frameSine)));
 3198        Signed576 inverseDenominator = WideArithmetic.MultiplySigned320(
 3199            WideArithmetic.AddSigned320(
 3200                WideArithmetic.MultiplySigned192(
 3201                    Signed192.Raw(frameCosine),
 3202                    Signed192.Raw(frameCosine)),
 3203                WideArithmetic.MultiplySigned192(
 3204                    Signed192.Raw(frameSine),
 3205                    Signed192.Raw(frameSine))),
 3206            Signed320.ExtendValue(
 3207                FixedPointAnchorTerm3d.Denominator));
 3208        bool representable = Fixed64.TryGetSignedRawRatio(
 3209                xNumerator,
 3210                inverseDenominator,
 3211                out Fixed64 x)
 3212            & Fixed64.TryGetSignedRawRatio(
 3213                yNumerator,
 3214                inverseDenominator,
 3215                out Fixed64 y);
 3216        localPoint = representable ? new Vector2d(x, y) : default;
 3217        return representable;
 218    }
 219
 220    internal static int CompareSquaredDistances(
 221        in FixedPointAnchor2d reference,
 222        in FixedPointAnchor2d first,
 223        in FixedPointAnchor2d second)
 224    {
 9225        Signed576 firstSquaredDistance = GetSquaredDistanceNumerator(
 9226            reference,
 9227            first);
 9228        Signed576 secondSquaredDistance = GetSquaredDistanceNumerator(
 9229            reference,
 9230            second);
 9231        return WideArithmetic.CompareNonNegative(
 9232            firstSquaredDistance,
 9233            secondSquaredDistance);
 234    }
 235
 236    private static Signed576 GetSquaredDistanceNumerator(
 237        in FixedPointAnchor2d reference,
 238        in FixedPointAnchor2d point)
 239    {
 18240        GetExactRelativeOffsetRatio(
 18241            point.Origin,
 18242            point.LocalPoint,
 18243            point.LocalDisplacement,
 18244            point.ExactLocalTerm,
 18245            point.Rotation,
 18246            reference.Origin,
 18247            reference.LocalPoint,
 18248            reference.LocalDisplacement,
 18249            reference.ExactLocalTerm,
 18250            reference.Rotation,
 18251            out Signed320 x,
 18252            out Signed320 y,
 18253            // Every point-anchor term uses the same fixed denominator, so the
 18254            // squared coordinate numerators can be compared directly.
 18255            out _);
 18256        return WideArithmetic.AddSigned576(
 18257            WideArithmetic.MultiplySigned320(x, x),
 18258            WideArithmetic.MultiplySigned320(y, y));
 259    }
 260
 261    internal static void GetExactRelativeOffsetRatio(
 262        Vector2d firstOrigin,
 263        Vector2d firstLocalPoint,
 264        Vector2d firstLocalDisplacement,
 265        FixedPointAnchorTerm2d firstExactLocalTerm,
 266        Fixed64 firstAngleInRadians,
 267        Vector2d secondOrigin,
 268        Vector2d secondLocalPoint,
 269        Vector2d secondLocalDisplacement,
 270        FixedPointAnchorTerm2d secondExactLocalTerm,
 271        Fixed64 secondAngleInRadians,
 272        out Signed320 x,
 273        out Signed320 y,
 274        out Signed320 denominator)
 275    {
 54276        GetExactCoordinates(
 54277            firstOrigin,
 54278            firstLocalPoint,
 54279            firstLocalDisplacement,
 54280            firstExactLocalTerm,
 54281            FixedMath.Cos(firstAngleInRadians),
 54282            FixedMath.Sin(firstAngleInRadians),
 54283            out Signed320 firstX,
 54284            out Signed320 firstY);
 54285        GetExactCoordinates(
 54286            secondOrigin,
 54287            secondLocalPoint,
 54288            secondLocalDisplacement,
 54289            secondExactLocalTerm,
 54290            FixedMath.Cos(secondAngleInRadians),
 54291            FixedMath.Sin(secondAngleInRadians),
 54292            out Signed320 secondX,
 54293            out Signed320 secondY);
 54294        x = WideArithmetic.SubtractSigned320(firstX, secondX);
 54295        y = WideArithmetic.SubtractSigned320(firstY, secondY);
 54296        denominator = GetExactAnchorDenominator();
 54297    }
 298
 299    private static void GetExactCoordinates(
 300        Vector2d origin,
 301        Vector2d localPoint,
 302        Vector2d localDisplacement,
 303        FixedPointAnchorTerm2d exactLocalTerm,
 304        Fixed64 cosine,
 305        Fixed64 sine,
 306        out Signed320 x,
 307        out Signed320 y)
 308    {
 190309        WideVector2dTransform.GetRotationNumerators(
 190310            localPoint,
 190311            localDisplacement,
 190312            cosine,
 190313            sine,
 190314            out Signed320 roundedX,
 190315            out Signed320 roundedY);
 190316        Signed320 scaledRoundedX = Signed320.NarrowValue(
 190317            WideArithmetic.MultiplySigned320(
 190318                WideArithmetic.AddSigned320(
 190319                    WideVector2dTransform.Scale(origin.X),
 190320                    roundedX),
 190321                Signed320.ExtendValue(
 190322                    FixedPointAnchorTerm3d.Denominator)));
 190323        Signed320 scaledRoundedY = Signed320.NarrowValue(
 190324            WideArithmetic.MultiplySigned320(
 190325                WideArithmetic.AddSigned320(
 190326                    WideVector2dTransform.Scale(origin.Y),
 190327                    roundedY),
 190328                Signed320.ExtendValue(
 190329                    FixedPointAnchorTerm3d.Denominator)));
 190330        Signed320 residualX = WideArithmetic.SubtractSigned320(
 190331            WideArithmetic.MultiplySigned192(
 190332                Signed192.Signed(exactLocalTerm.X),
 190333                Signed192.Raw(cosine)),
 190334            WideArithmetic.MultiplySigned192(
 190335                Signed192.Signed(exactLocalTerm.Y),
 190336                Signed192.Raw(sine)));
 190337        Signed320 residualY = WideArithmetic.AddSigned320(
 190338            WideArithmetic.MultiplySigned192(
 190339                Signed192.Signed(exactLocalTerm.X),
 190340                Signed192.Raw(sine)),
 190341            WideArithmetic.MultiplySigned192(
 190342                Signed192.Signed(exactLocalTerm.Y),
 190343                Signed192.Raw(cosine)));
 190344        x = WideArithmetic.AddSigned320(scaledRoundedX, residualX);
 190345        y = WideArithmetic.AddSigned320(scaledRoundedY, residualY);
 190346    }
 347
 348    private static Signed320 GetExactAnchorDenominator() =>
 66349        WideArithmetic.MultiplySigned192(
 66350            Signed192.One,
 66351            FixedPointAnchorTerm3d.Denominator);
 352}

Methods/Properties

TryGetPoint(FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,FixedMathSharp.Geometry.FixedPointAnchorTerm2d,FixedMathSharp.Fixed64,FixedMathSharp.Vector2d&)
TryGetRelativeOffset(FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,FixedMathSharp.Geometry.FixedPointAnchorTerm2d,FixedMathSharp.Fixed64,FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,FixedMathSharp.Geometry.FixedPointAnchorTerm2d,FixedMathSharp.Fixed64,FixedMathSharp.Vector2d&)
RepresentsSamePoint(FixedMathSharp.Geometry.FixedPointAnchor2d&,FixedMathSharp.Geometry.FixedPointAnchor2d&)
TryGetLocalPointIn(FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,FixedMathSharp.Geometry.FixedPointAnchorTerm2d,FixedMathSharp.Fixed64,FixedMathSharp.Vector2d,FixedMathSharp.Fixed64,FixedMathSharp.Vector2d&)
CompareSquaredDistances(FixedMathSharp.Geometry.FixedPointAnchor2d&,FixedMathSharp.Geometry.FixedPointAnchor2d&,FixedMathSharp.Geometry.FixedPointAnchor2d&)
GetSquaredDistanceNumerator(FixedMathSharp.Geometry.FixedPointAnchor2d&,FixedMathSharp.Geometry.FixedPointAnchor2d&)
GetExactRelativeOffsetRatio(FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,FixedMathSharp.Geometry.FixedPointAnchorTerm2d,FixedMathSharp.Fixed64,FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,FixedMathSharp.Geometry.FixedPointAnchorTerm2d,FixedMathSharp.Fixed64,FixedMathSharp.Signed320&,FixedMathSharp.Signed320&,FixedMathSharp.Signed320&)
GetExactCoordinates(FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,FixedMathSharp.Vector2d,FixedMathSharp.Geometry.FixedPointAnchorTerm2d,FixedMathSharp.Fixed64,FixedMathSharp.Fixed64,FixedMathSharp.Signed320&,FixedMathSharp.Signed320&)
GetExactAnchorDenominator()