< Summary

Information
Class: Gravitas.CollisionHandling.ContactWarmStartGroup
Assembly: Gravitas
File(s): /home/runner/work/Gravitas/Gravitas/src/Gravitas/CollisionHandling/Contacts/3D/ContactWarmStartGroup.cs
Line coverage
100%
Covered lines: 66
Uncovered lines: 0
Coverable lines: 66
Total lines: 163
Line coverage: 100%
Branch coverage
100%
Covered branches: 40
Total branches: 40
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Set(...)100%1414100%
TryGet(...)100%44100%
Remove(...)100%66100%
GetContactId(...)100%44100%
GetImpulseUnchecked(...)100%44100%
SetContactIdUnchecked(...)100%44100%
SetImpulseUnchecked(...)100%44100%

File(s)

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

#LineLine coverage
 1//=======================================================================
 2// ContactWarmStartGroup.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;
 10
 11namespace Gravitas.CollisionHandling;
 12
 13/// <summary>
 14/// Four cached point impulses belonging to one geometric surface group.
 15/// </summary>
 16internal struct ContactWarmStartGroup
 17{
 18    internal ContactGroupKey Key;
 19    private ulong _contactId0;
 20    private ulong _contactId1;
 21    private ulong _contactId2;
 22    private ulong _contactId3;
 23    private ContactWarmStartImpulse _impulse0;
 24    private ContactWarmStartImpulse _impulse1;
 25    private ContactWarmStartImpulse _impulse2;
 26    private ContactWarmStartImpulse _impulse3;
 27
 28    public int Count { get; private set; }
 29
 30    public void Set(
 31        ulong contactId,
 32        Vector3d normal,
 33        Fixed64 normalImpulse,
 34        Fixed64 tangentImpulse,
 35        Fixed64 secondaryTangentImpulse = default)
 36    {
 96169337        ContactWarmStartImpulse impulse = new(normal, normalImpulse, tangentImpulse, secondaryTangentImpulse);
 392240638        for (int i = 0; i < Count; i++)
 39        {
 185317840            if (GetContactId(i) != contactId)
 41                continue;
 42
 85366843            SetImpulseUnchecked(i, impulse);
 85366844            return;
 45        }
 46
 47        // Retain() removes vanished features before solving. A full stale cache
 48        // still stays bounded; insert by identity so replay is independent of discovery.
 10802549        int insertion = 0;
 32221150        while (insertion < Count && GetContactId(insertion) < contactId) insertion++;
 10802551        int last = Count < ContactManifold.MaxContactsPerGroup
 10802552            ? Count++ : ContactManifold.MaxContactsPerGroup - 1;
 10802653        if (insertion > last) insertion = last;
 21640454        for (int i = last; i > insertion; i--)
 55        {
 17756            SetContactIdUnchecked(i, GetContactId(i - 1));
 17757            SetImpulseUnchecked(i, GetImpulseUnchecked(i - 1));
 58        }
 10802559        SetContactIdUnchecked(insertion, contactId);
 10802560        SetImpulseUnchecked(insertion, impulse);
 10802561    }
 62
 63    public bool TryGet(ulong contactId, out ContactWarmStartImpulse impulse)
 64    {
 318946865        for (int i = 0; i < Count; i++)
 66        {
 153910767            if (GetContactId(i) != contactId)
 68                continue;
 69
 72015670            impulse = GetImpulseUnchecked(i);
 72015671            return true;
 72        }
 73
 5562774        impulse = default;
 5562775        return false;
 76    }
 77
 78    public bool Remove(ulong contactId)
 79    {
 25414080        for (int index = 0; index < Count; index++)
 81        {
 12706882            if (GetContactId(index) != contactId)
 83                continue;
 84
 6731785            Count--;
 13487286            for (int shift = index; shift < Count; shift++)
 87            {
 11988                SetContactIdUnchecked(
 11989                    shift,
 11990                    GetContactId(shift + 1));
 11991                SetImpulseUnchecked(
 11992                    shift,
 11993                    GetImpulseUnchecked(shift + 1));
 94            }
 95
 6731796            SetContactIdUnchecked(Count, 0UL);
 6731797            SetImpulseUnchecked(Count, default);
 6731798            return true;
 99        }
 100
 2101        return false;
 102    }
 103
 104    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 105    internal ulong GetContactId(int index) =>
 3780737106        index switch
 3780737107        {
 1897020108            0 => _contactId0,
 1057789109            1 => _contactId1,
 576984110            2 => _contactId2,
 248944111            _ => _contactId3
 3780737112        };
 113
 114    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 115    internal ContactWarmStartImpulse GetImpulseUnchecked(int index) =>
 724347116        index switch
 724347117        {
 321562118            0 => _impulse0,
 180821119            1 => _impulse1,
 122374120            2 => _impulse2,
 99590121            _ => _impulse3
 724347122        };
 123
 124    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 125    private void SetContactIdUnchecked(int index, ulong contactId)
 126    {
 127        switch (index)
 128        {
 129            case 0:
 80964130                _contactId0 = contactId;
 80964131                break;
 132            case 1:
 44033133                _contactId1 = contactId;
 44033134                break;
 135            case 2:
 28567136                _contactId2 = contactId;
 28567137                break;
 138            default:
 22074139                _contactId3 = contactId;
 140                break;
 141        }
 22074142    }
 143
 144    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 145    private void SetImpulseUnchecked(int index, ContactWarmStartImpulse impulse)
 146    {
 147        switch (index)
 148        {
 149            case 0:
 439514150                _impulse0 = impulse;
 439514151                break;
 152            case 1:
 265646153                _impulse1 = impulse;
 265646154                break;
 155            case 2:
 178456156                _impulse2 = impulse;
 178456157                break;
 158            default:
 145690159                _impulse3 = impulse;
 160                break;
 161        }
 145690162    }
 163}