< 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: 57
Uncovered lines: 0
Coverable lines: 57
Total lines: 208
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
get_GeometryPoint()100%11100%
.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 System.Runtime.CompilerServices;
 9using FixedMathSharp;
 10using FixedMathSharp.Geometry;
 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;
 7121    internal FixedPointAnchor2d GeometryPoint => _point;
 22
 23    /// <summary>
 24    /// Creates a planar contact anchor in an identity-rotation rigid frame.
 25    /// </summary>
 26    public ContactAnchor2D(Vector2d origin, Vector2d offset)
 2987227        : this(origin, Fixed64.Zero, offset)
 28    {
 2987229    }
 30
 31    /// <summary>
 32    /// Creates a planar contact anchor from a canonical local point.
 33    /// </summary>
 34    public ContactAnchor2D(
 35        Vector2d origin,
 36        Fixed64 rotation,
 37        Vector2d localPoint)
 5166338        : this(origin, rotation, localPoint, Vector2d.Zero)
 39    {
 5166340    }
 41
 42    /// <summary>
 43    /// Creates a planar anchor whose two local terms remain separate until
 44    /// the exact rotated point is evaluated.
 45    /// </summary>
 46    public ContactAnchor2D(
 47        Vector2d origin,
 48        Fixed64 rotation,
 49        Vector2d localPoint,
 50        Vector2d localDisplacement)
 51    {
 5194452        _point = new FixedPointAnchor2d(
 5194453            origin,
 5194454            rotation,
 5194455            localPoint,
 5194456            localDisplacement);
 5194457    }
 58
 59    /// <summary>
 60    /// Creates a physics-domain wrapper for an exact planar point anchor.
 61    /// </summary>
 62    public ContactAnchor2D(FixedPointAnchor2d point)
 63    {
 4874564        _point = point;
 4874565    }
 66
 67    /// <summary>
 68    /// Gets the representable world-space origin.
 69    /// </summary>
 610370    public Vector2d Origin => _point.Origin;
 71
 72    /// <summary>
 73    /// Gets the point in the contact frame's local coordinates.
 74    /// </summary>
 603275    public Vector2d LocalPoint => _point.LocalPoint;
 76
 77    /// <summary>
 78    /// Gets the secondary local displacement retained separately from
 79    /// <see cref="LocalPoint"/>.
 80    /// </summary>
 603081    public Vector2d LocalDisplacement => _point.LocalDisplacement;
 82
 83    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 84    internal int CompareLocalFeature(in ContactAnchor2D other) =>
 1083185        _point.CompareLocalFeature(other._point);
 86
 87    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 88    internal ulong GetLocalFeatureHash64() =>
 2278689        _point.GetLocalFeatureHash64();
 90
 91    /// <summary>
 92    /// Gets the rotation applied to <see cref="LocalPoint"/>.
 93    /// </summary>
 335394    public Fixed64 Rotation => _point.Rotation;
 95
 96    /// <summary>
 97    /// Gets the rotated composite offset from the rigid-frame origin.
 98    /// </summary>
 99    /// <exception cref="System.InvalidOperationException">
 100    /// The rotated offset is outside the representable scalar domain. Use
 101    /// <see cref="TryGetOffset(out Vector2d)"/> when handling domain edges.
 102    /// </exception>
 103    public Vector2d Offset
 104    {
 105        get
 106        {
 44107            bool succeeded = TryGetOffset(out Vector2d offset);
 44108            ChronicleGuard.ThrowIfTrue(
 44109                !succeeded,
 44110                nameof(Offset),
 44111                "The planar contact offset is outside the representable coordinate domain.");
 43112            return offset;
 113        }
 114    }
 115
 116    /// <summary>
 117    /// Creates an anchor for an already materialized world point.
 118    /// </summary>
 119    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 120    public static ContactAnchor2D FromWorldPoint(Vector2d point) =>
 4889121        new(Vector2d.Zero, point);
 122
 123    /// <summary>
 124    /// Attempts to materialize the rotated composite rigid-frame offset.
 125    /// </summary>
 126    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 127    public bool TryGetOffset(out Vector2d offset) =>
 47128        _point.TryGetOffsetFrom(
 47129            new FixedPointAnchor2d(
 47130                Origin,
 47131                Fixed64.Zero,
 47132                Vector2d.Zero),
 47133            out offset);
 134
 135    /// <summary>
 136    /// Attempts to materialize the absolute world point without saturation.
 137    /// </summary>
 138    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 139    public bool TryGetWorldPoint(out Vector2d point) =>
 1174140        _point.TryGetPoint(out point);
 141
 142    /// <summary>
 143    /// Attempts to rebase the conceptual point onto another origin with one
 144    /// final exact component-wise add/subtract operation.
 145    /// </summary>
 146    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 147    public bool TryGetOffsetFrom(Vector2d origin, out Vector2d offset) =>
 1918148        _point.TryGetOffsetFrom(
 1918149            new FixedPointAnchor2d(
 1918150                origin,
 1918151                Fixed64.Zero,
 1918152                Vector2d.Zero),
 1918153            out offset);
 154
 155    /// <summary>
 156    /// Attempts to obtain this point's exact world-space offset from another
 157    /// planar contact anchor.
 158    /// </summary>
 159    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 160    public bool TryGetOffsetFrom(
 161        ContactAnchor2D other,
 162        out Vector2d offset) =>
 34684163        _point.TryGetOffsetFrom(other._point, out offset);
 164
 165    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 166    internal ExactLever3D GetXZLeverFrom(in ContactAnchor2D other) =>
 964167        ExactLever3D.CreateXZ(_point, other._point);
 168
 169    /// <summary>
 170    /// Attempts to express the same conceptual point relative to another
 171    /// representable origin.
 172    /// </summary>
 173    public bool TryRebase(Vector2d origin, out ContactAnchor2D anchor)
 174    {
 2175        if (!_point.TryReframe(
 2176                origin,
 2177                Fixed64.Zero,
 2178                out FixedPointAnchor2d point))
 179        {
 1180            anchor = default;
 1181            return false;
 182        }
 183
 1184        anchor = new ContactAnchor2D(point);
 1185        return true;
 186    }
 187
 188    /// <summary>
 189    /// Attempts to express the same conceptual point in another rotated frame.
 190    /// </summary>
 191    public bool TryRebase(
 192        Vector2d origin,
 193        Fixed64 rotation,
 194        out ContactAnchor2D anchor)
 195    {
 2196        if (!_point.TryReframe(
 2197                origin,
 2198                rotation,
 2199                out FixedPointAnchor2d point))
 200        {
 1201            anchor = default;
 1202            return false;
 203        }
 204
 1205        anchor = new ContactAnchor2D(point);
 1206        return true;
 207    }
 208}