| | | 1 | | //======================================================================= |
| | | 2 | | // GridTracer.NavigationBody.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using System; |
| | | 9 | | using System.Collections.Generic; |
| | | 10 | | using FixedMathSharp; |
| | | 11 | | using FixedMathSharp.Geometry; |
| | | 12 | | using GridForge.Grids; |
| | | 13 | | using GridForge.Grids.Storage; |
| | | 14 | | using GridForge.Grids.Topology; |
| | | 15 | | using GridForge.Spatial; |
| | | 16 | | using SwiftCollections; |
| | | 17 | | |
| | | 18 | | namespace GridForge.Utility; |
| | | 19 | | |
| | | 20 | | /// <content>Provides bounded swept navigation-body coverage.</content> |
| | | 21 | | public static partial class GridTracer |
| | | 22 | | { |
| | | 23 | | /// <summary>Writes the canonical cells required by one direct upright-body sweep.</summary> |
| | | 24 | | /// <remarks> |
| | | 25 | | /// The start and end bodies must have closed-set contact with the declared source and target |
| | | 26 | | /// prisms respectively; exact endpoint tangency is admitted for that identity check. |
| | | 27 | | /// A prism is claimed only when its planar and vertical interiors overlap the swept body at |
| | | 28 | | /// one shared continuous parameter. Boundary-only coincidence and tangency are excluded. |
| | | 29 | | /// Grid, address, output, and combined candidate-work ceilings are independent. |
| | | 30 | | /// </remarks> |
| | | 31 | | public static GridNavigationBodyTraceReport TraceNavigationBodyInto( |
| | | 32 | | GridWorld world, |
| | | 33 | | WorldVoxelIndex source, |
| | | 34 | | WorldVoxelIndex target, |
| | | 35 | | Vector3d startFoot, |
| | | 36 | | Vector3d endFoot, |
| | | 37 | | Fixed64 horizontalRadius, |
| | | 38 | | Fixed64 bodyHeight, |
| | | 39 | | SwiftList<GridNavigationBodyTraceCell> results, |
| | | 40 | | GridNavigationBodyTraceScratch scratch, |
| | | 41 | | int gridCandidateLimit, |
| | | 42 | | int addressCandidateLimit, |
| | | 43 | | int outputLimit, |
| | | 44 | | long candidateWorkLimit) |
| | | 45 | | { |
| | 78 | 46 | | SwiftThrowHelper.ThrowIfNull(results, nameof(results)); |
| | 78 | 47 | | SwiftThrowHelper.ThrowIfNull(scratch, nameof(scratch)); |
| | 78 | 48 | | SwiftThrowHelper.ThrowIfNegative(gridCandidateLimit, nameof(gridCandidateLimit)); |
| | 77 | 49 | | SwiftThrowHelper.ThrowIfNegative(addressCandidateLimit, nameof(addressCandidateLimit)); |
| | 76 | 50 | | SwiftThrowHelper.ThrowIfNegative(outputLimit, nameof(outputLimit)); |
| | 75 | 51 | | if (candidateWorkLimit < 0L) |
| | 1 | 52 | | throw new ArgumentOutOfRangeException(nameof(candidateWorkLimit)); |
| | | 53 | | |
| | 74 | 54 | | results.Clear(); |
| | 74 | 55 | | scratch.Clear(); |
| | 74 | 56 | | if (world == null |
| | 74 | 57 | | || !world.IsActive |
| | 74 | 58 | | || horizontalRadius < Fixed64.Zero |
| | 74 | 59 | | || bodyHeight <= Fixed64.Zero) |
| | | 60 | | { |
| | 4 | 61 | | return CreateNavigationBodyTraceReport( |
| | 4 | 62 | | GridNavigationBodyTraceStatus.InvalidOrUnrepresentableGeometry, |
| | 4 | 63 | | 0, |
| | 4 | 64 | | 0, |
| | 4 | 65 | | results, |
| | 4 | 66 | | default); |
| | | 67 | | } |
| | | 68 | | |
| | 70 | 69 | | if (!Fixed64.TryAdd(startFoot.Y, bodyHeight, out Fixed64 startTop) |
| | 70 | 70 | | || !Fixed64.TryAdd(endFoot.Y, bodyHeight, out Fixed64 endTop) |
| | 70 | 71 | | || !TryCreateNavigationBodyBounds( |
| | 70 | 72 | | startFoot, |
| | 70 | 73 | | endFoot, |
| | 70 | 74 | | startTop, |
| | 70 | 75 | | endTop, |
| | 70 | 76 | | horizontalRadius, |
| | 70 | 77 | | out Vector3d queryMin, |
| | 70 | 78 | | out Vector3d queryMax)) |
| | | 79 | | { |
| | 8 | 80 | | return CreateNavigationBodyTraceReport( |
| | 8 | 81 | | GridNavigationBodyTraceStatus.ArithmeticOverflow, |
| | 8 | 82 | | 0, |
| | 8 | 83 | | 0, |
| | 8 | 84 | | results, |
| | 8 | 85 | | default); |
| | | 86 | | } |
| | | 87 | | |
| | 62 | 88 | | world.EnterReadLock(); |
| | | 89 | | try |
| | | 90 | | { |
| | 62 | 91 | | if (!world.TryGetGrid(source, out VoxelGrid? sourceGridValue) |
| | 62 | 92 | | || !world.TryGetGrid(target, out VoxelGrid? targetGridValue)) |
| | | 93 | | { |
| | 1 | 94 | | return FailNavigationBodyTrace( |
| | 1 | 95 | | results, |
| | 1 | 96 | | GridNavigationBodyTraceStatus.InvalidOrUnrepresentableGeometry, |
| | 1 | 97 | | 0, |
| | 1 | 98 | | 0, |
| | 1 | 99 | | default); |
| | | 100 | | } |
| | | 101 | | |
| | 61 | 102 | | VoxelGrid sourceGrid = sourceGridValue!; |
| | 61 | 103 | | VoxelGrid targetGrid = targetGridValue!; |
| | 61 | 104 | | if (!GridCellGeometry.TryCreatePrism( |
| | 61 | 105 | | sourceGrid.Configuration.TopologyKind, |
| | 61 | 106 | | sourceGrid.Configuration.TopologyMetrics, |
| | 61 | 107 | | sourceGrid.GetWorldPosition(source.VoxelIndex), |
| | 61 | 108 | | source, |
| | 61 | 109 | | out GridCellPrism sourcePrism) |
| | 61 | 110 | | || !GridCellGeometry.TryCreatePrism( |
| | 61 | 111 | | targetGrid.Configuration.TopologyKind, |
| | 61 | 112 | | targetGrid.Configuration.TopologyMetrics, |
| | 61 | 113 | | targetGrid.GetWorldPosition(target.VoxelIndex), |
| | 61 | 114 | | target, |
| | 61 | 115 | | out GridCellPrism targetPrism)) |
| | | 116 | | { |
| | 2 | 117 | | return FailNavigationBodyTrace( |
| | 2 | 118 | | results, |
| | 2 | 119 | | GridNavigationBodyTraceStatus.InvalidOrUnrepresentableGeometry, |
| | 2 | 120 | | 0, |
| | 2 | 121 | | 0, |
| | 2 | 122 | | default); |
| | | 123 | | } |
| | | 124 | | |
| | 59 | 125 | | if (!HasClosedNavigationBodyPrismContact( |
| | 59 | 126 | | sourcePrism, |
| | 59 | 127 | | startFoot, |
| | 59 | 128 | | startTop, |
| | 59 | 129 | | horizontalRadius) |
| | 59 | 130 | | || !HasClosedNavigationBodyPrismContact( |
| | 59 | 131 | | targetPrism, |
| | 59 | 132 | | endFoot, |
| | 59 | 133 | | endTop, |
| | 59 | 134 | | horizontalRadius)) |
| | | 135 | | { |
| | 5 | 136 | | return FailNavigationBodyTrace( |
| | 5 | 137 | | results, |
| | 5 | 138 | | GridNavigationBodyTraceStatus.InvalidOrUnrepresentableGeometry, |
| | 5 | 139 | | 0, |
| | 5 | 140 | | 0, |
| | 5 | 141 | | default); |
| | | 142 | | } |
| | | 143 | | |
| | 54 | 144 | | Span<GridCellPrism> closurePrisms = stackalloc GridCellPrism[8]; |
| | 54 | 145 | | int closureCount = GetNavigationClosure( |
| | 54 | 146 | | sourceGrid, |
| | 54 | 147 | | sourcePrism, |
| | 54 | 148 | | targetPrism, |
| | 54 | 149 | | closurePrisms); |
| | 54 | 150 | | if (closureCount == 0) |
| | | 151 | | { |
| | 2 | 152 | | return FailNavigationBodyTrace( |
| | 2 | 153 | | results, |
| | 2 | 154 | | GridNavigationBodyTraceStatus.InvalidOrUnrepresentableGeometry, |
| | 2 | 155 | | 0, |
| | 2 | 156 | | 0, |
| | 2 | 157 | | default); |
| | | 158 | | } |
| | | 159 | | |
| | 52 | 160 | | if (!TryExpandNavigationBodyBounds( |
| | 52 | 161 | | queryMin, |
| | 52 | 162 | | queryMax, |
| | 52 | 163 | | world.MaxTopologyCellEdge, |
| | 52 | 164 | | out Vector3d candidateMin, |
| | 52 | 165 | | out Vector3d candidateMax)) |
| | | 166 | | { |
| | 7 | 167 | | return FailNavigationBodyTrace( |
| | 7 | 168 | | results, |
| | 7 | 169 | | GridNavigationBodyTraceStatus.ArithmeticOverflow, |
| | 7 | 170 | | 0, |
| | 7 | 171 | | 0, |
| | 7 | 172 | | default); |
| | | 173 | | } |
| | | 174 | | |
| | 45 | 175 | | bool gridWorkLimitIsTighter = candidateWorkLimit < gridCandidateLimit; |
| | 45 | 176 | | int effectiveGridLimit = gridWorkLimitIsTighter |
| | 45 | 177 | | ? (int)candidateWorkLimit |
| | 45 | 178 | | : gridCandidateLimit; |
| | 45 | 179 | | if (!world.CollectGridCandidates( |
| | 45 | 180 | | candidateMin, |
| | 45 | 181 | | candidateMax, |
| | 45 | 182 | | scratch.CandidateGrids, |
| | 45 | 183 | | effectiveGridLimit)) |
| | | 184 | | { |
| | 3 | 185 | | return FailNavigationBodyTrace( |
| | 3 | 186 | | results, |
| | 3 | 187 | | gridWorkLimitIsTighter |
| | 3 | 188 | | ? GridNavigationBodyTraceStatus.CandidateWorkLimitExceeded |
| | 3 | 189 | | : GridNavigationBodyTraceStatus.GridCandidateLimitExceeded, |
| | 3 | 190 | | scratch.CandidateGrids.Count, |
| | 3 | 191 | | 0, |
| | 3 | 192 | | default); |
| | | 193 | | } |
| | | 194 | | |
| | 42 | 195 | | SortGridIndices(world, scratch.CandidateGrids); |
| | 42 | 196 | | long remainingWork = candidateWorkLimit - scratch.CandidateGrids.Count; |
| | 42 | 197 | | bool workLimitIsTighter = remainingWork < addressCandidateLimit; |
| | 42 | 198 | | int effectiveAddressLimit = workLimitIsTighter |
| | 42 | 199 | | ? (int)remainingWork |
| | 42 | 200 | | : addressCandidateLimit; |
| | 42 | 201 | | int addressCandidateCount = 0; |
| | 208 | 202 | | for (int gridOrdinal = 0; gridOrdinal < scratch.CandidateGrids.Count; gridOrdinal++) |
| | | 203 | | { |
| | 65 | 204 | | VoxelGrid grid = world.ActiveGrids[scratch.CandidateGrids[gridOrdinal]]; |
| | 65 | 205 | | if (!TopologyVoxelRangeUtility.TryGetPrismCandidateRange( |
| | 65 | 206 | | grid, |
| | 65 | 207 | | queryMin, |
| | 65 | 208 | | queryMax, |
| | 65 | 209 | | out VoxelIndex minimum, |
| | 65 | 210 | | out VoxelIndex maximum)) |
| | | 211 | | { |
| | | 212 | | continue; |
| | | 213 | | } |
| | | 214 | | |
| | 344 | 215 | | for (int x = minimum.x; x <= maximum.x; x++) |
| | | 216 | | { |
| | 478 | 217 | | for (int y = minimum.y; y <= maximum.y; y++) |
| | | 218 | | { |
| | 764 | 219 | | for (int z = minimum.z; z <= maximum.z; z++) |
| | | 220 | | { |
| | 253 | 221 | | if (addressCandidateCount >= effectiveAddressLimit) |
| | | 222 | | { |
| | 2 | 223 | | return FailNavigationBodyTrace( |
| | 2 | 224 | | results, |
| | 2 | 225 | | workLimitIsTighter |
| | 2 | 226 | | ? GridNavigationBodyTraceStatus.CandidateWorkLimitExceeded |
| | 2 | 227 | | : GridNavigationBodyTraceStatus.AddressLimitExceeded, |
| | 2 | 228 | | scratch.CandidateGrids.Count, |
| | 2 | 229 | | addressCandidateCount, |
| | 2 | 230 | | default); |
| | | 231 | | } |
| | | 232 | | |
| | 251 | 233 | | addressCandidateCount++; |
| | 251 | 234 | | VoxelIndex index = new(x, y, z); |
| | 251 | 235 | | WorldVoxelIndex cell = new( |
| | 251 | 236 | | world.SpawnToken, |
| | 251 | 237 | | grid.GridIndex, |
| | 251 | 238 | | grid.SpawnToken, |
| | 251 | 239 | | index); |
| | 251 | 240 | | if (!GridCellGeometry.TryCreatePrism( |
| | 251 | 241 | | grid.Configuration.TopologyKind, |
| | 251 | 242 | | grid.Configuration.TopologyMetrics, |
| | 251 | 243 | | grid.GetWorldPosition(index), |
| | 251 | 244 | | cell, |
| | 251 | 245 | | out GridCellPrism prism)) |
| | | 246 | | { |
| | 1 | 247 | | return FailNavigationBodyTrace( |
| | 1 | 248 | | results, |
| | 1 | 249 | | GridNavigationBodyTraceStatus.InvalidOrUnrepresentableGeometry, |
| | 1 | 250 | | scratch.CandidateGrids.Count, |
| | 1 | 251 | | addressCandidateCount, |
| | 1 | 252 | | default); |
| | | 253 | | } |
| | | 254 | | |
| | 250 | 255 | | bool hasPositiveOverlap = |
| | 250 | 256 | | GridCellGeometry.HasPositiveNavigationBodyPrismOverlap( |
| | 250 | 257 | | prism, |
| | 250 | 258 | | startFoot, |
| | 250 | 259 | | endFoot, |
| | 250 | 260 | | horizontalRadius, |
| | 250 | 261 | | bodyHeight); |
| | 250 | 262 | | bool isClosure = IsNavigationClosurePrism( |
| | 250 | 263 | | prism, |
| | 250 | 264 | | closurePrisms, |
| | 250 | 265 | | closureCount); |
| | 250 | 266 | | if (!hasPositiveOverlap && !isClosure) |
| | | 267 | | continue; |
| | | 268 | | |
| | 153 | 269 | | scratch.AddressCandidates.Add(new GridNavigationBodyTraceCandidate( |
| | 153 | 270 | | grid, |
| | 153 | 271 | | index, |
| | 153 | 272 | | prism, |
| | 153 | 273 | | hasPositiveOverlap, |
| | 153 | 274 | | isClosure)); |
| | | 275 | | } |
| | | 276 | | } |
| | | 277 | | } |
| | | 278 | | } |
| | | 279 | | |
| | 39 | 280 | | GridCoveredAddressRunStamp runStamp = SnapshotNavigationBodyCandidates( |
| | 39 | 281 | | world, |
| | 39 | 282 | | scratch.AddressCandidates); |
| | 39 | 283 | | scratch.AddressCandidates.SortInPlace( |
| | 39 | 284 | | default(GridNavigationBodyTraceCandidateComparer)); |
| | 39 | 285 | | if (!HasNavigationBodyUnionCoverage( |
| | 39 | 286 | | sourceGrid, |
| | 39 | 287 | | source.VoxelIndex, |
| | 39 | 288 | | targetGrid, |
| | 39 | 289 | | target.VoxelIndex, |
| | 39 | 290 | | startFoot, |
| | 39 | 291 | | endFoot, |
| | 39 | 292 | | horizontalRadius, |
| | 39 | 293 | | bodyHeight, |
| | 39 | 294 | | scratch.AddressCandidates, |
| | 39 | 295 | | scratch.UnionMembers)) |
| | | 296 | | { |
| | 2 | 297 | | return FailNavigationBodyTrace( |
| | 2 | 298 | | results, |
| | 2 | 299 | | GridNavigationBodyTraceStatus.InvalidOrUnrepresentableGeometry, |
| | 2 | 300 | | scratch.CandidateGrids.Count, |
| | 2 | 301 | | addressCandidateCount, |
| | 2 | 302 | | default); |
| | | 303 | | } |
| | 37 | 304 | | int alternativeEvidenceCount = CountMissingNavigationBodyAlternativeEvidence( |
| | 37 | 305 | | scratch.AddressCandidates, |
| | 37 | 306 | | sourceGrid, |
| | 37 | 307 | | source.VoxelIndex, |
| | 37 | 308 | | targetGrid, |
| | 37 | 309 | | target.VoxelIndex); |
| | 37 | 310 | | if (scratch.UnionMembers.Count > outputLimit |
| | 37 | 311 | | || alternativeEvidenceCount > outputLimit - scratch.UnionMembers.Count) |
| | | 312 | | { |
| | 2 | 313 | | return FailNavigationBodyTrace( |
| | 2 | 314 | | results, |
| | 2 | 315 | | GridNavigationBodyTraceStatus.OutputLimitExceeded, |
| | 2 | 316 | | scratch.CandidateGrids.Count, |
| | 2 | 317 | | addressCandidateCount, |
| | 2 | 318 | | default); |
| | | 319 | | } |
| | | 320 | | |
| | 35 | 321 | | bool hasMissingPhysicalCell = false; |
| | 300 | 322 | | for (int i = 0; i < scratch.UnionMembers.Count; i++) |
| | | 323 | | { |
| | 115 | 324 | | GridNavigationBodyTraceCandidate candidate = |
| | 115 | 325 | | scratch.AddressCandidates[scratch.UnionMembers[i]]; |
| | 115 | 326 | | hasMissingPhysicalCell |= !candidate.IsPhysicallyPresent; |
| | 115 | 327 | | results.Add(new GridNavigationBodyTraceCell( |
| | 115 | 328 | | new WorldVoxelIndex( |
| | 115 | 329 | | world.SpawnToken, |
| | 115 | 330 | | candidate.Grid.GridIndex, |
| | 115 | 331 | | candidate.Grid.SpawnToken, |
| | 115 | 332 | | candidate.Index), |
| | 115 | 333 | | candidate.Grid.Configuration.ToGridKey(), |
| | 115 | 334 | | candidate.IsPhysicallyPresent, |
| | 115 | 335 | | candidate.GridLastChangeSequence, |
| | 115 | 336 | | GridNavigationBodyTraceCellRole.RequiredCoverage)); |
| | | 337 | | } |
| | | 338 | | |
| | 35 | 339 | | if (hasMissingPhysicalCell) |
| | | 340 | | { |
| | 10 | 341 | | AppendMissingNavigationBodyAlternativeEvidence( |
| | 10 | 342 | | world, |
| | 10 | 343 | | scratch.AddressCandidates, |
| | 10 | 344 | | results, |
| | 10 | 345 | | sourceGrid, |
| | 10 | 346 | | source.VoxelIndex, |
| | 10 | 347 | | targetGrid, |
| | 10 | 348 | | target.VoxelIndex); |
| | | 349 | | } |
| | | 350 | | |
| | 35 | 351 | | results.SortInPlace(default(GridNavigationBodyTraceCellComparer)); |
| | 35 | 352 | | return CreateNavigationBodyTraceReport( |
| | 35 | 353 | | hasMissingPhysicalCell |
| | 35 | 354 | | ? GridNavigationBodyTraceStatus.IncompletePhysicalCoverage |
| | 35 | 355 | | : GridNavigationBodyTraceStatus.Complete, |
| | 35 | 356 | | scratch.CandidateGrids.Count, |
| | 35 | 357 | | addressCandidateCount, |
| | 35 | 358 | | results, |
| | 35 | 359 | | runStamp); |
| | | 360 | | } |
| | | 361 | | finally |
| | | 362 | | { |
| | 62 | 363 | | world.ExitReadLock(); |
| | 62 | 364 | | scratch.Clear(); |
| | 62 | 365 | | } |
| | 62 | 366 | | } |
| | | 367 | | |
| | | 368 | | private static bool HasClosedNavigationBodyPrismContact( |
| | | 369 | | in GridCellPrism prism, |
| | | 370 | | Vector3d foot, |
| | | 371 | | Fixed64 bodyTop, |
| | | 372 | | Fixed64 horizontalRadius) |
| | | 373 | | { |
| | 113 | 374 | | if (foot.Y > prism.VerticalMax || bodyTop < prism.VerticalMin) |
| | 1 | 375 | | return false; |
| | | 376 | | |
| | 112 | 377 | | Span<Vector2d> offsets = stackalloc Vector2d[6]; |
| | 112 | 378 | | Vector2d planarOrigin = new(prism.Center.X, prism.Center.Z); |
| | 1136 | 379 | | for (int i = 0; i < prism.FootprintVertexCount; i++) |
| | 456 | 380 | | offsets[i] = prism.GetFootprintVertex(i) - planarOrigin; |
| | | 381 | | |
| | 112 | 382 | | return FixedConvex2dRelations.TryGetCircleContact( |
| | 112 | 383 | | new Vector2d(foot.X, foot.Z), |
| | 112 | 384 | | Fixed64.Zero, |
| | 112 | 385 | | horizontalRadius, |
| | 112 | 386 | | planarOrigin, |
| | 112 | 387 | | Fixed64.Zero, |
| | 112 | 388 | | offsets[..prism.FootprintVertexCount], |
| | 112 | 389 | | out _, |
| | 112 | 390 | | out _, |
| | 112 | 391 | | out _, |
| | 112 | 392 | | out _, |
| | 112 | 393 | | out _); |
| | | 394 | | } |
| | | 395 | | |
| | | 396 | | private static int GetNavigationClosure( |
| | | 397 | | VoxelGrid sourceGrid, |
| | | 398 | | in GridCellPrism source, |
| | | 399 | | in GridCellPrism target, |
| | | 400 | | Span<GridCellPrism> closure) |
| | | 401 | | { |
| | 54 | 402 | | Span<VoxelIndex> offsets = stackalloc VoxelIndex[8]; |
| | 54 | 403 | | VoxelIndex targetOffset = default; |
| | 54 | 404 | | bool foundTarget = AreSameNavigationBodyPrism(source, target); |
| | 504 | 405 | | for (int slot = 0; !foundTarget && slot < sourceGrid.Topology.NeighborSlotCount; slot++) |
| | | 406 | | { |
| | 199 | 407 | | VoxelIndex offset = sourceGrid.Topology.GetNeighborOffset(slot); |
| | 199 | 408 | | if (!Vector3d.TryAdd( |
| | 199 | 409 | | source.Center, |
| | 199 | 410 | | sourceGrid.Topology.GetWorldOffset((offset.x, offset.y, offset.z)), |
| | 199 | 411 | | out Vector3d center) |
| | 199 | 412 | | || !GridCellGeometry.TryCreatePrism( |
| | 199 | 413 | | sourceGrid.Configuration.TopologyKind, |
| | 199 | 414 | | sourceGrid.Configuration.TopologyMetrics, |
| | 199 | 415 | | center, |
| | 199 | 416 | | default, |
| | 199 | 417 | | out GridCellPrism neighbor)) |
| | | 418 | | { |
| | 1 | 419 | | return 0; |
| | | 420 | | } |
| | 198 | 421 | | if (AreSameNavigationBodyPrism(neighbor, target)) |
| | | 422 | | { |
| | 18 | 423 | | targetOffset = offset; |
| | 18 | 424 | | foundTarget = true; |
| | | 425 | | } |
| | | 426 | | } |
| | 53 | 427 | | if (!foundTarget) |
| | 1 | 428 | | return 0; |
| | | 429 | | |
| | | 430 | | int closureCount; |
| | 52 | 431 | | if (targetOffset == default) |
| | | 432 | | { |
| | 34 | 433 | | offsets[0] = default; |
| | 34 | 434 | | closureCount = 1; |
| | | 435 | | } |
| | 18 | 436 | | else if (sourceGrid.Configuration.TopologyKind == GridTopologyKind.RectangularPrism) |
| | | 437 | | { |
| | 16 | 438 | | RectangularDirection direction = RectangularDirectionUtility.GetDirectionFromOffset( |
| | 16 | 439 | | (targetOffset.x, targetOffset.y, targetOffset.z)); |
| | 16 | 440 | | closureCount = RectangularDirectionUtility.CopyNavigationClosureOffsets(direction, offsets); |
| | | 441 | | } |
| | | 442 | | else |
| | | 443 | | { |
| | 2 | 444 | | closureCount = HexDirectionUtility.CopyNavigationClosureOffsets(targetOffset, offsets); |
| | | 445 | | } |
| | | 446 | | |
| | 304 | 447 | | for (int i = 0; i < closureCount; i++) |
| | | 448 | | { |
| | 100 | 449 | | VoxelIndex offset = offsets[i]; |
| | 100 | 450 | | bool usesTargetPlanarCoordinates = offset.x != 0 || offset.z != 0; |
| | 100 | 451 | | Vector3d center = sourceGrid.Configuration.TopologyKind == GridTopologyKind.RectangularPrism |
| | 100 | 452 | | ? new Vector3d( |
| | 100 | 453 | | offset.x == 0 ? source.Center.X : target.Center.X, |
| | 100 | 454 | | offset.y == 0 ? source.Center.Y : target.Center.Y, |
| | 100 | 455 | | offset.z == 0 ? source.Center.Z : target.Center.Z) |
| | 100 | 456 | | : new Vector3d( |
| | 100 | 457 | | usesTargetPlanarCoordinates ? target.Center.X : source.Center.X, |
| | 100 | 458 | | offset.y == 0 ? source.Center.Y : target.Center.Y, |
| | 100 | 459 | | usesTargetPlanarCoordinates ? target.Center.Z : source.Center.Z); |
| | | 460 | | |
| | | 461 | | // Every coordinate comes from one of the two already validated endpoint prisms. |
| | 100 | 462 | | _ = GridCellGeometry.TryCreatePrism( |
| | 100 | 463 | | sourceGrid.Configuration.TopologyKind, |
| | 100 | 464 | | sourceGrid.Configuration.TopologyMetrics, |
| | 100 | 465 | | center, |
| | 100 | 466 | | default, |
| | 100 | 467 | | out closure[i]); |
| | | 468 | | } |
| | | 469 | | |
| | 52 | 470 | | return closureCount; |
| | | 471 | | } |
| | | 472 | | |
| | | 473 | | private static bool IsNavigationClosurePrism( |
| | | 474 | | in GridCellPrism prism, |
| | | 475 | | ReadOnlySpan<GridCellPrism> closure, |
| | | 476 | | int count) |
| | | 477 | | { |
| | 1476 | 478 | | for (int i = 0; i < count; i++) |
| | | 479 | | { |
| | 581 | 480 | | if (AreSameNavigationBodyPrism(prism, closure[i])) |
| | 93 | 481 | | return true; |
| | | 482 | | } |
| | | 483 | | |
| | 157 | 484 | | return false; |
| | | 485 | | } |
| | | 486 | | |
| | | 487 | | private static GridCoveredAddressRunStamp SnapshotNavigationBodyCandidates( |
| | | 488 | | GridWorld world, |
| | | 489 | | SwiftList<GridNavigationBodyTraceCandidate> candidates) |
| | | 490 | | { |
| | | 491 | | GridCoveredAddressRunStamp runStamp; |
| | 39 | 492 | | lock (world.ChangeSyncRoot) |
| | | 493 | | { |
| | 39 | 494 | | runStamp = new GridCoveredAddressRunStamp( |
| | 39 | 495 | | world.SpawnToken, |
| | 39 | 496 | | world.Version, |
| | 39 | 497 | | world.ChangeSequence); |
| | 366 | 498 | | for (int i = 0; i < candidates.Count; i++) |
| | | 499 | | { |
| | 144 | 500 | | GridNavigationBodyTraceCandidate candidate = candidates[i]; |
| | 144 | 501 | | bool isPresent = candidate.Grid.StorageKind == GridStorageKind.Dense |
| | 144 | 502 | | || candidate.Grid.TryGetVoxel(candidate.Index, out _); |
| | 144 | 503 | | candidates[i] = candidate.WithPhysicalEvidence( |
| | 144 | 504 | | isPresent, |
| | 144 | 505 | | candidate.Grid.LastChangeSequence); |
| | | 506 | | } |
| | 39 | 507 | | } |
| | | 508 | | |
| | 39 | 509 | | return runStamp; |
| | | 510 | | } |
| | | 511 | | |
| | | 512 | | private static bool TryCreateNavigationBodyBounds( |
| | | 513 | | Vector3d startFoot, |
| | | 514 | | Vector3d endFoot, |
| | | 515 | | Fixed64 startTop, |
| | | 516 | | Fixed64 endTop, |
| | | 517 | | Fixed64 radius, |
| | | 518 | | out Vector3d minimum, |
| | | 519 | | out Vector3d maximum) |
| | | 520 | | { |
| | 67 | 521 | | minimum = default; |
| | 67 | 522 | | maximum = default; |
| | 67 | 523 | | return Fixed64.TrySubtract(FixedMath.Min(startFoot.X, endFoot.X), radius, out Fixed64 minX) |
| | 67 | 524 | | && Fixed64.TrySubtract(FixedMath.Min(startFoot.Z, endFoot.Z), radius, out Fixed64 minZ) |
| | 67 | 525 | | && Fixed64.TryAdd(FixedMath.Max(startFoot.X, endFoot.X), radius, out Fixed64 maxX) |
| | 67 | 526 | | && Fixed64.TryAdd(FixedMath.Max(startFoot.Z, endFoot.Z), radius, out Fixed64 maxZ) |
| | 67 | 527 | | && AssignNavigationBodyBounds( |
| | 67 | 528 | | minX, |
| | 67 | 529 | | FixedMath.Min(startFoot.Y, endFoot.Y), |
| | 67 | 530 | | minZ, |
| | 67 | 531 | | maxX, |
| | 67 | 532 | | FixedMath.Max(startTop, endTop), |
| | 67 | 533 | | maxZ, |
| | 67 | 534 | | out minimum, |
| | 67 | 535 | | out maximum); |
| | | 536 | | } |
| | | 537 | | |
| | | 538 | | private static bool AssignNavigationBodyBounds( |
| | | 539 | | Fixed64 minX, |
| | | 540 | | Fixed64 minY, |
| | | 541 | | Fixed64 minZ, |
| | | 542 | | Fixed64 maxX, |
| | | 543 | | Fixed64 maxY, |
| | | 544 | | Fixed64 maxZ, |
| | | 545 | | out Vector3d minimum, |
| | | 546 | | out Vector3d maximum) |
| | | 547 | | { |
| | 62 | 548 | | minimum = new Vector3d(minX, minY, minZ); |
| | 62 | 549 | | maximum = new Vector3d(maxX, maxY, maxZ); |
| | 62 | 550 | | return true; |
| | | 551 | | } |
| | | 552 | | |
| | | 553 | | private static bool TryExpandNavigationBodyBounds( |
| | | 554 | | Vector3d minimum, |
| | | 555 | | Vector3d maximum, |
| | | 556 | | Fixed64 expansion, |
| | | 557 | | out Vector3d expandedMinimum, |
| | | 558 | | out Vector3d expandedMaximum) |
| | | 559 | | { |
| | 52 | 560 | | if (!Fixed64.TrySubtract(minimum.X, expansion, out Fixed64 minX) |
| | 52 | 561 | | || !Fixed64.TrySubtract(minimum.Y, expansion, out Fixed64 minY) |
| | 52 | 562 | | || !Fixed64.TrySubtract(minimum.Z, expansion, out Fixed64 minZ) |
| | 52 | 563 | | || !Fixed64.TryAdd(maximum.X, expansion, out Fixed64 maxX) |
| | 52 | 564 | | || !Fixed64.TryAdd(maximum.Y, expansion, out Fixed64 maxY) |
| | 52 | 565 | | || !Fixed64.TryAdd(maximum.Z, expansion, out Fixed64 maxZ)) |
| | | 566 | | { |
| | 7 | 567 | | expandedMinimum = default; |
| | 7 | 568 | | expandedMaximum = default; |
| | 7 | 569 | | return false; |
| | | 570 | | } |
| | | 571 | | |
| | 45 | 572 | | expandedMinimum = new Vector3d(minX, minY, minZ); |
| | 45 | 573 | | expandedMaximum = new Vector3d(maxX, maxY, maxZ); |
| | 45 | 574 | | return true; |
| | | 575 | | } |
| | | 576 | | |
| | | 577 | | private static bool HasNavigationBodyUnionCoverage( |
| | | 578 | | VoxelGrid sourceGrid, |
| | | 579 | | VoxelIndex source, |
| | | 580 | | VoxelGrid targetGrid, |
| | | 581 | | VoxelIndex target, |
| | | 582 | | Vector3d startFoot, |
| | | 583 | | Vector3d endFoot, |
| | | 584 | | Fixed64 horizontalRadius, |
| | | 585 | | Fixed64 bodyHeight, |
| | | 586 | | SwiftList<GridNavigationBodyTraceCandidate> candidates, |
| | | 587 | | SwiftList<int> unionMembers) |
| | | 588 | | { |
| | 39 | 589 | | int sourceCandidate = FindNavigationBodyCandidate(candidates, sourceGrid, source); |
| | 39 | 590 | | int targetCandidate = FindNavigationBodyCandidate(candidates, targetGrid, target); |
| | | 591 | | // A connected body intersects a connected set of interiors in the source topology's |
| | | 592 | | // exact lattice. Closing every positive-overlap neighbor therefore proves containment; |
| | | 593 | | // exact coincident prisms let aligned adjacent grids continue the same lattice. |
| | 39 | 594 | | AddNavigationBodyUnionMember(candidates, unionMembers, sourceCandidate); |
| | 39 | 595 | | if (targetCandidate != sourceCandidate |
| | 39 | 596 | | && AreSameNavigationBodyPrism( |
| | 39 | 597 | | candidates[sourceCandidate].Prism, |
| | 39 | 598 | | candidates[targetCandidate].Prism)) |
| | | 599 | | { |
| | 2 | 600 | | AddNavigationBodyUnionMember(candidates, unionMembers, targetCandidate); |
| | | 601 | | } |
| | 344 | 602 | | for (int memberOrdinal = 0; memberOrdinal < unionMembers.Count; memberOrdinal++) |
| | | 603 | | { |
| | 135 | 604 | | GridCellPrism member = candidates[unionMembers[memberOrdinal]].Prism; |
| | 7100 | 605 | | for (int slot = 0; slot < sourceGrid.Topology.NeighborSlotCount; slot++) |
| | | 606 | | { |
| | 3417 | 607 | | VoxelIndex offset = sourceGrid.Topology.GetNeighborOffset(slot); |
| | | 608 | | // Candidate bounds were expanded by the world's maximum topology edge, so every |
| | | 609 | | // immediate neighbor of a selected member is exactly representable here. |
| | 3417 | 610 | | _ = Vector3d.TryAdd( |
| | 3417 | 611 | | member.Center, |
| | 3417 | 612 | | sourceGrid.Topology.GetWorldOffset((offset.x, offset.y, offset.z)), |
| | 3417 | 613 | | out Vector3d neighborCenter); |
| | 3417 | 614 | | _ = GridCellGeometry.TryCreatePrism( |
| | 3417 | 615 | | sourceGrid.Configuration.TopologyKind, |
| | 3417 | 616 | | sourceGrid.Configuration.TopologyMetrics, |
| | 3417 | 617 | | neighborCenter, |
| | 3417 | 618 | | default, |
| | 3417 | 619 | | out GridCellPrism neighbor); |
| | | 620 | | |
| | 3417 | 621 | | int matchingCandidate = FindBestMatchingNavigationBodyPrism( |
| | 3417 | 622 | | candidates, |
| | 3417 | 623 | | neighbor, |
| | 3417 | 624 | | sourceCandidate, |
| | 3417 | 625 | | targetCandidate); |
| | 3417 | 626 | | bool overlapsBody = matchingCandidate >= 0 |
| | 3417 | 627 | | ? candidates[matchingCandidate].HasPositiveOverlap |
| | 3417 | 628 | | : GridCellGeometry.HasPositiveNavigationBodyPrismOverlap( |
| | 3417 | 629 | | neighbor, |
| | 3417 | 630 | | startFoot, |
| | 3417 | 631 | | endFoot, |
| | 3417 | 632 | | horizontalRadius, |
| | 3417 | 633 | | bodyHeight); |
| | 3417 | 634 | | if (overlapsBody && matchingCandidate < 0) |
| | 2 | 635 | | return false; |
| | 3415 | 636 | | if (matchingCandidate >= 0 |
| | 3415 | 637 | | && !candidates[matchingCandidate].IsVisited) |
| | | 638 | | { |
| | 94 | 639 | | AddNavigationBodyUnionMember(candidates, unionMembers, matchingCandidate); |
| | | 640 | | } |
| | | 641 | | } |
| | | 642 | | } |
| | | 643 | | |
| | 37 | 644 | | return true; |
| | | 645 | | } |
| | | 646 | | |
| | | 647 | | private static int FindNavigationBodyCandidate( |
| | | 648 | | SwiftList<GridNavigationBodyTraceCandidate> candidates, |
| | | 649 | | VoxelGrid grid, |
| | | 650 | | VoxelIndex index) |
| | | 651 | | { |
| | 181 | 652 | | for (int i = 0;; i++) |
| | | 653 | | { |
| | 181 | 654 | | GridNavigationBodyTraceCandidate candidate = candidates[i]; |
| | 181 | 655 | | if (candidate.Grid == grid && candidate.Index == index) |
| | 78 | 656 | | return i; |
| | | 657 | | } |
| | | 658 | | } |
| | | 659 | | |
| | | 660 | | private static int FindBestMatchingNavigationBodyPrism( |
| | | 661 | | SwiftList<GridNavigationBodyTraceCandidate> candidates, |
| | | 662 | | in GridCellPrism prism, |
| | | 663 | | int sourceCandidate, |
| | | 664 | | int targetCandidate) |
| | | 665 | | { |
| | 3417 | 666 | | if (AreSameNavigationBodyPrism(candidates[sourceCandidate].Prism, prism)) |
| | 94 | 667 | | return sourceCandidate; |
| | 3323 | 668 | | if (AreSameNavigationBodyPrism(candidates[targetCandidate].Prism, prism)) |
| | 46 | 669 | | return targetCandidate; |
| | | 670 | | |
| | 3277 | 671 | | if (!FindNavigationBodyPrismRange(candidates, prism, out int start, out int end)) |
| | 2977 | 672 | | return -1; |
| | | 673 | | |
| | 300 | 674 | | int best = start; |
| | 630 | 675 | | for (int i = start + 1; i < end; i++) |
| | | 676 | | { |
| | 15 | 677 | | if (IsPreferredNavigationBodyCandidate(candidates[i], candidates[best])) |
| | 5 | 678 | | best = i; |
| | | 679 | | } |
| | | 680 | | |
| | 300 | 681 | | return best; |
| | | 682 | | } |
| | | 683 | | |
| | | 684 | | private static bool IsPreferredNavigationBodyCandidate( |
| | | 685 | | GridNavigationBodyTraceCandidate candidate, |
| | | 686 | | GridNavigationBodyTraceCandidate current) |
| | | 687 | | { |
| | 15 | 688 | | if (candidate.IsPhysicallyPresent != current.IsPhysicallyPresent) |
| | 5 | 689 | | return candidate.IsPhysicallyPresent; |
| | | 690 | | |
| | 10 | 691 | | return CompareGridIdentity(candidate.Grid, current.Grid) < 0; |
| | | 692 | | } |
| | | 693 | | |
| | | 694 | | private static bool AreSameNavigationBodyPrism( |
| | | 695 | | in GridCellPrism first, |
| | 7591 | 696 | | in GridCellPrism second) => CompareNavigationBodyPrisms(first, second) == 0; |
| | | 697 | | |
| | | 698 | | private static int CompareNavigationBodyPrisms( |
| | | 699 | | in GridCellPrism first, |
| | | 700 | | in GridCellPrism second) |
| | | 701 | | { |
| | 19780 | 702 | | int comparison = (int)first.TopologyKind - (int)second.TopologyKind; |
| | 19780 | 703 | | if (comparison != 0) |
| | 42 | 704 | | return comparison; |
| | 19738 | 705 | | comparison = first.Center.X.CompareTo(second.Center.X); |
| | 19738 | 706 | | if (comparison != 0) |
| | 11828 | 707 | | return comparison; |
| | 7910 | 708 | | comparison = first.Center.Y.CompareTo(second.Center.Y); |
| | 7910 | 709 | | if (comparison != 0) |
| | 5118 | 710 | | return comparison; |
| | 2792 | 711 | | comparison = first.Center.Z.CompareTo(second.Center.Z); |
| | 2792 | 712 | | if (comparison != 0) |
| | 1848 | 713 | | return comparison; |
| | 944 | 714 | | comparison = first.VerticalMin.CompareTo(second.VerticalMin); |
| | 944 | 715 | | if (comparison != 0) |
| | 3 | 716 | | return comparison; |
| | 941 | 717 | | comparison = first.PlanarInradius.CompareTo(second.PlanarInradius); |
| | 941 | 718 | | if (comparison != 0) |
| | 3 | 719 | | return comparison; |
| | | 720 | | |
| | 9516 | 721 | | for (int i = 0; i < first.FootprintVertexCount; i++) |
| | | 722 | | { |
| | 3826 | 723 | | Vector2d firstVertex = first.GetFootprintVertex(i); |
| | 3826 | 724 | | Vector2d secondVertex = second.GetFootprintVertex(i); |
| | 3826 | 725 | | comparison = firstVertex.X.CompareTo(secondVertex.X); |
| | 3826 | 726 | | if (comparison != 0) |
| | 3 | 727 | | return comparison; |
| | 3823 | 728 | | comparison = firstVertex.Y.CompareTo(secondVertex.Y); |
| | 3823 | 729 | | if (comparison != 0) |
| | 3 | 730 | | return comparison; |
| | | 731 | | } |
| | | 732 | | |
| | 932 | 733 | | return 0; |
| | | 734 | | } |
| | | 735 | | |
| | | 736 | | private static bool FindNavigationBodyPrismRange( |
| | | 737 | | SwiftList<GridNavigationBodyTraceCandidate> candidates, |
| | | 738 | | in GridCellPrism prism, |
| | | 739 | | out int start, |
| | | 740 | | out int end) |
| | | 741 | | { |
| | 3277 | 742 | | int low = 0; |
| | 3277 | 743 | | int high = candidates.Count; |
| | 12234 | 744 | | while (low < high) |
| | | 745 | | { |
| | 8957 | 746 | | int middle = low + ((high - low) >> 1); |
| | 8957 | 747 | | if (CompareNavigationBodyPrisms(candidates[middle].Prism, prism) < 0) |
| | 3605 | 748 | | low = middle + 1; |
| | | 749 | | else |
| | 5352 | 750 | | high = middle; |
| | | 751 | | } |
| | | 752 | | |
| | 3277 | 753 | | start = low; |
| | 3277 | 754 | | if (start >= candidates.Count |
| | 3277 | 755 | | || CompareNavigationBodyPrisms(candidates[start].Prism, prism) != 0) |
| | | 756 | | { |
| | 2977 | 757 | | end = start; |
| | 2977 | 758 | | return false; |
| | | 759 | | } |
| | | 760 | | |
| | 300 | 761 | | low = start + 1; |
| | 300 | 762 | | high = candidates.Count; |
| | 964 | 763 | | while (low < high) |
| | | 764 | | { |
| | 664 | 765 | | int middle = low + ((high - low) >> 1); |
| | 664 | 766 | | if (CompareNavigationBodyPrisms(candidates[middle].Prism, prism) <= 0) |
| | 15 | 767 | | low = middle + 1; |
| | | 768 | | else |
| | 649 | 769 | | high = middle; |
| | | 770 | | } |
| | | 771 | | |
| | 300 | 772 | | end = low; |
| | 300 | 773 | | return true; |
| | | 774 | | } |
| | | 775 | | |
| | | 776 | | private static void AddNavigationBodyUnionMember( |
| | | 777 | | SwiftList<GridNavigationBodyTraceCandidate> candidates, |
| | | 778 | | SwiftList<int> unionMembers, |
| | | 779 | | int candidateIndex) |
| | | 780 | | { |
| | 135 | 781 | | candidates[candidateIndex] = candidates[candidateIndex].WithVisited(); |
| | 135 | 782 | | unionMembers.Add(candidateIndex); |
| | 135 | 783 | | } |
| | | 784 | | |
| | | 785 | | private static void AppendMissingNavigationBodyAlternativeEvidence( |
| | | 786 | | GridWorld world, |
| | | 787 | | SwiftList<GridNavigationBodyTraceCandidate> candidates, |
| | | 788 | | SwiftList<GridNavigationBodyTraceCell> results, |
| | | 789 | | VoxelGrid sourceGrid, |
| | | 790 | | VoxelIndex source, |
| | | 791 | | VoxelGrid targetGrid, |
| | | 792 | | VoxelIndex target) |
| | | 793 | | { |
| | 59 | 794 | | for (int start = 0; start < candidates.Count;) |
| | | 795 | | { |
| | 39 | 796 | | int end = GetNavigationBodyPrismGroupEnd(candidates, start); |
| | 39 | 797 | | if (!IsMissingNavigationBodyAlternativeGroup( |
| | 39 | 798 | | candidates, |
| | 39 | 799 | | start, |
| | 39 | 800 | | end, |
| | 39 | 801 | | sourceGrid, |
| | 39 | 802 | | source, |
| | 39 | 803 | | targetGrid, |
| | 39 | 804 | | target)) |
| | | 805 | | { |
| | 33 | 806 | | start = end; |
| | 33 | 807 | | continue; |
| | | 808 | | } |
| | | 809 | | |
| | 26 | 810 | | for (int candidateIndex = start; candidateIndex < end; candidateIndex++) |
| | | 811 | | { |
| | 7 | 812 | | GridNavigationBodyTraceCandidate candidate = candidates[candidateIndex]; |
| | 7 | 813 | | if (candidate.IsVisited) |
| | | 814 | | continue; |
| | | 815 | | |
| | 1 | 816 | | candidates[candidateIndex] = candidate.WithVisited(); |
| | 1 | 817 | | results.Add(new GridNavigationBodyTraceCell( |
| | 1 | 818 | | new WorldVoxelIndex( |
| | 1 | 819 | | world.SpawnToken, |
| | 1 | 820 | | candidate.Grid.GridIndex, |
| | 1 | 821 | | candidate.Grid.SpawnToken, |
| | 1 | 822 | | candidate.Index), |
| | 1 | 823 | | candidate.Grid.Configuration.ToGridKey(), |
| | 1 | 824 | | isPhysicallyPresent: false, |
| | 1 | 825 | | candidate.GridLastChangeSequence, |
| | 1 | 826 | | GridNavigationBodyTraceCellRole.PhysicalAlternativeDependency)); |
| | | 827 | | } |
| | | 828 | | |
| | 6 | 829 | | start = end; |
| | | 830 | | } |
| | 10 | 831 | | } |
| | | 832 | | |
| | | 833 | | private static int CountMissingNavigationBodyAlternativeEvidence( |
| | | 834 | | SwiftList<GridNavigationBodyTraceCandidate> candidates, |
| | | 835 | | VoxelGrid sourceGrid, |
| | | 836 | | VoxelIndex source, |
| | | 837 | | VoxelGrid targetGrid, |
| | | 838 | | VoxelIndex target) |
| | | 839 | | { |
| | 37 | 840 | | int count = 0; |
| | 210 | 841 | | for (int start = 0; start < candidates.Count;) |
| | | 842 | | { |
| | 136 | 843 | | int end = GetNavigationBodyPrismGroupEnd(candidates, start); |
| | 136 | 844 | | if (IsMissingNavigationBodyAlternativeGroup( |
| | 136 | 845 | | candidates, |
| | 136 | 846 | | start, |
| | 136 | 847 | | end, |
| | 136 | 848 | | sourceGrid, |
| | 136 | 849 | | source, |
| | 136 | 850 | | targetGrid, |
| | 136 | 851 | | target)) |
| | | 852 | | { |
| | 32 | 853 | | for (int candidateIndex = start; candidateIndex < end; candidateIndex++) |
| | 9 | 854 | | count += candidates[candidateIndex].IsVisited ? 0 : 1; |
| | | 855 | | } |
| | | 856 | | |
| | 136 | 857 | | start = end; |
| | | 858 | | } |
| | | 859 | | |
| | 37 | 860 | | return count; |
| | | 861 | | } |
| | | 862 | | |
| | | 863 | | private static int GetNavigationBodyPrismGroupEnd( |
| | | 864 | | SwiftList<GridNavigationBodyTraceCandidate> candidates, |
| | | 865 | | int start) |
| | | 866 | | { |
| | 175 | 867 | | GridCellPrism prism = candidates[start].Prism; |
| | 175 | 868 | | int end = start + 1; |
| | 184 | 869 | | while (end < candidates.Count |
| | 184 | 870 | | && CompareNavigationBodyPrisms(candidates[end].Prism, prism) == 0) |
| | | 871 | | { |
| | 9 | 872 | | end++; |
| | | 873 | | } |
| | | 874 | | |
| | 175 | 875 | | return end; |
| | | 876 | | } |
| | | 877 | | |
| | | 878 | | private static bool IsMissingNavigationBodyAlternativeGroup( |
| | | 879 | | SwiftList<GridNavigationBodyTraceCandidate> candidates, |
| | | 880 | | int start, |
| | | 881 | | int end, |
| | | 882 | | VoxelGrid sourceGrid, |
| | | 883 | | VoxelIndex source, |
| | | 884 | | VoxelGrid targetGrid, |
| | | 885 | | VoxelIndex target) |
| | | 886 | | { |
| | 175 | 887 | | bool hasMissingSelectedNonEndpoint = false; |
| | 392 | 888 | | for (int candidateIndex = start; candidateIndex < end; candidateIndex++) |
| | | 889 | | { |
| | 179 | 890 | | GridNavigationBodyTraceCandidate candidate = candidates[candidateIndex]; |
| | 179 | 891 | | if (candidate.IsPhysicallyPresent) |
| | 158 | 892 | | return false; |
| | 21 | 893 | | hasMissingSelectedNonEndpoint |= candidate.IsVisited |
| | 21 | 894 | | && !IsNavigationBodyEndpoint( |
| | 21 | 895 | | candidate, |
| | 21 | 896 | | sourceGrid, |
| | 21 | 897 | | source, |
| | 21 | 898 | | targetGrid, |
| | 21 | 899 | | target); |
| | | 900 | | } |
| | | 901 | | |
| | 17 | 902 | | return hasMissingSelectedNonEndpoint; |
| | | 903 | | } |
| | | 904 | | |
| | | 905 | | private static bool IsNavigationBodyEndpoint( |
| | | 906 | | GridNavigationBodyTraceCandidate candidate, |
| | | 907 | | VoxelGrid sourceGrid, |
| | | 908 | | VoxelIndex source, |
| | | 909 | | VoxelGrid targetGrid, |
| | | 910 | | VoxelIndex target) => |
| | 17 | 911 | | (candidate.Grid == sourceGrid && candidate.Index == source) |
| | 17 | 912 | | || (candidate.Grid == targetGrid && candidate.Index == target); |
| | | 913 | | |
| | | 914 | | private static GridNavigationBodyTraceReport CreateNavigationBodyTraceReport( |
| | | 915 | | GridNavigationBodyTraceStatus status, |
| | | 916 | | int gridCandidateCount, |
| | | 917 | | int addressCandidateCount, |
| | | 918 | | SwiftList<GridNavigationBodyTraceCell> results, |
| | | 919 | | GridCoveredAddressRunStamp runStamp) => |
| | 74 | 920 | | new( |
| | 74 | 921 | | status, |
| | 74 | 922 | | gridCandidateCount, |
| | 74 | 923 | | addressCandidateCount, |
| | 74 | 924 | | gridCandidateCount + (long)addressCandidateCount, |
| | 74 | 925 | | results.Count, |
| | 74 | 926 | | runStamp); |
| | | 927 | | |
| | | 928 | | private static GridNavigationBodyTraceReport FailNavigationBodyTrace( |
| | | 929 | | SwiftList<GridNavigationBodyTraceCell> results, |
| | | 930 | | GridNavigationBodyTraceStatus status, |
| | | 931 | | int gridCandidateCount, |
| | | 932 | | int addressCandidateCount, |
| | | 933 | | GridCoveredAddressRunStamp runStamp) |
| | | 934 | | { |
| | 27 | 935 | | results.Clear(); |
| | 27 | 936 | | return CreateNavigationBodyTraceReport( |
| | 27 | 937 | | status, |
| | 27 | 938 | | gridCandidateCount, |
| | 27 | 939 | | addressCandidateCount, |
| | 27 | 940 | | results, |
| | 27 | 941 | | runStamp); |
| | | 942 | | } |
| | | 943 | | |
| | | 944 | | private readonly struct GridNavigationBodyTraceCandidateComparer : |
| | | 945 | | IComparer<GridNavigationBodyTraceCandidate> |
| | | 946 | | { |
| | | 947 | | public int Compare( |
| | | 948 | | GridNavigationBodyTraceCandidate first, |
| | | 949 | | GridNavigationBodyTraceCandidate second) => |
| | 108 | 950 | | CompareNavigationBodyPrisms(first.Prism, second.Prism); |
| | | 951 | | } |
| | | 952 | | |
| | | 953 | | private readonly struct GridNavigationBodyTraceCellComparer : |
| | | 954 | | IComparer<GridNavigationBodyTraceCell> |
| | | 955 | | { |
| | | 956 | | public int Compare( |
| | | 957 | | GridNavigationBodyTraceCell first, |
| | | 958 | | GridNavigationBodyTraceCell second) => |
| | 144 | 959 | | CompareNavigationBodyTraceCells(first, second); |
| | | 960 | | } |
| | | 961 | | |
| | | 962 | | private static int CompareNavigationBodyTraceCells( |
| | | 963 | | GridNavigationBodyTraceCell first, |
| | | 964 | | GridNavigationBodyTraceCell second) |
| | | 965 | | { |
| | 144 | 966 | | int comparison = CompareConfigurationKeys(first.ConfigurationKey, second.ConfigurationKey); |
| | 144 | 967 | | return comparison != 0 |
| | 144 | 968 | | ? comparison |
| | 144 | 969 | | : first.Cell.VoxelIndex.CompareTo(second.Cell.VoxelIndex); |
| | | 970 | | } |
| | | 971 | | } |