| | | 1 | | //======================================================================= |
| | | 2 | | // GridTraversal.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 | | /// Selects which topology edge measurement a grid traversal should use for padding. |
| | | 20 | | /// </summary> |
| | | 21 | | public enum GridTraversalPaddingMode |
| | | 22 | | { |
| | | 23 | | /// <summary> |
| | | 24 | | /// Use the largest three-dimensional cell edge. |
| | | 25 | | /// </summary> |
| | | 26 | | MaxCellEdge, |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Use the largest X/Z-plane cell edge. |
| | | 30 | | /// </summary> |
| | | 31 | | PlanarMaxCellEdge |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Tracks per-grid traversal padding while suppressing duplicate voxel visits. |
| | | 36 | | /// </summary> |
| | | 37 | | public struct GridTraversalState |
| | | 38 | | { |
| | | 39 | | private readonly GridWorld _world; |
| | | 40 | | private readonly GridTraversalPaddingMode _paddingMode; |
| | | 41 | | private ushort _currentGridIndex; |
| | | 42 | | private long _currentGridSpawnToken; |
| | | 43 | | private VoxelGrid? _currentGrid; |
| | | 44 | | private Fixed64 _cellEdge; |
| | | 45 | | private bool _hasCachedGrid; |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Initializes traversal state for one world and padding mode. |
| | | 49 | | /// </summary> |
| | | 50 | | public GridTraversalState(GridWorld world, GridTraversalPaddingMode paddingMode) |
| | | 51 | | { |
| | | 52 | | _world = world; |
| | | 53 | | _paddingMode = paddingMode; |
| | | 54 | | _currentGridIndex = 0; |
| | | 55 | | _currentGridSpawnToken = 0; |
| | | 56 | | _currentGrid = null; |
| | | 57 | | _cellEdge = Fixed64.Zero; |
| | | 58 | | _hasCachedGrid = false; |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Visits a voxel only once and returns the selected cell-edge measurement for its grid. |
| | | 63 | | /// </summary> |
| | | 64 | | /// <returns>True when the voxel belongs to an active grid generation and was not already visited; otherwise false.< |
| | | 65 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 66 | | public bool TryVisitUnique(Voxel voxel, SwiftHashSet<WorldVoxelIndex> visited, out Fixed64 cellEdge) |
| | | 67 | | { |
| | | 68 | | cellEdge = Fixed64.Zero; |
| | | 69 | | WorldVoxelIndex voxelIndex = voxel.WorldIndex; |
| | | 70 | | if (!visited.Add(voxelIndex)) |
| | | 71 | | return false; |
| | | 72 | | |
| | | 73 | | if (!TryResolveGrid(voxelIndex, out VoxelGrid? grid)) |
| | | 74 | | { |
| | | 75 | | visited.Remove(voxelIndex); |
| | | 76 | | return false; |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | cellEdge = GetCellEdge(grid!); |
| | | 80 | | return true; |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Gets the selected cell-edge measurement for a voxel's grid, caching repeated grid lookups. |
| | | 85 | | /// </summary> |
| | | 86 | | /// <exception cref="InvalidOperationException">The voxel does not belong to an active grid generation in this trave |
| | | 87 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 88 | | public Fixed64 GetCellEdge(Voxel voxel) |
| | | 89 | | { |
| | | 90 | | bool isCurrent = TryResolveGrid(voxel.WorldIndex, out VoxelGrid? grid); |
| | | 91 | | SwiftThrowHelper.ThrowIfTrue( |
| | | 92 | | !isCurrent, |
| | | 93 | | nameof(voxel), |
| | | 94 | | "The voxel does not belong to an active grid generation in this traversal's world."); |
| | | 95 | | |
| | | 96 | | return GetCellEdge(grid!); |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 100 | | private Fixed64 GetCellEdge(VoxelGrid grid) |
| | | 101 | | { |
| | | 102 | | if (_hasCachedGrid |
| | | 103 | | && grid.GridIndex == _currentGridIndex |
| | | 104 | | && grid.SpawnToken == _currentGridSpawnToken) |
| | | 105 | | { |
| | | 106 | | return _cellEdge; |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | _currentGridIndex = grid.GridIndex; |
| | | 110 | | _currentGridSpawnToken = grid.SpawnToken; |
| | | 111 | | _currentGrid = grid; |
| | | 112 | | _hasCachedGrid = true; |
| | | 113 | | _cellEdge = _paddingMode == GridTraversalPaddingMode.PlanarMaxCellEdge |
| | | 114 | | ? GridTopologyMetricUtility.GetPlanarMaxCellEdge(grid) |
| | | 115 | | : GridTopologyMetricUtility.GetMaxCellEdge(grid); |
| | | 116 | | return _cellEdge; |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 120 | | private bool TryResolveGrid(WorldVoxelIndex voxelIndex, out VoxelGrid? grid) |
| | | 121 | | { |
| | | 122 | | grid = _currentGrid; |
| | | 123 | | if (_hasCachedGrid |
| | | 124 | | && voxelIndex.WorldSpawnToken == _world.SpawnToken |
| | | 125 | | && voxelIndex.GridIndex == _currentGridIndex |
| | | 126 | | && voxelIndex.GridSpawnToken == _currentGridSpawnToken |
| | | 127 | | && grid != null |
| | | 128 | | && grid.IsActive |
| | | 129 | | && ReferenceEquals(grid.World, _world) |
| | | 130 | | && grid.GridIndex == _currentGridIndex |
| | | 131 | | && grid.SpawnToken == _currentGridSpawnToken) |
| | | 132 | | { |
| | | 133 | | return true; |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | return _world.TryGetGrid(voxelIndex, out grid); |
| | | 137 | | } |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | /// <summary> |
| | | 141 | | /// Provides deterministic helpers for duplicate-safe voxel traversal. |
| | | 142 | | /// </summary> |
| | | 143 | | public static class GridTraversal |
| | | 144 | | { |
| | | 145 | | /// <summary> |
| | | 146 | | /// Gets a voxel partition once per exact world-scoped voxel identity. |
| | | 147 | | /// </summary> |
| | | 148 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 149 | | public static bool TryGetUniquePartition<TPartition>( |
| | | 150 | | Voxel voxel, |
| | | 151 | | SwiftHashSet<WorldVoxelIndex> visited, |
| | | 152 | | out TPartition? partition) |
| | | 153 | | where TPartition : class, IVoxelPartition |
| | | 154 | | { |
| | 2082 | 155 | | partition = null; |
| | 2082 | 156 | | return visited.Add(voxel.WorldIndex) |
| | 2082 | 157 | | && voxel.TryGetPartition(out partition); |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | /// <summary> |
| | | 161 | | /// Tests whether a 3D world position lies inside bounds expanded by half of a cell edge. |
| | | 162 | | /// </summary> |
| | | 163 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 164 | | public static bool IsWorldPositionInPaddedBounds( |
| | | 165 | | Vector3d min, |
| | | 166 | | Vector3d max, |
| | | 167 | | Fixed64 cellEdge, |
| | | 168 | | Vector3d worldPosition) |
| | | 169 | | { |
| | 2 | 170 | | Fixed64 padding = cellEdge * Fixed64.Half; |
| | 2 | 171 | | return worldPosition.X >= min.X - padding |
| | 2 | 172 | | && worldPosition.X <= max.X + padding |
| | 2 | 173 | | && worldPosition.Y >= min.Y - padding |
| | 2 | 174 | | && worldPosition.Y <= max.Y + padding |
| | 2 | 175 | | && worldPosition.Z >= min.Z - padding |
| | 2 | 176 | | && worldPosition.Z <= max.Z + padding; |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | /// <summary> |
| | | 180 | | /// Tests whether a world position's X/Z projection lies inside bounds expanded by half of a cell edge. |
| | | 181 | | /// </summary> |
| | | 182 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 183 | | public static bool IsPlanarPositionInPaddedBounds( |
| | | 184 | | Vector2d min, |
| | | 185 | | Vector2d max, |
| | | 186 | | Fixed64 cellEdge, |
| | | 187 | | Vector3d worldPosition) |
| | | 188 | | { |
| | 2 | 189 | | Fixed64 padding = cellEdge * Fixed64.Half; |
| | 2 | 190 | | return worldPosition.X >= min.X - padding |
| | 2 | 191 | | && worldPosition.X <= max.X + padding |
| | 2 | 192 | | && worldPosition.Z >= min.Y - padding |
| | 2 | 193 | | && worldPosition.Z <= max.Y + padding; |
| | | 194 | | } |
| | | 195 | | } |