< 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    {
 5123        if (exactLocalTerm.IsZero)
 24        {
 4525            return WideVector2dTransform.TryTransformCompositePoint(
 4526                origin,
 4527                localPoint,
 4528                localDisplacement,
 4529                angleInRadians,
 4530                out result);
 31        }
 32
 633        Fixed64 cosine = FixedMath.Cos(angleInRadians);
 634        Fixed64 sine = FixedMath.Sin(angleInRadians);
 635        Signed320 denominator = GetExactAnchorDenominator();
 636        GetExactCoordinates(
 637            origin,
 638            localPoint,
 639            localDisplacement,
 640            exactLocalTerm,
 641            cosine,
 642            sine,
 643            out Signed320 xNumerator,
 644            out Signed320 yNumerator);
 645        bool representable = Fixed64.TryGetSignedRawRatio(
 646                Signed576.ExtendValue(xNumerator),
 647                Signed576.ExtendValue(denominator),
 648                out Fixed64 x)
 649            & Fixed64.TryGetSignedRawRatio(
 650                Signed576.ExtendValue(yNumerator),
 651                Signed576.ExtendValue(denominator),
 652                out Fixed64 y);
 653        result = representable ? new Vector2d(x, y) : default;
 654        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    {
 4670        if (firstExactLocalTerm.IsZero
 4671            && 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
 485        GetExactRelativeOffsetRatio(
 486            firstOrigin,
 487            firstLocalPoint,
 488            firstLocalDisplacement,
 489            firstExactLocalTerm,
 490            firstAngleInRadians,
 491            secondOrigin,
 492            secondLocalPoint,
 493            secondLocalDisplacement,
 494            secondExactLocalTerm,
 495            secondAngleInRadians,
 496            out Signed320 xNumerator,
 497            out Signed320 yNumerator,
 498            out Signed320 coordinateDenominator);
 499        Signed576 denominator =
 4100            Signed576.ExtendValue(coordinateDenominator);
 4101        bool representable = Fixed64.TryGetSignedRawRatio(
 4102                Signed576.ExtendValue(xNumerator),
 4103                denominator,
 4104                out Fixed64 x)
 4105            & Fixed64.TryGetSignedRawRatio(
 4106                Signed576.ExtendValue(yNumerator),
 4107                denominator,
 4108                out Fixed64 y);
 4109        result = representable ? new Vector2d(x, y) : default;
 4110        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    {
 6225        Signed576 firstSquaredDistance = GetSquaredDistanceNumerator(
 6226            reference,
 6227            first);
 6228        Signed576 secondSquaredDistance = GetSquaredDistanceNumerator(
 6229            reference,
 6230            second);
 6231        return WideArithmetic.CompareNonNegative(
 6232            firstSquaredDistance,
 6233            secondSquaredDistance);
 234    }
 235
 236    private static Signed576 GetSquaredDistanceNumerator(
 237        in FixedPointAnchor2d reference,
 238        in FixedPointAnchor2d point)
 239    {
 12240        GetExactRelativeOffsetRatio(
 12241            point.Origin,
 12242            point.LocalPoint,
 12243            point.LocalDisplacement,
 12244            point.ExactLocalTerm,
 12245            point.Rotation,
 12246            reference.Origin,
 12247            reference.LocalPoint,
 12248            reference.LocalDisplacement,
 12249            reference.ExactLocalTerm,
 12250            reference.Rotation,
 12251            out Signed320 x,
 12252            out Signed320 y,
 12253            // Every point-anchor term uses the same fixed denominator, so the
 12254            // squared coordinate numerators can be compared directly.
 12255            out _);
 12256        return WideArithmetic.AddSigned576(
 12257            WideArithmetic.MultiplySigned320(x, x),
 12258            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    {
 16276        GetExactCoordinates(
 16277            firstOrigin,
 16278            firstLocalPoint,
 16279            firstLocalDisplacement,
 16280            firstExactLocalTerm,
 16281            FixedMath.Cos(firstAngleInRadians),
 16282            FixedMath.Sin(firstAngleInRadians),
 16283            out Signed320 firstX,
 16284            out Signed320 firstY);
 16285        GetExactCoordinates(
 16286            secondOrigin,
 16287            secondLocalPoint,
 16288            secondLocalDisplacement,
 16289            secondExactLocalTerm,
 16290            FixedMath.Cos(secondAngleInRadians),
 16291            FixedMath.Sin(secondAngleInRadians),
 16292            out Signed320 secondX,
 16293            out Signed320 secondY);
 16294        x = WideArithmetic.SubtractSigned320(firstX, secondX);
 16295        y = WideArithmetic.SubtractSigned320(firstY, secondY);
 16296        denominator = GetExactAnchorDenominator();
 16297    }
 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    {
 111309        WideVector2dTransform.GetRotationNumerators(
 111310            localPoint,
 111311            localDisplacement,
 111312            cosine,
 111313            sine,
 111314            out Signed320 roundedX,
 111315            out Signed320 roundedY);
 111316        Signed320 scaledRoundedX = Signed320.NarrowValue(
 111317            WideArithmetic.MultiplySigned320(
 111318                WideArithmetic.AddSigned320(
 111319                    WideVector2dTransform.Scale(origin.X),
 111320                    roundedX),
 111321                Signed320.ExtendValue(
 111322                    FixedPointAnchorTerm3d.Denominator)));
 111323        Signed320 scaledRoundedY = Signed320.NarrowValue(
 111324            WideArithmetic.MultiplySigned320(
 111325                WideArithmetic.AddSigned320(
 111326                    WideVector2dTransform.Scale(origin.Y),
 111327                    roundedY),
 111328                Signed320.ExtendValue(
 111329                    FixedPointAnchorTerm3d.Denominator)));
 111330        Signed320 residualX = WideArithmetic.SubtractSigned320(
 111331            WideArithmetic.MultiplySigned192(
 111332                Signed192.Signed(exactLocalTerm.X),
 111333                Signed192.Raw(cosine)),
 111334            WideArithmetic.MultiplySigned192(
 111335                Signed192.Signed(exactLocalTerm.Y),
 111336                Signed192.Raw(sine)));
 111337        Signed320 residualY = WideArithmetic.AddSigned320(
 111338            WideArithmetic.MultiplySigned192(
 111339                Signed192.Signed(exactLocalTerm.X),
 111340                Signed192.Raw(sine)),
 111341            WideArithmetic.MultiplySigned192(
 111342                Signed192.Signed(exactLocalTerm.Y),
 111343                Signed192.Raw(cosine)));
 111344        x = WideArithmetic.AddSigned320(scaledRoundedX, residualX);
 111345        y = WideArithmetic.AddSigned320(scaledRoundedY, residualY);
 111346    }
 347
 348    private static Signed320 GetExactAnchorDenominator() =>
 25349        WideArithmetic.MultiplySigned192(
 25350            Signed192.One,
 25351            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()