| | | 1 | | //======================================================================= |
| | | 2 | | // HexPrismTopology.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 FixedMathSharp; |
| | | 9 | | using GridForge.Spatial; |
| | | 10 | | using System.Runtime.CompilerServices; |
| | | 11 | | |
| | | 12 | | namespace GridForge.Grids.Topology; |
| | | 13 | | |
| | | 14 | | internal sealed class HexPrismTopology : IGridTopology |
| | | 15 | | { |
| | 169 | 16 | | public GridTopologyKind Kind => GridTopologyKind.HexPrism; |
| | | 17 | | |
| | | 18 | | public GridTopologyMetrics Metrics { get; } |
| | | 19 | | |
| | 22 | 20 | | public Fixed64 OverlapTolerance => MaxCellEdge * Fixed64.Half; |
| | | 21 | | |
| | 83 | 22 | | public Fixed64 MaxCellEdge => Metrics.LargestHexEdge; |
| | | 23 | | |
| | 60 | 24 | | public int NeighborSlotCount => HexDirectionUtility.Offsets.Length; |
| | | 25 | | |
| | 70 | 26 | | public HexPrismTopology(GridTopologyMetrics metrics) |
| | | 27 | | { |
| | 70 | 28 | | Metrics = GridTopologyMetrics.Normalize(GridTopologyKind.HexPrism, metrics); |
| | 70 | 29 | | } |
| | | 30 | | |
| | | 31 | | public GridDimensions CalculateDimensions(Vector3d boundsMin, Vector3d boundsMax) |
| | | 32 | | { |
| | 116 | 33 | | VoxelIndex maxIndex = GetMaxIndex(boundsMin, boundsMax); |
| | 116 | 34 | | int height = ((boundsMax.Y - boundsMin.Y) / Metrics.LayerHeight).FloorToInt() + 1; |
| | 116 | 35 | | return new GridDimensions(maxIndex.x + 1, height, maxIndex.z + 1); |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | public (Vector3d min, Vector3d max) NormalizeBounds(Vector3d min, Vector3d max, Fixed64? padding = null) |
| | | 39 | | { |
| | 70 | 40 | | Fixed64 fixedPadding = padding.HasValue && padding.Value > Fixed64.Zero |
| | 70 | 41 | | ? padding.Value |
| | 70 | 42 | | : Fixed64.Zero; |
| | | 43 | | |
| | 70 | 44 | | min -= fixedPadding; |
| | 70 | 45 | | max += fixedPadding; |
| | | 46 | | |
| | 70 | 47 | | Fixed64 yMin = FloorToLayerOrigin(min.Y); |
| | 70 | 48 | | Fixed64 yMax = CeilToLayerOrigin(max.Y); |
| | 70 | 49 | | Vector3d normalizedMin = new(min.X, yMin, min.Z); |
| | | 50 | | |
| | 70 | 51 | | HexCoordinateUtility.WorldOffsetToAxial( |
| | 70 | 52 | | max.X - normalizedMin.X, |
| | 70 | 53 | | max.Z - normalizedMin.Z, |
| | 70 | 54 | | Metrics, |
| | 70 | 55 | | out Fixed64 qMax, |
| | 70 | 56 | | out Fixed64 rMax); |
| | | 57 | | |
| | 70 | 58 | | int maxQ = HexCoordinateUtility.CeilToIntWithTolerance(FixedMath.Max(qMax, Fixed64.Zero)); |
| | 70 | 59 | | int maxR = HexCoordinateUtility.CeilToIntWithTolerance(FixedMath.Max(rMax, Fixed64.Zero)); |
| | 70 | 60 | | int maxY = ((yMax - yMin) / Metrics.LayerHeight).FloorToInt(); |
| | 70 | 61 | | Vector3d normalizedMax = normalizedMin + HexCoordinateUtility.AxialToWorldOffset(new VoxelIndex(maxQ, maxY, maxR |
| | | 62 | | |
| | 70 | 63 | | return (normalizedMin, normalizedMax); |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 67 | | public bool IsInBounds( |
| | | 68 | | Vector3d boundsMin, |
| | | 69 | | Vector3d boundsMax, |
| | | 70 | | int width, |
| | | 71 | | int height, |
| | | 72 | | int length, |
| | | 73 | | Vector3d position) => |
| | 44 | 74 | | TryGetVoxelIndex(boundsMin, boundsMax, width, height, length, position, out _); |
| | | 75 | | |
| | | 76 | | public bool TryGetVoxelIndex( |
| | | 77 | | Vector3d boundsMin, |
| | | 78 | | Vector3d boundsMax, |
| | | 79 | | int width, |
| | | 80 | | int height, |
| | | 81 | | int length, |
| | | 82 | | Vector3d position, |
| | | 83 | | out VoxelIndex result) |
| | | 84 | | { |
| | 86 | 85 | | result = default; |
| | | 86 | | |
| | 86 | 87 | | if (!IsInsideCoarseBounds(boundsMin, boundsMax, position)) |
| | 21 | 88 | | return false; |
| | | 89 | | |
| | 65 | 90 | | HexCoordinateUtility.WorldOffsetToAxial( |
| | 65 | 91 | | position.X - boundsMin.X, |
| | 65 | 92 | | position.Z - boundsMin.Z, |
| | 65 | 93 | | Metrics, |
| | 65 | 94 | | out Fixed64 q, |
| | 65 | 95 | | out Fixed64 r); |
| | | 96 | | |
| | 65 | 97 | | Fixed64 layer = (position.Y - boundsMin.Y) / Metrics.LayerHeight; |
| | 65 | 98 | | VoxelIndex rounded = HexCoordinateUtility.RoundAxial(q, layer, r); |
| | 65 | 99 | | if ((uint)rounded.x >= (uint)width |
| | 65 | 100 | | || (uint)rounded.y >= (uint)height |
| | 65 | 101 | | || (uint)rounded.z >= (uint)length) |
| | | 102 | | { |
| | 2 | 103 | | return false; |
| | | 104 | | } |
| | | 105 | | |
| | 63 | 106 | | result = rounded; |
| | 63 | 107 | | return true; |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | public VoxelIndex GetClosestVoxelIndex( |
| | | 111 | | Vector3d boundsMin, |
| | | 112 | | int width, |
| | | 113 | | int height, |
| | | 114 | | int length, |
| | | 115 | | Vector3d position) |
| | | 116 | | { |
| | 4 | 117 | | HexCoordinateUtility.WorldOffsetToAxial( |
| | 4 | 118 | | position.X - boundsMin.X, |
| | 4 | 119 | | position.Z - boundsMin.Z, |
| | 4 | 120 | | Metrics, |
| | 4 | 121 | | out Fixed64 q, |
| | 4 | 122 | | out Fixed64 r); |
| | 4 | 123 | | HexCoordinateUtility.RoundCube(q, r, out int roundedQ, out int roundedR); |
| | | 124 | | |
| | 4 | 125 | | int roundedY = ((position.Y - boundsMin.Y) / Metrics.LayerHeight).RoundToInt(); |
| | 4 | 126 | | return new VoxelIndex( |
| | 4 | 127 | | FixedMath.Clamp(roundedQ, 0, width - 1), |
| | 4 | 128 | | FixedMath.Clamp(roundedY, 0, height - 1), |
| | 4 | 129 | | FixedMath.Clamp(roundedR, 0, length - 1)); |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 133 | | public Vector3d GetWorldPosition(Vector3d boundsMin, VoxelIndex index) => |
| | 2472 | 134 | | boundsMin + HexCoordinateUtility.AxialToWorldOffset(index, Metrics); |
| | | 135 | | |
| | | 136 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 137 | | public Vector3d GetWorldOffset((int x, int y, int z) offset) => |
| | 19 | 138 | | HexCoordinateUtility.AxialToWorldOffset(new VoxelIndex(offset.x, offset.y, offset.z), Metrics); |
| | | 139 | | |
| | 44 | 140 | | public VoxelIndex GetNeighborOffset(int slot) => HexDirectionUtility.Offsets[slot]; |
| | | 141 | | |
| | | 142 | | public bool TryGetNeighborSlotFromWorldDelta(Vector3d worldDelta, out int slot) |
| | | 143 | | { |
| | 18 | 144 | | HexCoordinateUtility.WorldOffsetToAxial( |
| | 18 | 145 | | worldDelta.X, |
| | 18 | 146 | | worldDelta.Z, |
| | 18 | 147 | | Metrics, |
| | 18 | 148 | | out Fixed64 q, |
| | 18 | 149 | | out Fixed64 r); |
| | 18 | 150 | | HexCoordinateUtility.RoundCube(q, r, out int roundedQ, out int roundedR); |
| | | 151 | | |
| | 18 | 152 | | int yDirection = worldDelta.Y.Sign(); |
| | 18 | 153 | | if (yDirection != 0) |
| | | 154 | | { |
| | 7 | 155 | | if (roundedQ == 0 && roundedR == 0) |
| | 1 | 156 | | return TryGetNeighborSlot(new VoxelIndex(0, yDirection, 0), out slot); |
| | | 157 | | |
| | 6 | 158 | | VoxelIndex planarOffset = NormalizePlanarOffset(roundedQ, roundedR); |
| | 6 | 159 | | if (planarOffset == default) |
| | | 160 | | { |
| | 1 | 161 | | slot = -1; |
| | 1 | 162 | | return false; |
| | | 163 | | } |
| | | 164 | | |
| | 5 | 165 | | return TryGetNeighborSlot(new VoxelIndex(planarOffset.x, yDirection, planarOffset.z), out slot); |
| | | 166 | | } |
| | | 167 | | |
| | 11 | 168 | | return TryGetNeighborSlot(NormalizePlanarOffset(roundedQ, roundedR), out slot); |
| | | 169 | | } |
| | | 170 | | |
| | | 171 | | public bool IsFacingBoundary(VoxelIndex voxelIndex, int slot, int width, int height, int length) |
| | | 172 | | { |
| | 4 | 173 | | VoxelIndex offset = HexDirectionUtility.Offsets[slot]; |
| | 4 | 174 | | return RectangularDirectionUtility.IsAxisFacingBoundary(voxelIndex.x, offset.x, width) |
| | 4 | 175 | | && RectangularDirectionUtility.IsAxisFacingBoundary(voxelIndex.y, offset.y, height) |
| | 4 | 176 | | && RectangularDirectionUtility.IsAxisFacingBoundary(voxelIndex.z, offset.z, length); |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | public void GetBoundaryRange( |
| | | 180 | | int slot, |
| | | 181 | | int width, |
| | | 182 | | int height, |
| | | 183 | | int length, |
| | | 184 | | out int xStart, |
| | | 185 | | out int xEnd, |
| | | 186 | | out int yStart, |
| | | 187 | | out int yEnd, |
| | | 188 | | out int zStart, |
| | | 189 | | out int zEnd) |
| | | 190 | | { |
| | 1 | 191 | | VoxelIndex offset = HexDirectionUtility.Offsets[slot]; |
| | 1 | 192 | | (xStart, xEnd) = RectangularDirectionUtility.GetBoundaryRange(offset.x, width); |
| | 1 | 193 | | (yStart, yEnd) = RectangularDirectionUtility.GetBoundaryRange(offset.y, height); |
| | 1 | 194 | | (zStart, zEnd) = RectangularDirectionUtility.GetBoundaryRange(offset.z, length); |
| | 1 | 195 | | } |
| | | 196 | | |
| | | 197 | | public Vector3d FloorToGrid( |
| | | 198 | | Vector3d boundsMin, |
| | | 199 | | Vector3d boundsMax, |
| | | 200 | | int width, |
| | | 201 | | int height, |
| | | 202 | | int length, |
| | | 203 | | Vector3d position) |
| | | 204 | | { |
| | 16 | 205 | | VoxelIndex index = ClampToGrid(boundsMin, width, height, length, position); |
| | 16 | 206 | | return GetWorldPosition(boundsMin, index); |
| | | 207 | | } |
| | | 208 | | |
| | | 209 | | public Vector3d CeilToGrid( |
| | | 210 | | Vector3d boundsMin, |
| | | 211 | | Vector3d boundsMax, |
| | | 212 | | int width, |
| | | 213 | | int height, |
| | | 214 | | int length, |
| | | 215 | | Vector3d position) |
| | | 216 | | { |
| | 1 | 217 | | VoxelIndex index = ClampToGrid(boundsMin, width, height, length, position); |
| | 1 | 218 | | return GetWorldPosition(boundsMin, index); |
| | | 219 | | } |
| | | 220 | | |
| | | 221 | | public (int x, int y, int z) SnapToScanCell(Vector3d boundsMin, Vector3d position, int scanCellSize) |
| | | 222 | | { |
| | 1 | 223 | | HexCoordinateUtility.WorldOffsetToAxial( |
| | 1 | 224 | | position.X - boundsMin.X, |
| | 1 | 225 | | position.Z - boundsMin.Z, |
| | 1 | 226 | | Metrics, |
| | 1 | 227 | | out Fixed64 q, |
| | 1 | 228 | | out Fixed64 r); |
| | | 229 | | |
| | 1 | 230 | | Fixed64 layer = (position.Y - boundsMin.Y) / Metrics.LayerHeight; |
| | 1 | 231 | | VoxelIndex index = HexCoordinateUtility.RoundAxial(q, layer, r); |
| | 1 | 232 | | return (index.x / scanCellSize, index.y / scanCellSize, index.z / scanCellSize); |
| | | 233 | | } |
| | | 234 | | |
| | | 235 | | private VoxelIndex ClampToGrid( |
| | | 236 | | Vector3d boundsMin, |
| | | 237 | | int width, |
| | | 238 | | int height, |
| | | 239 | | int length, |
| | | 240 | | Vector3d position) |
| | | 241 | | { |
| | 17 | 242 | | HexCoordinateUtility.WorldOffsetToAxial( |
| | 17 | 243 | | position.X - boundsMin.X, |
| | 17 | 244 | | position.Z - boundsMin.Z, |
| | 17 | 245 | | Metrics, |
| | 17 | 246 | | out Fixed64 q, |
| | 17 | 247 | | out Fixed64 r); |
| | | 248 | | |
| | 17 | 249 | | Fixed64 layer = (position.Y - boundsMin.Y) / Metrics.LayerHeight; |
| | 17 | 250 | | VoxelIndex rounded = HexCoordinateUtility.RoundAxial(q, layer, r); |
| | | 251 | | |
| | 17 | 252 | | return new VoxelIndex( |
| | 17 | 253 | | FixedMath.Clamp(rounded.x, 0, width - 1), |
| | 17 | 254 | | FixedMath.Clamp(rounded.y, 0, height - 1), |
| | 17 | 255 | | FixedMath.Clamp(rounded.z, 0, length - 1)); |
| | | 256 | | } |
| | | 257 | | |
| | | 258 | | private VoxelIndex GetMaxIndex(Vector3d boundsMin, Vector3d boundsMax) |
| | | 259 | | { |
| | 116 | 260 | | HexCoordinateUtility.WorldOffsetToAxial( |
| | 116 | 261 | | boundsMax.X - boundsMin.X, |
| | 116 | 262 | | boundsMax.Z - boundsMin.Z, |
| | 116 | 263 | | Metrics, |
| | 116 | 264 | | out Fixed64 qMax, |
| | 116 | 265 | | out Fixed64 rMax); |
| | | 266 | | |
| | 116 | 267 | | int maxQ = HexCoordinateUtility.CeilToIntWithTolerance(FixedMath.Max(qMax, Fixed64.Zero)); |
| | 116 | 268 | | int maxR = HexCoordinateUtility.CeilToIntWithTolerance(FixedMath.Max(rMax, Fixed64.Zero)); |
| | | 269 | | |
| | 116 | 270 | | return new VoxelIndex( |
| | 116 | 271 | | maxQ, |
| | 116 | 272 | | 0, |
| | 116 | 273 | | maxR); |
| | | 274 | | } |
| | | 275 | | |
| | | 276 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 277 | | private bool IsInsideCoarseBounds(Vector3d boundsMin, Vector3d boundsMax, Vector3d position) => |
| | 86 | 278 | | boundsMin.X <= position.X && position.X <= boundsMax.X |
| | 86 | 279 | | && boundsMin.Y <= position.Y && position.Y <= boundsMax.Y |
| | 86 | 280 | | && boundsMin.Z <= position.Z && position.Z <= boundsMax.Z; |
| | | 281 | | |
| | | 282 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 283 | | private Fixed64 FloorToLayerOrigin(Fixed64 coordinate) => |
| | 70 | 284 | | (coordinate / Metrics.LayerHeight).FloorToInt() * Metrics.LayerHeight; |
| | | 285 | | |
| | | 286 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 287 | | private Fixed64 CeilToLayerOrigin(Fixed64 coordinate) => |
| | 70 | 288 | | (coordinate / Metrics.LayerHeight).CeilToInt() * Metrics.LayerHeight; |
| | | 289 | | |
| | | 290 | | private static bool TryGetNeighborSlot(VoxelIndex neighborOffset, out int slot) |
| | | 291 | | { |
| | 262 | 292 | | for (int i = 0; i < HexDirectionUtility.Offsets.Length; i++) |
| | | 293 | | { |
| | 130 | 294 | | if (HexDirectionUtility.Offsets[i] == neighborOffset) |
| | | 295 | | { |
| | 16 | 296 | | slot = i; |
| | 16 | 297 | | return true; |
| | | 298 | | } |
| | | 299 | | } |
| | | 300 | | |
| | 1 | 301 | | slot = -1; |
| | 1 | 302 | | return false; |
| | | 303 | | } |
| | | 304 | | |
| | | 305 | | private static VoxelIndex NormalizePlanarOffset(int q, int r) |
| | | 306 | | { |
| | 17 | 307 | | if (q > 0 && r == 0) |
| | 7 | 308 | | return new VoxelIndex(1, 0, 0); |
| | 10 | 309 | | if (q > 0 && r < 0) |
| | 1 | 310 | | return new VoxelIndex(1, 0, -1); |
| | 9 | 311 | | if (q == 0 && r < 0) |
| | 1 | 312 | | return new VoxelIndex(0, 0, -1); |
| | 8 | 313 | | if (q < 0 && r == 0) |
| | 3 | 314 | | return new VoxelIndex(-1, 0, 0); |
| | 5 | 315 | | if (q < 0 && r > 0) |
| | 2 | 316 | | return new VoxelIndex(-1, 0, 1); |
| | 3 | 317 | | if (q == 0 && r > 0) |
| | 1 | 318 | | return new VoxelIndex(0, 0, 1); |
| | | 319 | | |
| | 2 | 320 | | return default; |
| | | 321 | | } |
| | | 322 | | } |