| | | 1 | | //======================================================================= |
| | | 2 | | // RectangularDirectionUtility.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 | | |
| | | 11 | | namespace GridForge.Spatial; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Provides rectangular-prism neighbor direction utilities. |
| | | 15 | | /// </summary> |
| | | 16 | | public static class RectangularDirectionUtility |
| | | 17 | | { |
| | | 18 | | #region Fields & Properties |
| | | 19 | | |
| | | 20 | | private const int OffsetLookupSize = 27; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Predefined offsets for a rectangular-prism 3x3x3 neighbor structure, excluding the center position. |
| | | 24 | | /// </summary> |
| | 1258 | 25 | | public static ReadOnlySpan<(int x, int y, int z)> Offsets => OffsetValues; |
| | | 26 | | |
| | 2 | 27 | | private static readonly (int x, int y, int z)[] OffsetValues = new (int x, int y, int z)[26] |
| | 2 | 28 | | { |
| | 2 | 29 | | (-1, 0, 0), |
| | 2 | 30 | | (0, 0, -1), |
| | 2 | 31 | | (1, 0, 0), |
| | 2 | 32 | | (0, 0, 1), |
| | 2 | 33 | | (0, -1, 0), |
| | 2 | 34 | | (0, 1, 0), |
| | 2 | 35 | | (-1, 0, -1), |
| | 2 | 36 | | (-1, 0, 1), |
| | 2 | 37 | | (1, 0, -1), |
| | 2 | 38 | | (1, 0, 1), |
| | 2 | 39 | | (-1, -1, 0), |
| | 2 | 40 | | (0, -1, -1), |
| | 2 | 41 | | (1, -1, 0), |
| | 2 | 42 | | (0, -1, 1), |
| | 2 | 43 | | (-1, 1, 0), |
| | 2 | 44 | | (0, 1, -1), |
| | 2 | 45 | | (1, 1, 0), |
| | 2 | 46 | | (0, 1, 1), |
| | 2 | 47 | | (-1, -1, -1), |
| | 2 | 48 | | (-1, -1, 1), |
| | 2 | 49 | | (1, -1, -1), |
| | 2 | 50 | | (1, -1, 1), |
| | 2 | 51 | | (-1, 1, -1), |
| | 2 | 52 | | (-1, 1, 1), |
| | 2 | 53 | | (1, 1, -1), |
| | 2 | 54 | | (1, 1, 1) |
| | 2 | 55 | | }; |
| | | 56 | | |
| | 2 | 57 | | private static readonly RectangularDirection[] DirectionLookup = CreateDirectionLookup(); |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// All 26 neighbor directions excluding None. |
| | | 61 | | /// </summary> |
| | 58 | 62 | | public static ReadOnlySpan<RectangularDirection> All => AllValues; |
| | | 63 | | |
| | 2 | 64 | | private static readonly RectangularDirection[] AllValues = |
| | 2 | 65 | | { |
| | 2 | 66 | | RectangularDirection.West, |
| | 2 | 67 | | RectangularDirection.South, |
| | 2 | 68 | | RectangularDirection.East, |
| | 2 | 69 | | RectangularDirection.North, |
| | 2 | 70 | | RectangularDirection.Below, |
| | 2 | 71 | | RectangularDirection.Above, |
| | 2 | 72 | | RectangularDirection.SouthWest, |
| | 2 | 73 | | RectangularDirection.NorthWest, |
| | 2 | 74 | | RectangularDirection.SouthEast, |
| | 2 | 75 | | RectangularDirection.NorthEast, |
| | 2 | 76 | | RectangularDirection.BelowWest, |
| | 2 | 77 | | RectangularDirection.BelowSouth, |
| | 2 | 78 | | RectangularDirection.BelowEast, |
| | 2 | 79 | | RectangularDirection.BelowNorth, |
| | 2 | 80 | | RectangularDirection.AboveWest, |
| | 2 | 81 | | RectangularDirection.AboveSouth, |
| | 2 | 82 | | RectangularDirection.AboveEast, |
| | 2 | 83 | | RectangularDirection.AboveNorth, |
| | 2 | 84 | | RectangularDirection.BelowSouthWest, |
| | 2 | 85 | | RectangularDirection.BelowNorthWest, |
| | 2 | 86 | | RectangularDirection.BelowSouthEast, |
| | 2 | 87 | | RectangularDirection.BelowNorthEast, |
| | 2 | 88 | | RectangularDirection.AboveSouthWest, |
| | 2 | 89 | | RectangularDirection.AboveNorthWest, |
| | 2 | 90 | | RectangularDirection.AboveSouthEast, |
| | 2 | 91 | | RectangularDirection.AboveNorthEast |
| | 2 | 92 | | }; |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// The 6 face-adjacent rectangular-prism directions. |
| | | 96 | | /// </summary> |
| | 2 | 97 | | public static ReadOnlySpan<RectangularDirection> Primary => PrimaryValues; |
| | | 98 | | |
| | 2 | 99 | | private static readonly RectangularDirection[] PrimaryValues = |
| | 2 | 100 | | { |
| | 2 | 101 | | RectangularDirection.West, |
| | 2 | 102 | | RectangularDirection.South, |
| | 2 | 103 | | RectangularDirection.East, |
| | 2 | 104 | | RectangularDirection.North, |
| | 2 | 105 | | RectangularDirection.Below, |
| | 2 | 106 | | RectangularDirection.Above |
| | 2 | 107 | | }; |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// All 6 perpendicular neighbor directions excluding None. |
| | | 111 | | /// </summary> |
| | 2 | 112 | | public static ReadOnlySpan<RectangularDirection> Perpendicular => PerpendicularValues; |
| | | 113 | | |
| | 2 | 114 | | private static readonly RectangularDirection[] PerpendicularValues = |
| | 2 | 115 | | { |
| | 2 | 116 | | RectangularDirection.West, |
| | 2 | 117 | | RectangularDirection.South, |
| | 2 | 118 | | RectangularDirection.East, |
| | 2 | 119 | | RectangularDirection.North, |
| | 2 | 120 | | RectangularDirection.Below, |
| | 2 | 121 | | RectangularDirection.Above |
| | 2 | 122 | | }; |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// All 8 same-layer XZ-plane directions excluding None. |
| | | 126 | | /// </summary> |
| | 2 | 127 | | public static ReadOnlySpan<RectangularDirection> Planar => PlanarValues; |
| | | 128 | | |
| | 2 | 129 | | private static readonly RectangularDirection[] PlanarValues = |
| | 2 | 130 | | { |
| | 2 | 131 | | RectangularDirection.West, |
| | 2 | 132 | | RectangularDirection.South, |
| | 2 | 133 | | RectangularDirection.East, |
| | 2 | 134 | | RectangularDirection.North, |
| | 2 | 135 | | RectangularDirection.SouthWest, |
| | 2 | 136 | | RectangularDirection.NorthWest, |
| | 2 | 137 | | RectangularDirection.SouthEast, |
| | 2 | 138 | | RectangularDirection.NorthEast |
| | 2 | 139 | | }; |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// All 2 vertical directions. |
| | | 143 | | /// </summary> |
| | 1 | 144 | | public static ReadOnlySpan<RectangularDirection> Vertical => VerticalValues; |
| | | 145 | | |
| | 2 | 146 | | private static readonly RectangularDirection[] VerticalValues = |
| | 2 | 147 | | { |
| | 2 | 148 | | RectangularDirection.Below, |
| | 2 | 149 | | RectangularDirection.Above |
| | 2 | 150 | | }; |
| | | 151 | | |
| | | 152 | | /// <summary> |
| | | 153 | | /// All 9 neighbor directions on the layer below. |
| | | 154 | | /// </summary> |
| | 2 | 155 | | public static ReadOnlySpan<RectangularDirection> BelowLayer => BelowLayerValues; |
| | | 156 | | |
| | 2 | 157 | | private static readonly RectangularDirection[] BelowLayerValues = |
| | 2 | 158 | | { |
| | 2 | 159 | | RectangularDirection.Below, |
| | 2 | 160 | | RectangularDirection.BelowWest, |
| | 2 | 161 | | RectangularDirection.BelowSouth, |
| | 2 | 162 | | RectangularDirection.BelowEast, |
| | 2 | 163 | | RectangularDirection.BelowNorth, |
| | 2 | 164 | | RectangularDirection.BelowSouthWest, |
| | 2 | 165 | | RectangularDirection.BelowNorthWest, |
| | 2 | 166 | | RectangularDirection.BelowSouthEast, |
| | 2 | 167 | | RectangularDirection.BelowNorthEast |
| | 2 | 168 | | }; |
| | | 169 | | |
| | | 170 | | /// <summary> |
| | | 171 | | /// All 9 neighbor directions on the layer above. |
| | | 172 | | /// </summary> |
| | 2 | 173 | | public static ReadOnlySpan<RectangularDirection> AboveLayer => AboveLayerValues; |
| | | 174 | | |
| | 2 | 175 | | private static readonly RectangularDirection[] AboveLayerValues = |
| | 2 | 176 | | { |
| | 2 | 177 | | RectangularDirection.Above, |
| | 2 | 178 | | RectangularDirection.AboveWest, |
| | 2 | 179 | | RectangularDirection.AboveSouth, |
| | 2 | 180 | | RectangularDirection.AboveEast, |
| | 2 | 181 | | RectangularDirection.AboveNorth, |
| | 2 | 182 | | RectangularDirection.AboveSouthWest, |
| | 2 | 183 | | RectangularDirection.AboveNorthWest, |
| | 2 | 184 | | RectangularDirection.AboveSouthEast, |
| | 2 | 185 | | RectangularDirection.AboveNorthEast |
| | 2 | 186 | | }; |
| | | 187 | | |
| | | 188 | | /// <summary> |
| | | 189 | | /// All 16 non-vertical directions on the layers above and below. |
| | | 190 | | /// </summary> |
| | 2 | 191 | | public static ReadOnlySpan<RectangularDirection> VerticalDiagonal => VerticalDiagonalValues; |
| | | 192 | | |
| | 2 | 193 | | private static readonly RectangularDirection[] VerticalDiagonalValues = |
| | 2 | 194 | | { |
| | 2 | 195 | | RectangularDirection.BelowWest, |
| | 2 | 196 | | RectangularDirection.BelowSouth, |
| | 2 | 197 | | RectangularDirection.BelowEast, |
| | 2 | 198 | | RectangularDirection.BelowNorth, |
| | 2 | 199 | | RectangularDirection.BelowSouthWest, |
| | 2 | 200 | | RectangularDirection.BelowNorthWest, |
| | 2 | 201 | | RectangularDirection.BelowSouthEast, |
| | 2 | 202 | | RectangularDirection.BelowNorthEast, |
| | 2 | 203 | | RectangularDirection.AboveWest, |
| | 2 | 204 | | RectangularDirection.AboveSouth, |
| | 2 | 205 | | RectangularDirection.AboveEast, |
| | 2 | 206 | | RectangularDirection.AboveNorth, |
| | 2 | 207 | | RectangularDirection.AboveSouthWest, |
| | 2 | 208 | | RectangularDirection.AboveNorthWest, |
| | 2 | 209 | | RectangularDirection.AboveSouthEast, |
| | 2 | 210 | | RectangularDirection.AboveNorthEast |
| | 2 | 211 | | }; |
| | | 212 | | |
| | | 213 | | /// <summary> |
| | | 214 | | /// All 20 diagonal neighbor directions excluding None. |
| | | 215 | | /// </summary> |
| | 3 | 216 | | public static ReadOnlySpan<RectangularDirection> Diagonal => DiagonalValues; |
| | | 217 | | |
| | 2 | 218 | | private static readonly RectangularDirection[] DiagonalValues = |
| | 2 | 219 | | { |
| | 2 | 220 | | RectangularDirection.SouthWest, |
| | 2 | 221 | | RectangularDirection.NorthWest, |
| | 2 | 222 | | RectangularDirection.SouthEast, |
| | 2 | 223 | | RectangularDirection.NorthEast, |
| | 2 | 224 | | RectangularDirection.BelowWest, |
| | 2 | 225 | | RectangularDirection.BelowSouth, |
| | 2 | 226 | | RectangularDirection.BelowEast, |
| | 2 | 227 | | RectangularDirection.BelowNorth, |
| | 2 | 228 | | RectangularDirection.AboveWest, |
| | 2 | 229 | | RectangularDirection.AboveSouth, |
| | 2 | 230 | | RectangularDirection.AboveEast, |
| | 2 | 231 | | RectangularDirection.AboveNorth, |
| | 2 | 232 | | RectangularDirection.BelowSouthWest, |
| | 2 | 233 | | RectangularDirection.BelowNorthWest, |
| | 2 | 234 | | RectangularDirection.BelowSouthEast, |
| | 2 | 235 | | RectangularDirection.BelowNorthEast, |
| | 2 | 236 | | RectangularDirection.AboveSouthWest, |
| | 2 | 237 | | RectangularDirection.AboveNorthWest, |
| | 2 | 238 | | RectangularDirection.AboveSouthEast, |
| | 2 | 239 | | RectangularDirection.AboveNorthEast |
| | 2 | 240 | | }; |
| | | 241 | | |
| | | 242 | | #endregion |
| | | 243 | | |
| | | 244 | | #region Methods |
| | | 245 | | |
| | | 246 | | /// <summary>True for pure axis-aligned directions.</summary> |
| | | 247 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 248 | | public static bool IsPerpendicularNeighbor(RectangularDirection dir) => |
| | 2 | 249 | | (uint)dir <= (uint)RectangularDirection.Above; |
| | | 250 | | |
| | | 251 | | /// <summary>True for any diagonal step (multiple axes).</summary> |
| | | 252 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 253 | | public static bool IsDiagonalNeighbor(RectangularDirection dir) => |
| | 2 | 254 | | (uint)((int)dir - (int)RectangularDirection.SouthWest) < Diagonal.Length; |
| | | 255 | | |
| | | 256 | | /// <summary> |
| | | 257 | | /// Converts a rectangular local offset in the range [-1, 1] on each axis into a direction. |
| | | 258 | | /// </summary> |
| | | 259 | | public static RectangularDirection GetDirectionFromOffset((int x, int y, int z) offset) |
| | | 260 | | { |
| | 77 | 261 | | return TryGetOffsetLookupKey(offset, out int lookupKey) |
| | 77 | 262 | | ? DirectionLookup[lookupKey] |
| | 77 | 263 | | : RectangularDirection.None; |
| | | 264 | | } |
| | | 265 | | |
| | | 266 | | /// <summary> |
| | | 267 | | /// Gets the boundary range for a given offset and size, |
| | | 268 | | /// returning (0, 0) for negative offsets, (size-1, size-1) for positive offsets, |
| | | 269 | | /// and (0, size-1) for zero offsets. |
| | | 270 | | /// </summary> |
| | | 271 | | /// <param name="offset">The offset value.</param> |
| | | 272 | | /// <param name="size">The size of the dimension.</param> |
| | | 273 | | /// <returns>A tuple representing the start and end of the boundary range.</returns> |
| | | 274 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 275 | | public static (int start, int end) GetBoundaryRange(int offset, int size) |
| | | 276 | | { |
| | 9 | 277 | | return offset switch |
| | 9 | 278 | | { |
| | 2 | 279 | | < 0 => (0, 0), |
| | 4 | 280 | | > 0 => (size - 1, size - 1), |
| | 3 | 281 | | _ => (0, size - 1) |
| | 9 | 282 | | }; |
| | | 283 | | } |
| | | 284 | | |
| | | 285 | | /// <summary> |
| | | 286 | | /// Determines if a coordinate is facing the boundary of an axis given an offset and size. |
| | | 287 | | /// </summary> |
| | | 288 | | /// <param name="coordinate">The coordinate value.</param> |
| | | 289 | | /// <param name="offset">The offset value.</param> |
| | | 290 | | /// <param name="size">The size of the dimension.</param> |
| | | 291 | | /// <returns>True if the coordinate is facing the boundary, otherwise false.</returns> |
| | | 292 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 293 | | public static bool IsAxisFacingBoundary(int coordinate, int offset, int size) |
| | | 294 | | { |
| | 130 | 295 | | return offset switch |
| | 130 | 296 | | { |
| | 44 | 297 | | < 0 => coordinate == 0, |
| | 45 | 298 | | > 0 => coordinate == size - 1, |
| | 41 | 299 | | _ => true |
| | 130 | 300 | | }; |
| | | 301 | | } |
| | | 302 | | |
| | | 303 | | private static RectangularDirection[] CreateDirectionLookup() |
| | | 304 | | { |
| | 2 | 305 | | RectangularDirection[] lookup = new RectangularDirection[OffsetLookupSize]; |
| | 2 | 306 | | Array.Fill(lookup, RectangularDirection.None); |
| | | 307 | | |
| | 108 | 308 | | for (int i = 0; i < OffsetValues.Length; i++) |
| | | 309 | | { |
| | 52 | 310 | | TryGetOffsetLookupKey(OffsetValues[i], out int lookupKey); |
| | 52 | 311 | | lookup[lookupKey] = (RectangularDirection)i; |
| | | 312 | | } |
| | | 313 | | |
| | 2 | 314 | | return lookup; |
| | | 315 | | } |
| | | 316 | | |
| | | 317 | | private static bool TryGetOffsetLookupKey((int x, int y, int z) offset, out int lookupKey) |
| | | 318 | | { |
| | 129 | 319 | | int x = offset.x + 1; |
| | 129 | 320 | | int y = offset.y + 1; |
| | 129 | 321 | | int z = offset.z + 1; |
| | | 322 | | |
| | 129 | 323 | | if ((uint)x > 2u || (uint)y > 2u || (uint)z > 2u) |
| | | 324 | | { |
| | 2 | 325 | | lookupKey = -1; |
| | 2 | 326 | | return false; |
| | | 327 | | } |
| | | 328 | | |
| | 127 | 329 | | lookupKey = x * 9 + y * 3 + z; |
| | 127 | 330 | | return true; |
| | | 331 | | } |
| | | 332 | | |
| | | 333 | | #endregion |
| | | 334 | | } |