< Summary

Information
Class: FixedMathSharp.Geometry.FixedPointAnchor2d
Assembly: FixedMathSharp
File(s): /home/runner/work/FixedMathSharp/FixedMathSharp/src/FixedMathSharp/Geometry/Anchors/FixedPointAnchor2d.cs
Line coverage
100%
Covered lines: 123
Uncovered lines: 0
Coverable lines: 123
Total lines: 314
Line coverage: 100%
Branch coverage
100%
Covered branches: 32
Total branches: 32
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%
.ctor(...)100%11100%
.ctor(...)100%11100%
TryGetPoint(...)100%11100%
TryGetOffsetFrom(...)100%11100%
CompareSquaredDistance(...)100%11100%
TryGetLocalPointIn(...)100%11100%
TryReframe(...)100%88100%
CompareLocalFeature(...)100%1010100%
GetLocalFeatureHash64()100%11100%
Equals(...)100%88100%
Equals(...)100%22100%
GetHashCode()100%22100%
op_Equality(...)100%11100%
op_Inequality(...)100%11100%
CompareRaw(...)100%22100%
KeepHash(...)100%11100%

File(s)

/home/runner/work/FixedMathSharp/FixedMathSharp/src/FixedMathSharp/Geometry/Anchors/FixedPointAnchor2d.cs

