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