| | | 1 | | //======================================================================= |
| | | 2 | | // GridTraversalState.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.Runtime.CompilerServices; |
| | | 10 | | using FixedMathSharp; |
| | | 11 | | using GridForge.Grids; |
| | | 12 | | using GridForge.Grids.Topology; |
| | | 13 | | using GridForge.Spatial; |
| | | 14 | | using SwiftCollections; |
| | | 15 | | |
| | | 16 | | namespace GridForge.Utility; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Tracks per-grid traversal padding while suppressing duplicate voxel visits. |
| | | 20 | | /// </summary> |
| | | 21 | | public struct GridTraversalState |
| | | 22 | | { |
| | | 23 | | private readonly GridWorld _world; |
| | | 24 | | private readonly GridTraversalPaddingMode _paddingMode; |
| | | 25 | | private ushort _currentGridIndex; |
| | | 26 | | private long _currentGridSpawnToken; |
| | | 27 | | private VoxelGrid? _currentGrid; |
| | | 28 | | private Fixed64 _cellEdge; |
| | | 29 | | private bool _hasCachedGrid; |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Initializes traversal state for one world and padding mode. |
| | | 33 | | /// </summary> |
| | | 34 | | public GridTraversalState(GridWorld world, GridTraversalPaddingMode paddingMode) |
| | | 35 | | { |
| | 1045 | 36 | | _world = world; |
| | 1045 | 37 | | _paddingMode = paddingMode; |
| | 1045 | 38 | | _currentGridIndex = 0; |
| | 1045 | 39 | | _currentGridSpawnToken = 0; |
| | 1045 | 40 | | _currentGrid = null; |
| | 1045 | 41 | | _cellEdge = Fixed64.Zero; |
| | 1045 | 42 | | _hasCachedGrid = false; |
| | 1045 | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Visits a voxel only once and returns the selected cell-edge measurement for its grid. |
| | | 47 | | /// </summary> |
| | | 48 | | /// <returns>True when the voxel belongs to an active grid generation and was not already visited; otherwise false.< |
| | | 49 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 50 | | public bool TryVisitUnique(Voxel voxel, SwiftHashSet<WorldVoxelIndex> visited, out Fixed64 cellEdge) |
| | | 51 | | { |
| | 2089 | 52 | | cellEdge = Fixed64.Zero; |
| | 2089 | 53 | | WorldVoxelIndex voxelIndex = voxel.WorldIndex; |
| | 2089 | 54 | | if (!visited.Add(voxelIndex)) |
| | 1042 | 55 | | return false; |
| | | 56 | | |
| | 1047 | 57 | | if (!TryResolveGrid(voxelIndex, out VoxelGrid? grid)) |
| | | 58 | | { |
| | 1 | 59 | | visited.Remove(voxelIndex); |
| | 1 | 60 | | return false; |
| | | 61 | | } |
| | | 62 | | |
| | 1046 | 63 | | cellEdge = GetCellEdge(grid!); |
| | 1046 | 64 | | return true; |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Gets the selected cell-edge measurement for a voxel's grid, caching repeated grid lookups. |
| | | 69 | | /// </summary> |
| | | 70 | | /// <exception cref="InvalidOperationException">The voxel does not belong to an active grid generation in this trave |
| | | 71 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 72 | | public Fixed64 GetCellEdge(Voxel voxel) |
| | | 73 | | { |
| | 2 | 74 | | bool isCurrent = TryResolveGrid(voxel.WorldIndex, out VoxelGrid? grid); |
| | 2 | 75 | | ChronicleGuard.ThrowIfTrue( |
| | 2 | 76 | | !isCurrent, |
| | 2 | 77 | | nameof(voxel), |
| | 2 | 78 | | "The voxel does not belong to an active grid generation in this traversal's world."); |
| | | 79 | | |
| | 1 | 80 | | return GetCellEdge(grid!); |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 84 | | private Fixed64 GetCellEdge(VoxelGrid grid) |
| | | 85 | | { |
| | 1047 | 86 | | if (_hasCachedGrid |
| | 1047 | 87 | | && grid.GridIndex == _currentGridIndex |
| | 1047 | 88 | | && grid.SpawnToken == _currentGridSpawnToken) |
| | | 89 | | { |
| | 1 | 90 | | return _cellEdge; |
| | | 91 | | } |
| | | 92 | | |
| | 1046 | 93 | | _currentGridIndex = grid.GridIndex; |
| | 1046 | 94 | | _currentGridSpawnToken = grid.SpawnToken; |
| | 1046 | 95 | | _currentGrid = grid; |
| | 1046 | 96 | | _hasCachedGrid = true; |
| | 1046 | 97 | | _cellEdge = _paddingMode == GridTraversalPaddingMode.PlanarMaxCellEdge |
| | 1046 | 98 | | ? GridTopologyMetricUtility.GetPlanarMaxCellEdge(grid) |
| | 1046 | 99 | | : GridTopologyMetricUtility.GetMaxCellEdge(grid); |
| | 1046 | 100 | | return _cellEdge; |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 104 | | private bool TryResolveGrid(WorldVoxelIndex voxelIndex, out VoxelGrid? grid) |
| | | 105 | | { |
| | 1049 | 106 | | grid = _currentGrid; |
| | 1049 | 107 | | if (_hasCachedGrid |
| | 1049 | 108 | | && voxelIndex.WorldSpawnToken == _world.SpawnToken |
| | 1049 | 109 | | && voxelIndex.GridIndex == _currentGridIndex |
| | 1049 | 110 | | && voxelIndex.GridSpawnToken == _currentGridSpawnToken |
| | 1049 | 111 | | && grid != null |
| | 1049 | 112 | | && grid.IsActive |
| | 1049 | 113 | | && ReferenceEquals(grid.World, _world) |
| | 1049 | 114 | | && grid.GridIndex == _currentGridIndex |
| | 1049 | 115 | | && grid.SpawnToken == _currentGridSpawnToken) |
| | | 116 | | { |
| | 1 | 117 | | return true; |
| | | 118 | | } |
| | | 119 | | |
| | 1048 | 120 | | return _world.TryGetGrid(voxelIndex, out grid); |
| | | 121 | | } |
| | | 122 | | } |