#LineLine coverage
 1//=======================================================================
 2// FixedPointAnchor2d.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;
 9
 10namespace FixedMathSharp.Geometry;
 11
 12/// <summary>
 13/// Identifies a conceptual 2D point by an origin, scalar rotation, and local
 14/// point without requiring the transformed point to be representable.
 15/// </summary>
 16/// <remarks>
 17/// Relation-produced anchors can retain an exact sub-lattice feature term that
 18/// is intentionally hidden behind the narrow comparison and materialization
 19/// APIs. Anchors with identical rounded public local components can therefore
 20/// remain distinct when they identify different exact features.
 21/// </remarks>
 22public readonly struct FixedPointAnchor2d : IEquatable<FixedPointAnchor2d>
 23{
 24    /// <summary>
 25    /// Gets the point frame's world-space origin.
 26    /// </summary>
 27    public Vector2d Origin { get; }
 28
 29    /// <summary>
 30    /// Gets the point frame's counterclockwise rotation in radians.
 31    /// </summary>
 32    public Fixed64 Rotation { get; }
 33
 34    /// <summary>
 35    /// Gets the point in its supplied local frame.
 36    /// </summary>
 37    public Vector2d LocalPoint { get; }
 38
 39    /// <summary>
 40    /// An additional local-space feature displacement that exact operations
 41    /// add to <see cref="LocalPoint"/> without a scalar intermediate.
 42    /// </summary>
 43    public Vector2d LocalDisplacement { get; }
 44
 45    /// <summary>
 46    /// Creates a transformed point anchor.
 47    /// </summary>
 48    public FixedPointAnchor2d(
 49        Vector2d origin,
 50        Fixed64 rotation,
 51        Vector2d localPoint)
 14552        : this(origin, rotation, localPoint, Vector2d.Zero)
 14553    { }
 54
 55    /// <summary>
 56    /// Creates a transformed point anchor from two additive local-space
 57    /// feature components.
 58    /// </summary>
 59    public FixedPointAnchor2d(
 60        Vector2d origin,
 61        Fixed64 rotation,
 62        Vector2d localPoint,
 63        Vector2d localDisplacement)
 15364        : this(
 15365            origin,
 15366            rotation,
 15367            localPoint,
 15368            localDisplacement,
 15369            default)
 15370    { }
 71
 72    internal FixedPointAnchor2d(
 73        Vector2d origin,
 74        Fixed64 rotation,
 75        Vector2d localPoint,
 76        Vector2d localDisplacement,
 77        FixedPointAnchorTerm2d exactLocalTerm)
 78    {
 37379        Origin = origin;
 37380        Rotation = rotation;
 37381        LocalPoint = localPoint;
 37382        LocalDisplacement = localDisplacement;
 37383        ExactLocalTerm = exactLocalTerm;
 37384    }
 85
 86    internal FixedPointAnchorTerm2d ExactLocalTerm { get; }
 87
 88    /// <summary>
 89    /// Attempts to materialize the conceptual world-space point with one
 90    /// final round-half-to-even conversion per component.
 91    /// </summary>
 92    public bool TryGetPoint(out Vector2d point) =>
 5193        WidePointAnchor2d.TryGetPoint(
 5194            Origin,
 5195            LocalPoint,
 5196            LocalDisplacement,
 5197            ExactLocalTerm,
 5198            Rotation,
 5199            out point);
 100
 101    /// <summary>
 102    /// Attempts to obtain this point's exact offset from another transformed
 103    /// point without materializing either world-space point.
 104    /// </summary>
 105    public bool TryGetOffsetFrom(
 106        in FixedPointAnchor2d other,
 107        out Vector2d offset) =>
 46108        WidePointAnchor2d.TryGetRelativeOffset(
 46109            Origin,
 46110            LocalPoint,
 46111            LocalDisplacement,
 46112            ExactLocalTerm,
 46113            Rotation,
 46114            other.Origin,
 46115            other.LocalPoint,
 46116            other.LocalDisplacement,
 46117            other.ExactLocalTerm,
 46118            other.Rotation,
 46119            out offset);
 120
 121    /// <summary>
 122    /// Compares the exact squared distance from this point to two other
 123    /// anchored points.
 124    /// </summary>
 125    /// <returns>
 126    /// A negative value when <paramref name="first"/> is closer, zero when the
 127    /// distances are equal, or a positive value when
 128    /// <paramref name="second"/> is closer.
 129    /// </returns>
 130    public int CompareSquaredDistance(
 131        in FixedPointAnchor2d first,
 132        in FixedPointAnchor2d second) =>
 6133        WidePointAnchor2d.CompareSquaredDistances(
 6134            this,
 6135            first,
 6136            second);
 137
 138    /// <summary>
 139    /// Attempts to express this conceptual point in another rotated frame
 140    /// without materializing the world-space point.
 141    /// </summary>
 142    public bool TryGetLocalPointIn(
 143        Vector2d frameOrigin,
 144        Fixed64 frameRotation,
 145        out Vector2d localPoint) =>
 76146        WidePointAnchor2d.TryGetLocalPointIn(
 76147            Origin,
 76148            LocalPoint,
 76149            LocalDisplacement,
 76150            ExactLocalTerm,
 76151            Rotation,
 76152            frameOrigin,
 76153            frameRotation,
 76154            out localPoint);
 155
 156    /// <summary>
 157    /// Attempts to express this exact conceptual point in another rotated
 158    /// frame without discarding sub-lattice feature information.
 159    /// </summary>
 160    /// <remarks>
 161    /// The operation fails when the target frame would require a general
 162    /// rational local coordinate that a compact point anchor cannot retain.
 163    /// </remarks>
 164    public bool TryReframe(
 165        Vector2d frameOrigin,
 166        Fixed64 frameRotation,
 167        out FixedPointAnchor2d anchor)
 168    {
 37169        if (Origin == frameOrigin && Rotation == frameRotation)
 170        {
 1171            anchor = this;
 1172            return true;
 173        }
 36174        if (!TryGetLocalPointIn(
 36175                frameOrigin,
 36176                frameRotation,
 36177                out Vector2d localPoint))
 178        {
 1179            anchor = default;
 1180            return false;
 181        }
 182
 35183        var candidate = new FixedPointAnchor2d(
 35184            frameOrigin,
 35185            frameRotation,
 35186            localPoint);
 35187        if (!WidePointAnchor2d.RepresentsSamePoint(
 35188                this,
 35189                candidate))
 190        {
 1191            anchor = default;
 1192            return false;
 193        }
 194
 34195        anchor = candidate;
 34196        return true;
 197    }
 198
 199    /// <summary>
 200    /// Compares the complete local feature identity of this anchor with
 201    /// <paramref name="other"/> in deterministic component order.
 202    /// </summary>
 203    /// <remarks>
 204    /// Frame origin and rotation are intentionally excluded. Exact
 205    /// sub-lattice centered-axis residuals are included after the public local
 206    /// components.
 207    /// </remarks>
 208    public int CompareLocalFeature(in FixedPointAnchor2d other)
 209    {
 15210        int comparison = CompareRaw(
 15211            LocalPoint.X.m_rawValue,
 15212            other.LocalPoint.X.m_rawValue);
 15213        if (comparison != 0)
 2214            return comparison;
 13215        comparison = CompareRaw(
 13216            LocalPoint.Y.m_rawValue,
 13217            other.LocalPoint.Y.m_rawValue);
 13218        if (comparison != 0)
 2219            return comparison;
 11220        comparison = CompareRaw(
 11221            LocalDisplacement.X.m_rawValue,
 11222            other.LocalDisplacement.X.m_rawValue);
 11223        if (comparison != 0)
 2224            return comparison;
 9225        comparison = CompareRaw(
 9226            LocalDisplacement.Y.m_rawValue,
 9227            other.LocalDisplacement.Y.m_rawValue);
 9228        if (comparison != 0)
 2229            return comparison;
 7230        comparison = CompareRaw(
 7231            ExactLocalTerm.X,
 7232            other.ExactLocalTerm.X);
 7233        return comparison != 0
 7234            ? comparison
 7235            : CompareRaw(
 7236                ExactLocalTerm.Y,
 7237                other.ExactLocalTerm.Y);
 238    }
 239
 240    /// <summary>
 241    /// Returns a stable 64-bit hash of the complete local feature identity.
 242    /// </summary>
 243    public ulong GetLocalFeatureHash64()
 244    {
 18245        ulong hash = 14695981039346656037UL;
 18246        KeepHash(ref hash, LocalPoint.X.m_rawValue);
 18247        KeepHash(ref hash, LocalPoint.Y.m_rawValue);
 18248        KeepHash(ref hash, LocalDisplacement.X.m_rawValue);
 18249        KeepHash(ref hash, LocalDisplacement.Y.m_rawValue);
 18250        KeepHash(ref hash, ExactLocalTerm.X);
 18251        KeepHash(ref hash, ExactLocalTerm.Y);
 18252        return hash;
 253    }
 254
 255    /// <summary>
 256    /// Returns whether both anchors have identical frames, rounded local
 257    /// components, and any retained exact feature identity.
 258    /// </summary>
 259    public bool Equals(FixedPointAnchor2d other) =>
 17260        Origin == other.Origin
 17261        && Rotation == other.Rotation
 17262        && LocalPoint == other.LocalPoint
 17263        && LocalDisplacement == other.LocalDisplacement
 17264        && ExactLocalTerm.Equals(other.ExactLocalTerm);
 265
 266    /// <summary>
 267    /// Returns a hash code for the complete frame and exact feature identity.
 268    /// </summary>
 269    public override bool Equals(object? obj) =>
 4270        obj is FixedPointAnchor2d other && Equals(other);
 271
 272    /// <inheritdoc />
 273    public override int GetHashCode()
 274    {
 275        unchecked
 276        {
 5277            int hash = 17;
 5278            hash = (hash * 31) + Origin.GetHashCode();
 5279            hash = (hash * 31) + Rotation.GetHashCode();
 5280            hash = (hash * 31) + LocalPoint.GetHashCode();
 5281            hash = (hash * 31) + LocalDisplacement.GetHashCode();
 5282            if (!ExactLocalTerm.IsZero)
 2283                hash = (hash * 31) + ExactLocalTerm.GetHashCode();
 5284            return hash;
 285        }
 286    }
 287
 288    /// <summary>
 289    /// Returns whether two anchors have identical frame and local-point
 290    /// components.
 291    /// </summary>
 292    public static bool operator ==(
 293        FixedPointAnchor2d left,
 294        FixedPointAnchor2d right) =>
 2295        left.Equals(right);
 296
 297    /// <summary>
 298    /// Returns whether two anchors differ in any frame or local-point
 299    /// component.
 300    /// </summary>
 301    public static bool operator !=(
 302        FixedPointAnchor2d left,
 303        FixedPointAnchor2d right) =>
 2304        !left.Equals(right);
 305
 306    private static int CompareRaw(long left, long right) =>
 59307        left < right ? -1 : left > right ? 1 : 0;
 308
 309    private static void KeepHash(ref ulong hash, long value)
 310    {
 108311        hash ^= unchecked((ulong)value);
 108312        hash *= 1099511628211UL;
 108313    }
 314}