| | | 1 | | //======================================================================= |
| | | 2 | | // RectangularPrismTopology.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 RectangularPrismTopology : IGridTopology |
| | | 15 | | { |
| | 1383 | 16 | | public GridTopologyKind Kind => GridTopologyKind.RectangularPrism; |
| | | 17 | | |
| | | 18 | | public GridTopologyMetrics Metrics { get; } |
| | | 19 | | |
| | 67 | 20 | | public Fixed64 OverlapTolerance => Metrics.SmallestRectangularEdge * Fixed64.Half; |
| | | 21 | | |
| | 387 | 22 | | public Fixed64 MaxCellEdge => Metrics.LargestRectangularEdge; |
| | | 23 | | |
| | 579 | 24 | | public int NeighborSlotCount => RectangularDirectionUtility.Offsets.Length; |
| | | 25 | | |
| | 367 | 26 | | public RectangularPrismTopology(GridTopologyMetrics metrics) |
| | | 27 | | { |
| | 367 | 28 | | Metrics = GridTopologyMetrics.Normalize(GridTopologyKind.RectangularPrism, metrics); |
| | 367 | 29 | | } |
| | | 30 | | |
| | | 31 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 32 | | public GridDimensions CalculateDimensions(Vector3d boundsMin, Vector3d boundsMax) => |
| | 716 | 33 | | new(((boundsMax.X - boundsMin.X) / Metrics.CellWidth).FloorToInt() + 1, |
| | 716 | 34 | | ((boundsMax.Y - boundsMin.Y) / Metrics.LayerHeight).FloorToInt() + 1, |
| | 716 | 35 | | ((boundsMax.Z - boundsMin.Z) / Metrics.CellLength).FloorToInt() + 1); |
| | | 36 | | |
| | | 37 | | public (Vector3d min, Vector3d max) NormalizeBounds(Vector3d min, Vector3d max, Fixed64? padding = null) |
| | | 38 | | { |
| | 1134 | 39 | | Fixed64 fixedPadding = padding.HasValue && padding.Value > Fixed64.Zero |
| | 1134 | 40 | | ? padding.Value |
| | 1134 | 41 | | : Fixed64.Zero; |
| | | 42 | | |
| | 1134 | 43 | | min -= fixedPadding; |
| | 1134 | 44 | | max += fixedPadding; |
| | | 45 | | |
| | 1134 | 46 | | Vector3d snapMin = FloorToCellOrigin(min); |
| | 1134 | 47 | | Vector3d snapMax = CeilToCellOrigin(max); |
| | | 48 | | |
| | 1134 | 49 | | (snapMin.X, snapMax.X) = snapMin.X > snapMax.X ? (snapMax.X, snapMin.X) : (snapMin.X, snapMax.X); |
| | 1134 | 50 | | (snapMin.Y, snapMax.Y) = snapMin.Y > snapMax.Y ? (snapMax.Y, snapMin.Y) : (snapMin.Y, snapMax.Y); |
| | 1134 | 51 | | (snapMin.Z, snapMax.Z) = snapMin.Z > snapMax.Z ? (snapMax.Z, snapMin.Z) : (snapMin.Z, snapMax.Z); |
| | | 52 | | |
| | 1134 | 53 | | return (snapMin, snapMax); |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 57 | | public bool IsInBounds( |
| | | 58 | | Vector3d boundsMin, |
| | | 59 | | Vector3d boundsMax, |
| | | 60 | | int width, |
| | | 61 | | int height, |
| | | 62 | | int length, |
| | | 63 | | Vector3d position) => |
| | 1587 | 64 | | boundsMin.X <= position.X && position.X <= boundsMax.X |
| | 1587 | 65 | | && boundsMin.Y <= position.Y && position.Y <= boundsMax.Y |
| | 1587 | 66 | | && boundsMin.Z <= position.Z && position.Z <= boundsMax.Z; |
| | | 67 | | |
| | | 68 | | public bool TryGetVoxelIndex( |
| | | 69 | | Vector3d boundsMin, |
| | | 70 | | Vector3d boundsMax, |
| | | 71 | | int width, |
| | | 72 | | int height, |
| | | 73 | | int length, |
| | | 74 | | Vector3d position, |
| | | 75 | | out VoxelIndex result) |
| | | 76 | | { |
| | 1464 | 77 | | result = default; |
| | 1464 | 78 | | if (!IsInBounds(boundsMin, boundsMax, width, height, length, position)) |
| | 15 | 79 | | return false; |
| | | 80 | | |
| | 1449 | 81 | | result = new VoxelIndex( |
| | 1449 | 82 | | ((position.X - boundsMin.X) / Metrics.CellWidth).FloorToInt(), |
| | 1449 | 83 | | ((position.Y - boundsMin.Y) / Metrics.LayerHeight).FloorToInt(), |
| | 1449 | 84 | | ((position.Z - boundsMin.Z) / Metrics.CellLength).FloorToInt()); |
| | 1449 | 85 | | return true; |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | public VoxelIndex GetClosestVoxelIndex( |
| | | 89 | | Vector3d boundsMin, |
| | | 90 | | int width, |
| | | 91 | | int height, |
| | | 92 | | int length, |
| | | 93 | | Vector3d position) => |
| | 20 | 94 | | new( |
| | 20 | 95 | | GetClosestAxisIndex(position.X - boundsMin.X, Metrics.CellWidth, width), |
| | 20 | 96 | | GetClosestAxisIndex(position.Y - boundsMin.Y, Metrics.LayerHeight, height), |
| | 20 | 97 | | GetClosestAxisIndex(position.Z - boundsMin.Z, Metrics.CellLength, length)); |
| | | 98 | | |
| | | 99 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 100 | | public Vector3d GetWorldPosition(Vector3d boundsMin, VoxelIndex index) => |
| | 93576 | 101 | | new(boundsMin.X + index.x * Metrics.CellWidth, |
| | 93576 | 102 | | boundsMin.Y + index.y * Metrics.LayerHeight, |
| | 93576 | 103 | | boundsMin.Z + index.z * Metrics.CellLength); |
| | | 104 | | |
| | | 105 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 106 | | public Vector3d GetWorldOffset((int x, int y, int z) offset) => |
| | 73 | 107 | | new(offset.x * Metrics.CellWidth, |
| | 73 | 108 | | offset.y * Metrics.LayerHeight, |
| | 73 | 109 | | offset.z * Metrics.CellLength); |
| | | 110 | | |
| | | 111 | | public VoxelIndex GetNeighborOffset(int slot) |
| | | 112 | | { |
| | 437 | 113 | | (int x, int y, int z) offset = RectangularDirectionUtility.Offsets[slot]; |
| | 437 | 114 | | return new VoxelIndex(offset.x, offset.y, offset.z); |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | public bool TryGetNeighborSlotFromWorldDelta(Vector3d worldDelta, out int slot) |
| | | 118 | | { |
| | 74 | 119 | | RectangularDirection direction = RectangularDirectionUtility.GetDirectionFromOffset(( |
| | 74 | 120 | | worldDelta.X.Sign(), |
| | 74 | 121 | | worldDelta.Y.Sign(), |
| | 74 | 122 | | worldDelta.Z.Sign())); |
| | 74 | 123 | | slot = (int)direction; |
| | 74 | 124 | | return direction != RectangularDirection.None; |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | public bool IsFacingBoundary(VoxelIndex voxelIndex, int slot, int width, int height, int length) |
| | | 128 | | { |
| | 54 | 129 | | (int x, int y, int z) offset = RectangularDirectionUtility.Offsets[slot]; |
| | 54 | 130 | | return RectangularDirectionUtility.IsAxisFacingBoundary(voxelIndex.x, offset.x, width) |
| | 54 | 131 | | && RectangularDirectionUtility.IsAxisFacingBoundary(voxelIndex.y, offset.y, height) |
| | 54 | 132 | | && RectangularDirectionUtility.IsAxisFacingBoundary(voxelIndex.z, offset.z, length); |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | public void GetBoundaryRange( |
| | | 136 | | int slot, |
| | | 137 | | int width, |
| | | 138 | | int height, |
| | | 139 | | int length, |
| | | 140 | | out int xStart, |
| | | 141 | | out int xEnd, |
| | | 142 | | out int yStart, |
| | | 143 | | out int yEnd, |
| | | 144 | | out int zStart, |
| | | 145 | | out int zEnd) |
| | | 146 | | { |
| | 1 | 147 | | (int x, int y, int z) offset = RectangularDirectionUtility.Offsets[slot]; |
| | 1 | 148 | | (xStart, xEnd) = RectangularDirectionUtility.GetBoundaryRange(offset.x, width); |
| | 1 | 149 | | (yStart, yEnd) = RectangularDirectionUtility.GetBoundaryRange(offset.y, height); |
| | 1 | 150 | | (zStart, zEnd) = RectangularDirectionUtility.GetBoundaryRange(offset.z, length); |
| | 1 | 151 | | } |
| | | 152 | | |
| | | 153 | | public Vector3d FloorToGrid( |
| | | 154 | | Vector3d boundsMin, |
| | | 155 | | Vector3d boundsMax, |
| | | 156 | | int width, |
| | | 157 | | int height, |
| | | 158 | | int length, |
| | | 159 | | Vector3d position) |
| | | 160 | | { |
| | 197 | 161 | | return new Vector3d( |
| | 197 | 162 | | FixedMath.Clamp(((position.X - boundsMin.X) / Metrics.CellWidth).FloorToInt() * Metrics.CellWidth + boundsMi |
| | 197 | 163 | | FixedMath.Clamp(((position.Y - boundsMin.Y) / Metrics.LayerHeight).FloorToInt() * Metrics.LayerHeight + boun |
| | 197 | 164 | | FixedMath.Clamp(((position.Z - boundsMin.Z) / Metrics.CellLength).FloorToInt() * Metrics.CellLength + bounds |
| | | 165 | | } |
| | | 166 | | |
| | | 167 | | public Vector3d CeilToGrid( |
| | | 168 | | Vector3d boundsMin, |
| | | 169 | | Vector3d boundsMax, |
| | | 170 | | int width, |
| | | 171 | | int height, |
| | | 172 | | int length, |
| | | 173 | | Vector3d position) |
| | | 174 | | { |
| | 2 | 175 | | return new Vector3d( |
| | 2 | 176 | | FixedMath.Clamp(((position.X - boundsMin.X) / Metrics.CellWidth).CeilToInt() * Metrics.CellWidth + boundsMin |
| | 2 | 177 | | FixedMath.Clamp(((position.Y - boundsMin.Y) / Metrics.LayerHeight).CeilToInt() * Metrics.LayerHeight + bound |
| | 2 | 178 | | FixedMath.Clamp(((position.Z - boundsMin.Z) / Metrics.CellLength).CeilToInt() * Metrics.CellLength + boundsM |
| | | 179 | | } |
| | | 180 | | |
| | | 181 | | public (int x, int y, int z) SnapToScanCell(Vector3d boundsMin, Vector3d position, int scanCellSize) |
| | | 182 | | { |
| | 1084 | 183 | | return (((position.X - boundsMin.X) / Metrics.CellWidth).FloorToInt() / scanCellSize, |
| | 1084 | 184 | | ((position.Y - boundsMin.Y) / Metrics.LayerHeight).FloorToInt() / scanCellSize, |
| | 1084 | 185 | | ((position.Z - boundsMin.Z) / Metrics.CellLength).FloorToInt() / scanCellSize); |
| | | 186 | | } |
| | | 187 | | |
| | | 188 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 189 | | private Vector3d FloorToCellOrigin(Vector3d position) => |
| | 1134 | 190 | | new(FloorToCellOrigin(position.X, Metrics.CellWidth), |
| | 1134 | 191 | | FloorToCellOrigin(position.Y, Metrics.LayerHeight), |
| | 1134 | 192 | | FloorToCellOrigin(position.Z, Metrics.CellLength)); |
| | | 193 | | |
| | | 194 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 195 | | private Vector3d CeilToCellOrigin(Vector3d position) => |
| | 1134 | 196 | | new(CeilToCellOrigin(position.X, Metrics.CellWidth), |
| | 1134 | 197 | | CeilToCellOrigin(position.Y, Metrics.LayerHeight), |
| | 1134 | 198 | | CeilToCellOrigin(position.Z, Metrics.CellLength)); |
| | | 199 | | |
| | | 200 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 201 | | private static Fixed64 FloorToCellOrigin(Fixed64 coordinate, Fixed64 cellSize) => |
| | 3402 | 202 | | (coordinate / cellSize).FloorToInt() * cellSize; |
| | | 203 | | |
| | | 204 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 205 | | private static Fixed64 CeilToCellOrigin(Fixed64 coordinate, Fixed64 cellSize) => |
| | 3402 | 206 | | (coordinate / cellSize).CeilToInt() * cellSize; |
| | | 207 | | |
| | | 208 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 209 | | private static int GetClosestAxisIndex(Fixed64 offset, Fixed64 cellSize, int size) => |
| | 60 | 210 | | FixedMath.Clamp((offset / cellSize).RoundToInt(), 0, size - 1); |
| | | 211 | | |
| | | 212 | | } |