| | | 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.Runtime.CompilerServices; |
| | | 9 | | using FixedMathSharp; |
| | | 10 | | using GridForge.Grids; |
| | | 11 | | using GridForge.Spatial; |
| | | 12 | | using SwiftCollections; |
| | | 13 | | |
| | | 14 | | namespace GridForge.Utility; |
| | | 15 | | |
| | | 16 | | |
| | | 17 | | |
| | | 18 | | |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Provides deterministic helpers for duplicate-safe voxel traversal. |
| | | 22 | | /// </summary> |
| | | 23 | | public static class GridTraversal |
| | | 24 | | { |
| | | 25 | | /// <summary> |
| | | 26 | | /// Gets a voxel partition once per exact world-scoped voxel identity. |
| | | 27 | | /// </summary> |
| | | 28 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 29 | | public static bool TryGetUniquePartition<TPartition>( |
| | | 30 | | Voxel voxel, |
| | | 31 | | SwiftHashSet<WorldVoxelIndex> visited, |
| | | 32 | | out TPartition? partition) |
| | | 33 | | where TPartition : class, IVoxelPartition |
| | | 34 | | { |
| | 2082 | 35 | | partition = null; |
| | 2082 | 36 | | return visited.Add(voxel.WorldIndex) |
| | 2082 | 37 | | && voxel.TryGetPartition(out partition); |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Tests whether a 3D world position lies inside bounds expanded by half of a cell edge. |
| | | 42 | | /// </summary> |
| | | 43 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 44 | | public static bool IsWorldPositionInPaddedBounds( |
| | | 45 | | Vector3d min, |
| | | 46 | | Vector3d max, |
| | | 47 | | Fixed64 cellEdge, |
| | | 48 | | Vector3d worldPosition) |
| | | 49 | | { |
| | 2 | 50 | | Fixed64 padding = cellEdge * Fixed64.Half; |
| | 2 | 51 | | return worldPosition.X >= min.X - padding |
| | 2 | 52 | | && worldPosition.X <= max.X + padding |
| | 2 | 53 | | && worldPosition.Y >= min.Y - padding |
| | 2 | 54 | | && worldPosition.Y <= max.Y + padding |
| | 2 | 55 | | && worldPosition.Z >= min.Z - padding |
| | 2 | 56 | | && worldPosition.Z <= max.Z + padding; |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Tests whether a world position's X/Z projection lies inside bounds expanded by half of a cell edge. |
| | | 61 | | /// </summary> |
| | | 62 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 63 | | public static bool IsPlanarPositionInPaddedBounds( |
| | | 64 | | Vector2d min, |
| | | 65 | | Vector2d max, |
| | | 66 | | Fixed64 cellEdge, |
| | | 67 | | Vector3d worldPosition) |
| | | 68 | | { |
| | 2 | 69 | | Fixed64 padding = cellEdge * Fixed64.Half; |
| | 2 | 70 | | return worldPosition.X >= min.X - padding |
| | 2 | 71 | | && worldPosition.X <= max.X + padding |
| | 2 | 72 | | && worldPosition.Z >= min.Y - padding |
| | 2 | 73 | | && worldPosition.Z <= max.Y + padding; |
| | | 74 | | } |
| | | 75 | | } |