< Summary

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

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Add(...)100%1010100%
get_Item(...)100%11100%
FindShallowestReplacementIndex(...)100%1212100%
IsDeeper(...)100%66100%
HasEqualDepth(...)100%22100%
SortContactsById()100%66100%
GetContactUnchecked(...)100%44100%
SetContactUnchecked(...)100%44100%

File(s)

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

#LineLine coverage
 1//=======================================================================
 2// ContactGroup.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;
 9
 10namespace Gravitas.CollisionHandling;
 11
 12/// <summary>Four inline point samples belonging to one geometric region.</summary>
 13internal struct ContactGroup
 14{
 15    internal ContactGroupKey Key;
 16    internal int Count;
 17    private ManifoldContact _contact0, _contact1, _contact2, _contact3;
 18
 19    internal int Add(in ManifoldContact contact)
 20    {
 111819621        for (int i = 0; i < Count; i++)
 22        {
 30251323            ManifoldContact existing = GetContactUnchecked(i);
 30251324            if (existing.ContactId != contact.ContactId)
 25                continue;
 123826            if (IsDeeper(contact, existing))
 427                SetContactUnchecked(i, contact);
 123828            SortContactsById();
 123829            return 0;
 30        }
 25658531        if (Count < ContactManifold.MaxContactsPerGroup)
 32        {
 23988633            SetContactUnchecked(Count++, contact);
 23988634            SortContactsById();
 23988635            return 1;
 36        }
 1669937        int replacement = FindShallowestReplacementIndex(contact);
 1669938        if (replacement >= 0)
 39        {
 286340            SetContactUnchecked(replacement, contact);
 286341            SortContactsById();
 42        }
 1669943        return 0;
 44    }
 45
 174147846    internal ManifoldContact this[int index] => GetContactUnchecked(index);
 47
 48    private int FindShallowestReplacementIndex(ManifoldContact candidate)
 49    {
 1669950        int replaceIndex = 0;
 1669951        ManifoldContact shallowest = _contact0;
 52
 13359253        for (int i = 1; i < Count; i++)
 54        {
 5009755            ManifoldContact contact = GetContactUnchecked(i);
 56            // Samples are already sorted by identity; a later equal-depth
 57            // sample loses the deterministic identity tie.
 5009758            if (IsDeeper(shallowest, contact) || HasEqualDepth(contact, shallowest))
 59            {
 4994560                shallowest = contact;
 4994561                replaceIndex = i;
 62            }
 63        }
 64
 1669965        if (IsDeeper(candidate, shallowest))
 4466            return replaceIndex;
 67
 1665568        if (HasEqualDepth(candidate, shallowest) && candidate.ContactId < shallowest.ContactId)
 281969            return replaceIndex;
 70
 1383671        return -1;
 72    }
 73
 74    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 75    private static bool IsDeeper(ManifoldContact candidate, ManifoldContact existing) =>
 6803476        candidate.Depth > existing.Depth
 6803477        || candidate.Depth == existing.Depth
 6803478        && candidate.DepthIsClamped
 6803479        && !existing.DepthIsClamped;
 80
 81    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 82    private static bool HasEqualDepth(ManifoldContact left, ManifoldContact right) =>
 6672983        left.Depth == right.Depth
 6672984        && left.DepthIsClamped == right.DepthIsClamped;
 85
 86    private void SortContactsById()
 87    {
 97835088        for (int i = 1; i < Count; i++)
 89        {
 24518890            ManifoldContact contact = GetContactUnchecked(i);
 24518891            int j = i - 1;
 33805392            while (j >= 0 && GetContactUnchecked(j).ContactId > contact.ContactId)
 93            {
 9286594                SetContactUnchecked(j + 1, GetContactUnchecked(j));
 9286595                j--;
 96            }
 97
 24518898            SetContactUnchecked(j + 1, contact);
 99        }
 243987100    }
 101
 102    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 103    private ManifoldContact GetContactUnchecked(int index) =>
 2734564104        index switch
 2734564105        {
 1165724106            0 => _contact0,
 770419107            1 => _contact1,
 490577108            2 => _contact2,
 307844109            _ => _contact3
 2734564110        };
 111
 112    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 113    private void SetContactUnchecked(int index, ManifoldContact contact)
 114    {
 115        switch (index)
 116        {
 117            case 0:
 149399118                _contact0 = contact;
 149399119                break;
 120            case 1:
 217588121                _contact1 = contact;
 217588122                break;
 123            case 2:
 139319124                _contact2 = contact;
 139319125                break;
 126            default:
 74500127                _contact3 = contact;
 128                break;
 129        }
 74500130    }
 131
 132}