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