| | | 1 | | //======================================================================= |
| | | 2 | | // GridDiagnosticGeometry.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.Grids.Topology; |
| | | 10 | | using GridForge.Spatial; |
| | | 11 | | using System; |
| | | 12 | | using System.Runtime.CompilerServices; |
| | | 13 | | |
| | | 14 | | namespace GridForge.Diagnostics; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Topology-aware diagnostic geometry helpers for cell descriptors. |
| | | 18 | | /// </summary> |
| | | 19 | | public static class GridDiagnosticGeometry |
| | | 20 | | { |
| | | 21 | | /// <summary> |
| | | 22 | | /// Number of vertices in a rectangular-prism diagnostic cell. |
| | | 23 | | /// </summary> |
| | | 24 | | public const int RectangularPrismVertexCount = 8; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Number of vertices in a hex-prism diagnostic cell. |
| | | 28 | | /// </summary> |
| | | 29 | | public const int HexPrismVertexCount = 12; |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Number of wireframe edges in a rectangular-prism diagnostic cell. |
| | | 33 | | /// </summary> |
| | | 34 | | public const int RectangularPrismEdgeCount = 12; |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Number of wireframe edges in a hex-prism diagnostic cell. |
| | | 38 | | /// </summary> |
| | | 39 | | public const int HexPrismEdgeCount = 18; |
| | | 40 | | |
| | 1 | 41 | | private static readonly GridDiagnosticEdge[] RectangularPrismEdges = |
| | 1 | 42 | | { |
| | 1 | 43 | | new GridDiagnosticEdge(0, 1), |
| | 1 | 44 | | new GridDiagnosticEdge(1, 2), |
| | 1 | 45 | | new GridDiagnosticEdge(2, 3), |
| | 1 | 46 | | new GridDiagnosticEdge(3, 0), |
| | 1 | 47 | | new GridDiagnosticEdge(4, 5), |
| | 1 | 48 | | new GridDiagnosticEdge(5, 6), |
| | 1 | 49 | | new GridDiagnosticEdge(6, 7), |
| | 1 | 50 | | new GridDiagnosticEdge(7, 4), |
| | 1 | 51 | | new GridDiagnosticEdge(0, 4), |
| | 1 | 52 | | new GridDiagnosticEdge(1, 5), |
| | 1 | 53 | | new GridDiagnosticEdge(2, 6), |
| | 1 | 54 | | new GridDiagnosticEdge(3, 7) |
| | 1 | 55 | | }; |
| | | 56 | | |
| | 1 | 57 | | private static readonly GridDiagnosticEdge[] HexPrismEdges = |
| | 1 | 58 | | { |
| | 1 | 59 | | new GridDiagnosticEdge(0, 1), |
| | 1 | 60 | | new GridDiagnosticEdge(1, 2), |
| | 1 | 61 | | new GridDiagnosticEdge(2, 3), |
| | 1 | 62 | | new GridDiagnosticEdge(3, 4), |
| | 1 | 63 | | new GridDiagnosticEdge(4, 5), |
| | 1 | 64 | | new GridDiagnosticEdge(5, 0), |
| | 1 | 65 | | new GridDiagnosticEdge(6, 7), |
| | 1 | 66 | | new GridDiagnosticEdge(7, 8), |
| | 1 | 67 | | new GridDiagnosticEdge(8, 9), |
| | 1 | 68 | | new GridDiagnosticEdge(9, 10), |
| | 1 | 69 | | new GridDiagnosticEdge(10, 11), |
| | 1 | 70 | | new GridDiagnosticEdge(11, 6), |
| | 1 | 71 | | new GridDiagnosticEdge(0, 6), |
| | 1 | 72 | | new GridDiagnosticEdge(1, 7), |
| | 1 | 73 | | new GridDiagnosticEdge(2, 8), |
| | 1 | 74 | | new GridDiagnosticEdge(3, 9), |
| | 1 | 75 | | new GridDiagnosticEdge(4, 10), |
| | 1 | 76 | | new GridDiagnosticEdge(5, 11) |
| | 1 | 77 | | }; |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Gets the number of vertices written for the supplied topology kind. |
| | | 81 | | /// </summary> |
| | | 82 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 83 | | public static int GetVertexCount(GridTopologyKind topologyKind) => topologyKind switch |
| | 2 | 84 | | { |
| | 1 | 85 | | GridTopologyKind.RectangularPrism => RectangularPrismVertexCount, |
| | 1 | 86 | | GridTopologyKind.HexPrism => HexPrismVertexCount, |
| | 0 | 87 | | _ => 0 |
| | 2 | 88 | | }; |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Gets the number of edges exposed for the supplied topology kind. |
| | | 92 | | /// </summary> |
| | | 93 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 94 | | public static int GetEdgeCount(GridTopologyKind topologyKind) => topologyKind switch |
| | 2 | 95 | | { |
| | 1 | 96 | | GridTopologyKind.RectangularPrism => RectangularPrismEdgeCount, |
| | 1 | 97 | | GridTopologyKind.HexPrism => HexPrismEdgeCount, |
| | 0 | 98 | | _ => 0 |
| | 2 | 99 | | }; |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Writes topology-aware world-space cell vertices into caller-owned |
| | | 103 | | /// storage. |
| | | 104 | | /// </summary> |
| | | 105 | | /// <returns>The number of vertices written, or <c>0</c> when the span is too small.</returns> |
| | | 106 | | public static int WriteVertices( |
| | | 107 | | in GridDiagnosticCell cell, |
| | 7 | 108 | | Span<Vector3d> vertices) => cell.TopologyKind switch |
| | 7 | 109 | | { |
| | 2 | 110 | | GridTopologyKind.RectangularPrism => WriteRectangularVertices(cell.WorldPosition, cell.TopologyMetrics, vert |
| | 5 | 111 | | GridTopologyKind.HexPrism => WriteHexVertices(cell.WorldPosition, cell.TopologyMetrics, vertices), |
| | 0 | 112 | | _ => 0 |
| | 7 | 113 | | }; |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Gets immutable edge topology for the supplied cell topology kind. |
| | | 117 | | /// </summary> |
| | | 118 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 3 | 119 | | public static ReadOnlySpan<GridDiagnosticEdge> GetEdges(GridTopologyKind topologyKind) => topologyKind switch |
| | 3 | 120 | | { |
| | 2 | 121 | | GridTopologyKind.RectangularPrism => RectangularPrismEdges, |
| | 1 | 122 | | GridTopologyKind.HexPrism => HexPrismEdges, |
| | 0 | 123 | | _ => ReadOnlySpan<GridDiagnosticEdge>.Empty |
| | 3 | 124 | | }; |
| | | 125 | | |
| | | 126 | | private static int WriteRectangularVertices( |
| | | 127 | | Vector3d center, |
| | | 128 | | GridTopologyMetrics metrics, |
| | | 129 | | Span<Vector3d> vertices) |
| | | 130 | | { |
| | 2 | 131 | | if (vertices.Length < RectangularPrismVertexCount) |
| | 1 | 132 | | return 0; |
| | | 133 | | |
| | 1 | 134 | | Fixed64 halfX = metrics.CellWidth * Fixed64.Half; |
| | 1 | 135 | | Fixed64 halfY = metrics.LayerHeight * Fixed64.Half; |
| | 1 | 136 | | Fixed64 halfZ = metrics.CellLength * Fixed64.Half; |
| | 1 | 137 | | Fixed64 minX = center.X - halfX; |
| | 1 | 138 | | Fixed64 maxX = center.X + halfX; |
| | 1 | 139 | | Fixed64 minY = center.Y - halfY; |
| | 1 | 140 | | Fixed64 maxY = center.Y + halfY; |
| | 1 | 141 | | Fixed64 minZ = center.Z - halfZ; |
| | 1 | 142 | | Fixed64 maxZ = center.Z + halfZ; |
| | | 143 | | |
| | 1 | 144 | | vertices[0] = new Vector3d(minX, minY, minZ); |
| | 1 | 145 | | vertices[1] = new Vector3d(maxX, minY, minZ); |
| | 1 | 146 | | vertices[2] = new Vector3d(maxX, minY, maxZ); |
| | 1 | 147 | | vertices[3] = new Vector3d(minX, minY, maxZ); |
| | 1 | 148 | | vertices[4] = new Vector3d(minX, maxY, minZ); |
| | 1 | 149 | | vertices[5] = new Vector3d(maxX, maxY, minZ); |
| | 1 | 150 | | vertices[6] = new Vector3d(maxX, maxY, maxZ); |
| | 1 | 151 | | vertices[7] = new Vector3d(minX, maxY, maxZ); |
| | | 152 | | |
| | 1 | 153 | | return RectangularPrismVertexCount; |
| | | 154 | | } |
| | | 155 | | |
| | | 156 | | private static int WriteHexVertices( |
| | | 157 | | Vector3d center, |
| | | 158 | | GridTopologyMetrics metrics, |
| | | 159 | | Span<Vector3d> vertices) |
| | | 160 | | { |
| | 5 | 161 | | if (vertices.Length < HexPrismVertexCount) |
| | 1 | 162 | | return 0; |
| | | 163 | | |
| | 4 | 164 | | Fixed64 halfY = metrics.LayerHeight * Fixed64.Half; |
| | 4 | 165 | | Fixed64 bottomY = center.Y - halfY; |
| | 4 | 166 | | Fixed64 topY = center.Y + halfY; |
| | | 167 | | |
| | 4 | 168 | | if (metrics.HexOrientation == HexOrientation.FlatTop) |
| | | 169 | | { |
| | 3 | 170 | | WriteFlatTopHexRing(center.X, bottomY, center.Z, metrics.CellRadius, vertices); |
| | 3 | 171 | | WriteFlatTopHexRing(center.X, topY, center.Z, metrics.CellRadius, vertices.Slice(6)); |
| | 3 | 172 | | return HexPrismVertexCount; |
| | | 173 | | } |
| | | 174 | | |
| | 1 | 175 | | WritePointyTopHexRing(center.X, bottomY, center.Z, metrics.CellRadius, vertices); |
| | 1 | 176 | | WritePointyTopHexRing(center.X, topY, center.Z, metrics.CellRadius, vertices.Slice(6)); |
| | 1 | 177 | | return HexPrismVertexCount; |
| | | 178 | | } |
| | | 179 | | |
| | | 180 | | private static void WritePointyTopHexRing( |
| | | 181 | | Fixed64 centerX, |
| | | 182 | | Fixed64 y, |
| | | 183 | | Fixed64 centerZ, |
| | | 184 | | Fixed64 radius, |
| | | 185 | | Span<Vector3d> vertices) |
| | | 186 | | { |
| | 2 | 187 | | Fixed64 halfRadius = radius * Fixed64.Half; |
| | 2 | 188 | | Fixed64 halfWidth = HexCoordinateUtility.Sqrt3 * radius * Fixed64.Half; |
| | | 189 | | |
| | 2 | 190 | | vertices[0] = new Vector3d(centerX + halfWidth, y, centerZ + halfRadius); |
| | 2 | 191 | | vertices[1] = new Vector3d(centerX, y, centerZ + radius); |
| | 2 | 192 | | vertices[2] = new Vector3d(centerX - halfWidth, y, centerZ + halfRadius); |
| | 2 | 193 | | vertices[3] = new Vector3d(centerX - halfWidth, y, centerZ - halfRadius); |
| | 2 | 194 | | vertices[4] = new Vector3d(centerX, y, centerZ - radius); |
| | 2 | 195 | | vertices[5] = new Vector3d(centerX + halfWidth, y, centerZ - halfRadius); |
| | 2 | 196 | | } |
| | | 197 | | |
| | | 198 | | private static void WriteFlatTopHexRing( |
| | | 199 | | Fixed64 centerX, |
| | | 200 | | Fixed64 y, |
| | | 201 | | Fixed64 centerZ, |
| | | 202 | | Fixed64 radius, |
| | | 203 | | Span<Vector3d> vertices) |
| | | 204 | | { |
| | 6 | 205 | | Fixed64 halfRadius = radius * Fixed64.Half; |
| | 6 | 206 | | Fixed64 halfWidth = HexCoordinateUtility.Sqrt3 * radius * Fixed64.Half; |
| | | 207 | | |
| | 6 | 208 | | vertices[0] = new Vector3d(centerX + radius, y, centerZ); |
| | 6 | 209 | | vertices[1] = new Vector3d(centerX + halfRadius, y, centerZ + halfWidth); |
| | 6 | 210 | | vertices[2] = new Vector3d(centerX - halfRadius, y, centerZ + halfWidth); |
| | 6 | 211 | | vertices[3] = new Vector3d(centerX - radius, y, centerZ); |
| | 6 | 212 | | vertices[4] = new Vector3d(centerX - halfRadius, y, centerZ - halfWidth); |
| | 6 | 213 | | vertices[5] = new Vector3d(centerX + halfRadius, y, centerZ - halfWidth); |
| | 6 | 214 | | } |
| | | 215 | | } |