| | | 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 | | |
| | | 8 | | using System.Runtime.CompilerServices; |
| | | 9 | | |
| | | 10 | | namespace Gravitas.CollisionHandling; |
| | | 11 | | |
| | | 12 | | /// <summary>Four inline point samples belonging to one geometric region.</summary> |
| | | 13 | | internal 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 | | { |
| | 1118196 | 21 | | for (int i = 0; i < Count; i++) |
| | | 22 | | { |
| | 302513 | 23 | | ManifoldContact existing = GetContactUnchecked(i); |
| | 302513 | 24 | | if (existing.ContactId != contact.ContactId) |
| | | 25 | | continue; |
| | 1238 | 26 | | if (IsDeeper(contact, existing)) |
| | 4 | 27 | | SetContactUnchecked(i, contact); |
| | 1238 | 28 | | SortContactsById(); |
| | 1238 | 29 | | return 0; |
| | | 30 | | } |
| | 256585 | 31 | | if (Count < ContactManifold.MaxContactsPerGroup) |
| | | 32 | | { |
| | 239886 | 33 | | SetContactUnchecked(Count++, contact); |
| | 239886 | 34 | | SortContactsById(); |
| | 239886 | 35 | | return 1; |
| | | 36 | | } |
| | 16699 | 37 | | int replacement = FindShallowestReplacementIndex(contact); |
| | 16699 | 38 | | if (replacement >= 0) |
| | | 39 | | { |
| | 2863 | 40 | | SetContactUnchecked(replacement, contact); |
| | 2863 | 41 | | SortContactsById(); |
| | | 42 | | } |
| | 16699 | 43 | | return 0; |
| | | 44 | | } |
| | | 45 | | |
| | 1741478 | 46 | | internal ManifoldContact this[int index] => GetContactUnchecked(index); |
| | | 47 | | |
| | | 48 | | private int FindShallowestReplacementIndex(ManifoldContact candidate) |
| | | 49 | | { |
| | 16699 | 50 | | int replaceIndex = 0; |
| | 16699 | 51 | | ManifoldContact shallowest = _contact0; |
| | | 52 | | |
| | 133592 | 53 | | for (int i = 1; i < Count; i++) |
| | | 54 | | { |
| | 50097 | 55 | | ManifoldContact contact = GetContactUnchecked(i); |
| | | 56 | | // Samples are already sorted by identity; a later equal-depth |
| | | 57 | | // sample loses the deterministic identity tie. |
| | 50097 | 58 | | if (IsDeeper(shallowest, contact) || HasEqualDepth(contact, shallowest)) |
| | | 59 | | { |
| | 49945 | 60 | | shallowest = contact; |
| | 49945 | 61 | | replaceIndex = i; |
| | | 62 | | } |
| | | 63 | | } |
| | | 64 | | |
| | 16699 | 65 | | if (IsDeeper(candidate, shallowest)) |
| | 44 | 66 | | return replaceIndex; |
| | | 67 | | |
| | 16655 | 68 | | if (HasEqualDepth(candidate, shallowest) && candidate.ContactId < shallowest.ContactId) |
| | 2819 | 69 | | return replaceIndex; |
| | | 70 | | |
| | 13836 | 71 | | return -1; |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 75 | | private static bool IsDeeper(ManifoldContact candidate, ManifoldContact existing) => |
| | 68034 | 76 | | candidate.Depth > existing.Depth |
| | 68034 | 77 | | || candidate.Depth == existing.Depth |
| | 68034 | 78 | | && candidate.DepthIsClamped |
| | 68034 | 79 | | && !existing.DepthIsClamped; |
| | | 80 | | |
| | | 81 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 82 | | private static bool HasEqualDepth(ManifoldContact left, ManifoldContact right) => |
| | 66729 | 83 | | left.Depth == right.Depth |
| | 66729 | 84 | | && left.DepthIsClamped == right.DepthIsClamped; |
| | | 85 | | |
| | | 86 | | private void SortContactsById() |
| | | 87 | | { |
| | 978350 | 88 | | for (int i = 1; i < Count; i++) |
| | | 89 | | { |
| | 245188 | 90 | | ManifoldContact contact = GetContactUnchecked(i); |
| | 245188 | 91 | | int j = i - 1; |
| | 338053 | 92 | | while (j >= 0 && GetContactUnchecked(j).ContactId > contact.ContactId) |
| | | 93 | | { |
| | 92865 | 94 | | SetContactUnchecked(j + 1, GetContactUnchecked(j)); |
| | 92865 | 95 | | j--; |
| | | 96 | | } |
| | | 97 | | |
| | 245188 | 98 | | SetContactUnchecked(j + 1, contact); |
| | | 99 | | } |
| | 243987 | 100 | | } |
| | | 101 | | |
| | | 102 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 103 | | private ManifoldContact GetContactUnchecked(int index) => |
| | 2734564 | 104 | | index switch |
| | 2734564 | 105 | | { |
| | 1165724 | 106 | | 0 => _contact0, |
| | 770419 | 107 | | 1 => _contact1, |
| | 490577 | 108 | | 2 => _contact2, |
| | 307844 | 109 | | _ => _contact3 |
| | 2734564 | 110 | | }; |
| | | 111 | | |
| | | 112 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 113 | | private void SetContactUnchecked(int index, ManifoldContact contact) |
| | | 114 | | { |
| | | 115 | | switch (index) |
| | | 116 | | { |
| | | 117 | | case 0: |
| | 149399 | 118 | | _contact0 = contact; |
| | 149399 | 119 | | break; |
| | | 120 | | case 1: |
| | 217588 | 121 | | _contact1 = contact; |
| | 217588 | 122 | | break; |
| | | 123 | | case 2: |
| | 139319 | 124 | | _contact2 = contact; |
| | 139319 | 125 | | break; |
| | | 126 | | default: |
| | 74500 | 127 | | _contact3 = contact; |
| | | 128 | | break; |
| | | 129 | | } |
| | 74500 | 130 | | } |
| | | 131 | | |
| | | 132 | | } |