| | | 1 | | //======================================================================= |
| | | 2 | | // DynamicCcdCandidateIndex.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 | | using SwiftCollections.Query; |
| | | 12 | | |
| | | 13 | | namespace Gravitas.CollisionHandling; |
| | | 14 | | |
| | | 15 | | /// <summary>Indexes swept 3D bounds by minimum X for broad-phase CCD candidate queries.</summary> |
| | | 16 | | internal sealed class DynamicCcdCandidateIndex |
| | | 17 | | { |
| | | 18 | | private readonly SwiftList<Entry> _entries; |
| | | 19 | | private readonly SwiftDictionary<int, int>? _entryIndices; |
| | | 20 | | private Fixed64 _maxExtentX; |
| | | 21 | | private int _maxExtentXCount; |
| | | 22 | | private int _unrepresentableExtentXCount; |
| | 40772 | 23 | | private bool _isSorted = true; |
| | | 24 | | |
| | 40772 | 25 | | public DynamicCcdCandidateIndex(int capacity = 0, bool supportsUpdates = false) |
| | | 26 | | { |
| | 40772 | 27 | | _entries = capacity > 0 ? new SwiftList<Entry>(capacity) : new SwiftList<Entry>(); |
| | 40772 | 28 | | _entryIndices = !supportsUpdates |
| | 40772 | 29 | | ? null |
| | 40772 | 30 | | : capacity > 0 |
| | 40772 | 31 | | ? new SwiftDictionary<int, int>(capacity) |
| | 40772 | 32 | | : new SwiftDictionary<int, int>(); |
| | 40772 | 33 | | } |
| | | 34 | | |
| | 4743 | 35 | | public int Count => _entries.Count; |
| | | 36 | | |
| | | 37 | | public void Clear() |
| | | 38 | | { |
| | 176344 | 39 | | _entries.FastClear(); |
| | 176344 | 40 | | _entryIndices?.Clear(); |
| | 176344 | 41 | | _maxExtentX = Fixed64.Zero; |
| | 176344 | 42 | | _maxExtentXCount = 0; |
| | 176344 | 43 | | _unrepresentableExtentXCount = 0; |
| | 176344 | 44 | | _isSorted = true; |
| | 176344 | 45 | | } |
| | | 46 | | |
| | | 47 | | public void Add(int dynamicId, FixedBoundVolume bounds) |
| | | 48 | | { |
| | 81499 | 49 | | _entryIndices?.Add(dynamicId, _entries.Count); |
| | 81499 | 50 | | _entries.Add(new Entry(dynamicId, bounds)); |
| | 81499 | 51 | | IncludeExtent(bounds.Min.X, bounds.Max.X); |
| | | 52 | | |
| | 81499 | 53 | | _isSorted = false; |
| | 81499 | 54 | | } |
| | | 55 | | |
| | | 56 | | public void AddOrUpdate(int dynamicId, FixedBoundVolume bounds) |
| | | 57 | | { |
| | 1253 | 58 | | SwiftDictionary<int, int>? entryIndices = _entryIndices; |
| | 1253 | 59 | | ChronicleGuard.ThrowIfTrue( |
| | 1253 | 60 | | entryIndices == null, |
| | 1253 | 61 | | nameof(DynamicCcdCandidateIndex), |
| | 1253 | 62 | | "Candidate index was not configured for updates."); |
| | 1253 | 63 | | if (entryIndices.TryGetValue(dynamicId, out int index)) |
| | | 64 | | { |
| | 323 | 65 | | Entry previous = _entries[index]; |
| | 323 | 66 | | _entries[index] = new Entry(dynamicId, bounds); |
| | 323 | 67 | | bool remainsSorted = _isSorted && IsOrderedAt(index); |
| | 323 | 68 | | if (!DynamicCcdExtentMetadata.IsEquivalent( |
| | 323 | 69 | | previous.MinX, |
| | 323 | 70 | | previous.MaxX, |
| | 323 | 71 | | bounds.Min.X, |
| | 323 | 72 | | bounds.Max.X)) |
| | | 73 | | { |
| | 107 | 74 | | if (RemoveExtent(previous.MinX, previous.MaxX)) |
| | 88 | 75 | | RebuildExtents(); |
| | | 76 | | else |
| | 19 | 77 | | IncludeExtent(bounds.Min.X, bounds.Max.X); |
| | | 78 | | } |
| | | 79 | | |
| | 323 | 80 | | _isSorted = remainsSorted; |
| | 323 | 81 | | return; |
| | | 82 | | } |
| | | 83 | | |
| | 930 | 84 | | Add(dynamicId, bounds); |
| | 930 | 85 | | } |
| | | 86 | | |
| | | 87 | | public bool Remove(int dynamicId) |
| | | 88 | | { |
| | 15 | 89 | | SwiftDictionary<int, int>? entryIndices = _entryIndices; |
| | 15 | 90 | | if (entryIndices == null || !entryIndices.TryGetValue(dynamicId, out int index)) |
| | 6 | 91 | | return false; |
| | | 92 | | |
| | 9 | 93 | | Entry removed = _entries[index]; |
| | 9 | 94 | | int lastIndex = _entries.Count - 1; |
| | 9 | 95 | | if (index != lastIndex) |
| | | 96 | | { |
| | 3 | 97 | | Entry moved = _entries[lastIndex]; |
| | 3 | 98 | | _entries[index] = moved; |
| | 3 | 99 | | entryIndices[moved.DynamicId] = index; |
| | | 100 | | } |
| | | 101 | | |
| | 9 | 102 | | _entries.RemoveAt(lastIndex); |
| | 9 | 103 | | entryIndices.Remove(dynamicId); |
| | 9 | 104 | | if (RemoveExtent(removed.MinX, removed.MaxX)) |
| | 6 | 105 | | RebuildExtents(); |
| | 9 | 106 | | _isSorted = false; |
| | 9 | 107 | | return true; |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | public void Sort() |
| | | 111 | | { |
| | 97275 | 112 | | if (!_isSorted && _entries.Count > 1) |
| | 3846 | 113 | | HeapSort(); |
| | | 114 | | |
| | 97275 | 115 | | _isSorted = true; |
| | 97275 | 116 | | } |
| | | 117 | | |
| | | 118 | | public void Query(FixedBoundVolume queryBounds, SwiftList<int> results) |
| | | 119 | | { |
| | 35553 | 120 | | results.FastClear(); |
| | 35553 | 121 | | if (_entries.Count == 0) |
| | 17828 | 122 | | return; |
| | | 123 | | |
| | 17725 | 124 | | Sort(); |
| | 17725 | 125 | | Fixed64 scanMinX = _unrepresentableExtentXCount > 0 |
| | 17725 | 126 | | || !Fixed64.TrySubtract(queryBounds.Min.X, _maxExtentX, out Fixed64 representableScanMinX) |
| | 17725 | 127 | | ? Fixed64.MinValue |
| | 17725 | 128 | | : representableScanMinX; |
| | 17725 | 129 | | int index = FindFirstCandidateIndex(scanMinX); |
| | 424025 | 130 | | for (; index < _entries.Count; index++) |
| | | 131 | | { |
| | 214902 | 132 | | Entry entry = _entries[index]; |
| | 214902 | 133 | | if (entry.MinX > queryBounds.Max.X) |
| | | 134 | | break; |
| | | 135 | | |
| | 203150 | 136 | | if (entry.Intersects(queryBounds)) |
| | 18123 | 137 | | results.Add(entry.DynamicId); |
| | | 138 | | } |
| | 17725 | 139 | | } |
| | | 140 | | |
| | | 141 | | public static FixedBoundVolume CreateSweptSphereBounds(Vector3d start, Vector3d displacement, Fixed64 radius) |
| | 16591 | 142 | | => CreateBoundsBetween(start, start + displacement, Vector3d.One * radius); |
| | | 143 | | |
| | | 144 | | public static FixedBoundVolume CreateSweptBounds( |
| | | 145 | | Vector3d start, |
| | | 146 | | Vector3d displacement, |
| | | 147 | | Vector3d extents) => |
| | 1 | 148 | | CreateBoundsBetween(start, start + displacement, extents); |
| | | 149 | | |
| | | 150 | | public static FixedBoundVolume CreateBoundsBetween( |
| | | 151 | | Vector3d start, |
| | | 152 | | Vector3d end, |
| | | 153 | | Vector3d extents) |
| | | 154 | | { |
| | 91620 | 155 | | return new FixedBoundVolume(Vector3d.Min(start, end) - extents, Vector3d.Max(start, end) + extents); |
| | | 156 | | } |
| | | 157 | | |
| | | 158 | | private void IncludeExtent(Fixed64 minX, Fixed64 maxX) |
| | | 159 | | { |
| | 81640 | 160 | | if (!Fixed64.TrySubtract(maxX, minX, out Fixed64 extentX)) |
| | | 161 | | { |
| | 6 | 162 | | _unrepresentableExtentXCount++; |
| | 6 | 163 | | return; |
| | | 164 | | } |
| | | 165 | | |
| | 81634 | 166 | | if (extentX > _maxExtentX) |
| | | 167 | | { |
| | 64889 | 168 | | _maxExtentX = extentX; |
| | 64889 | 169 | | _maxExtentXCount = 1; |
| | | 170 | | } |
| | 16745 | 171 | | else if (extentX == _maxExtentX) |
| | | 172 | | { |
| | 12217 | 173 | | _maxExtentXCount++; |
| | | 174 | | } |
| | 16745 | 175 | | } |
| | | 176 | | |
| | | 177 | | private bool RemoveExtent(Fixed64 minX, Fixed64 maxX) |
| | | 178 | | { |
| | 116 | 179 | | if (!Fixed64.TrySubtract(maxX, minX, out Fixed64 extentX)) |
| | | 180 | | { |
| | 2 | 181 | | _unrepresentableExtentXCount--; |
| | 2 | 182 | | return false; |
| | | 183 | | } |
| | | 184 | | |
| | 114 | 185 | | return extentX == _maxExtentX && --_maxExtentXCount == 0; |
| | | 186 | | } |
| | | 187 | | |
| | | 188 | | private void RebuildExtents() |
| | | 189 | | { |
| | 94 | 190 | | _maxExtentX = Fixed64.Zero; |
| | 94 | 191 | | _maxExtentXCount = 0; |
| | 94 | 192 | | _unrepresentableExtentXCount = 0; |
| | 432 | 193 | | for (int i = 0; i < _entries.Count; i++) |
| | 122 | 194 | | IncludeExtent(_entries[i].MinX, _entries[i].MaxX); |
| | 94 | 195 | | } |
| | | 196 | | |
| | | 197 | | private int FindFirstCandidateIndex(Fixed64 minX) |
| | | 198 | | { |
| | 17725 | 199 | | int low = 0; |
| | 17725 | 200 | | int high = _entries.Count; |
| | 123362 | 201 | | while (low < high) |
| | | 202 | | { |
| | 105637 | 203 | | int middle = low + ((high - low) >> 1); |
| | 105637 | 204 | | if (_entries[middle].MinX < minX) |
| | 67748 | 205 | | low = middle + 1; |
| | | 206 | | else |
| | 37889 | 207 | | high = middle; |
| | | 208 | | } |
| | | 209 | | |
| | 17725 | 210 | | return low; |
| | | 211 | | } |
| | | 212 | | |
| | | 213 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 214 | | private bool IsOrderedAt(int index) |
| | | 215 | | { |
| | 252 | 216 | | Entry entry = _entries[index]; |
| | 252 | 217 | | return (index == 0 || Compare(_entries[index - 1], entry) <= 0) |
| | 252 | 218 | | && (index == _entries.Count - 1 || Compare(entry, _entries[index + 1]) <= 0); |
| | | 219 | | } |
| | | 220 | | |
| | | 221 | | private void HeapSort() |
| | | 222 | | { |
| | 3846 | 223 | | int count = _entries.Count; |
| | 30112 | 224 | | for (int start = (count >> 1) - 1; start >= 0; start--) |
| | 11210 | 225 | | SiftDown(start, count); |
| | | 226 | | |
| | 45968 | 227 | | for (int end = count - 1; end > 0; end--) |
| | | 228 | | { |
| | 19138 | 229 | | Swap(0, end); |
| | 19138 | 230 | | SiftDown(0, end); |
| | | 231 | | } |
| | 3846 | 232 | | } |
| | | 233 | | |
| | | 234 | | private void SiftDown(int root, int count) |
| | | 235 | | { |
| | 60738 | 236 | | while (true) |
| | | 237 | | { |
| | 91086 | 238 | | int child = (root << 1) + 1; |
| | 91086 | 239 | | if (child >= count) |
| | 26774 | 240 | | return; |
| | | 241 | | |
| | 64312 | 242 | | int swapIndex = root; |
| | 64312 | 243 | | if (Compare(_entries[swapIndex], _entries[child]) < 0) |
| | 59014 | 244 | | swapIndex = child; |
| | | 245 | | |
| | 64312 | 246 | | int right = child + 1; |
| | 64312 | 247 | | if (right < count && Compare(_entries[swapIndex], _entries[right]) < 0) |
| | 25287 | 248 | | swapIndex = right; |
| | | 249 | | |
| | 64312 | 250 | | if (swapIndex == root) |
| | 3574 | 251 | | return; |
| | | 252 | | |
| | 60738 | 253 | | Swap(root, swapIndex); |
| | 60738 | 254 | | root = swapIndex; |
| | | 255 | | } |
| | | 256 | | } |
| | | 257 | | |
| | | 258 | | private void Swap(int first, int second) |
| | | 259 | | { |
| | 79876 | 260 | | Entry firstEntry = _entries[first]; |
| | 79876 | 261 | | Entry secondEntry = _entries[second]; |
| | 79876 | 262 | | _entries[first] = secondEntry; |
| | 79876 | 263 | | _entries[second] = firstEntry; |
| | 79876 | 264 | | if (_entryIndices != null) |
| | | 265 | | { |
| | 128 | 266 | | _entryIndices[secondEntry.DynamicId] = first; |
| | 128 | 267 | | _entryIndices[firstEntry.DynamicId] = second; |
| | | 268 | | } |
| | 79876 | 269 | | } |
| | | 270 | | |
| | | 271 | | private static int Compare(Entry x, Entry y) |
| | | 272 | | { |
| | 120004 | 273 | | int result = x.MinX.CompareTo(y.MinX); |
| | 120004 | 274 | | if (result != 0) |
| | 97476 | 275 | | return result; |
| | | 276 | | |
| | 22528 | 277 | | result = x.MinY.CompareTo(y.MinY); |
| | 22528 | 278 | | if (result != 0) |
| | 844 | 279 | | return result; |
| | | 280 | | |
| | 21684 | 281 | | result = x.MinZ.CompareTo(y.MinZ); |
| | 21684 | 282 | | if (result != 0) |
| | 21652 | 283 | | return result; |
| | | 284 | | |
| | 32 | 285 | | result = x.MaxX.CompareTo(y.MaxX); |
| | 32 | 286 | | if (result != 0) |
| | 7 | 287 | | return result; |
| | | 288 | | |
| | 25 | 289 | | result = x.MaxY.CompareTo(y.MaxY); |
| | 25 | 290 | | if (result != 0) |
| | 2 | 291 | | return result; |
| | | 292 | | |
| | 23 | 293 | | result = x.MaxZ.CompareTo(y.MaxZ); |
| | 23 | 294 | | if (result != 0) |
| | 3 | 295 | | return result; |
| | | 296 | | |
| | 20 | 297 | | return x.DynamicId.CompareTo(y.DynamicId); |
| | | 298 | | } |
| | | 299 | | |
| | | 300 | | private readonly struct Entry |
| | | 301 | | { |
| | | 302 | | public Entry(int dynamicId, FixedBoundVolume bounds) |
| | | 303 | | { |
| | 81822 | 304 | | DynamicId = dynamicId; |
| | 81822 | 305 | | MinX = bounds.Min.X; |
| | 81822 | 306 | | MinY = bounds.Min.Y; |
| | 81822 | 307 | | MinZ = bounds.Min.Z; |
| | 81822 | 308 | | MaxX = bounds.Max.X; |
| | 81822 | 309 | | MaxY = bounds.Max.Y; |
| | 81822 | 310 | | MaxZ = bounds.Max.Z; |
| | 81822 | 311 | | } |
| | | 312 | | |
| | | 313 | | public int DynamicId { get; } |
| | | 314 | | public Fixed64 MinX { get; } |
| | | 315 | | public Fixed64 MinY { get; } |
| | | 316 | | public Fixed64 MinZ { get; } |
| | | 317 | | public Fixed64 MaxX { get; } |
| | | 318 | | public Fixed64 MaxY { get; } |
| | | 319 | | public Fixed64 MaxZ { get; } |
| | | 320 | | |
| | | 321 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 322 | | public bool Intersects(FixedBoundVolume queryBounds) |
| | | 323 | | { |
| | 203150 | 324 | | return !(MinX > queryBounds.Max.X || MaxX < queryBounds.Min.X || |
| | 203150 | 325 | | MinY > queryBounds.Max.Y || MaxY < queryBounds.Min.Y || |
| | 203150 | 326 | | MinZ > queryBounds.Max.Z || MaxZ < queryBounds.Min.Z); |
| | | 327 | | } |
| | | 328 | | } |
| | | 329 | | } |