| | | 1 | | //======================================================================= |
| | | 2 | | // GridDiagnostics.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.Runtime.CompilerServices; |
| | | 9 | | using FixedMathSharp; |
| | | 10 | | using GridForge.Grids; |
| | | 11 | | using GridForge.Grids.Storage; |
| | | 12 | | using GridForge.Grids.Topology; |
| | | 13 | | using GridForge.Spatial; |
| | | 14 | | using SwiftCollections; |
| | | 15 | | |
| | | 16 | | namespace GridForge.Diagnostics; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Engine-agnostic diagnostic query helpers for GridForge worlds and grids. |
| | | 20 | | /// </summary> |
| | | 21 | | public static class GridDiagnostics |
| | | 22 | | { |
| | | 23 | | /// <summary> |
| | | 24 | | /// Clears and fills caller-owned storage with diagnostic cells. |
| | | 25 | | /// </summary> |
| | | 26 | | public static GridDiagnosticQueryResult GetCellsInto( |
| | | 27 | | GridWorld world, |
| | | 28 | | in GridDiagnosticQuery query, |
| | | 29 | | SwiftList<GridDiagnosticCell> results, |
| | | 30 | | GridDiagnosticScratch? scratch = null) |
| | | 31 | | { |
| | 38 | 32 | | SwiftThrowHelper.ThrowIfNull(results, nameof(results)); |
| | | 33 | | |
| | 37 | 34 | | results.Clear(); |
| | | 35 | | |
| | 37 | 36 | | ResultListVisitor visitor = new(results); |
| | 37 | 37 | | return VisitCellsCore(world, query, ref visitor, scratch); |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Visits diagnostic cells without requiring an intermediate result list. |
| | | 42 | | /// </summary> |
| | | 43 | | public static GridDiagnosticQueryResult VisitCells<TVisitor>( |
| | | 44 | | GridWorld world, |
| | | 45 | | in GridDiagnosticQuery query, |
| | | 46 | | ref TVisitor visitor, |
| | | 47 | | GridDiagnosticScratch? scratch = null) |
| | | 48 | | where TVisitor : struct, IGridDiagnosticCellVisitor |
| | | 49 | | { |
| | 36 | 50 | | return VisitCellsCore(world, query, ref visitor, scratch); |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | private static GridDiagnosticQueryResult VisitCellsCore<TVisitor>( |
| | | 54 | | GridWorld world, |
| | | 55 | | in GridDiagnosticQuery query, |
| | | 56 | | ref TVisitor visitor, |
| | | 57 | | GridDiagnosticScratch? scratch) |
| | | 58 | | where TVisitor : struct, IGridDiagnosticCellVisitor |
| | | 59 | | { |
| | 73 | 60 | | scratch?.Clear(); |
| | 73 | 61 | | if (world == null || !world.IsActive) |
| | 3 | 62 | | return new GridDiagnosticQueryResult(GridDiagnosticQueryStatus.InactiveWorld, 0, 0); |
| | | 63 | | |
| | 70 | 64 | | bool hasBounds = TryGetQueryBounds(query, out TopologyVoxelAabb queryBounds); |
| | 70 | 65 | | if (query.GridIndex.HasValue) |
| | | 66 | | { |
| | 7 | 67 | | if (!world.TryGetGrid(query.GridIndex.Value, out VoxelGrid? requestedGrid)) |
| | 1 | 68 | | return new GridDiagnosticQueryResult(GridDiagnosticQueryStatus.InvalidGrid, 0, 0); |
| | | 69 | | |
| | 6 | 70 | | VoxelGrid grid = requestedGrid!; |
| | 6 | 71 | | if (RequiresMissingAddressBounds(grid, query, hasBounds)) |
| | 1 | 72 | | return new GridDiagnosticQueryResult(GridDiagnosticQueryStatus.MissingAddressSpaceRequiresBounds, 0, 0); |
| | | 73 | | |
| | 5 | 74 | | return VisitGridCells(world, grid, query, hasBounds, queryBounds, ref visitor); |
| | | 75 | | } |
| | | 76 | | |
| | 63 | 77 | | int cellCount = 0; |
| | 63 | 78 | | int skippedCellCount = 0; |
| | 63 | 79 | | if (RequiresMissingAddressBounds(world, query, hasBounds)) |
| | 2 | 80 | | return new GridDiagnosticQueryResult(GridDiagnosticQueryStatus.MissingAddressSpaceRequiresBounds, 0, 0); |
| | | 81 | | |
| | 316 | 82 | | foreach (VoxelGrid grid in world.ActiveGrids) |
| | | 83 | | { |
| | 99 | 84 | | if (!ShouldVisitGrid(grid, query)) |
| | | 85 | | continue; |
| | | 86 | | |
| | 96 | 87 | | GridDiagnosticQueryStatus status = VisitGridCells( |
| | 96 | 88 | | world, |
| | 96 | 89 | | grid, |
| | 96 | 90 | | query, |
| | 96 | 91 | | hasBounds, |
| | 96 | 92 | | queryBounds, |
| | 96 | 93 | | ref visitor, |
| | 96 | 94 | | ref cellCount, |
| | 96 | 95 | | ref skippedCellCount); |
| | | 96 | | |
| | 96 | 97 | | if (status != GridDiagnosticQueryStatus.Completed) |
| | 4 | 98 | | return new GridDiagnosticQueryResult(status, cellCount, skippedCellCount); |
| | | 99 | | } |
| | | 100 | | |
| | 57 | 101 | | return new GridDiagnosticQueryResult(GridDiagnosticQueryStatus.Completed, cellCount, skippedCellCount); |
| | 4 | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Resolves a physical diagnostic cell descriptor back to its active grid |
| | | 106 | | /// and voxel. |
| | | 107 | | /// </summary> |
| | | 108 | | public static bool TryResolvePhysicalCell( |
| | | 109 | | GridWorld world, |
| | | 110 | | in GridDiagnosticCell cell, |
| | | 111 | | out VoxelGrid? grid, |
| | | 112 | | out Voxel? voxel) |
| | | 113 | | { |
| | 10 | 114 | | grid = null; |
| | 10 | 115 | | voxel = null; |
| | | 116 | | |
| | 10 | 117 | | if (world == null |
| | 10 | 118 | | || cell.Kind != GridDiagnosticCellKind.Physical |
| | 10 | 119 | | || cell.WorldSpawnToken != world.SpawnToken |
| | 10 | 120 | | || cell.WorldIndex.WorldSpawnToken != cell.WorldSpawnToken |
| | 10 | 121 | | || cell.WorldIndex.GridIndex != cell.GridIndex |
| | 10 | 122 | | || cell.WorldIndex.GridSpawnToken != cell.GridSpawnToken |
| | 10 | 123 | | || cell.WorldIndex.VoxelIndex != cell.Index) |
| | | 124 | | { |
| | 9 | 125 | | return false; |
| | | 126 | | } |
| | | 127 | | |
| | 1 | 128 | | return world.TryGetGridAndVoxel(cell.WorldIndex, out grid, out voxel); |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | private static GridDiagnosticQueryResult VisitGridCells<TVisitor>( |
| | | 132 | | GridWorld world, |
| | | 133 | | VoxelGrid grid, |
| | | 134 | | in GridDiagnosticQuery query, |
| | | 135 | | bool hasBounds, |
| | | 136 | | TopologyVoxelAabb queryBounds, |
| | | 137 | | ref TVisitor visitor) |
| | | 138 | | where TVisitor : struct, IGridDiagnosticCellVisitor |
| | | 139 | | { |
| | 5 | 140 | | int cellCount = 0; |
| | 5 | 141 | | int skippedCellCount = 0; |
| | | 142 | | |
| | 5 | 143 | | if (!ShouldVisitGrid(grid, query)) |
| | 2 | 144 | | return new GridDiagnosticQueryResult(GridDiagnosticQueryStatus.Completed, 0, 0); |
| | | 145 | | |
| | 3 | 146 | | GridDiagnosticQueryStatus status = VisitGridCells( |
| | 3 | 147 | | world, |
| | 3 | 148 | | grid, |
| | 3 | 149 | | query, |
| | 3 | 150 | | hasBounds, |
| | 3 | 151 | | queryBounds, |
| | 3 | 152 | | ref visitor, |
| | 3 | 153 | | ref cellCount, |
| | 3 | 154 | | ref skippedCellCount); |
| | | 155 | | |
| | 3 | 156 | | return new GridDiagnosticQueryResult(status, cellCount, skippedCellCount); |
| | | 157 | | } |
| | | 158 | | |
| | | 159 | | private static GridDiagnosticQueryStatus VisitGridCells<TVisitor>( |
| | | 160 | | GridWorld world, |
| | | 161 | | VoxelGrid grid, |
| | | 162 | | in GridDiagnosticQuery query, |
| | | 163 | | bool hasBounds, |
| | | 164 | | TopologyVoxelAabb queryBounds, |
| | | 165 | | ref TVisitor visitor, |
| | | 166 | | ref int cellCount, |
| | | 167 | | ref int skippedCellCount) |
| | | 168 | | where TVisitor : struct, IGridDiagnosticCellVisitor => |
| | 99 | 169 | | UsesSparseAddressTraversal(grid, query) |
| | 99 | 170 | | ? VisitSparseAddressCells( |
| | 99 | 171 | | world, |
| | 99 | 172 | | grid, |
| | 99 | 173 | | query, |
| | 99 | 174 | | hasBounds, |
| | 99 | 175 | | queryBounds, |
| | 99 | 176 | | ref visitor, |
| | 99 | 177 | | ref cellCount, |
| | 99 | 178 | | ref skippedCellCount) |
| | 99 | 179 | | : VisitPhysicalCells( |
| | 99 | 180 | | world, |
| | 99 | 181 | | grid, |
| | 99 | 182 | | query, |
| | 99 | 183 | | hasBounds, |
| | 99 | 184 | | queryBounds, |
| | 99 | 185 | | ref visitor, |
| | 99 | 186 | | ref cellCount, |
| | 99 | 187 | | ref skippedCellCount); |
| | | 188 | | |
| | | 189 | | private static GridDiagnosticQueryStatus VisitPhysicalCells<TVisitor>( |
| | | 190 | | GridWorld world, |
| | | 191 | | VoxelGrid grid, |
| | | 192 | | in GridDiagnosticQuery query, |
| | | 193 | | bool hasBounds, |
| | | 194 | | TopologyVoxelAabb queryBounds, |
| | | 195 | | ref TVisitor visitor, |
| | | 196 | | ref int cellCount, |
| | | 197 | | ref int skippedCellCount) |
| | | 198 | | where TVisitor : struct, IGridDiagnosticCellVisitor |
| | | 199 | | { |
| | 90 | 200 | | if (query.AddressMode == GridDiagnosticAddressMode.MissingOnly) |
| | 2 | 201 | | return GridDiagnosticQueryStatus.Completed; |
| | | 202 | | |
| | 88 | 203 | | VoxelIndex minIndex = default; |
| | 88 | 204 | | VoxelIndex maxIndex = default; |
| | 88 | 205 | | if (hasBounds |
| | 88 | 206 | | && !TopologyVoxelRangeUtility.TryGetCandidateRange(grid, queryBounds, out minIndex, out maxIndex)) |
| | | 207 | | { |
| | 1 | 208 | | return GridDiagnosticQueryStatus.Completed; |
| | | 209 | | } |
| | | 210 | | |
| | 87 | 211 | | PhysicalCellVisitor<TVisitor> physicalVisitor = new( |
| | 87 | 212 | | world, |
| | 87 | 213 | | grid, |
| | 87 | 214 | | query, |
| | 87 | 215 | | hasBounds, |
| | 87 | 216 | | queryBounds, |
| | 87 | 217 | | minIndex, |
| | 87 | 218 | | maxIndex, |
| | 87 | 219 | | visitor, |
| | 87 | 220 | | cellCount, |
| | 87 | 221 | | skippedCellCount); |
| | | 222 | | |
| | 87 | 223 | | grid.VisitVoxels(ref physicalVisitor); |
| | 87 | 224 | | visitor = physicalVisitor.Visitor; |
| | 87 | 225 | | cellCount = physicalVisitor.CellCount; |
| | 87 | 226 | | skippedCellCount = physicalVisitor.SkippedCellCount; |
| | 87 | 227 | | return physicalVisitor.Status; |
| | | 228 | | } |
| | | 229 | | |
| | | 230 | | private static GridDiagnosticQueryStatus VisitSparseAddressCells<TVisitor>( |
| | | 231 | | GridWorld world, |
| | | 232 | | VoxelGrid grid, |
| | | 233 | | in GridDiagnosticQuery query, |
| | | 234 | | bool hasBounds, |
| | | 235 | | TopologyVoxelAabb queryBounds, |
| | | 236 | | ref TVisitor visitor, |
| | | 237 | | ref int cellCount, |
| | | 238 | | ref int skippedCellCount) |
| | | 239 | | where TVisitor : struct, IGridDiagnosticCellVisitor |
| | | 240 | | { |
| | 9 | 241 | | if (!TryGetSparseAddressRange(grid, hasBounds, queryBounds, out VoxelIndex minIndex, out VoxelIndex maxIndex)) |
| | 1 | 242 | | return GridDiagnosticQueryStatus.Completed; |
| | | 243 | | |
| | 42 | 244 | | for (int x = minIndex.x; x <= maxIndex.x; x++) |
| | | 245 | | { |
| | 72 | 246 | | for (int y = minIndex.y; y <= maxIndex.y; y++) |
| | | 247 | | { |
| | 152 | 248 | | for (int z = minIndex.z; z <= maxIndex.z; z++) |
| | | 249 | | { |
| | 55 | 250 | | VoxelIndex index = new(x, y, z); |
| | 55 | 251 | | if (hasBounds && !IsIndexInQueryBounds(grid, index, queryBounds)) |
| | | 252 | | continue; |
| | | 253 | | |
| | 55 | 254 | | if (grid.ContainsVoxel(index)) |
| | | 255 | | { |
| | 8 | 256 | | if (query.AddressMode == GridDiagnosticAddressMode.MissingOnly) |
| | | 257 | | continue; |
| | | 258 | | |
| | 4 | 259 | | if (!grid.TryGetVoxel(index, out Voxel? voxel) || voxel == null) |
| | | 260 | | continue; |
| | | 261 | | |
| | 4 | 262 | | GridDiagnosticCellState state = GetCellState(voxel); |
| | 4 | 263 | | if (!MatchesStateFilters(state, query.RequiredStates, query.ExcludedStates)) |
| | | 264 | | continue; |
| | | 265 | | |
| | 4 | 266 | | GridDiagnosticCell physicalCell = CreatePhysicalCell(world, grid, voxel, state); |
| | 4 | 267 | | if (!TryVisitCell(in physicalCell, query.MaxCells, ref visitor, ref cellCount, ref skippedCellCo |
| | 1 | 268 | | return physicalStatus; |
| | | 269 | | |
| | | 270 | | continue; |
| | | 271 | | } |
| | | 272 | | |
| | 47 | 273 | | GridDiagnosticCellState missingState = GetMissingAddressCellState(grid, index); |
| | 47 | 274 | | if (!MatchesStateFilters(missingState, query.RequiredStates, query.ExcludedStates)) |
| | | 275 | | continue; |
| | | 276 | | |
| | 47 | 277 | | GridDiagnosticCell missingCell = CreateMissingAddressCell(world, grid, index, missingState); |
| | 47 | 278 | | if (!TryVisitCell(in missingCell, query.MaxCells, ref visitor, ref cellCount, ref skippedCellCount, |
| | 1 | 279 | | return missingStatus; |
| | | 280 | | } |
| | | 281 | | } |
| | | 282 | | } |
| | | 283 | | |
| | 6 | 284 | | return GridDiagnosticQueryStatus.Completed; |
| | | 285 | | } |
| | | 286 | | |
| | | 287 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 288 | | private static bool ShouldVisitGrid( |
| | | 289 | | VoxelGrid grid, |
| | | 290 | | in GridDiagnosticQuery query) |
| | | 291 | | { |
| | 113 | 292 | | if (query.TopologyKind.HasValue && grid.Configuration.TopologyKind != query.TopologyKind.Value) |
| | 4 | 293 | | return false; |
| | | 294 | | |
| | 109 | 295 | | return !query.StorageKind.HasValue || grid.StorageKind == query.StorageKind.Value; |
| | | 296 | | } |
| | | 297 | | |
| | | 298 | | private static bool RequiresMissingAddressBounds( |
| | | 299 | | GridWorld world, |
| | | 300 | | in GridDiagnosticQuery query, |
| | | 301 | | bool hasBounds) |
| | | 302 | | { |
| | 63 | 303 | | if (!RequiresMissingAddressBounds(query, hasBounds)) |
| | 60 | 304 | | return false; |
| | | 305 | | |
| | 10 | 306 | | foreach (VoxelGrid grid in world.ActiveGrids) |
| | | 307 | | { |
| | 3 | 308 | | if (ShouldVisitGrid(grid, query) && grid.StorageKind == GridStorageKind.Sparse) |
| | 2 | 309 | | return true; |
| | | 310 | | } |
| | | 311 | | |
| | 1 | 312 | | return false; |
| | 2 | 313 | | } |
| | | 314 | | |
| | | 315 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 316 | | private static bool RequiresMissingAddressBounds( |
| | | 317 | | VoxelGrid grid, |
| | | 318 | | in GridDiagnosticQuery query, |
| | | 319 | | bool hasBounds) => |
| | 6 | 320 | | ShouldVisitGrid(grid, query) |
| | 6 | 321 | | && grid.StorageKind == GridStorageKind.Sparse |
| | 6 | 322 | | && RequiresMissingAddressBounds(query, hasBounds); |
| | | 323 | | |
| | | 324 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 325 | | private static bool RequiresMissingAddressBounds( |
| | | 326 | | in GridDiagnosticQuery query, |
| | | 327 | | bool hasBounds) => |
| | 66 | 328 | | UsesMissingAddressMode(query.AddressMode) |
| | 66 | 329 | | && !hasBounds |
| | 66 | 330 | | && !query.AllowFullAddressSpaceScan; |
| | | 331 | | |
| | | 332 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 333 | | private static bool UsesSparseAddressTraversal( |
| | | 334 | | VoxelGrid grid, |
| | | 335 | | in GridDiagnosticQuery query) => |
| | 99 | 336 | | grid.StorageKind == GridStorageKind.Sparse |
| | 99 | 337 | | && UsesMissingAddressMode(query.AddressMode); |
| | | 338 | | |
| | | 339 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 340 | | private static bool UsesMissingAddressMode(GridDiagnosticAddressMode addressMode) => |
| | 112 | 341 | | addressMode == GridDiagnosticAddressMode.PhysicalAndMissing |
| | 112 | 342 | | || addressMode == GridDiagnosticAddressMode.MissingOnly; |
| | | 343 | | |
| | | 344 | | private static bool TryGetQueryBounds( |
| | | 345 | | in GridDiagnosticQuery query, |
| | | 346 | | out TopologyVoxelAabb bounds) |
| | | 347 | | { |
| | 70 | 348 | | bounds = default; |
| | 70 | 349 | | if (!query.BoundsMin.HasValue || !query.BoundsMax.HasValue) |
| | 55 | 350 | | return false; |
| | | 351 | | |
| | 15 | 352 | | Vector3d min = query.BoundsMin.Value; |
| | 15 | 353 | | Vector3d max = query.BoundsMax.Value; |
| | 15 | 354 | | bounds = new TopologyVoxelAabb( |
| | 15 | 355 | | new Vector3d( |
| | 15 | 356 | | FixedMath.Min(min.X, max.X), |
| | 15 | 357 | | FixedMath.Min(min.Y, max.Y), |
| | 15 | 358 | | FixedMath.Min(min.Z, max.Z)), |
| | 15 | 359 | | new Vector3d( |
| | 15 | 360 | | FixedMath.Max(min.X, max.X), |
| | 15 | 361 | | FixedMath.Max(min.Y, max.Y), |
| | 15 | 362 | | FixedMath.Max(min.Z, max.Z))); |
| | 15 | 363 | | return true; |
| | | 364 | | } |
| | | 365 | | |
| | | 366 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 367 | | private static bool IsVoxelInQueryBounds( |
| | | 368 | | VoxelGrid grid, |
| | | 369 | | Voxel voxel, |
| | | 370 | | TopologyVoxelAabb queryBounds, |
| | | 371 | | VoxelIndex minIndex, |
| | | 372 | | VoxelIndex maxIndex) |
| | | 373 | | { |
| | 67 | 374 | | VoxelIndex index = voxel.Index; |
| | 67 | 375 | | if (index.x < minIndex.x |
| | 67 | 376 | | || index.x > maxIndex.x |
| | 67 | 377 | | || index.y < minIndex.y |
| | 67 | 378 | | || index.y > maxIndex.y |
| | 67 | 379 | | || index.z < minIndex.z |
| | 67 | 380 | | || index.z > maxIndex.z) |
| | | 381 | | { |
| | 47 | 382 | | return false; |
| | | 383 | | } |
| | | 384 | | |
| | 20 | 385 | | TopologyVoxelAabb voxelBounds = TopologyVoxelAabb.FromVoxel(grid, voxel); |
| | 20 | 386 | | return voxelBounds.Overlaps(queryBounds, Fixed64.Zero); |
| | | 387 | | } |
| | | 388 | | |
| | | 389 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 390 | | private static bool IsIndexInQueryBounds( |
| | | 391 | | VoxelGrid grid, |
| | | 392 | | VoxelIndex index, |
| | | 393 | | TopologyVoxelAabb queryBounds) |
| | | 394 | | { |
| | 52 | 395 | | TopologyVoxelAabb voxelBounds = TopologyVoxelAabb.FromIndex(grid, index); |
| | 52 | 396 | | return voxelBounds.Overlaps(queryBounds, Fixed64.Zero); |
| | | 397 | | } |
| | | 398 | | |
| | | 399 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 400 | | private static GridDiagnosticCellState GetCellState(Voxel voxel) |
| | | 401 | | { |
| | 8789 | 402 | | GridDiagnosticCellState state = GridDiagnosticCellState.None; |
| | 8789 | 403 | | if (!voxel.IsOccupied && !voxel.IsBlocked) |
| | 8777 | 404 | | state |= GridDiagnosticCellState.Empty; |
| | | 405 | | |
| | 8789 | 406 | | if (voxel.IsOccupied) |
| | 6 | 407 | | state |= GridDiagnosticCellState.Occupied; |
| | | 408 | | |
| | 8789 | 409 | | if (voxel.IsBlocked) |
| | 6 | 410 | | state |= GridDiagnosticCellState.Blocked; |
| | | 411 | | |
| | 8789 | 412 | | if (voxel.IsBoundaryVoxel) |
| | 8782 | 413 | | state |= GridDiagnosticCellState.Boundary; |
| | | 414 | | |
| | 8789 | 415 | | if (voxel.IsPartioned) |
| | 6 | 416 | | state |= GridDiagnosticCellState.Partitioned; |
| | | 417 | | |
| | 8789 | 418 | | return state; |
| | | 419 | | } |
| | | 420 | | |
| | | 421 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 422 | | private static GridDiagnosticCellState GetMissingAddressCellState( |
| | | 423 | | VoxelGrid grid, |
| | | 424 | | VoxelIndex index) |
| | | 425 | | { |
| | 47 | 426 | | GridDiagnosticCellState state = GridDiagnosticCellState.MissingSparseAddress; |
| | 47 | 427 | | if (grid.IsOnBoundary(index)) |
| | 46 | 428 | | state |= GridDiagnosticCellState.Boundary; |
| | | 429 | | |
| | 47 | 430 | | return state; |
| | | 431 | | } |
| | | 432 | | |
| | | 433 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 434 | | private static bool MatchesStateFilters( |
| | | 435 | | GridDiagnosticCellState state, |
| | | 436 | | GridDiagnosticCellState requiredStates, |
| | | 437 | | GridDiagnosticCellState excludedStates) |
| | | 438 | | { |
| | 8836 | 439 | | if (requiredStates != GridDiagnosticCellState.None && (state & requiredStates) != requiredStates) |
| | 81 | 440 | | return false; |
| | | 441 | | |
| | 8755 | 442 | | return excludedStates == GridDiagnosticCellState.None || (state & excludedStates) == 0; |
| | | 443 | | } |
| | | 444 | | |
| | | 445 | | private static bool TryGetSparseAddressRange( |
| | | 446 | | VoxelGrid grid, |
| | | 447 | | bool hasBounds, |
| | | 448 | | TopologyVoxelAabb queryBounds, |
| | | 449 | | out VoxelIndex minIndex, |
| | | 450 | | out VoxelIndex maxIndex) |
| | | 451 | | { |
| | 9 | 452 | | if (hasBounds) |
| | 8 | 453 | | return TopologyVoxelRangeUtility.TryGetCandidateRange(grid, queryBounds, out minIndex, out maxIndex); |
| | | 454 | | |
| | 1 | 455 | | minIndex = new VoxelIndex(0, 0, 0); |
| | 1 | 456 | | maxIndex = new VoxelIndex(grid.Width - 1, grid.Height - 1, grid.Length - 1); |
| | 1 | 457 | | return true; |
| | | 458 | | } |
| | | 459 | | |
| | | 460 | | private static bool TryVisitCell<TVisitor>( |
| | | 461 | | in GridDiagnosticCell cell, |
| | | 462 | | int maxCells, |
| | | 463 | | ref TVisitor visitor, |
| | | 464 | | ref int cellCount, |
| | | 465 | | ref int skippedCellCount, |
| | | 466 | | out GridDiagnosticQueryStatus status) |
| | | 467 | | where TVisitor : struct, IGridDiagnosticCellVisitor |
| | | 468 | | { |
| | 8729 | 469 | | if (cellCount >= maxCells) |
| | | 470 | | { |
| | 3 | 471 | | skippedCellCount++; |
| | 3 | 472 | | status = GridDiagnosticQueryStatus.MaxCellsExceeded; |
| | 3 | 473 | | return false; |
| | | 474 | | } |
| | | 475 | | |
| | 8726 | 476 | | cellCount++; |
| | 8726 | 477 | | if (!visitor.Visit(in cell)) |
| | | 478 | | { |
| | 1 | 479 | | skippedCellCount++; |
| | 1 | 480 | | status = GridDiagnosticQueryStatus.Truncated; |
| | 1 | 481 | | return false; |
| | | 482 | | } |
| | | 483 | | |
| | 8725 | 484 | | status = GridDiagnosticQueryStatus.Completed; |
| | 8725 | 485 | | return true; |
| | | 486 | | } |
| | | 487 | | |
| | | 488 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 489 | | private static GridDiagnosticCell CreatePhysicalCell( |
| | | 490 | | GridWorld world, |
| | | 491 | | VoxelGrid grid, |
| | | 492 | | Voxel voxel, |
| | | 493 | | GridDiagnosticCellState state) => |
| | 8682 | 494 | | new( |
| | 8682 | 495 | | GridDiagnosticCellKind.Physical, |
| | 8682 | 496 | | world.SpawnToken, |
| | 8682 | 497 | | grid.GridIndex, |
| | 8682 | 498 | | grid.SpawnToken, |
| | 8682 | 499 | | voxel.Index, |
| | 8682 | 500 | | voxel.WorldPosition, |
| | 8682 | 501 | | grid.Configuration.TopologyKind, |
| | 8682 | 502 | | grid.StorageKind, |
| | 8682 | 503 | | grid.Configuration.TopologyMetrics, |
| | 8682 | 504 | | state, |
| | 8682 | 505 | | voxel.WorldIndex); |
| | | 506 | | |
| | | 507 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 508 | | private static GridDiagnosticCell CreateMissingAddressCell( |
| | | 509 | | GridWorld world, |
| | | 510 | | VoxelGrid grid, |
| | | 511 | | VoxelIndex index, |
| | | 512 | | GridDiagnosticCellState state) => |
| | 47 | 513 | | new( |
| | 47 | 514 | | GridDiagnosticCellKind.MissingSparseAddress, |
| | 47 | 515 | | world.SpawnToken, |
| | 47 | 516 | | grid.GridIndex, |
| | 47 | 517 | | grid.SpawnToken, |
| | 47 | 518 | | index, |
| | 47 | 519 | | grid.GetWorldPosition(index), |
| | 47 | 520 | | grid.Configuration.TopologyKind, |
| | 47 | 521 | | GridStorageKind.Sparse, |
| | 47 | 522 | | grid.Configuration.TopologyMetrics, |
| | 47 | 523 | | state, |
| | 47 | 524 | | new WorldVoxelIndex(world.SpawnToken, grid.GridIndex, grid.SpawnToken, index)); |
| | | 525 | | |
| | | 526 | | private struct ResultListVisitor : IGridDiagnosticCellVisitor |
| | | 527 | | { |
| | | 528 | | private readonly SwiftList<GridDiagnosticCell> _results; |
| | | 529 | | |
| | | 530 | | public ResultListVisitor(SwiftList<GridDiagnosticCell> results) |
| | | 531 | | { |
| | 37 | 532 | | _results = results; |
| | 37 | 533 | | } |
| | | 534 | | |
| | | 535 | | public bool Visit(in GridDiagnosticCell cell) |
| | | 536 | | { |
| | 143 | 537 | | _results.Add(cell); |
| | 143 | 538 | | return true; |
| | | 539 | | } |
| | | 540 | | } |
| | | 541 | | |
| | | 542 | | private struct PhysicalCellVisitor<TVisitor> : IVoxelStorageVisitor |
| | | 543 | | where TVisitor : struct, IGridDiagnosticCellVisitor |
| | | 544 | | { |
| | | 545 | | private readonly GridWorld _world; |
| | | 546 | | private readonly VoxelGrid _grid; |
| | | 547 | | private readonly GridDiagnosticQuery _query; |
| | | 548 | | private readonly bool _hasBounds; |
| | | 549 | | private readonly TopologyVoxelAabb _queryBounds; |
| | | 550 | | private readonly VoxelIndex _minIndex; |
| | | 551 | | private readonly VoxelIndex _maxIndex; |
| | | 552 | | |
| | | 553 | | public TVisitor Visitor; |
| | | 554 | | public int CellCount; |
| | | 555 | | public int SkippedCellCount; |
| | | 556 | | public GridDiagnosticQueryStatus Status; |
| | | 557 | | |
| | | 558 | | public PhysicalCellVisitor( |
| | | 559 | | GridWorld world, |
| | | 560 | | VoxelGrid grid, |
| | | 561 | | in GridDiagnosticQuery query, |
| | | 562 | | bool hasBounds, |
| | | 563 | | TopologyVoxelAabb queryBounds, |
| | | 564 | | VoxelIndex minIndex, |
| | | 565 | | VoxelIndex maxIndex, |
| | | 566 | | TVisitor visitor, |
| | | 567 | | int cellCount, |
| | | 568 | | int skippedCellCount) |
| | | 569 | | { |
| | 87 | 570 | | _world = world; |
| | 87 | 571 | | _grid = grid; |
| | 87 | 572 | | _query = query; |
| | 87 | 573 | | _hasBounds = hasBounds; |
| | 87 | 574 | | _queryBounds = queryBounds; |
| | 87 | 575 | | _minIndex = minIndex; |
| | 87 | 576 | | _maxIndex = maxIndex; |
| | 87 | 577 | | Visitor = visitor; |
| | 87 | 578 | | CellCount = cellCount; |
| | 87 | 579 | | SkippedCellCount = skippedCellCount; |
| | 87 | 580 | | Status = GridDiagnosticQueryStatus.Completed; |
| | 87 | 581 | | } |
| | | 582 | | |
| | | 583 | | public bool Visit(Voxel voxel) |
| | | 584 | | { |
| | 8840 | 585 | | if (_hasBounds && !IsVoxelInQueryBounds(_grid, voxel, _queryBounds, _minIndex, _maxIndex)) |
| | 55 | 586 | | return true; |
| | | 587 | | |
| | 8785 | 588 | | GridDiagnosticCellState state = GetCellState(voxel); |
| | 8785 | 589 | | if (!MatchesStateFilters(state, _query.RequiredStates, _query.ExcludedStates)) |
| | 107 | 590 | | return true; |
| | | 591 | | |
| | 8678 | 592 | | GridDiagnosticCell cell = CreatePhysicalCell(_world, _grid, voxel, state); |
| | 8678 | 593 | | return TryVisitCell( |
| | 8678 | 594 | | in cell, |
| | 8678 | 595 | | _query.MaxCells, |
| | 8678 | 596 | | ref Visitor, |
| | 8678 | 597 | | ref CellCount, |
| | 8678 | 598 | | ref SkippedCellCount, |
| | 8678 | 599 | | out Status); |
| | | 600 | | } |
| | | 601 | | } |
| | | 602 | | } |