| | | 1 | | //======================================================================= |
| | | 2 | | // TopologyVoxelRangeUtility.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.Diagnostics; |
| | | 9 | | using System.Runtime.CompilerServices; |
| | | 10 | | using FixedMathSharp; |
| | | 11 | | using GridForge.Spatial; |
| | | 12 | | |
| | | 13 | | namespace GridForge.Grids.Topology; |
| | | 14 | | |
| | | 15 | | internal static class TopologyVoxelRangeUtility |
| | | 16 | | { |
| | | 17 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 18 | | internal static bool TryGetCandidateRange( |
| | | 19 | | VoxelGrid grid, |
| | | 20 | | TopologyVoxelAabb bounds, |
| | | 21 | | out VoxelIndex minIndex, |
| | | 22 | | out VoxelIndex maxIndex) => |
| | 398 | 23 | | TryGetCandidateRange(grid, bounds.Min, bounds.Max, out minIndex, out maxIndex); |
| | | 24 | | |
| | | 25 | | internal static bool TryGetCandidateRange( |
| | | 26 | | VoxelGrid grid, |
| | | 27 | | Vector3d queryMin, |
| | | 28 | | Vector3d queryMax, |
| | | 29 | | out VoxelIndex minIndex, |
| | | 30 | | out VoxelIndex maxIndex) |
| | | 31 | | { |
| | 872 | 32 | | if (!grid.IsActive) |
| | | 33 | | { |
| | 1 | 34 | | minIndex = default; |
| | 1 | 35 | | maxIndex = default; |
| | 1 | 36 | | return false; |
| | | 37 | | } |
| | | 38 | | |
| | 871 | 39 | | return grid.Topology.Kind == GridTopologyKind.HexPrism |
| | 871 | 40 | | ? TryGetHexCandidateRange(grid, queryMin, queryMax, out minIndex, out maxIndex) |
| | 871 | 41 | | : TryGetRectangularCandidateRange(grid, queryMin, queryMax, out minIndex, out maxIndex); |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | internal static bool TryGetPrismCandidateRange( |
| | | 45 | | VoxelGrid grid, |
| | | 46 | | Vector3d queryMin, |
| | | 47 | | Vector3d queryMax, |
| | | 48 | | out VoxelIndex minIndex, |
| | | 49 | | out VoxelIndex maxIndex) |
| | | 50 | | { |
| | 194 | 51 | | GridTopologyMetrics metrics = grid.Topology.Metrics; |
| | | 52 | | Vector3d rangeMin; |
| | | 53 | | Vector3d rangeMax; |
| | 194 | 54 | | if (grid.Topology.Kind == GridTopologyKind.RectangularPrism) |
| | | 55 | | { |
| | 144 | 56 | | Vector3d halfExtents = new( |
| | 144 | 57 | | metrics.CellWidth * Fixed64.Half, |
| | 144 | 58 | | metrics.LayerHeight * Fixed64.Half, |
| | 144 | 59 | | metrics.CellLength * Fixed64.Half); |
| | 144 | 60 | | rangeMin = queryMin - halfExtents; |
| | 144 | 61 | | rangeMax = queryMax + halfExtents; |
| | | 62 | | } |
| | | 63 | | else |
| | | 64 | | { |
| | 50 | 65 | | Fixed64 halfHeight = metrics.LayerHeight * Fixed64.Half; |
| | 50 | 66 | | rangeMin = new Vector3d(queryMin.X, queryMin.Y - halfHeight, queryMin.Z); |
| | 50 | 67 | | rangeMax = new Vector3d(queryMax.X, queryMax.Y + halfHeight, queryMax.Z); |
| | | 68 | | } |
| | | 69 | | |
| | 194 | 70 | | return TryGetCandidateRange(grid, rangeMin, rangeMax, out minIndex, out maxIndex); |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | private static bool TryGetRectangularCandidateRange( |
| | | 74 | | VoxelGrid grid, |
| | | 75 | | Vector3d queryMin, |
| | | 76 | | Vector3d queryMax, |
| | | 77 | | out VoxelIndex minIndex, |
| | | 78 | | out VoxelIndex maxIndex) |
| | | 79 | | { |
| | 771 | 80 | | minIndex = default; |
| | 771 | 81 | | maxIndex = default; |
| | | 82 | | |
| | 771 | 83 | | (Vector3d snappedMin, Vector3d snappedMax) = grid.NormalizeBounds(queryMin, queryMax); |
| | 771 | 84 | | if (!TryClipBoundsToGrid(grid, snappedMin, snappedMax, out Vector3d clippedMin, out Vector3d clippedMax)) |
| | 4 | 85 | | return false; |
| | | 86 | | |
| | 767 | 87 | | bool minResolved = grid.TryGetVoxelIndex(clippedMin, out minIndex); |
| | 767 | 88 | | bool maxResolved = grid.TryGetVoxelIndex(clippedMax, out maxIndex); |
| | | 89 | | Debug.Assert(minResolved && maxResolved); |
| | 767 | 90 | | return true; |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | private static bool TryGetHexCandidateRange( |
| | | 94 | | VoxelGrid grid, |
| | | 95 | | Vector3d queryMin, |
| | | 96 | | Vector3d queryMax, |
| | | 97 | | out VoxelIndex minIndex, |
| | | 98 | | out VoxelIndex maxIndex) |
| | | 99 | | { |
| | 100 | 100 | | minIndex = default; |
| | 100 | 101 | | maxIndex = default; |
| | | 102 | | |
| | 100 | 103 | | GridTopologyMetrics metrics = grid.Topology.Metrics; |
| | 100 | 104 | | Fixed64 horizontalExpansion = metrics.CellRadius; |
| | 100 | 105 | | Fixed64 layerHeight = metrics.LayerHeight; |
| | 100 | 106 | | Vector3d candidateMin = new( |
| | 100 | 107 | | queryMin.X - horizontalExpansion, |
| | 100 | 108 | | queryMin.Y, |
| | 100 | 109 | | queryMin.Z - horizontalExpansion); |
| | 100 | 110 | | Vector3d candidateMax = new( |
| | 100 | 111 | | queryMax.X + horizontalExpansion, |
| | 100 | 112 | | queryMax.Y, |
| | 100 | 113 | | queryMax.Z + horizontalExpansion); |
| | | 114 | | |
| | 100 | 115 | | if (!TryClipBoundsToGrid(grid, candidateMin, candidateMax, out Vector3d clippedMin, out Vector3d clippedMax)) |
| | 4 | 116 | | return false; |
| | | 117 | | |
| | 96 | 118 | | Fixed64 qMin = Fixed64.MaxValue; |
| | 96 | 119 | | Fixed64 qMax = Fixed64.MinValue; |
| | 96 | 120 | | Fixed64 rMin = Fixed64.MaxValue; |
| | 96 | 121 | | Fixed64 rMax = Fixed64.MinValue; |
| | | 122 | | |
| | 96 | 123 | | IncludeHexAxialCorner(grid.BoundsMin, metrics, clippedMin.X, clippedMin.Z, ref qMin, ref qMax, ref rMin, ref rMa |
| | 96 | 124 | | IncludeHexAxialCorner(grid.BoundsMin, metrics, clippedMin.X, clippedMax.Z, ref qMin, ref qMax, ref rMin, ref rMa |
| | 96 | 125 | | IncludeHexAxialCorner(grid.BoundsMin, metrics, clippedMax.X, clippedMin.Z, ref qMin, ref qMax, ref rMin, ref rMa |
| | 96 | 126 | | IncludeHexAxialCorner(grid.BoundsMin, metrics, clippedMax.X, clippedMax.Z, ref qMin, ref qMax, ref rMin, ref rMa |
| | | 127 | | |
| | 96 | 128 | | int xMin = System.Math.Max(0, qMin.FloorToInt() - 1); |
| | 96 | 129 | | int xMax = System.Math.Min(grid.Width - 1, qMax.CeilToInt() + 1); |
| | 96 | 130 | | int zMin = System.Math.Max(0, rMin.FloorToInt() - 1); |
| | 96 | 131 | | int zMax = System.Math.Min(grid.Length - 1, rMax.CeilToInt() + 1); |
| | 96 | 132 | | int yMin = System.Math.Max( |
| | 96 | 133 | | 0, |
| | 96 | 134 | | ((clippedMin.Y - grid.BoundsMin.Y) / layerHeight).FloorToInt()); |
| | 96 | 135 | | int yMax = System.Math.Min( |
| | 96 | 136 | | grid.Height - 1, |
| | 96 | 137 | | ((clippedMax.Y - grid.BoundsMin.Y) / layerHeight).CeilToInt()); |
| | | 138 | | |
| | 96 | 139 | | minIndex = new VoxelIndex(xMin, yMin, zMin); |
| | 96 | 140 | | maxIndex = new VoxelIndex(xMax, yMax, zMax); |
| | 96 | 141 | | return true; |
| | | 142 | | } |
| | | 143 | | |
| | | 144 | | internal static bool TryClipBoundsToGrid( |
| | | 145 | | VoxelGrid grid, |
| | | 146 | | Vector3d min, |
| | | 147 | | Vector3d max, |
| | | 148 | | out Vector3d clippedMin, |
| | | 149 | | out Vector3d clippedMax) |
| | | 150 | | { |
| | 1463 | 151 | | Fixed64 xMin = FixedMath.Max(min.X, grid.BoundsMin.X); |
| | 1463 | 152 | | Fixed64 yMin = FixedMath.Max(min.Y, grid.BoundsMin.Y); |
| | 1463 | 153 | | Fixed64 zMin = FixedMath.Max(min.Z, grid.BoundsMin.Z); |
| | 1463 | 154 | | Fixed64 xMax = FixedMath.Min(max.X, grid.BoundsMax.X); |
| | 1463 | 155 | | Fixed64 yMax = FixedMath.Min(max.Y, grid.BoundsMax.Y); |
| | 1463 | 156 | | Fixed64 zMax = FixedMath.Min(max.Z, grid.BoundsMax.Z); |
| | | 157 | | |
| | 1463 | 158 | | if (xMin > xMax || yMin > yMax || zMin > zMax) |
| | | 159 | | { |
| | 9 | 160 | | clippedMin = default; |
| | 9 | 161 | | clippedMax = default; |
| | 9 | 162 | | return false; |
| | | 163 | | } |
| | | 164 | | |
| | 1454 | 165 | | clippedMin = new Vector3d(xMin, yMin, zMin); |
| | 1454 | 166 | | clippedMax = new Vector3d(xMax, yMax, zMax); |
| | 1454 | 167 | | return true; |
| | | 168 | | } |
| | | 169 | | |
| | | 170 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 171 | | private static void IncludeHexAxialCorner( |
| | | 172 | | Vector3d gridBoundsMin, |
| | | 173 | | GridTopologyMetrics metrics, |
| | | 174 | | Fixed64 x, |
| | | 175 | | Fixed64 z, |
| | | 176 | | ref Fixed64 qMin, |
| | | 177 | | ref Fixed64 qMax, |
| | | 178 | | ref Fixed64 rMin, |
| | | 179 | | ref Fixed64 rMax) |
| | | 180 | | { |
| | 384 | 181 | | HexCoordinateUtility.WorldOffsetToAxial( |
| | 384 | 182 | | x - gridBoundsMin.X, |
| | 384 | 183 | | z - gridBoundsMin.Z, |
| | 384 | 184 | | metrics, |
| | 384 | 185 | | out Fixed64 q, |
| | 384 | 186 | | out Fixed64 r); |
| | | 187 | | |
| | 384 | 188 | | qMin = FixedMath.Min(qMin, q); |
| | 384 | 189 | | qMax = FixedMath.Max(qMax, q); |
| | 384 | 190 | | rMin = FixedMath.Min(rMin, r); |
| | 384 | 191 | | rMax = FixedMath.Max(rMax, r); |
| | 384 | 192 | | } |
| | | 193 | | } |