| | | 1 | | //======================================================================= |
| | | 2 | | // VoxelGrid.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 System.Runtime.CompilerServices; |
| | | 11 | | using FixedMathSharp; |
| | | 12 | | using GridForge.Configuration; |
| | | 13 | | using GridForge.Grids.Storage; |
| | | 14 | | using GridForge.Grids.Topology; |
| | | 15 | | using GridForge.Spatial; |
| | | 16 | | using SwiftCollections; |
| | | 17 | | using SwiftCollections.Dimensions; |
| | | 18 | | using SwiftCollections.Pool; |
| | | 19 | | |
| | | 20 | | namespace GridForge.Grids; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Represents a 3D grid structure for spatial organization, managing voxels and scan cells. |
| | | 24 | | /// Handles initialization, neighbor relationships, and occupancy tracking. |
| | | 25 | | /// </summary> |
| | | 26 | | public class VoxelGrid |
| | | 27 | | { |
| | | 28 | | #region Fields & Properties |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Nonzero 64-bit world-local allocation generation identifying this active grid instance. |
| | | 32 | | /// Zero indicates an inactive or unallocated grid. |
| | | 33 | | /// </summary> |
| | | 34 | | public long SpawnToken { get; private set; } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// World-local index of the grid within its owning world. |
| | | 38 | | /// </summary> |
| | | 39 | | public ushort GridIndex { get; private set; } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// The world that owns this grid instance. |
| | | 43 | | /// </summary> |
| | | 44 | | public GridWorld? World { get; private set; } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Synchronizes obstacle mutations for this grid. |
| | | 48 | | /// </summary> |
| | | 49 | | internal object ObstacleSyncRoot { get; } = new object(); |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Synchronizes occupant mutations for this grid. |
| | | 53 | | /// </summary> |
| | | 54 | | internal object OccupantSyncRoot { get; } = new object(); |
| | | 55 | | |
| | | 56 | | /// <inheritdoc cref="GridConfiguration"/> |
| | | 57 | | public GridConfiguration Configuration { get; private set; } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Minimum bounds of the grid in world coordinates. |
| | | 61 | | /// </summary> |
| | 141679 | 62 | | public Vector3d BoundsMin => Configuration.BoundsMin; |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Maximum bounds of the grid in world coordinates. |
| | | 66 | | /// </summary> |
| | 14757 | 67 | | public Vector3d BoundsMax => Configuration.BoundsMax; |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Center position of the grid in world space. |
| | | 71 | | /// </summary> |
| | 510 | 72 | | public Vector3d BoundsCenter => Configuration.GridCenter; |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Grid width in number of voxels. |
| | | 76 | | /// </summary> |
| | | 77 | | public int Width { get; private set; } |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Grid height in number of voxels. |
| | | 81 | | /// </summary> |
| | | 82 | | public int Height { get; private set; } |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Grid length in number of voxels. |
| | | 86 | | /// </summary> |
| | | 87 | | public int Length { get; private set; } |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Total addressable voxel count within the grid bounds. |
| | | 91 | | /// </summary> |
| | | 92 | | public int Size { get; private set; } |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// The number of physical voxels configured in the grid storage. |
| | | 96 | | /// Dense grids report <see cref="Size"/>; sparse grids report configured voxels only. |
| | | 97 | | /// </summary> |
| | | 98 | | public int ConfiguredVoxelCount |
| | | 99 | | { |
| | | 100 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 78 | 101 | | get => _storage?.ConfiguredVoxelCount ?? 0; |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// The physical voxel storage strategy used by this grid. |
| | | 106 | | /// </summary> |
| | | 107 | | public GridStorageKind StorageKind |
| | | 108 | | { |
| | | 109 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 9293 | 110 | | get => _storage?.Kind ?? GridStorageKind.Dense; |
| | | 111 | | } |
| | | 112 | | |
| | | 113 | | /// <summary> |
| | | 114 | | /// The dense 3D collection of voxels managed by this grid when dense storage is active. |
| | | 115 | | /// </summary> |
| | | 116 | | internal SwiftArray3D<Voxel>? Voxels |
| | | 117 | | { |
| | | 118 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 3 | 119 | | get => _denseStorage.Voxels; |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Stores topology-local neighbor slots for neighboring grids based on their relative positions. |
| | | 124 | | /// </summary> |
| | | 125 | | /// <remarks> |
| | | 126 | | /// Unlike voxel adjacency (which is always 1:1), grids can share multiple neighbors in the same direction. |
| | | 127 | | /// </remarks> |
| | | 128 | | public SwiftSparseMap<SwiftHashSet<int>>? Neighbors { get; private set; } |
| | | 129 | | |
| | | 130 | | /// <summary> |
| | | 131 | | /// Count of currently linked neighboring grids. |
| | | 132 | | /// </summary> |
| | | 133 | | public byte NeighborCount { get; private set; } |
| | | 134 | | |
| | | 135 | | /// <summary> |
| | | 136 | | /// Count of topology-local neighbor slots supported by this grid. |
| | | 137 | | /// </summary> |
| | 397 | 138 | | internal int NeighborSlotCount => _topology?.NeighborSlotCount ?? 0; |
| | | 139 | | |
| | | 140 | | /// <summary> |
| | | 141 | | /// The active topology kind for this grid. |
| | | 142 | | /// </summary> |
| | 125 | 143 | | internal GridTopologyKind? TopologyKind => _topology?.Kind; |
| | | 144 | | |
| | | 145 | | /// <summary> |
| | | 146 | | /// Determines whether this grid has any linked neighbors. |
| | | 147 | | /// </summary> |
| | 103 | 148 | | public bool IsConjoined => Neighbors != null && NeighborCount > 0; |
| | | 149 | | |
| | | 150 | | /// <summary> |
| | | 151 | | /// Size of a scan cell used for spatial partitioning. |
| | | 152 | | /// </summary> |
| | 375051 | 153 | | public int ScanCellSize => Configuration.ScanCellSize; |
| | | 154 | | |
| | | 155 | | /// <summary> |
| | | 156 | | /// Collection of scan cells indexed by their grid-local scan cell key. |
| | | 157 | | /// </summary> |
| | | 158 | | internal SwiftSparseMap<ScanCell>? ScanCells |
| | | 159 | | { |
| | | 160 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 3 | 161 | | get => _storage?.ScanCells; |
| | | 162 | | } |
| | | 163 | | |
| | | 164 | | /// <summary> |
| | | 165 | | /// Stores currently active (occupied) scan cells within the grid. |
| | | 166 | | /// </summary> |
| | | 167 | | public SwiftHashSet<int>? ActiveScanCells { get; internal set; } |
| | | 168 | | |
| | | 169 | | /// <summary> |
| | | 170 | | /// Indicates whether the grid is currently active. |
| | | 171 | | /// </summary> |
| | | 172 | | public bool IsActive { get; private set; } |
| | | 173 | | |
| | | 174 | | /// <summary> |
| | | 175 | | /// Determines whether the grid is occupied (active and containing occupants). |
| | | 176 | | /// </summary> |
| | 29 | 177 | | public bool IsOccupied => ActiveScanCells?.Count > 0; |
| | | 178 | | |
| | | 179 | | /// <summary> |
| | | 180 | | /// Tracks the number of obstacles currently registered in the grid. |
| | | 181 | | /// </summary> |
| | | 182 | | public int ObstacleCount { get; internal set; } |
| | | 183 | | |
| | | 184 | | /// <summary> |
| | | 185 | | /// Tracks the version of the grid, incremented when a <see cref="Voxel"/> is modified. |
| | | 186 | | /// </summary> |
| | | 187 | | public uint Version { get; private set; } |
| | | 188 | | |
| | | 189 | | internal ulong LastChangeSequence { get; set; } |
| | | 190 | | |
| | | 191 | | private int _scanWidth; |
| | | 192 | | private int _scanHeight; |
| | | 193 | | private int _scanLength; |
| | | 194 | | private int _scanLayerSize; |
| | | 195 | | |
| | | 196 | | private IGridTopology? _topology; |
| | | 197 | | private IVoxelGridStorage? _storage; |
| | 75 | 198 | | private readonly DenseVoxelGridStorage _denseStorage = new(); |
| | 75 | 199 | | private readonly SparseVoxelGridStorage _sparseStorage = new(); |
| | | 200 | | |
| | 146959 | 201 | | internal IGridTopology Topology => _topology!; |
| | | 202 | | |
| | | 203 | | internal int ScanWidth |
| | | 204 | | { |
| | | 205 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2078 | 206 | | get => _scanWidth; |
| | | 207 | | } |
| | | 208 | | |
| | | 209 | | internal int ScanHeight |
| | | 210 | | { |
| | | 211 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2379 | 212 | | get => _scanHeight; |
| | | 213 | | } |
| | | 214 | | |
| | | 215 | | internal int ScanLength |
| | | 216 | | { |
| | | 217 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 3599 | 218 | | get => _scanLength; |
| | | 219 | | } |
| | | 220 | | |
| | | 221 | | #endregion |
| | | 222 | | |
| | | 223 | | #region Initialization & Reset |
| | | 224 | | |
| | | 225 | | /// <summary> |
| | | 226 | | /// Initializes the grid with an explicit owning world and configured sparse voxel set. |
| | | 227 | | /// </summary> |
| | | 228 | | /// <param name="world">The world that will own this grid.</param> |
| | | 229 | | /// <param name="gridIndex">The unique index of this grid in the world.</param> |
| | | 230 | | /// <param name="spawnToken">The world-local allocation generation for this grid.</param> |
| | | 231 | | /// <param name="configuration">The normalized configuration settings for the grid.</param> |
| | | 232 | | /// <param name="topology">The validated topology instance for this grid.</param> |
| | | 233 | | /// <param name="configuredVoxels">The validated sparse voxel indices to materialize.</param> |
| | | 234 | | internal void Initialize( |
| | | 235 | | GridWorld world, |
| | | 236 | | ushort gridIndex, |
| | | 237 | | long spawnToken, |
| | | 238 | | GridConfiguration configuration, |
| | | 239 | | IGridTopology topology, |
| | | 240 | | VoxelIndex[] configuredVoxels) |
| | | 241 | | { |
| | 919 | 242 | | Version = 1; |
| | | 243 | | |
| | 919 | 244 | | World = world; |
| | 919 | 245 | | GridIndex = gridIndex; |
| | | 246 | | |
| | 919 | 247 | | Configuration = configuration; |
| | 919 | 248 | | _topology = topology; |
| | | 249 | | |
| | 919 | 250 | | SpawnToken = spawnToken; |
| | | 251 | | |
| | | 252 | | // +1 to account for inclusive bounds and to ensure that even the smallest grids (1x1x1) remain valid. |
| | 919 | 253 | | GridDimensions dimensions = topology.CalculateDimensions(BoundsMin, BoundsMax); |
| | 919 | 254 | | Width = dimensions.Width; |
| | 919 | 255 | | Height = dimensions.Height; |
| | 919 | 256 | | Length = dimensions.Length; |
| | 919 | 257 | | Size = Width * Height * Length; |
| | | 258 | | |
| | 919 | 259 | | ConfigureScanDimensions(); |
| | 919 | 260 | | if (configuration.StorageKind == GridStorageKind.Sparse) |
| | | 261 | | { |
| | 124 | 262 | | _sparseStorage.Initialize(this, configuredVoxels); |
| | 124 | 263 | | _storage = _sparseStorage; |
| | | 264 | | } |
| | | 265 | | else |
| | | 266 | | { |
| | 795 | 267 | | _denseStorage.Initialize(this); |
| | 795 | 268 | | _storage = _denseStorage; |
| | | 269 | | } |
| | | 270 | | |
| | 919 | 271 | | IsActive = true; |
| | 919 | 272 | | } |
| | | 273 | | |
| | | 274 | | /// <summary> |
| | | 275 | | /// Resets the grid, clearing all voxels and scan cells. |
| | | 276 | | /// </summary> |
| | | 277 | | internal void Reset() |
| | | 278 | | { |
| | 921 | 279 | | if (!IsActive) |
| | 2 | 280 | | return; |
| | | 281 | | |
| | 919 | 282 | | _storage!.Reset(this); |
| | 919 | 283 | | _storage = null; |
| | | 284 | | |
| | | 285 | | // Just in case since voxels should have already cleared any registered obstacles. |
| | 919 | 286 | | ObstacleCount = 0; |
| | | 287 | | |
| | 919 | 288 | | ReleaseActiveScanCells(); |
| | 919 | 289 | | ReleaseNeighbors(); |
| | | 290 | | |
| | 919 | 291 | | Configuration = default; |
| | 919 | 292 | | World = null; |
| | 919 | 293 | | _topology = null; |
| | | 294 | | |
| | 919 | 295 | | SpawnToken = 0; |
| | 919 | 296 | | Version = 0; |
| | 919 | 297 | | LastChangeSequence = 0; |
| | | 298 | | |
| | 919 | 299 | | GridIndex = ushort.MaxValue; |
| | | 300 | | |
| | 919 | 301 | | ClearDimensions(); |
| | | 302 | | |
| | 919 | 303 | | IsActive = false; |
| | 919 | 304 | | } |
| | | 305 | | |
| | | 306 | | private void ReleaseActiveScanCells() |
| | | 307 | | { |
| | 919 | 308 | | if (ActiveScanCells == null) |
| | 872 | 309 | | return; |
| | | 310 | | |
| | 47 | 311 | | SwiftHashSetPool<int>.Shared.Release(ActiveScanCells); |
| | 47 | 312 | | ActiveScanCells = null; |
| | 47 | 313 | | } |
| | | 314 | | |
| | | 315 | | private void ReleaseNeighbors() |
| | | 316 | | { |
| | 919 | 317 | | if (Neighbors == null) |
| | 820 | 318 | | return; |
| | | 319 | | |
| | 426 | 320 | | foreach (SwiftHashSet<int> neighbors in Neighbors.Values) |
| | 114 | 321 | | SwiftHashSetPool<int>.Shared.Release(neighbors); |
| | | 322 | | |
| | 99 | 323 | | Neighbors = null; |
| | 99 | 324 | | NeighborCount = 0; |
| | 99 | 325 | | } |
| | | 326 | | |
| | | 327 | | private void ClearDimensions() |
| | | 328 | | { |
| | 919 | 329 | | Width = 0; |
| | 919 | 330 | | Height = 0; |
| | 919 | 331 | | Length = 0; |
| | 919 | 332 | | Size = 0; |
| | 919 | 333 | | _scanWidth = 0; |
| | 919 | 334 | | _scanHeight = 0; |
| | 919 | 335 | | _scanLength = 0; |
| | 919 | 336 | | _scanLayerSize = 0; |
| | 919 | 337 | | } |
| | | 338 | | |
| | | 339 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 340 | | internal uint IncrementVersion() |
| | | 341 | | { |
| | 1932 | 342 | | Version = Version == uint.MaxValue ? 1u : Version + 1u; |
| | 1932 | 343 | | return Version; |
| | | 344 | | } |
| | | 345 | | |
| | | 346 | | #endregion |
| | | 347 | | |
| | | 348 | | #region Grid Construction |
| | | 349 | | |
| | | 350 | | private void ConfigureScanDimensions() |
| | | 351 | | { |
| | 919 | 352 | | _scanWidth = ((Width - 1) / ScanCellSize) + 1; |
| | 919 | 353 | | _scanHeight = ((Height - 1) / ScanCellSize) + 1; |
| | 919 | 354 | | _scanLength = ((Length - 1) / ScanCellSize) + 1; |
| | 919 | 355 | | _scanLayerSize = _scanWidth * _scanHeight; |
| | 919 | 356 | | } |
| | | 357 | | |
| | | 358 | | #endregion |
| | | 359 | | |
| | | 360 | | #region Boundary Management |
| | | 361 | | |
| | | 362 | | /// <summary> |
| | | 363 | | /// Determines the rectangular-prism direction from grid <paramref name="a"/> to neighboring grid <paramref name="b" |
| | | 364 | | /// </summary> |
| | | 365 | | /// <param name="a">The source rectangular-prism grid.</param> |
| | | 366 | | /// <param name="b">The neighboring rectangular-prism grid.</param> |
| | | 367 | | /// <returns>The rectangular direction from <paramref name="a"/> to <paramref name="b"/>, or <see cref="RectangularD |
| | | 368 | | public static RectangularDirection GetRectangularNeighborDirection(VoxelGrid a, VoxelGrid b) |
| | | 369 | | { |
| | 8 | 370 | | return TryGetNeighborSlot(a, b, GridTopologyKind.RectangularPrism, out int slot) |
| | 8 | 371 | | ? (RectangularDirection)slot |
| | 8 | 372 | | : RectangularDirection.None; |
| | | 373 | | } |
| | | 374 | | |
| | | 375 | | /// <summary> |
| | | 376 | | /// Determines the hex-prism direction from grid <paramref name="a"/> to neighboring grid <paramref name="b"/>. |
| | | 377 | | /// </summary> |
| | | 378 | | /// <param name="a">The source hex-prism grid.</param> |
| | | 379 | | /// <param name="b">The neighboring hex-prism grid.</param> |
| | | 380 | | /// <returns>The hex direction from <paramref name="a"/> to <paramref name="b"/>, or <see cref="HexDirection.None"/> |
| | | 381 | | public static HexDirection GetHexNeighborDirection(VoxelGrid a, VoxelGrid b) |
| | | 382 | | { |
| | 7 | 383 | | return TryGetNeighborSlot(a, b, GridTopologyKind.HexPrism, out int slot) |
| | 7 | 384 | | ? (HexDirection)slot |
| | 7 | 385 | | : HexDirection.None; |
| | | 386 | | } |
| | | 387 | | |
| | | 388 | | private static bool TryGetNeighborSlot(VoxelGrid a, VoxelGrid b, GridTopologyKind expectedKind, out int slot) |
| | | 389 | | { |
| | 15 | 390 | | if (a._topology?.Kind != expectedKind || b._topology?.Kind != expectedKind) |
| | | 391 | | { |
| | 10 | 392 | | slot = -1; |
| | 10 | 393 | | return false; |
| | | 394 | | } |
| | | 395 | | |
| | 5 | 396 | | return TryGetNeighborSlot(a, b, out slot); |
| | | 397 | | } |
| | | 398 | | |
| | | 399 | | private static bool TryGetNeighborSlot(VoxelGrid a, VoxelGrid b, out int slot) |
| | | 400 | | { |
| | 295 | 401 | | slot = -1; |
| | | 402 | | |
| | 295 | 403 | | if (a._topology == null |
| | 295 | 404 | | || b._topology == null |
| | 295 | 405 | | || a._topology.Kind != b._topology.Kind) |
| | | 406 | | { |
| | 40 | 407 | | return false; |
| | | 408 | | } |
| | | 409 | | |
| | 255 | 410 | | return a._topology.TryGetNeighborSlotFromWorldDelta(b.BoundsCenter - a.BoundsCenter, out slot) |
| | 255 | 411 | | && (uint)slot < (uint)a._topology.NeighborSlotCount; |
| | | 412 | | } |
| | | 413 | | |
| | | 414 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 455 | 415 | | internal VoxelIndex GetNeighborOffset(int slot) => Topology.GetNeighborOffset(slot); |
| | | 416 | | |
| | | 417 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 418 | | internal bool TryGetNeighborSlot(RectangularDirection direction, out int slot) |
| | | 419 | | { |
| | 131 | 420 | | slot = (int)direction; |
| | 131 | 421 | | return _topology?.Kind == GridTopologyKind.RectangularPrism |
| | 131 | 422 | | && (uint)slot < (uint)_topology.NeighborSlotCount; |
| | | 423 | | } |
| | | 424 | | |
| | | 425 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 426 | | internal bool TryGetNeighborSlot(HexDirection direction, out int slot) |
| | | 427 | | { |
| | 14 | 428 | | slot = (int)direction; |
| | 14 | 429 | | return _topology?.Kind == GridTopologyKind.HexPrism |
| | 14 | 430 | | && (uint)slot < (uint)_topology.NeighborSlotCount; |
| | | 431 | | } |
| | | 432 | | |
| | | 433 | | /// <summary> |
| | | 434 | | /// Adds a neighboring grid and updates relationships. |
| | | 435 | | /// </summary> |
| | | 436 | | /// <param name="neighborGrid">The neighboring grid to add.</param> |
| | | 437 | | internal bool TryAddGridNeighbor(VoxelGrid neighborGrid) |
| | | 438 | | { |
| | 264 | 439 | | if (!TryGetNeighborSlot(this, neighborGrid, out int neighborSlot)) |
| | 85 | 440 | | return false; |
| | | 441 | | |
| | | 442 | | // Ensure the neighbor array is allocated and store the new neighbor |
| | 179 | 443 | | Neighbors ??= new SwiftSparseMap<SwiftHashSet<int>>(); |
| | 179 | 444 | | if (!Neighbors.TryGetValue(neighborSlot, out SwiftHashSet<int> neighborSet)) |
| | | 445 | | { |
| | 128 | 446 | | neighborSet = SwiftHashSetPool<int>.Shared.Rent(); |
| | 128 | 447 | | Neighbors.Add(neighborSlot, neighborSet); |
| | | 448 | | } |
| | | 449 | | |
| | 179 | 450 | | if (!neighborSet.Add(neighborGrid.GridIndex)) |
| | 1 | 451 | | return false; |
| | | 452 | | |
| | 178 | 453 | | NeighborCount++; |
| | 178 | 454 | | IncrementVersion(); |
| | | 455 | | |
| | 178 | 456 | | return true; |
| | | 457 | | } |
| | | 458 | | |
| | | 459 | | /// <summary> |
| | | 460 | | /// Removes a neighboring grid relationship. |
| | | 461 | | /// </summary> |
| | | 462 | | /// <param name="neighborGrid">The neighboring grid to remove.</param> |
| | | 463 | | internal bool TryRemoveGridNeighbor(VoxelGrid neighborGrid) |
| | | 464 | | { |
| | 25 | 465 | | if (!TryGetGridNeighborSet(neighborGrid, out int neighborSlot, out SwiftHashSet<int>? neighborSet)) |
| | 2 | 466 | | return false; |
| | | 467 | | |
| | 23 | 468 | | if (!neighborSet!.Remove(neighborGrid.GridIndex)) |
| | 1 | 469 | | return false; |
| | | 470 | | |
| | 22 | 471 | | ReleaseNeighborSetIfEmpty(neighborSlot, neighborSet); |
| | | 472 | | |
| | 22 | 473 | | if (--NeighborCount == 0) |
| | 11 | 474 | | Neighbors = null; |
| | | 475 | | |
| | 22 | 476 | | IncrementVersion(); |
| | | 477 | | |
| | 22 | 478 | | return true; |
| | | 479 | | } |
| | | 480 | | |
| | | 481 | | private bool TryGetGridNeighborSet( |
| | | 482 | | VoxelGrid neighborGrid, |
| | | 483 | | out int neighborSlot, |
| | | 484 | | out SwiftHashSet<int>? neighborSet) |
| | | 485 | | { |
| | 25 | 486 | | neighborSet = null; |
| | 25 | 487 | | neighborSlot = -1; |
| | | 488 | | |
| | 25 | 489 | | if (!IsConjoined) |
| | 1 | 490 | | return false; |
| | | 491 | | |
| | 24 | 492 | | return TryGetNeighborSlot(this, neighborGrid, out neighborSlot) |
| | 24 | 493 | | && Neighbors!.TryGetValue(neighborSlot, out neighborSet); |
| | | 494 | | } |
| | | 495 | | |
| | | 496 | | private void ReleaseNeighborSetIfEmpty(int neighborIndex, SwiftHashSet<int> neighborSet) |
| | | 497 | | { |
| | 22 | 498 | | if (neighborSet.Count > 0) |
| | 8 | 499 | | return; |
| | | 500 | | |
| | 14 | 501 | | GridForgeLogger.Channel.Info($"Releasing unused neighbor collection."); |
| | 14 | 502 | | SwiftHashSetPool<int>.Shared.Release(neighborSet); |
| | 14 | 503 | | Neighbors!.Remove(neighborIndex); |
| | 14 | 504 | | } |
| | | 505 | | |
| | | 506 | | #endregion |
| | | 507 | | |
| | | 508 | | #region Grid Queries |
| | | 509 | | |
| | | 510 | | /// <summary> |
| | | 511 | | /// Determines if a voxel coordinate is at the boundary of the grid. |
| | | 512 | | /// Used to determine if a voxel should update when a neighboring grid is added/removed. |
| | | 513 | | /// </summary> |
| | | 514 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 515 | | public bool IsOnBoundary(VoxelIndex coord) => |
| | 123494 | 516 | | coord.x == 0 || coord.x == Width - 1 |
| | 123494 | 517 | | || coord.y == 0 || coord.y == Height - 1 |
| | 123494 | 518 | | || coord.z == 0 || coord.z == Length - 1; |
| | | 519 | | |
| | | 520 | | /// <summary> |
| | | 521 | | /// Checks whether a given position falls within the grid bounds. |
| | | 522 | | /// </summary> |
| | | 523 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 524 | | public bool IsInBounds(Vector3d target) => |
| | 89 | 525 | | IsActive && Topology.IsInBounds(BoundsMin, BoundsMax, Width, Height, Length, target); |
| | | 526 | | |
| | | 527 | | /// <summary> |
| | | 528 | | /// Retrieves all neighboring grids connected to this grid. |
| | | 529 | | /// </summary> |
| | | 530 | | /// <returns>An enumeration of all neighboring grids.</returns> |
| | | 531 | | public IEnumerable<VoxelGrid> GetAllGridNeighbors() |
| | | 532 | | { |
| | 13 | 533 | | if (!IsConjoined) |
| | 5 | 534 | | yield break; |
| | | 535 | | |
| | 8 | 536 | | var values = Neighbors!.DenseValues; |
| | 8 | 537 | | int count = Neighbors.Count; |
| | | 538 | | |
| | 38 | 539 | | for (int i = 0; i < count; i++) |
| | | 540 | | { |
| | 11 | 541 | | SwiftHashSet<int> neighborSet = values[i]; |
| | 44 | 542 | | foreach (int neighborIndex in neighborSet) |
| | | 543 | | { |
| | 11 | 544 | | if (World!.TryGetGrid(neighborIndex, out VoxelGrid? neighborGrid)) |
| | | 545 | | { |
| | 11 | 546 | | yield return neighborGrid!; |
| | | 547 | | } |
| | | 548 | | } |
| | | 549 | | } |
| | 8 | 550 | | } |
| | | 551 | | |
| | | 552 | | /// <summary> |
| | | 553 | | /// Determines whether the given voxel coordinates are within the valid range of the grid. |
| | | 554 | | /// </summary> |
| | | 555 | | public bool IsValidVoxelIndex(int x, int y, int z) |
| | | 556 | | { |
| | 3009 | 557 | | if (!IsActive) |
| | | 558 | | { |
| | 6 | 559 | | GridForgeLogger.Channel.Warn($"This Grid is not currently active."); |
| | 6 | 560 | | return false; |
| | | 561 | | } |
| | | 562 | | |
| | 3003 | 563 | | bool result = IsVoxelIndexInBounds(x, y, z); |
| | | 564 | | |
| | 3003 | 565 | | if (!result) |
| | 260 | 566 | | GridForgeLogger.Channel.Info($"The coordinate {(x, y, z)} is not valid for this grid."); |
| | | 567 | | |
| | 3003 | 568 | | return result; |
| | | 569 | | } |
| | | 570 | | |
| | | 571 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 572 | | private bool IsVoxelIndexInBounds(int x, int y, int z) => |
| | 3003 | 573 | | (uint)x < (uint)Width |
| | 3003 | 574 | | && (uint)y < (uint)Height |
| | 3003 | 575 | | && (uint)z < (uint)Length; |
| | | 576 | | |
| | | 577 | | /// <summary> |
| | | 578 | | /// Determines if a topology-local voxel index is facing the rectangular-prism boundary in the supplied direction. |
| | | 579 | | /// </summary> |
| | | 580 | | public bool IsFacingBoundary(VoxelIndex voxelIndex, RectangularDirection direction) => |
| | 56 | 581 | | TryGetNeighborSlot(direction, out int slot) |
| | 56 | 582 | | && _topology!.IsFacingBoundary(voxelIndex, slot, Width, Height, Length); |
| | | 583 | | |
| | | 584 | | /// <summary> |
| | | 585 | | /// Determines if a topology-local voxel index is facing the hex-prism boundary in the supplied direction. |
| | | 586 | | /// </summary> |
| | | 587 | | public bool IsFacingBoundary(VoxelIndex voxelIndex, HexDirection direction) => |
| | 5 | 588 | | TryGetNeighborSlot(direction, out int slot) |
| | 5 | 589 | | && _topology!.IsFacingBoundary(voxelIndex, slot, Width, Height, Length); |
| | | 590 | | |
| | | 591 | | /// <summary> |
| | | 592 | | /// Converts a world position to a topology-local voxel index within the grid. |
| | | 593 | | /// Rectangular-prism grids return X/Y/Z coordinates; hex-prism grids return axial Q, layer, and axial R in X/Y/Z fi |
| | | 594 | | /// </summary> |
| | | 595 | | public bool TryGetVoxelIndex(Vector3d position, out VoxelIndex result) |
| | | 596 | | { |
| | 2855 | 597 | | result = default; |
| | | 598 | | |
| | 2855 | 599 | | if (!IsActive) |
| | | 600 | | { |
| | 3 | 601 | | GridForgeLogger.Channel.Warn($"This Grid is not currently allocated."); |
| | 3 | 602 | | return false; |
| | | 603 | | } |
| | | 604 | | |
| | 2852 | 605 | | if (!Topology.TryGetVoxelIndex(BoundsMin, BoundsMax, Width, Height, Length, position, out VoxelIndex voxelIndex) |
| | | 606 | | { |
| | 18 | 607 | | GridForgeLogger.Channel.Warn($"Position does not fall in the bounds of this grid"); |
| | 18 | 608 | | return false; |
| | | 609 | | } |
| | | 610 | | |
| | 2834 | 611 | | result = voxelIndex; |
| | 2834 | 612 | | return true; |
| | | 613 | | } |
| | | 614 | | |
| | | 615 | | /// <summary> |
| | | 616 | | /// Converts a 2D XZ-plane world position on the default world Y layer to a voxel index within the grid. |
| | | 617 | | /// </summary> |
| | | 618 | | /// <param name="position">The 2D position whose X component maps to world X and Y component maps to world Z.</param |
| | | 619 | | /// <param name="result">The resolved voxel index, if found.</param> |
| | | 620 | | /// <returns>True if the position resolved to an allocated voxel index; otherwise false.</returns> |
| | | 621 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 622 | | public bool TryGetVoxelIndex(Vector2d position, out VoxelIndex result) => |
| | 1 | 623 | | TryGetVoxelIndex(position, default, out result); |
| | | 624 | | |
| | | 625 | | /// <summary> |
| | | 626 | | /// Converts a 2D XZ-plane world position on the supplied world Y layer to a voxel index within the grid. |
| | | 627 | | /// </summary> |
| | | 628 | | /// <param name="position">The 2D position whose X component maps to world X and Y component maps to world Z.</param |
| | | 629 | | /// <param name="layerY">The world Y layer to resolve. Defaults to zero when omitted by paired overloads.</param> |
| | | 630 | | /// <param name="result">The resolved voxel index, if found.</param> |
| | | 631 | | /// <returns>True if the position resolved to an allocated voxel index; otherwise false.</returns> |
| | | 632 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 633 | | public bool TryGetVoxelIndex(Vector2d position, Fixed64 layerY, out VoxelIndex result) => |
| | 5 | 634 | | TryGetVoxelIndex(GridPlane2d.ToWorld(position, layerY), out result); |
| | | 635 | | |
| | | 636 | | /// <summary> |
| | | 637 | | /// Checks if a voxel at the given topology-local coordinates is allocated within the grid. |
| | | 638 | | /// </summary> |
| | | 639 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 640 | | public bool IsVoxelAllocated(int x, int y, int z) => |
| | 69 | 641 | | IsValidVoxelIndex(x, y, z) && _storage!.TryGetVoxel(x, y, z, out _); |
| | | 642 | | |
| | | 643 | | /// <summary> |
| | | 644 | | /// Checks whether a physical voxel is configured at the supplied grid-local index. |
| | | 645 | | /// </summary> |
| | | 646 | | /// <param name="voxelIndex">The grid-local voxel index to test.</param> |
| | | 647 | | /// <returns>True when the index resolves to a configured voxel; otherwise false.</returns> |
| | | 648 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 649 | | public bool ContainsVoxel(VoxelIndex voxelIndex) => |
| | 61 | 650 | | IsVoxelAllocated(voxelIndex.x, voxelIndex.y, voxelIndex.z); |
| | | 651 | | |
| | | 652 | | /// <summary> |
| | | 653 | | /// Configures a sparse voxel at runtime. Dense grids, invalid indices, and already-configured |
| | | 654 | | /// sparse voxels return false. |
| | | 655 | | /// </summary> |
| | | 656 | | /// <param name="voxelIndex">The grid-local voxel index to configure.</param> |
| | | 657 | | /// <param name="voxel">The configured voxel when the operation succeeds.</param> |
| | | 658 | | /// <returns>True when a new sparse voxel was configured; otherwise false.</returns> |
| | | 659 | | public bool TryAddVoxel(VoxelIndex voxelIndex, out Voxel? voxel) |
| | | 660 | | { |
| | 41 | 661 | | voxel = null; |
| | | 662 | | bool drainCommittedChanges; |
| | 41 | 663 | | GridWorld? world = World; |
| | 41 | 664 | | if (world == null) |
| | 1 | 665 | | return false; |
| | | 666 | | |
| | 40 | 667 | | world.EnterReadLock(); |
| | | 668 | | try |
| | | 669 | | { |
| | 40 | 670 | | lock (world.ChangeSyncRoot) |
| | | 671 | | { |
| | 40 | 672 | | if (!CanMutateSparseVoxel(voxelIndex) |
| | 40 | 673 | | || !_sparseStorage.TryAddVoxel(this, voxelIndex, out voxel)) |
| | | 674 | | { |
| | 2 | 675 | | return false; |
| | | 676 | | } |
| | | 677 | | |
| | 38 | 678 | | uint gridVersion = IncrementVersion(); |
| | 38 | 679 | | voxel!.CachedGridVersion = gridVersion; |
| | 38 | 680 | | GridEventInfo eventInfo = world.CreateGridEventInfo( |
| | 38 | 681 | | this, |
| | 38 | 682 | | GridEventKind.SparseVoxelAdded, |
| | 38 | 683 | | voxelIndex, |
| | 38 | 684 | | voxel.WorldPosition, |
| | 38 | 685 | | voxel.WorldPosition, |
| | 38 | 686 | | world.AllocateChangeStamp(), |
| | 38 | 687 | | hasVoxelState: true, |
| | 38 | 688 | | isVoxelPresent: true, |
| | 38 | 689 | | obstacleCount: 0); |
| | 38 | 690 | | drainCommittedChanges = world.EnqueueCommittedChange(new GridCommittedChange(eventInfo)); |
| | 38 | 691 | | } |
| | | 692 | | } |
| | | 693 | | finally |
| | | 694 | | { |
| | 40 | 695 | | world.ExitReadLock(); |
| | 40 | 696 | | } |
| | | 697 | | |
| | 38 | 698 | | if (drainCommittedChanges) |
| | 37 | 699 | | world.DrainCommittedChanges(); |
| | 38 | 700 | | return true; |
| | 2 | 701 | | } |
| | | 702 | | |
| | | 703 | | /// <summary> |
| | | 704 | | /// Removes a configured sparse voxel at runtime when it has no unsafe runtime state. |
| | | 705 | | /// Dense grids, missing voxels, occupied voxels, voxels with obstacle tokens, partitioned voxels, |
| | | 706 | | /// and voxels with active event subscribers return false. |
| | | 707 | | /// </summary> |
| | | 708 | | /// <param name="voxelIndex">The grid-local voxel index to remove.</param> |
| | | 709 | | /// <returns>True when the sparse voxel was removed; otherwise false.</returns> |
| | | 710 | | public bool TryRemoveVoxel(VoxelIndex voxelIndex) |
| | | 711 | | { |
| | | 712 | | bool drainCommittedChanges; |
| | 23 | 713 | | GridWorld? world = World; |
| | 23 | 714 | | if (world == null) |
| | 1 | 715 | | return false; |
| | | 716 | | |
| | 22 | 717 | | world.EnterReadLock(); |
| | | 718 | | try |
| | | 719 | | { |
| | 22 | 720 | | lock (world.ChangeSyncRoot) |
| | | 721 | | { |
| | 22 | 722 | | if (!CanMutateSparseVoxel(voxelIndex) |
| | 22 | 723 | | || !TryGetVoxel(voxelIndex, out Voxel? voxel) |
| | 22 | 724 | | || !CanRemoveSparseVoxel(voxel!)) |
| | | 725 | | { |
| | 8 | 726 | | return false; |
| | | 727 | | } |
| | | 728 | | |
| | 14 | 729 | | Vector3d affectedPosition = voxel!.WorldPosition; |
| | 14 | 730 | | _sparseStorage.TryRemoveVoxel(this, voxelIndex, out _); |
| | | 731 | | |
| | 14 | 732 | | IncrementVersion(); |
| | 14 | 733 | | GridEventInfo eventInfo = world.CreateGridEventInfo( |
| | 14 | 734 | | this, |
| | 14 | 735 | | GridEventKind.SparseVoxelRemoved, |
| | 14 | 736 | | voxelIndex, |
| | 14 | 737 | | affectedPosition, |
| | 14 | 738 | | affectedPosition, |
| | 14 | 739 | | world.AllocateChangeStamp(), |
| | 14 | 740 | | hasVoxelState: true, |
| | 14 | 741 | | isVoxelPresent: false, |
| | 14 | 742 | | obstacleCount: 0); |
| | 14 | 743 | | drainCommittedChanges = world.EnqueueCommittedChange(new GridCommittedChange(eventInfo)); |
| | 14 | 744 | | } |
| | | 745 | | } |
| | | 746 | | finally |
| | | 747 | | { |
| | 22 | 748 | | world.ExitReadLock(); |
| | 22 | 749 | | } |
| | | 750 | | |
| | 14 | 751 | | if (drainCommittedChanges) |
| | 13 | 752 | | world.DrainCommittedChanges(); |
| | 14 | 753 | | return true; |
| | 8 | 754 | | } |
| | | 755 | | |
| | | 756 | | private bool CanMutateSparseVoxel(VoxelIndex voxelIndex) => |
| | 62 | 757 | | IsActive |
| | 62 | 758 | | && StorageKind == GridStorageKind.Sparse |
| | 62 | 759 | | && IsValidVoxelIndex(voxelIndex.x, voxelIndex.y, voxelIndex.z); |
| | | 760 | | |
| | | 761 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 762 | | private static bool CanRemoveSparseVoxel(Voxel voxel) => |
| | 20 | 763 | | voxel.IsAllocated |
| | 20 | 764 | | && !voxel.IsOccupied |
| | 20 | 765 | | && voxel.ObstacleCount == 0 |
| | 20 | 766 | | && !voxel.IsPartioned |
| | 20 | 767 | | && !voxel.HasEventSubscribers; |
| | | 768 | | |
| | | 769 | | /// <summary> |
| | | 770 | | /// Retrieves the <see cref="Voxel"/> at the specified topology-local coordinates, if allocated. |
| | | 771 | | /// </summary> |
| | | 772 | | public bool TryGetVoxel(int x, int y, int z, out Voxel? result) |
| | | 773 | | { |
| | 2864 | 774 | | result = null; |
| | | 775 | | |
| | 2864 | 776 | | if (!IsValidVoxelIndex(x, y, z)) |
| | 258 | 777 | | return false; |
| | | 778 | | |
| | 2606 | 779 | | return _storage!.TryGetVoxel(x, y, z, out result); |
| | | 780 | | } |
| | | 781 | | |
| | | 782 | | /// <summary> |
| | | 783 | | /// Enumerates physical voxels configured in this grid in deterministic storage order. |
| | | 784 | | /// </summary> |
| | | 785 | | public IEnumerable<Voxel> EnumerateVoxels() => |
| | 11 | 786 | | _storage?.EnumerateVoxels() ?? Array.Empty<Voxel>(); |
| | | 787 | | |
| | | 788 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 789 | | internal void VisitVoxels<TVisitor>(ref TVisitor visitor) |
| | | 790 | | where TVisitor : struct, IVoxelStorageVisitor => |
| | 91 | 791 | | _storage?.VisitVoxels(ref visitor); |
| | | 792 | | |
| | | 793 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 794 | | internal void AddVoxelsInIndexRange( |
| | | 795 | | VoxelIndex min, |
| | | 796 | | VoxelIndex max, |
| | | 797 | | SwiftList<Voxel> results, |
| | | 798 | | SwiftHashSet<Voxel> redundancy) => |
| | 549 | 799 | | _storage?.AddVoxelsInIndexRange(min, max, results, redundancy); |
| | | 800 | | |
| | | 801 | | /// <summary> |
| | | 802 | | /// Retrieves a grid voxel from a topology-local coordinate. |
| | | 803 | | /// </summary> |
| | | 804 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 805 | | public bool TryGetVoxel(VoxelIndex voxelIndex, out Voxel? result) => |
| | 1867 | 806 | | TryGetVoxel(voxelIndex.x, voxelIndex.y, voxelIndex.z, out result); |
| | | 807 | | |
| | | 808 | | /// <summary> |
| | | 809 | | /// Retrieve <see cref="Voxel"/> from world <see cref="Vector3d"/> points |
| | | 810 | | /// </summary> |
| | | 811 | | /// <returns><see cref="Voxel"/> at the given position or null if the position is not valid.</returns> |
| | | 812 | | public bool TryGetVoxel(Vector3d position, out Voxel? result) |
| | | 813 | | { |
| | 979 | 814 | | result = null; |
| | 979 | 815 | | return TryGetVoxelIndex(position, out VoxelIndex coordinate) |
| | 979 | 816 | | && TryGetVoxel(coordinate.x, coordinate.y, coordinate.z, out result); |
| | | 817 | | } |
| | | 818 | | |
| | | 819 | | /// <summary> |
| | | 820 | | /// Retrieves the physical voxel whose center is nearest to the supplied world position. |
| | | 821 | | /// Sparse grids only consider configured physical voxels. |
| | | 822 | | /// </summary> |
| | | 823 | | /// <param name="position">The world position to resolve.</param> |
| | | 824 | | /// <param name="result">The closest physical voxel, if found.</param> |
| | | 825 | | /// <returns>True if a physical voxel was resolved; otherwise false.</returns> |
| | | 826 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 827 | | public bool TryGetClosestVoxel(Vector3d position, out Voxel? result) => |
| | 14 | 828 | | TryGetClosestVoxel(position, out result, out _); |
| | | 829 | | |
| | | 830 | | internal bool TryGetClosestVoxel( |
| | | 831 | | Vector3d position, |
| | | 832 | | out Voxel? result, |
| | | 833 | | out Fixed64 distanceSquared) |
| | | 834 | | { |
| | 28 | 835 | | result = null; |
| | 28 | 836 | | distanceSquared = Fixed64.MaxValue; |
| | | 837 | | |
| | 28 | 838 | | if (!IsActive) |
| | | 839 | | { |
| | 3 | 840 | | GridForgeLogger.Channel.Warn($"This Grid is not currently allocated."); |
| | 3 | 841 | | return false; |
| | | 842 | | } |
| | | 843 | | |
| | 25 | 844 | | if (ConfiguredVoxelCount == 0) |
| | 1 | 845 | | return false; |
| | | 846 | | |
| | 24 | 847 | | VoxelIndex closestIndex = Topology.GetClosestVoxelIndex(BoundsMin, Width, Height, Length, position); |
| | 24 | 848 | | return _storage!.TryGetClosestVoxel(this, closestIndex, position, out result, out distanceSquared); |
| | | 849 | | } |
| | | 850 | | |
| | | 851 | | /// <summary> |
| | | 852 | | /// Retrieves a <see cref="Voxel"/> from a 2D XZ-plane world position on the default world Y layer. |
| | | 853 | | /// </summary> |
| | | 854 | | /// <param name="position">The 2D position whose X component maps to world X and Y component maps to world Z.</param |
| | | 855 | | /// <param name="result">The resolved voxel, if found.</param> |
| | | 856 | | /// <returns>True if the voxel was resolved; otherwise false.</returns> |
| | | 857 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 858 | | public bool TryGetVoxel(Vector2d position, out Voxel? result) => |
| | 2 | 859 | | TryGetVoxel(position, default, out result); |
| | | 860 | | |
| | | 861 | | /// <summary> |
| | | 862 | | /// Retrieves a <see cref="Voxel"/> from a 2D XZ-plane world position on the supplied world Y layer. |
| | | 863 | | /// </summary> |
| | | 864 | | /// <param name="position">The 2D position whose X component maps to world X and Y component maps to world Z.</param |
| | | 865 | | /// <param name="layerY">The world Y layer to resolve. Defaults to zero when omitted by paired overloads.</param> |
| | | 866 | | /// <param name="result">The resolved voxel, if found.</param> |
| | | 867 | | /// <returns>True if the voxel was resolved; otherwise false.</returns> |
| | | 868 | | |
| | | 869 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 870 | | public bool TryGetVoxel(Vector2d position, Fixed64 layerY, out Voxel? result) => |
| | 6 | 871 | | TryGetVoxel(GridPlane2d.ToWorld(position, layerY), out result); |
| | | 872 | | |
| | | 873 | | /// <summary> |
| | | 874 | | /// Retrieves the physical voxel whose center is nearest to a 2D XZ-plane world position on the default world Y laye |
| | | 875 | | /// Sparse grids only consider configured physical voxels. |
| | | 876 | | /// </summary> |
| | | 877 | | /// <param name="position">The 2D position whose X component maps to world X and Y component maps to world Z.</param |
| | | 878 | | /// <param name="result">The closest physical voxel, if found.</param> |
| | | 879 | | /// <returns>True if a physical voxel was resolved; otherwise false.</returns> |
| | | 880 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 881 | | public bool TryGetClosestVoxel(Vector2d position, out Voxel? result) => |
| | 1 | 882 | | TryGetClosestVoxel(position, default, out result); |
| | | 883 | | |
| | | 884 | | /// <summary> |
| | | 885 | | /// Retrieves the physical voxel whose center is nearest to a 2D XZ-plane world position on the supplied world Y lay |
| | | 886 | | /// Sparse grids only consider configured physical voxels. |
| | | 887 | | /// </summary> |
| | | 888 | | /// <param name="position">The 2D position whose X component maps to world X and Y component maps to world Z.</param |
| | | 889 | | /// <param name="layerY">The world Y layer to resolve. Defaults to zero when omitted by paired overloads.</param> |
| | | 890 | | /// <param name="result">The closest physical voxel, if found.</param> |
| | | 891 | | /// <returns>True if a physical voxel was resolved; otherwise false.</returns> |
| | | 892 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 893 | | public bool TryGetClosestVoxel(Vector2d position, Fixed64 layerY, out Voxel? result) => |
| | 2 | 894 | | TryGetClosestVoxel(GridPlane2d.ToWorld(position, layerY), out result); |
| | | 895 | | |
| | | 896 | | /// <summary> |
| | | 897 | | /// Computes the scan cell key for a given world position. |
| | | 898 | | /// </summary> |
| | | 899 | | public int GetScanCellKey(Vector3d position) |
| | | 900 | | { |
| | 33 | 901 | | if (!TryGetVoxelIndex(position, out VoxelIndex voxelIndex)) |
| | 1 | 902 | | return -1; |
| | | 903 | | |
| | 32 | 904 | | return GetScanCellKey(voxelIndex); |
| | | 905 | | } |
| | | 906 | | |
| | | 907 | | /// <summary> |
| | | 908 | | /// Calculates the spatial cell index for a given position. |
| | | 909 | | /// </summary> |
| | | 910 | | public int GetScanCellKey(VoxelIndex voxelIndex) |
| | | 911 | | { |
| | 123649 | 912 | | (int x, int y, int z) = ( |
| | 123649 | 913 | | voxelIndex.x / ScanCellSize, |
| | 123649 | 914 | | voxelIndex.y / ScanCellSize, |
| | 123649 | 915 | | voxelIndex.z / ScanCellSize |
| | 123649 | 916 | | ); |
| | | 917 | | |
| | 123649 | 918 | | int scanCellKey = GetScanCellKey(x, y, z); |
| | 123649 | 919 | | if (scanCellKey == -1) |
| | | 920 | | { |
| | 5 | 921 | | GridForgeLogger.Channel.Warn($"Position {voxelIndex} is not in the bounds for this grids Scan Cell overlay." |
| | 5 | 922 | | return -1; |
| | | 923 | | } |
| | | 924 | | |
| | 123644 | 925 | | return scanCellKey; |
| | | 926 | | } |
| | | 927 | | |
| | | 928 | | /// <summary> |
| | | 929 | | /// Calculates a unique scan cell key from grid-local scan cell coordinates. |
| | | 930 | | /// </summary> |
| | | 931 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 932 | | internal int GetScanCellKey(int x, int y, int z) |
| | | 933 | | { |
| | 128305 | 934 | | if ((uint)x >= (uint)_scanWidth |
| | 128305 | 935 | | || (uint)y >= (uint)_scanHeight |
| | 128305 | 936 | | || (uint)z >= (uint)_scanLength) |
| | | 937 | | { |
| | 9 | 938 | | return -1; |
| | | 939 | | } |
| | | 940 | | |
| | 128296 | 941 | | return x + y * _scanWidth + z * _scanLayerSize; |
| | | 942 | | } |
| | | 943 | | |
| | | 944 | | /// <summary> |
| | | 945 | | /// Retrieves a scan cell from the grid using its key. |
| | | 946 | | /// </summary> |
| | | 947 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 948 | | public bool TryGetScanCell(int key, out ScanCell? outScanCell) |
| | | 949 | | { |
| | 964 | 950 | | outScanCell = null; |
| | 964 | 951 | | return _storage?.TryGetScanCell(key, out outScanCell) == true; |
| | | 952 | | } |
| | | 953 | | |
| | | 954 | | /// <summary> |
| | | 955 | | /// Retrieves the scan cell corresponding to a given world position. |
| | | 956 | | /// </summary> |
| | | 957 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 958 | | public bool TryGetScanCell(Vector3d position, out ScanCell? outScanCell) |
| | | 959 | | { |
| | 28 | 960 | | int key = GetScanCellKey(position); |
| | 28 | 961 | | return TryGetScanCell(key, out outScanCell); |
| | | 962 | | } |
| | | 963 | | |
| | | 964 | | /// <summary> |
| | | 965 | | /// Retrieves the scan cell associated with the given voxel index. |
| | | 966 | | /// </summary> |
| | | 967 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 968 | | public bool TryGetScanCell(VoxelIndex voxelIndex, out ScanCell? outScanCell) |
| | | 969 | | { |
| | 7 | 970 | | outScanCell = null; |
| | 7 | 971 | | return TryGetVoxel(voxelIndex, out Voxel? voxel) |
| | 7 | 972 | | && TryGetScanCell(voxel!.ScanCellKey, out outScanCell); |
| | | 973 | | } |
| | | 974 | | |
| | | 975 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 976 | | internal void AddScanCellsInRange( |
| | | 977 | | int xMin, |
| | | 978 | | int yMin, |
| | | 979 | | int zMin, |
| | | 980 | | int xMax, |
| | | 981 | | int yMax, |
| | | 982 | | int zMax, |
| | | 983 | | SwiftList<ScanCell> results, |
| | | 984 | | SwiftHashSet<ScanCell> redundancy) => |
| | 600 | 985 | | _storage?.AddScanCellsInRange( |
| | 600 | 986 | | this, |
| | 600 | 987 | | xMin, |
| | 600 | 988 | | yMin, |
| | 600 | 989 | | zMin, |
| | 600 | 990 | | xMax, |
| | 600 | 991 | | yMax, |
| | 600 | 992 | | zMax, |
| | 600 | 993 | | results, |
| | 600 | 994 | | redundancy); |
| | | 995 | | |
| | | 996 | | /// <summary> |
| | | 997 | | /// Enumerates all currently active scan cells within the grid. |
| | | 998 | | /// </summary> |
| | | 999 | | public IEnumerable<ScanCell> GetActiveScanCells() |
| | | 1000 | | { |
| | 5 | 1001 | | if (!IsActive || !IsOccupied) |
| | 4 | 1002 | | yield break; |
| | | 1003 | | |
| | 6 | 1004 | | foreach (int activeCellKey in ActiveScanCells!) |
| | | 1005 | | { |
| | 2 | 1006 | | if (_storage!.TryGetScanCell(activeCellKey, out ScanCell? scanCell)) |
| | 2 | 1007 | | yield return scanCell!; |
| | | 1008 | | } |
| | 1 | 1009 | | } |
| | | 1010 | | |
| | | 1011 | | /// <summary> |
| | | 1012 | | /// Helper function to ceil snap a <see cref="Vector3d"/> through this grid's topology, ensuring it stays within gri |
| | | 1013 | | /// </summary> |
| | | 1014 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1015 | | public Vector3d CeilToGrid(Vector3d position) => |
| | 3 | 1016 | | Topology.CeilToGrid(BoundsMin, BoundsMax, Width, Height, Length, position); |
| | | 1017 | | |
| | | 1018 | | /// <summary> |
| | | 1019 | | /// Helper function to floor snap a <see cref="Vector3d"/> through this grid's topology, ensuring it stays within gr |
| | | 1020 | | /// </summary> |
| | | 1021 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1022 | | public Vector3d FloorToGrid(Vector3d position) => |
| | 382 | 1023 | | Topology.FloorToGrid(BoundsMin, BoundsMax, Width, Height, Length, position); |
| | | 1024 | | |
| | | 1025 | | /// <summary> |
| | | 1026 | | /// Snaps a given position to the topology-local scan cell in the grid. |
| | | 1027 | | /// </summary> |
| | | 1028 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1029 | | public (int x, int y, int z) SnapToScanCell(Vector3d position) => |
| | 1185 | 1030 | | Topology.SnapToScanCell(BoundsMin, position, ScanCellSize); |
| | | 1031 | | |
| | | 1032 | | /// <summary> |
| | | 1033 | | /// Normalizes world-space bounds to this grid's topology-aligned coverage bounds. |
| | | 1034 | | /// </summary> |
| | | 1035 | | /// <param name="min">The first world-space bounds corner.</param> |
| | | 1036 | | /// <param name="max">The second world-space bounds corner.</param> |
| | | 1037 | | /// <param name="padding">Optional non-negative padding applied before normalization.</param> |
| | | 1038 | | /// <returns>Topology-aligned minimum and maximum bounds suitable for deterministic coverage scans.</returns> |
| | | 1039 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1040 | | public (Vector3d min, Vector3d max) NormalizeBounds(Vector3d min, Vector3d max, Fixed64? padding = null) => |
| | 1446 | 1041 | | Topology.NormalizeBounds(min, max, padding); |
| | | 1042 | | |
| | | 1043 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1044 | | internal Vector3d GetWorldPosition(VoxelIndex index) => |
| | 124506 | 1045 | | Topology.GetWorldPosition(BoundsMin, index); |
| | | 1046 | | |
| | | 1047 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1048 | | internal Vector3d GetWorldOffset((int x, int y, int z) offset) => |
| | 92 | 1049 | | Topology.GetWorldOffset(offset); |
| | | 1050 | | |
| | | 1051 | | #endregion |
| | | 1052 | | } |