< Summary

Information
Class: Gravitas.CollisionHandling.ContactAnchor
Assembly: Gravitas
File(s): /home/runner/work/Gravitas/Gravitas/src/Gravitas/CollisionHandling/Contacts/3D/ContactAnchor.cs
Line coverage
100%
Covered lines: 62
Uncovered lines: 0
Coverable lines: 62
Total lines: 222
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
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%22100%
get_Origin()100%11100%
get_Rotation()100%11100%
get_IsValid()100%11100%
get_LocalPoint()100%11100%
get_LocalDisplacement()100%11100%
CompareLocalFeature(...)100%11100%
GetLocalFeatureHash64()100%11100%
get_Offset()100%11100%
FromWorldPoint(...)100%11100%
TryGetWorldPoint(...)100%11100%
TryGetOffset(...)100%11100%
TryGetOffsetFrom(...)100%11100%
TryGetOffsetFrom(...)100%11100%
GetLeverFrom(...)100%11100%
TryRebase(...)100%22100%
TryRebase(...)100%22100%

File(s)

/home/runner/work/Gravitas/Gravitas/src/Gravitas/CollisionHandling/Contacts/3D/ContactAnchor.cs

#LineLine coverage
 1//=======================================================================
 2// ContactAnchor.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 3D contact point in one rigid world-space frame.
 16/// </summary>
 17/// <remarks>
 18/// The underlying origin, rotation, and local point remain authoritative when
 19/// either the rotated offset or absolute world point lies outside the
 20/// <see cref="Fixed64"/> scalar range.
 21/// </remarks>
 22public readonly struct ContactAnchor
 23{
 24    private readonly FixedPointAnchor _point;
 26025    internal FixedPointAnchor GeometryPoint => _point;
 26
 27    /// <summary>
 28    /// Creates a contact anchor in an identity-rotation rigid frame.
 29    /// </summary>
 30    public ContactAnchor(Vector3d origin, Vector3d offset)
 38284531        : this(origin, FixedQuaternion.Identity, offset)
 32    {
 38284533    }
 34
 35    /// <summary>
 36    /// Creates a contact anchor in a rigid local frame.
 37    /// </summary>
 38    public ContactAnchor(
 39        Vector3d origin,
 40        FixedQuaternion rotation,
 41        Vector3d localPoint)
 79144342        : this(origin, rotation, localPoint, Vector3d.Zero)
 43    {
 79144344    }
 45
 46    /// <summary>
 47    /// Creates a contact anchor whose two local terms remain separate until
 48    /// the exact rotated point is evaluated.
 49    /// </summary>
 50    public ContactAnchor(
 51        Vector3d origin,
 52        FixedQuaternion rotation,
 53        Vector3d localPoint,
 54        Vector3d localDisplacement)
 55    {
 79411856        _point = new FixedPointAnchor(
 79411857            origin,
 79411858            rotation,
 79411859            localPoint,
 79411860            localDisplacement);
 79411861    }
 62
 63    /// <summary>
 64    /// Creates a physics-domain wrapper for an exact point anchor.
 65    /// </summary>
 66    public ContactAnchor(FixedPointAnchor point)
 67    {
 39714968        if (!point.Rotation.IsNormalized())
 69        {
 170            throw new System.ArgumentException(
 171                "The point anchor must contain a normalized rotation.",
 172                nameof(point));
 73        }
 74
 39714875        _point = point;
 39714876    }
 77
 78    /// <summary>
 79    /// Gets the representable world-space origin.
 80    /// </summary>
 3995381    public Vector3d Origin => _point.Origin;
 82
 83    /// <summary>
 84    /// Gets the normalized local-to-world frame rotation.
 85    /// </summary>
 129421186    public FixedQuaternion Rotation => _point.Rotation;
 87
 125566488    internal bool IsValid => Rotation.IsNormalized();
 89
 90    /// <summary>
 91    /// Gets the point in the contact frame's local coordinates.
 92    /// </summary>
 947793    public Vector3d LocalPoint => _point.LocalPoint;
 94
 95    /// <summary>
 96    /// Gets the secondary local displacement retained separately from
 97    /// <see cref="LocalPoint"/>.
 98    /// </summary>
 924199    public Vector3d LocalDisplacement => _point.LocalDisplacement;
 100
 101    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 102    internal int CompareLocalFeature(in ContactAnchor other) =>
 199734103        _point.CompareLocalFeature(other._point);
 104
 105    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 106    internal ulong GetLocalFeatureHash64() =>
 408860107        _point.GetLocalFeatureHash64();
 108
 109    /// <summary>
 110    /// Gets the rotated offset from <see cref="Origin"/> to the conceptual
 111    /// contact point.
 112    /// </summary>
 113    /// <exception cref="System.InvalidOperationException">
 114    /// The rotated offset lies outside the representable coordinate domain.
 115    /// </exception>
 116    public Vector3d Offset
 117    {
 118        get
 119        {
 13120            bool succeeded = TryGetOffset(out Vector3d offset);
 13121            ChronicleGuard.ThrowIfTrue(
 13122                !succeeded,
 13123                nameof(Offset),
 13124                "The contact offset is outside the representable coordinate domain.");
 12125            return offset;
 126        }
 127    }
 128
 129    /// <summary>
 130    /// Creates an anchor for an already materialized world point.
 131    /// </summary>
 132    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 133    public static ContactAnchor FromWorldPoint(Vector3d point) =>
 105104134        new(Vector3d.Zero, point);
 135
 136    /// <summary>
 137    /// Attempts to materialize the absolute world point without saturation.
 138    /// </summary>
 139    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 140    public bool TryGetWorldPoint(out Vector3d point) =>
 11597141        _point.TryGetPoint(out point);
 142
 143    /// <summary>
 144    /// Attempts to obtain the rotated offset from <see cref="Origin"/> to the
 145    /// conceptual contact point.
 146    /// </summary>
 147    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 148    public bool TryGetOffset(out Vector3d offset) =>
 15149        _point.TryGetOffsetFrom(
 15150            new FixedPointAnchor(
 15151                Origin,
 15152                FixedQuaternion.Identity,
 15153                Vector3d.Zero),
 15154            out offset);
 155
 156    /// <summary>
 157    /// Attempts to rebase the conceptual point onto another origin with one
 158    /// final exact component-wise add/subtract operation.
 159    /// </summary>
 160    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 161    public bool TryGetOffsetFrom(Vector3d origin, out Vector3d offset) =>
 2695162        _point.TryGetOffsetFrom(
 2695163            new FixedPointAnchor(
 2695164                origin,
 2695165                FixedQuaternion.Identity,
 2695166                Vector3d.Zero),
 2695167            out offset);
 168
 169    /// <summary>
 170    /// Attempts to obtain this point's exact world-space offset from another
 171    /// rigid-frame contact anchor.
 172    /// </summary>
 173    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 174    public bool TryGetOffsetFrom(
 175        in ContactAnchor other,
 176        out Vector3d offset) =>
 1606934177        _point.TryGetOffsetFrom(other._point, out offset);
 178
 179    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 180    internal ExactLever3D GetLeverFrom(in ContactAnchor other) =>
 30722181        ExactLever3D.Create(_point, other._point);
 182
 183    /// <summary>
 184    /// Attempts to express the same conceptual point relative to another
 185    /// representable origin.
 186    /// </summary>
 187    public bool TryRebase(Vector3d origin, out ContactAnchor anchor)
 188    {
 2189        if (!_point.TryReframe(
 2190                origin,
 2191                FixedQuaternion.Identity,
 2192                out FixedPointAnchor point))
 193        {
 1194            anchor = default;
 1195            return false;
 196        }
 197
 1198        anchor = new ContactAnchor(point);
 1199        return true;
 200    }
 201
 202    /// <summary>
 203    /// Attempts to express the same conceptual point in another rigid frame.
 204    /// </summary>
 205    public bool TryRebase(
 206        Vector3d origin,
 207        FixedQuaternion rotation,
 208        out ContactAnchor anchor)
 209    {
 3210        if (!_point.TryReframe(
 3211                origin,
 3212                rotation,
 3213                out FixedPointAnchor point))
 214        {
 1215            anchor = default;
 1216            return false;
 217        }
 218
 2219        anchor = new ContactAnchor(point);
 2220        return true;
 221    }
 222}