< Summary

Information
Class: Gravitas.CollisionHandling.ContactWarmStartCache
Assembly: Gravitas
File(s): /home/runner/work/Gravitas/Gravitas/src/Gravitas/CollisionHandling/Contacts/3D/ContactWarmStartCache.cs
Line coverage
100%
Covered lines: 70
Uncovered lines: 0
Coverable lines: 70
Total lines: 149
Line coverage: 100%
Branch coverage
100%
Covered branches: 66
Total branches: 66
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_Count()100%44100%
GetGroup(...)100%22100%
SetGroup(...)100%22100%
Clear()100%22100%
Set(...)100%11100%
Set(...)100%1818100%
TryGet(...)100%11100%
TryGet(...)100%44100%
Remove(...)100%11100%
Remove(...)100%88100%
Retain(...)100%1414100%
Find(...)100%88100%
RemoveGroup(...)100%44100%

File(s)

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

#LineLine coverage
 1//=======================================================================
 2// ContactWarmStartCache.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;
 9
 10namespace Gravitas.CollisionHandling;
 11
 12
 13
 14
 15/// <summary>Retained pair-owned warm starts with independent four-point groups.</summary>
 16internal struct ContactWarmStartCache
 17{
 18    private ContactWarmStartGroup first;
 19    private SwiftCollections.SwiftList<ContactWarmStartGroup>? additional;
 20    internal int GroupCount { get; private set; }
 21    public int Count
 22    {
 23        get
 24        {
 143225            int count = 0;
 856726            for (int i = 0; i < GroupCount; i++) count += i == 0 ? first.Count : additional!.InnerArray[i - 1].Count;
 143227            return count;
 28        }
 29    }
 30
 31    internal ContactWarmStartGroup GetGroup(int index) =>
 774332        index == 0 ? first : additional!.InnerArray[index - 1];
 33
 34    private void SetGroup(int index, in ContactWarmStartGroup value)
 35    {
 9166736        if (index == 0) first = value;
 1586937        else additional!.InnerArray[index - 1] = value;
 1586938    }
 39
 40    public void Clear()
 41    {
 7730442        GroupCount = 0;
 7730443        first = default;
 7730444        additional?.FastClear();
 1298245    }
 46
 47    public void Set(ulong contactId, Vector3d normal, Fixed64 normalImpulse,
 48        Fixed64 tangentImpulse, Fixed64 secondaryTangentImpulse = default) =>
 7449        Set(default, contactId, normal, normalImpulse, tangentImpulse, secondaryTangentImpulse);
 50
 51    internal void Set(in ContactGroupKey key, ulong contactId, Vector3d normal,
 52        Fixed64 normalImpulse, Fixed64 tangentImpulse, Fixed64 secondaryTangentImpulse)
 53    {
 192338654        int index = 0, order = -1;
 115629255        while (index < GroupCount)
 56        {
 57            // Key lookup must not copy the group's four retained impulse rows.
 110967658            ref readonly ContactWarmStartGroup candidate = ref (index == 0 ? ref first : ref additional!.InnerArray[inde
 110967659            order = candidate.Key.CompareTo(key);
 110967660            if (order >= 0) break;
 19459961            index++;
 62        }
 96169363        if (index == GroupCount || order != 0)
 64        {
 4791465            if (GroupCount > 0)
 66            {
 1131467                additional ??= new SwiftCollections.SwiftList<ContactWarmStartGroup>();
 1131468                additional.Add(default);
 4013369                for (int i = GroupCount; i > index; i--) SetGroup(i, GetGroup(i - 1));
 70            }
 4791471            SetGroup(index, new ContactWarmStartGroup { Key = key });
 4791472            GroupCount++;
 73        }
 96169374        ref ContactWarmStartGroup current = ref (index == 0 ? ref first : ref additional!.InnerArray[index - 1]);
 96169375        current.Set(contactId, normal, normalImpulse, tangentImpulse, secondaryTangentImpulse);
 96169376    }
 77
 78    public bool TryGet(ulong contactId, out ContactWarmStartImpulse impulse) =>
 8079        TryGet(default, contactId, out impulse);
 80
 81    internal bool TryGet(in ContactGroupKey key, ulong contactId, out ContactWarmStartImpulse impulse)
 82    {
 89378283        int index = Find(key);
 89378284        if (index >= 0)
 77578385            return index == 0 ? first.TryGet(contactId, out impulse)
 77578386                : additional!.InnerArray[index - 1].TryGet(contactId, out impulse);
 11799987        impulse = default;
 11799988        return false;
 89    }
 90
 291    public bool Remove(ulong contactId) => Remove(default, contactId);
 92
 93    internal bool Remove(in ContactGroupKey key, ulong contactId)
 94    {
 2095        int index = Find(key);
 2496        if (index < 0) return false;
 1697        ref ContactWarmStartGroup current = ref (index == 0 ? ref first : ref additional!.InnerArray[index - 1]);
 1898        if (!current.Remove(contactId)) return false;
 2799        if (current.Count == 0) RemoveGroup(index);
 14100        return true;
 101    }
 102
 103    internal void Retain(ContactManifold manifold)
 104    {
 389138105        for (int group = GroupCount - 1; group >= 0; group--)
 106        {
 79388107            ref ContactWarmStartGroup cached = ref (group == 0 ? ref first : ref additional!.InnerArray[group - 1]);
 79388108            int current = manifold.FindGroup(cached.Key);
 79388109            if (current < 0)
 110            {
 1313111                RemoveGroup(group);
 1313112                continue;
 113            }
 78075114            ref ContactGroup admitted = ref manifold.GetGroup(current);
 456098115            for (int point = cached.Count - 1; point >= 0; point--)
 116            {
 149974117                ulong id = cached.GetContactId(point);
 149974118                bool present = false;
 1529345119                for (int i = 0; i < admitted.Count; i++) present |= admitted[i].ContactId == id;
 217277120                if (!present) cached.Remove(id);
 121            }
 110993122            if (cached.Count == 0) RemoveGroup(group);
 123        }
 115181124    }
 125
 126    private int Find(in ContactGroupKey key)
 127    {
 1787604128        int low = 0, high = GroupCount - 1;
 1043441129        while (low <= high)
 130        {
 925438131            int middle = low + ((high - low) >> 1);
 925438132            ref readonly ContactWarmStartGroup current = ref (middle == 0 ? ref first : ref additional!.InnerArray[middl
 925438133            int order = current.Key.CompareTo(key);
 1701237134            if (order == 0) return middle;
 279599135            if (order < 0) low = middle + 1;
 19679136            else high = middle - 1;
 137        }
 118003138        return -1;
 139    }
 140
 141    private void RemoveGroup(int index)
 142    {
 68545143        for (int i = index; i < GroupCount - 1; i++) SetGroup(i, GetGroup(i + 1));
 42857144        if (GroupCount > 1) additional!.RemoveAt(additional.Count - 1);
 25631145        else first = default;
 34244146        GroupCount--;
 34244147    }
 148
 149}