| | | 1 | | using System; |
| | | 2 | | using System.Linq; |
| | | 3 | | using System.Runtime.CompilerServices; |
| | | 4 | | |
| | | 5 | | namespace GridForge.Spatial; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Provides global spatial awareness utilities to find neighboring grids or voxels |
| | | 9 | | /// </summary> |
| | | 10 | | public static class SpatialAwareness |
| | | 11 | | { |
| | | 12 | | #region Fields & Properties |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Predefined offsets for a 3x3x3 neighbor structure, excluding the center position. |
| | | 16 | | /// </summary> |
| | 2 | 17 | | public static readonly (int x, int y, int z)[] DirectionOffsets = new (int x, int y, int z)[26] |
| | 2 | 18 | | { |
| | 2 | 19 | | (-1, 0, 0), |
| | 2 | 20 | | (0, 0, -1), |
| | 2 | 21 | | (1, 0, 0), |
| | 2 | 22 | | (0, 0, 1), |
| | 2 | 23 | | (0, -1, 0), |
| | 2 | 24 | | (0, 1, 0), |
| | 2 | 25 | | (-1, 0, -1), |
| | 2 | 26 | | (-1, 0, 1), |
| | 2 | 27 | | (1, 0, -1), |
| | 2 | 28 | | (1, 0, 1), |
| | 2 | 29 | | (-1, -1, 0), |
| | 2 | 30 | | (0, -1, -1), |
| | 2 | 31 | | (1, -1, 0), |
| | 2 | 32 | | (0, -1, 1), |
| | 2 | 33 | | (-1, 1, 0), |
| | 2 | 34 | | (0, 1, -1), |
| | 2 | 35 | | (1, 1, 0), |
| | 2 | 36 | | (0, 1, 1), |
| | 2 | 37 | | (-1, -1, -1), |
| | 2 | 38 | | (-1, -1, 1), |
| | 2 | 39 | | (1, -1, -1), |
| | 2 | 40 | | (1, -1, 1), |
| | 2 | 41 | | (-1, 1, -1), |
| | 2 | 42 | | (-1, 1, 1), |
| | 2 | 43 | | (1, 1, -1), |
| | 2 | 44 | | (1, 1, 1) |
| | 2 | 45 | | }; |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// All 26 neighbor directions excluding None. |
| | | 49 | | /// </summary> |
| | 2 | 50 | | public static readonly SpatialDirection[] AllDirections = |
| | 2 | 51 | | Enum.GetValues(typeof(SpatialDirection)) |
| | 2 | 52 | | .Cast<SpatialDirection>() |
| | 54 | 53 | | .Where(d => d != SpatialDirection.None) |
| | 2 | 54 | | .ToArray(); |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// All 6 perpendicular neighbor directions excluding None. |
| | | 58 | | /// </summary> |
| | 2 | 59 | | public static readonly SpatialDirection[] PerpendicularDirections |
| | 2 | 60 | | = AllDirections |
| | 2 | 61 | | .Where(IsPerpendicularNeighbor) |
| | 2 | 62 | | .ToArray(); |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// All 20 perpendicular neighbor directions excluding None. |
| | | 66 | | /// </summary> |
| | 2 | 67 | | public static readonly SpatialDirection[] DiagonalDirections |
| | 2 | 68 | | = AllDirections |
| | 2 | 69 | | .Where(IsDiagonalNeighbor) |
| | 2 | 70 | | .ToArray(); |
| | | 71 | | |
| | | 72 | | #endregion |
| | | 73 | | |
| | | 74 | | #region Methods |
| | | 75 | | |
| | | 76 | | /// <summary>True for pure axis-aligned directions.</summary> |
| | | 77 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 52 | 78 | | public static bool IsPerpendicularNeighbor(SpatialDirection dir) => (int)dir < 6; |
| | | 79 | | |
| | | 80 | | /// <summary>True for any diagonal step (multiple axes).</summary> |
| | | 81 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 52 | 82 | | public static bool IsDiagonalNeighbor(SpatialDirection dir) => (int)dir >= 6; |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Gets the boundary range for a given offset and size, |
| | | 86 | | /// returning (0, 0) for negative offsets, (size-1, size-1) for positive offsets, |
| | | 87 | | /// and (0, size-1) for zero offsets. |
| | | 88 | | /// </summary> |
| | | 89 | | /// <param name="offset">The offset value.</param> |
| | | 90 | | /// <param name="size">The size of the dimension.</param> |
| | | 91 | | /// <returns>A tuple representing the start and end of the boundary range.</returns> |
| | | 92 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 93 | | public static (int start, int end) GetBoundaryRange(int offset, int size) |
| | | 94 | | { |
| | 111 | 95 | | return offset switch |
| | 111 | 96 | | { |
| | 21 | 97 | | < 0 => (0, 0), |
| | 31 | 98 | | > 0 => (size - 1, size - 1), |
| | 59 | 99 | | _ => (0, size - 1) |
| | 111 | 100 | | }; |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// Determines if a coordinate is facing the boundary of an axis given an offset and size. |
| | | 105 | | /// </summary> |
| | | 106 | | /// <param name="coordinate">The coordinate value.</param> |
| | | 107 | | /// <param name="offset">The offset value.</param> |
| | | 108 | | /// <param name="size">The size of the dimension.</param> |
| | | 109 | | /// <returns>True if the coordinate is facing the boundary, otherwise false.</returns> |
| | | 110 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 111 | | public static bool IsAxisFacingBoundary(int coordinate, int offset, int size) |
| | | 112 | | { |
| | 120 | 113 | | return offset switch |
| | 120 | 114 | | { |
| | 41 | 115 | | < 0 => coordinate == 0, |
| | 41 | 116 | | > 0 => coordinate == size - 1, |
| | 38 | 117 | | _ => true |
| | 120 | 118 | | }; |
| | | 119 | | } |
| | | 120 | | |
| | | 121 | | #endregion |
| | | 122 | | } |