| | | 1 | | //======================================================================= |
| | | 2 | | // GravitasQuery2DService.Batch.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 FixedMathSharp; |
| | | 9 | | using SwiftCollections; |
| | | 10 | | using System; |
| | | 11 | | |
| | | 12 | | namespace Gravitas.Queries; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Owns batched pure 2D query behavior for one <see cref="GravitasWorldContext"/>. |
| | | 16 | | /// </summary> |
| | | 17 | | public sealed partial class GravitasQuery2DService |
| | | 18 | | { |
| | 5083 | 19 | | private readonly SwiftList<Physics2DHit> _batch2DHits = new(); |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Gets the number of requests processed by the last pure 2D batch query call. |
| | | 23 | | /// </summary> |
| | | 24 | | public int LastBatchRequestCount { get; private set; } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Gets the number of hits accepted by the last pure 2D batch query call. |
| | | 28 | | /// </summary> |
| | | 29 | | public int LastBatchHitCount { get; private set; } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Gets the summed candidate count reported by the last pure 2D batch query call. |
| | | 33 | | /// </summary> |
| | | 34 | | public int LastBatchCandidateCount { get; private set; } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Executes multiple pure 2D segment raycasts and writes one closest-hit slot per request. |
| | | 38 | | /// </summary> |
| | | 39 | | public int RaycastBatch(ReadOnlySpan<PhysicsRaycast2DRequest> requests, Span<Physics2DHit> closestHits) |
| | | 40 | | { |
| | 209 | 41 | | ValidateClosestBatchOutput(requests.Length, closestHits); |
| | 209 | 42 | | ResetBatchCounters(requests.Length); |
| | | 43 | | |
| | 209 | 44 | | int hitCount = 0; |
| | 1256 | 45 | | for (int i = 0; i < requests.Length; i++) |
| | | 46 | | { |
| | 419 | 47 | | PhysicsRaycast2DRequest request = requests[i]; |
| | 419 | 48 | | bool found = Raycast(request.Start, request.End, request.LayerMask, out Physics2DHit hit); |
| | 419 | 49 | | WriteClosestHit(closestHits, i, hit, found, ref hitCount); |
| | | 50 | | } |
| | | 51 | | |
| | 209 | 52 | | LastBatchHitCount = hitCount; |
| | 209 | 53 | | return hitCount; |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Executes multiple pure 2D segment raycasts and appends all hits into one caller-owned hit buffer. |
| | | 58 | | /// </summary> |
| | | 59 | | public int RaycastAllBatch( |
| | | 60 | | ReadOnlySpan<PhysicsRaycast2DRequest> requests, |
| | | 61 | | SwiftList<Physics2DHit> hits, |
| | | 62 | | Span<PhysicsQueryHitRange> ranges) |
| | | 63 | | { |
| | 209 | 64 | | SwiftThrowHelper.ThrowIfNull(hits, nameof(hits)); |
| | 209 | 65 | | ValidateRangeBatchOutput(requests.Length, ranges); |
| | 209 | 66 | | ResetBatchCounters(requests.Length); |
| | 209 | 67 | | hits.FastClear(); |
| | | 68 | | |
| | 1256 | 69 | | for (int i = 0; i < requests.Length; i++) |
| | | 70 | | { |
| | 419 | 71 | | PhysicsRaycast2DRequest request = requests[i]; |
| | 419 | 72 | | int count = RaycastAll(request.Start, request.End, request.LayerMask, _batch2DHits); |
| | 419 | 73 | | AppendRange(hits, ranges, i, count); |
| | | 74 | | } |
| | | 75 | | |
| | 209 | 76 | | LastBatchHitCount = hits.Count; |
| | 209 | 77 | | return hits.Count; |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Executes multiple pure 2D circle overlaps and writes one closest-hit slot per request. |
| | | 82 | | /// </summary> |
| | | 83 | | public int OverlapCircleBatch(ReadOnlySpan<PhysicsOverlapCircle2DRequest> requests, Span<Physics2DHit> closestHits) |
| | | 84 | | { |
| | 209 | 85 | | ValidateClosestBatchOutput(requests.Length, closestHits); |
| | 209 | 86 | | ValidateOverlapCircleRequests(requests); |
| | 209 | 87 | | ResetBatchCounters(requests.Length); |
| | | 88 | | |
| | 209 | 89 | | int hitCount = 0; |
| | 1254 | 90 | | for (int i = 0; i < requests.Length; i++) |
| | | 91 | | { |
| | 418 | 92 | | PhysicsOverlapCircle2DRequest request = requests[i]; |
| | 418 | 93 | | bool found = OverlapCircle(request.Center, request.Radius, request.LayerMask, out Physics2DHit hit); |
| | 418 | 94 | | WriteClosestHit(closestHits, i, hit, found, ref hitCount); |
| | | 95 | | } |
| | | 96 | | |
| | 209 | 97 | | LastBatchHitCount = hitCount; |
| | 209 | 98 | | return hitCount; |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Executes multiple pure 2D circle overlaps and appends all hits into one caller-owned hit buffer. |
| | | 103 | | /// </summary> |
| | | 104 | | public int OverlapCircleAllBatch( |
| | | 105 | | ReadOnlySpan<PhysicsOverlapCircle2DRequest> requests, |
| | | 106 | | SwiftList<Physics2DHit> hits, |
| | | 107 | | Span<PhysicsQueryHitRange> ranges) |
| | | 108 | | { |
| | 208 | 109 | | SwiftThrowHelper.ThrowIfNull(hits, nameof(hits)); |
| | 208 | 110 | | ValidateRangeBatchOutput(requests.Length, ranges); |
| | 208 | 111 | | ValidateOverlapCircleRequests(requests); |
| | 208 | 112 | | ResetBatchCounters(requests.Length); |
| | 208 | 113 | | hits.FastClear(); |
| | | 114 | | |
| | 1248 | 115 | | for (int i = 0; i < requests.Length; i++) |
| | | 116 | | { |
| | 416 | 117 | | PhysicsOverlapCircle2DRequest request = requests[i]; |
| | 416 | 118 | | int count = OverlapCircleAll(request.Center, request.Radius, request.LayerMask, _batch2DHits); |
| | 416 | 119 | | AppendRange(hits, ranges, i, count); |
| | | 120 | | } |
| | | 121 | | |
| | 208 | 122 | | LastBatchHitCount = hits.Count; |
| | 208 | 123 | | return hits.Count; |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | /// <summary> |
| | | 127 | | /// Executes multiple pure 2D AABB overlaps and writes one closest-hit slot per request. |
| | | 128 | | /// </summary> |
| | | 129 | | public int OverlapAabbBatch(ReadOnlySpan<PhysicsOverlapAabb2DRequest> requests, Span<Physics2DHit> closestHits) |
| | | 130 | | { |
| | 209 | 131 | | ValidateClosestBatchOutput(requests.Length, closestHits); |
| | 209 | 132 | | ValidateOverlapAabbRequests(requests); |
| | 209 | 133 | | ResetBatchCounters(requests.Length); |
| | | 134 | | |
| | 209 | 135 | | int hitCount = 0; |
| | 1256 | 136 | | for (int i = 0; i < requests.Length; i++) |
| | | 137 | | { |
| | 419 | 138 | | PhysicsOverlapAabb2DRequest request = requests[i]; |
| | 419 | 139 | | bool found = OverlapAabb(request.Center, request.Size, request.LayerMask, out Physics2DHit hit); |
| | 419 | 140 | | WriteClosestHit(closestHits, i, hit, found, ref hitCount); |
| | | 141 | | } |
| | | 142 | | |
| | 209 | 143 | | LastBatchHitCount = hitCount; |
| | 209 | 144 | | return hitCount; |
| | | 145 | | } |
| | | 146 | | |
| | | 147 | | /// <summary> |
| | | 148 | | /// Executes multiple pure 2D AABB overlaps and appends all hits into one caller-owned hit buffer. |
| | | 149 | | /// </summary> |
| | | 150 | | public int OverlapAabbAllBatch( |
| | | 151 | | ReadOnlySpan<PhysicsOverlapAabb2DRequest> requests, |
| | | 152 | | SwiftList<Physics2DHit> hits, |
| | | 153 | | Span<PhysicsQueryHitRange> ranges) |
| | | 154 | | { |
| | 209 | 155 | | SwiftThrowHelper.ThrowIfNull(hits, nameof(hits)); |
| | 209 | 156 | | ValidateRangeBatchOutput(requests.Length, ranges); |
| | 209 | 157 | | ValidateOverlapAabbRequests(requests); |
| | 209 | 158 | | ResetBatchCounters(requests.Length); |
| | 209 | 159 | | hits.FastClear(); |
| | | 160 | | |
| | 1256 | 161 | | for (int i = 0; i < requests.Length; i++) |
| | | 162 | | { |
| | 419 | 163 | | PhysicsOverlapAabb2DRequest request = requests[i]; |
| | 419 | 164 | | int count = OverlapAabbAll(request.Center, request.Size, request.LayerMask, _batch2DHits); |
| | 419 | 165 | | AppendRange(hits, ranges, i, count); |
| | | 166 | | } |
| | | 167 | | |
| | 209 | 168 | | LastBatchHitCount = hits.Count; |
| | 209 | 169 | | return hits.Count; |
| | | 170 | | } |
| | | 171 | | |
| | | 172 | | /// <summary> |
| | | 173 | | /// Executes multiple pure 2D convex polygon overlaps and writes one closest-hit slot per request. |
| | | 174 | | /// </summary> |
| | | 175 | | public int OverlapPolygonBatch( |
| | | 176 | | ReadOnlySpan<PhysicsOverlapPolygon2DRequest> requests, |
| | | 177 | | ReadOnlySpan<Vector2d> vertices, |
| | | 178 | | Span<Physics2DHit> closestHits) |
| | | 179 | | { |
| | 5 | 180 | | ValidateClosestBatchOutput(requests.Length, closestHits); |
| | 5 | 181 | | ValidatePolygonRequestRanges(requests, vertices); |
| | 1 | 182 | | ResetBatchCounters(requests.Length); |
| | | 183 | | |
| | 1 | 184 | | int hitCount = 0; |
| | 6 | 185 | | for (int i = 0; i < requests.Length; i++) |
| | | 186 | | { |
| | 2 | 187 | | PhysicsOverlapPolygon2DRequest request = requests[i]; |
| | 2 | 188 | | ReadOnlySpan<Vector2d> polygon = vertices.Slice(request.VertexStart, request.VertexCount); |
| | 2 | 189 | | bool found = OverlapPolygon(polygon, request.LayerMask, out Physics2DHit hit); |
| | 2 | 190 | | WriteClosestHit(closestHits, i, hit, found, ref hitCount); |
| | | 191 | | } |
| | | 192 | | |
| | 1 | 193 | | LastBatchHitCount = hitCount; |
| | 1 | 194 | | return hitCount; |
| | | 195 | | } |
| | | 196 | | |
| | | 197 | | /// <summary> |
| | | 198 | | /// Executes multiple pure 2D convex polygon overlaps and appends all hits into one caller-owned hit buffer. |
| | | 199 | | /// </summary> |
| | | 200 | | public int OverlapPolygonAllBatch( |
| | | 201 | | ReadOnlySpan<PhysicsOverlapPolygon2DRequest> requests, |
| | | 202 | | ReadOnlySpan<Vector2d> vertices, |
| | | 203 | | SwiftList<Physics2DHit> hits, |
| | | 204 | | Span<PhysicsQueryHitRange> ranges) |
| | | 205 | | { |
| | 1 | 206 | | SwiftThrowHelper.ThrowIfNull(hits, nameof(hits)); |
| | 1 | 207 | | ValidateRangeBatchOutput(requests.Length, ranges); |
| | 1 | 208 | | ValidatePolygonRequestRanges(requests, vertices); |
| | 1 | 209 | | ResetBatchCounters(requests.Length); |
| | 1 | 210 | | hits.FastClear(); |
| | | 211 | | |
| | 6 | 212 | | for (int i = 0; i < requests.Length; i++) |
| | | 213 | | { |
| | 2 | 214 | | PhysicsOverlapPolygon2DRequest request = requests[i]; |
| | 2 | 215 | | ReadOnlySpan<Vector2d> polygon = vertices.Slice(request.VertexStart, request.VertexCount); |
| | 2 | 216 | | int count = OverlapPolygonAll(polygon, request.LayerMask, _batch2DHits); |
| | 2 | 217 | | AppendRange(hits, ranges, i, count); |
| | | 218 | | } |
| | | 219 | | |
| | 1 | 220 | | LastBatchHitCount = hits.Count; |
| | 1 | 221 | | return hits.Count; |
| | | 222 | | } |
| | | 223 | | |
| | | 224 | | /// <summary> |
| | | 225 | | /// Executes multiple pure 2D swept-circle queries and writes one closest-hit slot per request. |
| | | 226 | | /// </summary> |
| | | 227 | | public int SweepCircleBatch(ReadOnlySpan<PhysicsSweepCircle2DRequest> requests, Span<Physics2DHit> closestHits) |
| | | 228 | | { |
| | 2 | 229 | | ValidateClosestBatchOutput(requests.Length, closestHits); |
| | 2 | 230 | | ValidateSweepCircleRequests(requests); |
| | 2 | 231 | | ResetBatchCounters(requests.Length); |
| | | 232 | | |
| | 2 | 233 | | int hitCount = 0; |
| | 10 | 234 | | for (int i = 0; i < requests.Length; i++) |
| | | 235 | | { |
| | 3 | 236 | | PhysicsSweepCircle2DRequest request = requests[i]; |
| | 3 | 237 | | bool found = SweepCircle( |
| | 3 | 238 | | request.Start, |
| | 3 | 239 | | request.End, |
| | 3 | 240 | | request.Radius, |
| | 3 | 241 | | request.LayerMask, |
| | 3 | 242 | | out Physics2DHit hit, |
| | 3 | 243 | | request.ExcludedCollider, |
| | 3 | 244 | | request.IncludeTriggers); |
| | 3 | 245 | | WriteClosestHit(closestHits, i, hit, found, ref hitCount); |
| | | 246 | | } |
| | | 247 | | |
| | 2 | 248 | | LastBatchHitCount = hitCount; |
| | 2 | 249 | | return hitCount; |
| | | 250 | | } |
| | | 251 | | |
| | | 252 | | /// <summary> |
| | | 253 | | /// Executes multiple pure 2D swept-circle queries and appends all hits into one caller-owned hit buffer. |
| | | 254 | | /// </summary> |
| | | 255 | | public int SweepCircleAllBatch( |
| | | 256 | | ReadOnlySpan<PhysicsSweepCircle2DRequest> requests, |
| | | 257 | | SwiftList<Physics2DHit> hits, |
| | | 258 | | Span<PhysicsQueryHitRange> ranges) |
| | | 259 | | { |
| | 210 | 260 | | SwiftThrowHelper.ThrowIfNull(hits, nameof(hits)); |
| | 210 | 261 | | ValidateRangeBatchOutput(requests.Length, ranges); |
| | 210 | 262 | | ValidateSweepCircleRequests(requests); |
| | 210 | 263 | | ResetBatchCounters(requests.Length); |
| | 210 | 264 | | hits.FastClear(); |
| | | 265 | | |
| | 842 | 266 | | for (int i = 0; i < requests.Length; i++) |
| | | 267 | | { |
| | 211 | 268 | | PhysicsSweepCircle2DRequest request = requests[i]; |
| | 211 | 269 | | int count = SweepCircleAll( |
| | 211 | 270 | | request.Start, |
| | 211 | 271 | | request.End, |
| | 211 | 272 | | request.Radius, |
| | 211 | 273 | | request.LayerMask, |
| | 211 | 274 | | _batch2DHits, |
| | 211 | 275 | | request.ExcludedCollider, |
| | 211 | 276 | | request.IncludeTriggers); |
| | 211 | 277 | | AppendRange(hits, ranges, i, count); |
| | | 278 | | } |
| | | 279 | | |
| | 210 | 280 | | LastBatchHitCount = hits.Count; |
| | 210 | 281 | | return hits.Count; |
| | | 282 | | } |
| | | 283 | | |
| | | 284 | | private void WriteClosestHit(Span<Physics2DHit> closestHits, int index, Physics2DHit hit, bool found, ref int hitCou |
| | | 285 | | { |
| | 1261 | 286 | | if (found) |
| | | 287 | | { |
| | 1050 | 288 | | closestHits[index] = hit; |
| | 1050 | 289 | | hitCount++; |
| | 1050 | 290 | | AccumulateBatchCounters(1); |
| | | 291 | | } |
| | | 292 | | else |
| | | 293 | | { |
| | 211 | 294 | | closestHits[index] = default; |
| | 211 | 295 | | AccumulateBatchCounters(0); |
| | | 296 | | } |
| | 211 | 297 | | } |
| | | 298 | | |
| | | 299 | | private void AppendRange( |
| | | 300 | | SwiftList<Physics2DHit> hits, |
| | | 301 | | Span<PhysicsQueryHitRange> ranges, |
| | | 302 | | int index, |
| | | 303 | | int count) |
| | | 304 | | { |
| | 1467 | 305 | | int start = hits.Count; |
| | 1467 | 306 | | if (count > 0) |
| | 1256 | 307 | | hits.AddRange(_batch2DHits.AsReadOnlySpan()); |
| | | 308 | | |
| | 1467 | 309 | | ranges[index] = new PhysicsQueryHitRange(start, count); |
| | 1467 | 310 | | AccumulateBatchCounters(count); |
| | 1467 | 311 | | } |
| | | 312 | | |
| | | 313 | | private void ResetBatchCounters(int requestCount) |
| | | 314 | | { |
| | 1499 | 315 | | LastBatchRequestCount = requestCount; |
| | 1499 | 316 | | LastBatchHitCount = 0; |
| | 1499 | 317 | | LastBatchCandidateCount = 0; |
| | 1499 | 318 | | } |
| | | 319 | | |
| | | 320 | | private void AccumulateBatchCounters(int hitCount) |
| | | 321 | | { |
| | 2728 | 322 | | LastBatchHitCount += hitCount; |
| | 2728 | 323 | | LastBatchCandidateCount += LastQueryCandidateCount; |
| | 2728 | 324 | | } |
| | | 325 | | |
| | | 326 | | private static void ValidateClosestBatchOutput(int requestCount, Span<Physics2DHit> closestHits) |
| | | 327 | | { |
| | 634 | 328 | | SwiftThrowHelper.ThrowIfArgument( |
| | 634 | 329 | | closestHits.Length < requestCount, |
| | 634 | 330 | | nameof(closestHits), |
| | 634 | 331 | | "Closest-hit output span must contain at least one slot per request."); |
| | 634 | 332 | | } |
| | | 333 | | |
| | | 334 | | private static void ValidateRangeBatchOutput(int requestCount, Span<PhysicsQueryHitRange> ranges) |
| | | 335 | | { |
| | 837 | 336 | | SwiftThrowHelper.ThrowIfArgument( |
| | 837 | 337 | | ranges.Length < requestCount, |
| | 837 | 338 | | nameof(ranges), |
| | 837 | 339 | | "Range output span must contain at least one slot per request."); |
| | 837 | 340 | | } |
| | | 341 | | |
| | | 342 | | private static void ValidatePolygonRequestRanges( |
| | | 343 | | ReadOnlySpan<PhysicsOverlapPolygon2DRequest> requests, |
| | | 344 | | ReadOnlySpan<Vector2d> vertices) |
| | | 345 | | { |
| | 20 | 346 | | for (int i = 0; i < requests.Length; i++) |
| | | 347 | | { |
| | 8 | 348 | | PhysicsOverlapPolygon2DRequest request = requests[i]; |
| | 8 | 349 | | SwiftThrowHelper.ThrowIfArgument( |
| | 8 | 350 | | request.VertexStart < 0 |
| | 8 | 351 | | || request.VertexCount < 0 |
| | 8 | 352 | | || request.VertexStart > vertices.Length |
| | 8 | 353 | | || request.VertexCount > vertices.Length - request.VertexStart, |
| | 8 | 354 | | nameof(requests), |
| | 8 | 355 | | "Polygon batch request vertex range must be contained by the supplied vertex span."); |
| | | 356 | | |
| | 4 | 357 | | QueryDetection2D.ValidateConvexQueryPolygon(vertices.Slice(request.VertexStart, request.VertexCount)); |
| | | 358 | | } |
| | 2 | 359 | | } |
| | | 360 | | |
| | | 361 | | private static void ValidateOverlapCircleRequests(ReadOnlySpan<PhysicsOverlapCircle2DRequest> requests) |
| | | 362 | | { |
| | 2502 | 363 | | for (int i = 0; i < requests.Length; i++) |
| | | 364 | | { |
| | 834 | 365 | | SwiftThrowHelper.ThrowIfArgument( |
| | 834 | 366 | | requests[i].Radius < Fixed64.Zero, |
| | 834 | 367 | | nameof(requests), |
| | 834 | 368 | | "2D query radius cannot be negative."); |
| | | 369 | | } |
| | 417 | 370 | | } |
| | | 371 | | |
| | | 372 | | private static void ValidateOverlapAabbRequests(ReadOnlySpan<PhysicsOverlapAabb2DRequest> requests) |
| | | 373 | | { |
| | 2512 | 374 | | for (int i = 0; i < requests.Length; i++) |
| | 838 | 375 | | QueryDetection2D.ValidateAabbSize(requests[i].Size); |
| | 418 | 376 | | } |
| | | 377 | | |
| | | 378 | | private static void ValidateSweepCircleRequests(ReadOnlySpan<PhysicsSweepCircle2DRequest> requests) |
| | | 379 | | { |
| | 852 | 380 | | for (int i = 0; i < requests.Length; i++) |
| | | 381 | | { |
| | 214 | 382 | | SwiftThrowHelper.ThrowIfArgument( |
| | 214 | 383 | | requests[i].Radius <= Fixed64.Zero, |
| | 214 | 384 | | nameof(requests), |
| | 214 | 385 | | "2D sweep radius must be greater than zero."); |
| | | 386 | | } |
| | 212 | 387 | | } |
| | | 388 | | } |