< Summary

Information
Class: Gravitas.CollisionHandling.ContactAnchor2D
Assembly: Gravitas
File(s): /home/runner/work/Gravitas/Gravitas/src/Gravitas/CollisionHandling/Contacts/2D/ContactAnchor2D.cs
Line coverage
100%
Covered lines: 56
Uncovered lines: 0
Coverable lines: 56
Total lines: 207
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
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%
.ctor(...)100%11100%
get_Origin()100%11100%
get_LocalPoint()100%11100%
get_LocalDisplacement()100%11100%
CompareLocalFeature(...)100%11100%
GetLocalFeatureHash64()100%11100%
get_Rotation()100%11100%
get_Offset()100%11100%
FromWorldPoint(...)100%11100%
TryGetOffset(...)100%11100%
TryGetWorldPoint(...)100%11100%
TryGetOffsetFrom(...)100%11100%
TryGetOffsetFrom(...)100%11100%
GetXZLeverFrom(...)100%11100%
TryRebase(...)100%22100%
TryRebase(...)100%22100%

File(s)

/home/runner/work/Gravitas/Gravitas/src/Gravitas/CollisionHandling/Contacts/2D/ContactAnchor2D.cs

#LineLine coverage
 1//=======================================================================
 2// ContactAnchor2D.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;
 9using FixedMathSharp.Geometry;
 10using System.Runtime.CompilerServices;
 11
 12namespace Gravitas.CollisionHandling;
 13
 14/// <summary>
 15/// Represents a planar contact point as a world-space origin plus a rotated
 16/// local offset.
 17/// </summary>
 18public readonly struct ContactAnchor2D
 19{
 20    private readonly FixedPointAnchor2d _point;
 21
 22    /// <summary>
 23    /// Creates a planar contact anchor in an identity-rotation rigid frame.
 24    /// </summary>
 25    public ContactAnchor2D(Vector2d origin, Vector2d offset)
 2742426        : this(origin, Fixed64.Zero, offset)
 27    {
 2742428    }
 29
 30    /// <summary>
 31    /// Creates a planar contact anchor from a canonical local point.
 32    /// </summary>
 33    public ContactAnchor2D(
 34        Vector2d origin,
 35        Fixed64 rotation,
 36        Vector2d localPoint)
 3265137        : this(origin, rotation, localPoint, Vector2d.Zero)
 38    {
 3265139    }
 40
 41    /// <summary>
 42    /// Creates a planar anchor whose two local terms remain separate until
 43    /// the exact rotated point is evaluated.
 44    /// </summary>
 45    public ContactAnchor2D(
 46        Vector2d origin,
 47        Fixed64 rotation,
 48        Vector2d localPoint,
 49        Vector2d localDisplacement)
 50    {
 3292751        _point = new FixedPointAnchor2d(
 3292752            origin,
 3292753            rotation,
 3292754            localPoint,
 3292755            localDisplacement);
 3292756    }
 57
 58    /// <summary>
 59    /// Creates a physics-domain wrapper for an exact planar point anchor.
 60    /// </summary>
 61    public ContactAnchor2D(FixedPointAnchor2d point)
 62    {
 2705663        _point = point;
 2705664    }
 65
 66    /// <summary>
 67    /// Gets the representable world-space origin.
 68    /// </summary>
 279569    public Vector2d Origin => _point.Origin;
 70
 71    /// <summary>
 72    /// Gets the point in the contact frame's local coordinates.
 73    /// </summary>
 272674    public Vector2d LocalPoint => _point.LocalPoint;
 75
 76    /// <summary>
 77    /// Gets the secondary local displacement retained separately from
 78    /// <see cref="LocalPoint"/>.
 79    /// </summary>
 272480    public Vector2d LocalDisplacement => _point.LocalDisplacement;
 81
 82    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 83    internal int CompareLocalFeature(in ContactAnchor2D other) =>
 77384        _point.CompareLocalFeature(other._point);
 85
 86    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 87    internal ulong GetLocalFeatureHash64() =>
 162088        _point.GetLocalFeatureHash64();
 89
 90    /// <summary>
 91    /// Gets the rotation applied to <see cref="LocalPoint"/>.
 92    /// </summary>
 150393    public Fixed64 Rotation => _point.Rotation;
 94
 95    /// <summary>
 96    /// Gets the rotated composite offset from the rigid-frame origin.
 97    /// </summary>
 98    /// <exception cref="System.InvalidOperationException">
 99    /// The rotated offset is outside the representable scalar domain. Use
 100    /// <see cref="TryGetOffset(out Vector2d)"/> when handling domain edges.
 101    /// </exception>
 102    public Vector2d Offset
 103    {
 104        get
 105        {
 44106            bool succeeded = TryGetOffset(out Vector2d offset);
 44107            SwiftThrowHelper.ThrowIfTrue(
 44108                !succeeded,
 44109                nameof(Offset),
 44110                "The planar contact offset is outside the representable coordinate domain.");
 43111            return offset;
 112        }
 113    }
 114
 115    /// <summary>
 116    /// Creates an anchor for an already materialized world point.
 117    /// </summary>
 118    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 119    public static ContactAnchor2D FromWorldPoint(Vector2d point) =>
 2443120        new(Vector2d.Zero, point);
 121
 122    /// <summary>
 123    /// Attempts to materialize the rotated composite rigid-frame offset.
 124    /// </summary>
 125    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 126    public bool TryGetOffset(out Vector2d offset) =>
 47127        _point.TryGetOffsetFrom(
 47128            new FixedPointAnchor2d(
 47129                Origin,
 47130                Fixed64.Zero,
 47131                Vector2d.Zero),
 47132            out offset);
 133
 134    /// <summary>
 135    /// Attempts to materialize the absolute world point without saturation.
 136    /// </summary>
 137    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 138    public bool TryGetWorldPoint(out Vector2d point) =>
 373139        _point.TryGetPoint(out point);
 140
 141    /// <summary>
 142    /// Attempts to rebase the conceptual point onto another origin with one
 143    /// final exact component-wise add/subtract operation.
 144    /// </summary>
 145    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 146    public bool TryGetOffsetFrom(Vector2d origin, out Vector2d offset) =>
 1923147        _point.TryGetOffsetFrom(
 1923148            new FixedPointAnchor2d(
 1923149                origin,
 1923150                Fixed64.Zero,
 1923151                Vector2d.Zero),
 1923152            out offset);
 153
 154    /// <summary>
 155    /// Attempts to obtain this point's exact world-space offset from another
 156    /// planar contact anchor.
 157    /// </summary>
 158    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 159    public bool TryGetOffsetFrom(
 160        ContactAnchor2D other,
 161        out Vector2d offset) =>
 5130162        _point.TryGetOffsetFrom(other._point, out offset);
 163
 164    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 165    internal ExactLever3D GetXZLeverFrom(in ContactAnchor2D other) =>
 192166        ExactLever3D.CreateXZ(_point, other._point);
 167
 168    /// <summary>
 169    /// Attempts to express the same conceptual point relative to another
 170    /// representable origin.
 171    /// </summary>
 172    public bool TryRebase(Vector2d origin, out ContactAnchor2D anchor)
 173    {
 2174        if (!_point.TryReframe(
 2175                origin,
 2176                Fixed64.Zero,
 2177                out FixedPointAnchor2d point))
 178        {
 1179            anchor = default;
 1180            return false;
 181        }
 182
 1183        anchor = new ContactAnchor2D(point);
 1184        return true;
 185    }
 186
 187    /// <summary>
 188    /// Attempts to express the same conceptual point in another rotated frame.
 189    /// </summary>
 190    public bool TryRebase(
 191        Vector2d origin,
 192        Fixed64 rotation,
 193        out ContactAnchor2D anchor)
 194    {
 2195        if (!_point.TryReframe(
 2196                origin,
 2197                rotation,
 2198                out FixedPointAnchor2d point))
 199        {
 1200            anchor = default;
 1201            return false;
 202        }
 203
 1204        anchor = new ContactAnchor2D(point);
 1205        return true;
 206    }
 207}