< Summary

Information
Class: Gravitas.CollisionHandling.ContactGroup2D
Assembly: Gravitas
File(s): /home/runner/work/Gravitas/Gravitas/src/Gravitas/CollisionHandling/Contacts/2D/ContactGroup2D.cs
Line coverage
100%
Covered lines: 45
Uncovered lines: 0
Coverable lines: 45
Total lines: 105
Line coverage: 100%
Branch coverage
100%
Covered branches: 32
Total branches: 32
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_Item(...)100%11100%
Add(...)100%1010100%
FindShallowestReplacementIndex(...)100%66100%
IsDeeper(...)100%66100%
HasEqualDepth(...)100%22100%
SortContactsById()100%44100%
GetContactUnchecked(...)100%22100%
SetContactUnchecked(...)100%22100%

File(s)

/home/runner/work/Gravitas/Gravitas/src/Gravitas/CollisionHandling/Contacts/2D/ContactGroup2D.cs

#LineLine coverage
 1//=======================================================================
 2// ContactGroup2D.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>Two inline samples belonging to one independent planar surface region.</summary>
 13internal struct ContactGroup2D
 14{
 15    internal ContactGroupKey Key;
 16    internal int Count;
 17    private ManifoldContact2D _contact0, _contact1;
 18
 5355919    internal ManifoldContact2D this[int index] => GetContactUnchecked(index);
 20
 21    internal int Add(in ManifoldContact2D contact)
 22    {
 1130823        ulong contactId = contact.ContactId;
 2586624        for (int i = 0; i < Count; i++)
 25        {
 163026            ManifoldContact2D existing = GetContactUnchecked(i);
 163027            if (existing.ContactId != contactId)
 28                continue;
 29
 530            if (IsDeeper(contact, existing))
 331                SetContactUnchecked(i, contact);
 532            SortContactsById();
 533            return 0;
 34        }
 35
 1130336        if (Count < ContactManifold2D.MaxContactsPerGroup)
 37        {
 1129338            SetContactUnchecked(Count, contact);
 1129339            Count++;
 1129340            SortContactsById();
 1129341            return 1;
 42        }
 43
 1044        int replaceIndex = FindShallowestReplacementIndex(contact);
 1045        if (replaceIndex < 0)
 146            return 0;
 47
 948        SetContactUnchecked(replaceIndex, contact);
 949        SortContactsById();
 950        return 0;
 51    }
 52
 53    private int FindShallowestReplacementIndex(ManifoldContact2D candidate)
 54    {
 1055        int replaceIndex = !IsDeeper(_contact1, _contact0)
 1056            ? 1
 1057            : 0;
 58
 1059        ManifoldContact2D shallowest = GetContactUnchecked(replaceIndex);
 1060        if (IsDeeper(candidate, shallowest))
 861            return replaceIndex;
 62
 263        if (HasEqualDepth(candidate, shallowest) && candidate.ContactId < shallowest.ContactId)
 164            return replaceIndex;
 65
 166        return -1;
 67    }
 68
 69    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 70    private static bool IsDeeper(ManifoldContact2D candidate, ManifoldContact2D existing) =>
 2571        candidate.Depth > existing.Depth
 2572        || candidate.Depth == existing.Depth
 2573        && candidate.DepthIsClamped
 2574        && !existing.DepthIsClamped;
 75
 76    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 77    private static bool HasEqualDepth(ManifoldContact2D left, ManifoldContact2D right) =>
 278        left.Depth == right.Depth
 279        && left.DepthIsClamped == right.DepthIsClamped;
 80
 81    private void SortContactsById()
 82    {
 1130783        if (Count < 2 || _contact0.ContactId <= _contact1.ContactId)
 1081184            return;
 85
 49686        (_contact0, _contact1) = (_contact1, _contact0);
 49687    }
 88
 89    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 90    private ManifoldContact2D GetContactUnchecked(int index) =>
 5519991        index == 0 ? _contact0 : _contact1;
 92
 93    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 94    private void SetContactUnchecked(int index, ManifoldContact2D contact)
 95    {
 1130596        if (index == 0)
 97        {
 969898            _contact0 = contact;
 969899            return;
 100        }
 101
 1607102        _contact1 = contact;
 1607103    }
 104
 105}