| | | 1 | | //======================================================================= |
| | | 2 | | // DynamicCcdCandidateIndex2D.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 | | using FixedMathSharp; |
| | | 10 | | using SwiftCollections; |
| | | 11 | | |
| | | 12 | | namespace Gravitas.CollisionHandling; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Indexes planar bounds by minimum X for CCD and projected overlap queries. |
| | | 16 | | /// </summary> |
| | | 17 | | internal sealed class DynamicCcdCandidateIndex2D |
| | | 18 | | { |
| | | 19 | | private readonly SwiftList<Entry> _entries; |
| | | 20 | | private readonly SwiftDictionary<int, int>? _entryIndices; |
| | | 21 | | private Fixed64 _maxExtentX; |
| | | 22 | | private int _maxExtentXCount; |
| | | 23 | | private int _unrepresentableExtentXCount; |
| | 30581 | 24 | | private bool _isSorted = true; |
| | | 25 | | |
| | 30581 | 26 | | public DynamicCcdCandidateIndex2D(int capacity = 0, bool supportsUpdates = false) |
| | | 27 | | { |
| | 30581 | 28 | | _entries = capacity > 0 ? new SwiftList<Entry>(capacity) : new SwiftList<Entry>(); |
| | 30581 | 29 | | _entryIndices = !supportsUpdates |
| | 30581 | 30 | | ? null |
| | 30581 | 31 | | : capacity > 0 |
| | 30581 | 32 | | ? new SwiftDictionary<int, int>(capacity) |
| | 30581 | 33 | | : new SwiftDictionary<int, int>(); |
| | 30581 | 34 | | } |
| | | 35 | | |
| | 1491 | 36 | | public int Count => _entries.Count; |
| | | 37 | | |
| | | 38 | | public void Clear() |
| | | 39 | | { |
| | 50018 | 40 | | _entries.FastClear(); |
| | 50018 | 41 | | _entryIndices?.Clear(); |
| | 50018 | 42 | | _maxExtentX = Fixed64.Zero; |
| | 50018 | 43 | | _maxExtentXCount = 0; |
| | 50018 | 44 | | _unrepresentableExtentXCount = 0; |
| | 50018 | 45 | | _isSorted = true; |
| | 50018 | 46 | | } |
| | | 47 | | |
| | | 48 | | public void Add(int dynamicId, DynamicCcdPlanarBounds bounds) |
| | | 49 | | { |
| | 29102 | 50 | | _entryIndices?.Add(dynamicId, _entries.Count); |
| | 29102 | 51 | | _entries.Add(new Entry(dynamicId, bounds)); |
| | 29102 | 52 | | IncludeExtent(bounds.MinX, bounds.MaxX); |
| | | 53 | | |
| | 29102 | 54 | | _isSorted = false; |
| | 29102 | 55 | | } |
| | | 56 | | |
| | | 57 | | public void AddOrUpdate(int dynamicId, DynamicCcdPlanarBounds bounds) |
| | | 58 | | { |
| | 142184 | 59 | | SwiftDictionary<int, int>? entryIndices = _entryIndices; |
| | 142184 | 60 | | ChronicleGuard.ThrowIfTrue( |
| | 142184 | 61 | | entryIndices == null, |
| | 142184 | 62 | | nameof(DynamicCcdCandidateIndex2D), |
| | 142184 | 63 | | "Candidate index was not configured for updates."); |
| | 142184 | 64 | | if (entryIndices.TryGetValue(dynamicId, out int index)) |
| | | 65 | | { |
| | 128745 | 66 | | Entry previous = _entries[index]; |
| | 128745 | 67 | | _entries[index] = new Entry(dynamicId, bounds); |
| | 128745 | 68 | | bool remainsSorted = _isSorted && IsOrderedAt(index); |
| | 128745 | 69 | | if (!DynamicCcdExtentMetadata.IsEquivalent( |
| | 128745 | 70 | | previous.MinX, |
| | 128745 | 71 | | previous.MaxX, |
| | 128745 | 72 | | bounds.MinX, |
| | 128745 | 73 | | bounds.MaxX)) |
| | | 74 | | { |
| | 3962 | 75 | | if (RemoveExtent(previous.MinX, previous.MaxX)) |
| | 2627 | 76 | | RebuildExtents(); |
| | | 77 | | else |
| | 1335 | 78 | | IncludeExtent(bounds.MinX, bounds.MaxX); |
| | | 79 | | } |
| | | 80 | | |
| | 128745 | 81 | | _isSorted = remainsSorted; |
| | 128745 | 82 | | return; |
| | | 83 | | } |
| | | 84 | | |
| | 13439 | 85 | | Add(dynamicId, bounds); |
| | 13439 | 86 | | } |
| | | 87 | | |
| | | 88 | | public bool Remove(int dynamicId) |
| | | 89 | | { |
| | 3580 | 90 | | SwiftDictionary<int, int>? entryIndices = _entryIndices; |
| | 3580 | 91 | | if (entryIndices == null || !entryIndices.TryGetValue(dynamicId, out int index)) |
| | 3068 | 92 | | return false; |
| | | 93 | | |
| | 512 | 94 | | Entry removed = _entries[index]; |
| | 512 | 95 | | int lastIndex = _entries.Count - 1; |
| | 512 | 96 | | if (index != lastIndex) |
| | | 97 | | { |
| | 129 | 98 | | Entry moved = _entries[lastIndex]; |
| | 129 | 99 | | _entries[index] = moved; |
| | 129 | 100 | | entryIndices[moved.DynamicId] = index; |
| | | 101 | | } |
| | | 102 | | |
| | 512 | 103 | | _entries.RemoveAt(lastIndex); |
| | 512 | 104 | | entryIndices.Remove(dynamicId); |
| | 512 | 105 | | if (RemoveExtent(removed.MinX, removed.MaxX)) |
| | 247 | 106 | | RebuildExtents(); |
| | 512 | 107 | | _isSorted = false; |
| | 512 | 108 | | return true; |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | public void Sort() |
| | | 112 | | { |
| | 79910 | 113 | | if (!_isSorted && _entries.Count > 1) |
| | 2309 | 114 | | HeapSort(); |
| | | 115 | | |
| | 79910 | 116 | | _isSorted = true; |
| | 79910 | 117 | | } |
| | | 118 | | |
| | | 119 | | public void Query(DynamicCcdPlanarBounds queryBounds, SwiftList<int> results) |
| | | 120 | | { |
| | 57108 | 121 | | results.FastClear(); |
| | 57108 | 122 | | if (_entries.Count == 0) |
| | 1792 | 123 | | return; |
| | | 124 | | |
| | 55316 | 125 | | Sort(); |
| | 55316 | 126 | | Fixed64 scanMinX = _unrepresentableExtentXCount > 0 |
| | 55316 | 127 | | || !Fixed64.TrySubtract(queryBounds.MinX, _maxExtentX, out Fixed64 representableScanMinX) |
| | 55316 | 128 | | ? Fixed64.MinValue |
| | 55316 | 129 | | : representableScanMinX; |
| | 55316 | 130 | | int index = FindFirstCandidateIndex(scanMinX); |
| | 335326 | 131 | | for (; index < _entries.Count; index++) |
| | | 132 | | { |
| | 158436 | 133 | | Entry entry = _entries[index]; |
| | 158436 | 134 | | if (entry.MinX > queryBounds.MaxX) |
| | | 135 | | break; |
| | | 136 | | |
| | 140005 | 137 | | if (entry.Intersects(queryBounds)) |
| | 139433 | 138 | | results.Add(entry.DynamicId); |
| | | 139 | | } |
| | 55316 | 140 | | } |
| | | 141 | | |
| | | 142 | | public static DynamicCcdPlanarBounds CreateSweptCircleBounds(Vector2d start, Vector2d displacement, Fixed64 radius) |
| | 2168 | 143 | | => CreateBoundsBetween(start, start + displacement, radius); |
| | | 144 | | |
| | | 145 | | public static DynamicCcdPlanarBounds CreateBoundsBetween( |
| | | 146 | | Vector2d start, |
| | | 147 | | Vector2d end, |
| | | 148 | | Fixed64 radius) |
| | | 149 | | { |
| | 17879 | 150 | | Fixed64 minX = FixedMath.Min(start.X, end.X) - radius; |
| | 17879 | 151 | | Fixed64 maxX = FixedMath.Max(start.X, end.X) + radius; |
| | 17879 | 152 | | Fixed64 minZ = FixedMath.Min(start.Y, end.Y) - radius; |
| | 17879 | 153 | | Fixed64 maxZ = FixedMath.Max(start.Y, end.Y) + radius; |
| | 17879 | 154 | | return new DynamicCcdPlanarBounds(minX, minZ, maxX, maxZ); |
| | | 155 | | } |
| | | 156 | | |
| | | 157 | | private void IncludeExtent(Fixed64 minX, Fixed64 maxX) |
| | | 158 | | { |
| | 33274 | 159 | | if (!Fixed64.TrySubtract(maxX, minX, out Fixed64 extentX)) |
| | | 160 | | { |
| | 7 | 161 | | _unrepresentableExtentXCount++; |
| | 7 | 162 | | return; |
| | | 163 | | } |
| | | 164 | | |
| | 33267 | 165 | | if (extentX > _maxExtentX) |
| | | 166 | | { |
| | 24352 | 167 | | _maxExtentX = extentX; |
| | 24352 | 168 | | _maxExtentXCount = 1; |
| | | 169 | | } |
| | 8915 | 170 | | else if (extentX == _maxExtentX) |
| | | 171 | | { |
| | 4989 | 172 | | _maxExtentXCount++; |
| | | 173 | | } |
| | 8915 | 174 | | } |
| | | 175 | | |
| | | 176 | | private bool RemoveExtent(Fixed64 minX, Fixed64 maxX) |
| | | 177 | | { |
| | 4474 | 178 | | if (!Fixed64.TrySubtract(maxX, minX, out Fixed64 extentX)) |
| | | 179 | | { |
| | 2 | 180 | | _unrepresentableExtentXCount--; |
| | 2 | 181 | | return false; |
| | | 182 | | } |
| | | 183 | | |
| | 4472 | 184 | | return extentX == _maxExtentX && --_maxExtentXCount == 0; |
| | | 185 | | } |
| | | 186 | | |
| | | 187 | | private void RebuildExtents() |
| | | 188 | | { |
| | 2874 | 189 | | _maxExtentX = Fixed64.Zero; |
| | 2874 | 190 | | _maxExtentXCount = 0; |
| | 2874 | 191 | | _unrepresentableExtentXCount = 0; |
| | 11422 | 192 | | for (int i = 0; i < _entries.Count; i++) |
| | 2837 | 193 | | IncludeExtent(_entries[i].MinX, _entries[i].MaxX); |
| | 2874 | 194 | | } |
| | | 195 | | |
| | | 196 | | private int FindFirstCandidateIndex(Fixed64 minX) |
| | | 197 | | { |
| | 55316 | 198 | | int low = 0; |
| | 55316 | 199 | | int high = _entries.Count; |
| | 181418 | 200 | | while (low < high) |
| | | 201 | | { |
| | 126102 | 202 | | int middle = low + ((high - low) >> 1); |
| | 126102 | 203 | | if (_entries[middle].MinX < minX) |
| | 25019 | 204 | | low = middle + 1; |
| | | 205 | | else |
| | 101083 | 206 | | high = middle; |
| | | 207 | | } |
| | | 208 | | |
| | 55316 | 209 | | return low; |
| | | 210 | | } |
| | | 211 | | |
| | | 212 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 213 | | private bool IsOrderedAt(int index) |
| | | 214 | | { |
| | 71032 | 215 | | Entry entry = _entries[index]; |
| | 71032 | 216 | | return (index == 0 || Compare(_entries[index - 1], entry) <= 0) |
| | 71032 | 217 | | && (index == _entries.Count - 1 || Compare(entry, _entries[index + 1]) <= 0); |
| | | 218 | | } |
| | | 219 | | |
| | | 220 | | private void HeapSort() |
| | | 221 | | { |
| | 2309 | 222 | | int count = _entries.Count; |
| | 13822 | 223 | | for (int start = (count >> 1) - 1; start >= 0; start--) |
| | 4602 | 224 | | SiftDown(start, count); |
| | | 225 | | |
| | 20748 | 226 | | for (int end = count - 1; end > 0; end--) |
| | | 227 | | { |
| | 8065 | 228 | | Swap(0, end); |
| | 8065 | 229 | | SiftDown(0, end); |
| | | 230 | | } |
| | 2309 | 231 | | } |
| | | 232 | | |
| | | 233 | | private void SiftDown(int root, int count) |
| | | 234 | | { |
| | 18634 | 235 | | while (true) |
| | | 236 | | { |
| | 31301 | 237 | | int child = (root << 1) + 1; |
| | 31301 | 238 | | if (child >= count) |
| | 11304 | 239 | | return; |
| | | 240 | | |
| | 19997 | 241 | | int swapIndex = root; |
| | 19997 | 242 | | if (Compare(_entries[swapIndex], _entries[child]) < 0) |
| | 18191 | 243 | | swapIndex = child; |
| | | 244 | | |
| | 19997 | 245 | | int right = child + 1; |
| | 19997 | 246 | | if (right < count && Compare(_entries[swapIndex], _entries[right]) < 0) |
| | 7691 | 247 | | swapIndex = right; |
| | | 248 | | |
| | 19997 | 249 | | if (swapIndex == root) |
| | 1363 | 250 | | return; |
| | | 251 | | |
| | 18634 | 252 | | Swap(root, swapIndex); |
| | 18634 | 253 | | root = swapIndex; |
| | | 254 | | } |
| | | 255 | | } |
| | | 256 | | |
| | | 257 | | private void Swap(int first, int second) |
| | | 258 | | { |
| | 26699 | 259 | | Entry firstEntry = _entries[first]; |
| | 26699 | 260 | | Entry secondEntry = _entries[second]; |
| | 26699 | 261 | | _entries[first] = secondEntry; |
| | 26699 | 262 | | _entries[second] = firstEntry; |
| | 26699 | 263 | | if (_entryIndices != null) |
| | | 264 | | { |
| | 18568 | 265 | | _entryIndices[secondEntry.DynamicId] = first; |
| | 18568 | 266 | | _entryIndices[firstEntry.DynamicId] = second; |
| | | 267 | | } |
| | 26699 | 268 | | } |
| | | 269 | | |
| | | 270 | | private static int Compare(Entry x, Entry y) |
| | | 271 | | { |
| | 155090 | 272 | | int result = x.MinX.CompareTo(y.MinX); |
| | 155090 | 273 | | if (result != 0) |
| | 152003 | 274 | | return result; |
| | | 275 | | |
| | 3087 | 276 | | result = x.MinZ.CompareTo(y.MinZ); |
| | 3087 | 277 | | if (result != 0) |
| | 2988 | 278 | | return result; |
| | | 279 | | |
| | 99 | 280 | | result = x.MaxX.CompareTo(y.MaxX); |
| | 99 | 281 | | if (result != 0) |
| | 11 | 282 | | return result; |
| | | 283 | | |
| | 88 | 284 | | result = x.MaxZ.CompareTo(y.MaxZ); |
| | 88 | 285 | | if (result != 0) |
| | 6 | 286 | | return result; |
| | | 287 | | |
| | 82 | 288 | | return x.DynamicId.CompareTo(y.DynamicId); |
| | | 289 | | } |
| | | 290 | | |
| | | 291 | | private readonly struct Entry |
| | | 292 | | { |
| | | 293 | | public Entry(int dynamicId, DynamicCcdPlanarBounds bounds) |
| | | 294 | | { |
| | 157847 | 295 | | DynamicId = dynamicId; |
| | 157847 | 296 | | MinX = bounds.MinX; |
| | 157847 | 297 | | MinZ = bounds.MinZ; |
| | 157847 | 298 | | MaxX = bounds.MaxX; |
| | 157847 | 299 | | MaxZ = bounds.MaxZ; |
| | 157847 | 300 | | } |
| | | 301 | | |
| | | 302 | | public int DynamicId { get; } |
| | | 303 | | public Fixed64 MinX { get; } |
| | | 304 | | public Fixed64 MinZ { get; } |
| | | 305 | | public Fixed64 MaxX { get; } |
| | | 306 | | public Fixed64 MaxZ { get; } |
| | | 307 | | |
| | | 308 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 309 | | public bool Intersects(DynamicCcdPlanarBounds queryBounds) |
| | | 310 | | { |
| | 140005 | 311 | | return !(MinX > queryBounds.MaxX || MaxX < queryBounds.MinX || |
| | 140005 | 312 | | MinZ > queryBounds.MaxZ || MaxZ < queryBounds.MinZ); |
| | | 313 | | } |
| | | 314 | | } |
| | | 315 | | } |