| | | 1 | | //======================================================================= |
| | | 2 | | // HexDirectionUtility.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 hex-prism neighbor direction utilities. |
| | | 15 | | /// </summary> |
| | | 16 | | public static class HexDirectionUtility |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Hex-prism neighbor offsets in deterministic direction order. |
| | | 20 | | /// </summary> |
| | 371 | 21 | | public static ReadOnlySpan<VoxelIndex> Offsets => OffsetValues; |
| | | 22 | | |
| | 1 | 23 | | private static readonly VoxelIndex[] OffsetValues = |
| | 1 | 24 | | { |
| | 1 | 25 | | new(1, 0, 0), |
| | 1 | 26 | | new(1, 0, -1), |
| | 1 | 27 | | new(0, 0, -1), |
| | 1 | 28 | | new(-1, 0, 0), |
| | 1 | 29 | | new(-1, 0, 1), |
| | 1 | 30 | | new(0, 0, 1), |
| | 1 | 31 | | new(0, -1, 0), |
| | 1 | 32 | | new(1, -1, 0), |
| | 1 | 33 | | new(1, -1, -1), |
| | 1 | 34 | | new(0, -1, -1), |
| | 1 | 35 | | new(-1, -1, 0), |
| | 1 | 36 | | new(-1, -1, 1), |
| | 1 | 37 | | new(0, -1, 1), |
| | 1 | 38 | | new(0, 1, 0), |
| | 1 | 39 | | new(1, 1, 0), |
| | 1 | 40 | | new(1, 1, -1), |
| | 1 | 41 | | new(0, 1, -1), |
| | 1 | 42 | | new(-1, 1, 0), |
| | 1 | 43 | | new(-1, 1, 1), |
| | 1 | 44 | | new(0, 1, 1) |
| | 1 | 45 | | }; |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// All 20 hex-prism neighbor directions excluding None. |
| | | 49 | | /// </summary> |
| | 2 | 50 | | public static ReadOnlySpan<HexDirection> All => AllValues; |
| | | 51 | | |
| | 1 | 52 | | private static readonly HexDirection[] AllValues = |
| | 1 | 53 | | { |
| | 1 | 54 | | HexDirection.QPositive, |
| | 1 | 55 | | HexDirection.QPositiveRNegative, |
| | 1 | 56 | | HexDirection.RNegative, |
| | 1 | 57 | | HexDirection.QNegative, |
| | 1 | 58 | | HexDirection.QNegativeRPositive, |
| | 1 | 59 | | HexDirection.RPositive, |
| | 1 | 60 | | HexDirection.Below, |
| | 1 | 61 | | HexDirection.BelowQPositive, |
| | 1 | 62 | | HexDirection.BelowQPositiveRNegative, |
| | 1 | 63 | | HexDirection.BelowRNegative, |
| | 1 | 64 | | HexDirection.BelowQNegative, |
| | 1 | 65 | | HexDirection.BelowQNegativeRPositive, |
| | 1 | 66 | | HexDirection.BelowRPositive, |
| | 1 | 67 | | HexDirection.Above, |
| | 1 | 68 | | HexDirection.AboveQPositive, |
| | 1 | 69 | | HexDirection.AboveQPositiveRNegative, |
| | 1 | 70 | | HexDirection.AboveRNegative, |
| | 1 | 71 | | HexDirection.AboveQNegative, |
| | 1 | 72 | | HexDirection.AboveQNegativeRPositive, |
| | 1 | 73 | | HexDirection.AboveRPositive |
| | 1 | 74 | | }; |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// The 8 face-adjacent hex-prism directions. |
| | | 78 | | /// </summary> |
| | 2 | 79 | | public static ReadOnlySpan<HexDirection> Primary => PrimaryValues; |
| | | 80 | | |
| | 1 | 81 | | private static readonly HexDirection[] PrimaryValues = |
| | 1 | 82 | | { |
| | 1 | 83 | | HexDirection.QPositive, |
| | 1 | 84 | | HexDirection.QPositiveRNegative, |
| | 1 | 85 | | HexDirection.RNegative, |
| | 1 | 86 | | HexDirection.QNegative, |
| | 1 | 87 | | HexDirection.QNegativeRPositive, |
| | 1 | 88 | | HexDirection.RPositive, |
| | 1 | 89 | | HexDirection.Below, |
| | 1 | 90 | | HexDirection.Above |
| | 1 | 91 | | }; |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// All 6 planar axial hex directions. |
| | | 95 | | /// </summary> |
| | 2 | 96 | | public static ReadOnlySpan<HexDirection> Planar => PlanarValues; |
| | | 97 | | |
| | 1 | 98 | | private static readonly HexDirection[] PlanarValues = |
| | 1 | 99 | | { |
| | 1 | 100 | | HexDirection.QPositive, |
| | 1 | 101 | | HexDirection.QPositiveRNegative, |
| | 1 | 102 | | HexDirection.RNegative, |
| | 1 | 103 | | HexDirection.QNegative, |
| | 1 | 104 | | HexDirection.QNegativeRPositive, |
| | 1 | 105 | | HexDirection.RPositive |
| | 1 | 106 | | }; |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// All 2 vertical hex-prism directions. |
| | | 110 | | /// </summary> |
| | 2 | 111 | | public static ReadOnlySpan<HexDirection> Vertical => VerticalValues; |
| | | 112 | | |
| | 1 | 113 | | private static readonly HexDirection[] VerticalValues = |
| | 1 | 114 | | { |
| | 1 | 115 | | HexDirection.Below, |
| | 1 | 116 | | HexDirection.Above |
| | 1 | 117 | | }; |
| | | 118 | | |
| | | 119 | | /// <summary> |
| | | 120 | | /// All 7 neighbor directions on the layer below. |
| | | 121 | | /// </summary> |
| | 2 | 122 | | public static ReadOnlySpan<HexDirection> BelowLayer => BelowLayerValues; |
| | | 123 | | |
| | 1 | 124 | | private static readonly HexDirection[] BelowLayerValues = |
| | 1 | 125 | | { |
| | 1 | 126 | | HexDirection.Below, |
| | 1 | 127 | | HexDirection.BelowQPositive, |
| | 1 | 128 | | HexDirection.BelowQPositiveRNegative, |
| | 1 | 129 | | HexDirection.BelowRNegative, |
| | 1 | 130 | | HexDirection.BelowQNegative, |
| | 1 | 131 | | HexDirection.BelowQNegativeRPositive, |
| | 1 | 132 | | HexDirection.BelowRPositive |
| | 1 | 133 | | }; |
| | | 134 | | |
| | | 135 | | /// <summary> |
| | | 136 | | /// All 7 neighbor directions on the layer above. |
| | | 137 | | /// </summary> |
| | 2 | 138 | | public static ReadOnlySpan<HexDirection> AboveLayer => AboveLayerValues; |
| | | 139 | | |
| | 1 | 140 | | private static readonly HexDirection[] AboveLayerValues = |
| | 1 | 141 | | { |
| | 1 | 142 | | HexDirection.Above, |
| | 1 | 143 | | HexDirection.AboveQPositive, |
| | 1 | 144 | | HexDirection.AboveQPositiveRNegative, |
| | 1 | 145 | | HexDirection.AboveRNegative, |
| | 1 | 146 | | HexDirection.AboveQNegative, |
| | 1 | 147 | | HexDirection.AboveQNegativeRPositive, |
| | 1 | 148 | | HexDirection.AboveRPositive |
| | 1 | 149 | | }; |
| | | 150 | | |
| | | 151 | | /// <summary> |
| | | 152 | | /// All 12 non-vertical directions on the layers above and below. |
| | | 153 | | /// </summary> |
| | 2 | 154 | | public static ReadOnlySpan<HexDirection> VerticalDiagonal => VerticalDiagonalValues; |
| | | 155 | | |
| | 1 | 156 | | private static readonly HexDirection[] VerticalDiagonalValues = |
| | 1 | 157 | | { |
| | 1 | 158 | | HexDirection.BelowQPositive, |
| | 1 | 159 | | HexDirection.BelowQPositiveRNegative, |
| | 1 | 160 | | HexDirection.BelowRNegative, |
| | 1 | 161 | | HexDirection.BelowQNegative, |
| | 1 | 162 | | HexDirection.BelowQNegativeRPositive, |
| | 1 | 163 | | HexDirection.BelowRPositive, |
| | 1 | 164 | | HexDirection.AboveQPositive, |
| | 1 | 165 | | HexDirection.AboveQPositiveRNegative, |
| | 1 | 166 | | HexDirection.AboveRNegative, |
| | 1 | 167 | | HexDirection.AboveQNegative, |
| | 1 | 168 | | HexDirection.AboveQNegativeRPositive, |
| | 1 | 169 | | HexDirection.AboveRPositive |
| | 1 | 170 | | }; |
| | | 171 | | |
| | | 172 | | /// <summary> |
| | | 173 | | /// Gets the topology-local index offset for a hex direction. |
| | | 174 | | /// </summary> |
| | | 175 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 30 | 176 | | public static VoxelIndex GetOffset(HexDirection direction) => OffsetValues[(int)direction]; |
| | | 177 | | |
| | | 178 | | /// <summary> |
| | | 179 | | /// True for planar axial directions. |
| | | 180 | | /// </summary> |
| | | 181 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 182 | | public static bool IsPlanar(HexDirection direction) => |
| | 2 | 183 | | (uint)direction <= (uint)HexDirection.RPositive; |
| | | 184 | | |
| | | 185 | | /// <summary> |
| | | 186 | | /// True for vertical layer directions. |
| | | 187 | | /// </summary> |
| | | 188 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 189 | | public static bool IsVertical(HexDirection direction) => |
| | 3 | 190 | | direction == HexDirection.Below || direction == HexDirection.Above; |
| | | 191 | | } |