| | | 1 | | //======================================================================= |
| | | 2 | | // GridNavigationCorridorValidationCursor.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; |
| | | 9 | | using FixedMathSharp; |
| | | 10 | | |
| | | 11 | | namespace GridForge.Grids.Topology; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Describes the state of a resumable navigation-corridor validation. |
| | | 15 | | /// </summary> |
| | | 16 | | public enum GridNavigationCorridorValidationStatus : byte |
| | | 17 | | { |
| | | 18 | | /// <summary>The input or corridor geometry is invalid.</summary> |
| | | 19 | | Invalid = 0, |
| | | 20 | | |
| | | 21 | | /// <summary>More bounded work is required.</summary> |
| | | 22 | | InProgress = 1, |
| | | 23 | | |
| | | 24 | | /// <summary>The corridor certificate is complete.</summary> |
| | | 25 | | Complete = 2, |
| | | 26 | | |
| | | 27 | | /// <summary>The canonical geometric cost is not representable.</summary> |
| | | 28 | | CostOverflow = 3 |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Resumably validates one deterministic navigation corridor into caller-owned storage. |
| | | 33 | | /// </summary> |
| | | 34 | | /// <remarks> |
| | | 35 | | /// The ordered cell and waypoint spans are not retained. Their lengths and contents must remain |
| | | 36 | | /// stable between calls. A successful corridor of N cells consumes exactly 2N+1 work units. |
| | | 37 | | /// Advance one work unit at a time and call <see cref="TryGetCurrentPortal"/> after each call to |
| | | 38 | | /// consume every portal certificate in order. |
| | | 39 | | /// </remarks> |
| | | 40 | | public struct GridNavigationCorridorValidationCursor |
| | | 41 | | { |
| | | 42 | | private enum ValidationStage : byte |
| | | 43 | | { |
| | | 44 | | Cells, |
| | | 45 | | EntryAnchor, |
| | | 46 | | Portals, |
| | | 47 | | ExitAnchor |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | private readonly int _cellCount; |
| | | 51 | | private readonly Vector3d _entryAnchor; |
| | | 52 | | private readonly Vector3d _exitAnchor; |
| | | 53 | | private readonly Fixed64 _radiusClearance; |
| | | 54 | | private readonly Fixed64 _heightClearance; |
| | | 55 | | private GridNavigationCorridorValidationStatus _status; |
| | | 56 | | private ValidationStage _stage; |
| | | 57 | | private bool _hasCurrentPortal; |
| | | 58 | | private int _cellIndex; |
| | | 59 | | private int _portalIndex; |
| | | 60 | | private int _portalWaypointCount; |
| | | 61 | | private Vector3d _previousPoint; |
| | | 62 | | private Fixed64 _geometricCost; |
| | | 63 | | private GridNavigationPortal _selectedPortal; |
| | | 64 | | |
| | | 65 | | /// <summary>The current validation state.</summary> |
| | 9105 | 66 | | public readonly GridNavigationCorridorValidationStatus Status => _status; |
| | | 67 | | |
| | | 68 | | /// <summary>The number of canonical waypoints written, or zero after failure.</summary> |
| | 24 | 69 | | public readonly int PortalWaypointCount => _portalWaypointCount; |
| | | 70 | | |
| | | 71 | | /// <summary>The checked canonical polyline length accumulated so far.</summary> |
| | 24 | 72 | | public readonly Fixed64 GeometricCost => _geometricCost; |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Attempts to get the portal certificate produced by the last completed work unit. |
| | | 76 | | /// </summary> |
| | | 77 | | /// <remarks> |
| | | 78 | | /// The value is available only when the final work unit completed by the most recent advance |
| | | 79 | | /// was successful portal work. Use a one-unit budget to consume every certificate in order. |
| | | 80 | | /// </remarks> |
| | | 81 | | /// <param name="portal">The produced portal, or <see langword="default"/> when unavailable.</param> |
| | | 82 | | /// <returns><see langword="true"/> when a portal certificate is available.</returns> |
| | | 83 | | public readonly bool TryGetCurrentPortal(out GridNavigationPortal portal) |
| | | 84 | | { |
| | 7058 | 85 | | portal = _hasCurrentPortal ? _selectedPortal : default; |
| | 7058 | 86 | | return _hasCurrentPortal; |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Creates a cursor for one ordered source, witness, and destination cell chain. |
| | | 91 | | /// </summary> |
| | | 92 | | /// <param name="cellCount">The stable number of ordered cells supplied to every advance.</param> |
| | | 93 | | /// <param name="entryAnchor">The source-cell foot anchor.</param> |
| | | 94 | | /// <param name="exitAnchor">The destination-cell foot anchor.</param> |
| | | 95 | | /// <param name="radiusClearance">The required nonnegative horizontal body radius.</param> |
| | | 96 | | /// <param name="heightClearance">The required positive body height.</param> |
| | | 97 | | public GridNavigationCorridorValidationCursor( |
| | | 98 | | int cellCount, |
| | | 99 | | Vector3d entryAnchor, |
| | | 100 | | Vector3d exitAnchor, |
| | | 101 | | Fixed64 radiusClearance, |
| | | 102 | | Fixed64 heightClearance) |
| | | 103 | | { |
| | 1057 | 104 | | _cellCount = cellCount; |
| | 1057 | 105 | | _entryAnchor = entryAnchor; |
| | 1057 | 106 | | _exitAnchor = exitAnchor; |
| | 1057 | 107 | | _radiusClearance = radiusClearance; |
| | 1057 | 108 | | _heightClearance = heightClearance; |
| | 1057 | 109 | | _status = cellCount >= 2 |
| | 1057 | 110 | | && radiusClearance >= Fixed64.Zero |
| | 1057 | 111 | | && heightClearance > Fixed64.Zero |
| | 1057 | 112 | | ? GridNavigationCorridorValidationStatus.InProgress |
| | 1057 | 113 | | : GridNavigationCorridorValidationStatus.Invalid; |
| | 1057 | 114 | | _stage = ValidationStage.Cells; |
| | 1057 | 115 | | _cellIndex = 0; |
| | 1057 | 116 | | _portalIndex = 0; |
| | 1057 | 117 | | _portalWaypointCount = 0; |
| | 1057 | 118 | | _previousPoint = entryAnchor; |
| | 1057 | 119 | | _geometricCost = default; |
| | 1057 | 120 | | _selectedPortal = default; |
| | 1057 | 121 | | _hasCurrentPortal = false; |
| | 1057 | 122 | | } |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// Performs at most <paramref name="maxWork"/> bounded validation units. |
| | | 126 | | /// </summary> |
| | | 127 | | /// <param name="orderedCells">The unchanged ordered cells supplied for this cursor.</param> |
| | | 128 | | /// <param name="portalWaypoints">Caller-owned storage for twice the portal count.</param> |
| | | 129 | | /// <param name="maxWork">The nonnegative maximum number of work units to perform.</param> |
| | | 130 | | /// <returns>The resulting validation state.</returns> |
| | | 131 | | public GridNavigationCorridorValidationStatus Advance( |
| | | 132 | | ReadOnlySpan<GridCellPrism> orderedCells, |
| | | 133 | | Span<Vector3d> portalWaypoints, |
| | | 134 | | int maxWork) |
| | | 135 | | { |
| | 7113 | 136 | | if (maxWork < 0) |
| | 1 | 137 | | throw new ArgumentOutOfRangeException(nameof(maxWork)); |
| | | 138 | | |
| | 7112 | 139 | | _hasCurrentPortal = false; |
| | 7112 | 140 | | if (_status != GridNavigationCorridorValidationStatus.InProgress) |
| | 2 | 141 | | return _status; |
| | | 142 | | |
| | 7110 | 143 | | if (orderedCells.Length != _cellCount |
| | 7110 | 144 | | || _cellCount - 1 > portalWaypoints.Length / 2) |
| | | 145 | | { |
| | 1 | 146 | | return Fail(GridNavigationCorridorValidationStatus.Invalid); |
| | | 147 | | } |
| | | 148 | | |
| | 14364 | 149 | | while (maxWork-- > 0 && _status == GridNavigationCorridorValidationStatus.InProgress) |
| | 7255 | 150 | | PerformNext(orderedCells, portalWaypoints); |
| | | 151 | | |
| | 7109 | 152 | | return _status; |
| | | 153 | | } |
| | | 154 | | |
| | | 155 | | private void PerformNext( |
| | | 156 | | ReadOnlySpan<GridCellPrism> orderedCells, |
| | | 157 | | Span<Vector3d> portalWaypoints) |
| | | 158 | | { |
| | 7255 | 159 | | _hasCurrentPortal = false; |
| | 7255 | 160 | | switch (_stage) |
| | | 161 | | { |
| | | 162 | | case ValidationStage.Cells: |
| | 3122 | 163 | | GridCellPrism cell = orderedCells[_cellIndex++]; |
| | 3122 | 164 | | if (cell.FootprintVertexCount is not 4 and not 6 |
| | 3122 | 165 | | || cell.VerticalMax < cell.VerticalMin) |
| | | 166 | | { |
| | 2 | 167 | | Fail(GridNavigationCorridorValidationStatus.Invalid); |
| | 2 | 168 | | return; |
| | | 169 | | } |
| | | 170 | | |
| | 3120 | 171 | | if (_cellIndex == _cellCount) |
| | 1049 | 172 | | _stage = ValidationStage.EntryAnchor; |
| | 3120 | 173 | | return; |
| | | 174 | | |
| | | 175 | | case ValidationStage.EntryAnchor: |
| | 1049 | 176 | | GridCellGeometry.TryCreateNavigationPortal( |
| | 1049 | 177 | | orderedCells[0], |
| | 1049 | 178 | | orderedCells[1], |
| | 1049 | 179 | | out _selectedPortal); |
| | 1049 | 180 | | if (!GridCellGeometry.IsNavigationBodyAnchorValid( |
| | 1049 | 181 | | orderedCells[0], |
| | 1049 | 182 | | _entryAnchor, |
| | 1049 | 183 | | _radiusClearance, |
| | 1049 | 184 | | _heightClearance, |
| | 1049 | 185 | | _selectedPortal)) |
| | | 186 | | { |
| | 10 | 187 | | Fail(GridNavigationCorridorValidationStatus.Invalid); |
| | 10 | 188 | | return; |
| | | 189 | | } |
| | | 190 | | |
| | 1039 | 191 | | _stage = ValidationStage.Portals; |
| | 1039 | 192 | | return; |
| | | 193 | | |
| | | 194 | | case ValidationStage.Portals: |
| | 2054 | 195 | | ValidateNextPortal(orderedCells, portalWaypoints); |
| | 2054 | 196 | | return; |
| | | 197 | | |
| | | 198 | | default: |
| | 1030 | 199 | | ValidateExitAnchor(orderedCells); |
| | 1030 | 200 | | return; |
| | | 201 | | } |
| | | 202 | | } |
| | | 203 | | |
| | | 204 | | private void ValidateNextPortal( |
| | | 205 | | ReadOnlySpan<GridCellPrism> orderedCells, |
| | | 206 | | Span<Vector3d> portalWaypoints) |
| | | 207 | | { |
| | 2054 | 208 | | GridCellPrism source = orderedCells[_portalIndex]; |
| | 2054 | 209 | | GridCellPrism target = orderedCells[_portalIndex + 1]; |
| | 2054 | 210 | | GridNavigationPortal incomingPortal = _portalIndex == 0 |
| | 2054 | 211 | | ? default |
| | 2054 | 212 | | : _selectedPortal; |
| | 2054 | 213 | | GridNavigationPortal outgoingPortal = _selectedPortal; |
| | 2054 | 214 | | if ((_portalIndex > 0 |
| | 2054 | 215 | | && !GridCellGeometry.TryCreateNavigationPortal(source, target, out outgoingPortal)) |
| | 2054 | 216 | | || !outgoingPortal.IsValid |
| | 2054 | 217 | | || !outgoingPortal.TryResolveProfile( |
| | 2054 | 218 | | _radiusClearance, |
| | 2054 | 219 | | _heightClearance, |
| | 2054 | 220 | | out Vector3d sourcePoint, |
| | 2054 | 221 | | out Vector3d targetPoint) |
| | 2054 | 222 | | || !GridCellGeometry.IsNavigationBodySegmentValid( |
| | 2054 | 223 | | source, |
| | 2054 | 224 | | _previousPoint, |
| | 2054 | 225 | | sourcePoint, |
| | 2054 | 226 | | _radiusClearance, |
| | 2054 | 227 | | _heightClearance, |
| | 2054 | 228 | | incomingPortal, |
| | 2054 | 229 | | outgoingPortal, |
| | 2054 | 230 | | GridNavigationBodySegmentEndpointAllowance.None)) |
| | | 231 | | { |
| | 7 | 232 | | Fail(GridNavigationCorridorValidationStatus.Invalid); |
| | 7 | 233 | | return; |
| | | 234 | | } |
| | | 235 | | |
| | 2047 | 236 | | portalWaypoints[_portalWaypointCount++] = sourcePoint; |
| | 2047 | 237 | | if (!TryAccumulateDistance(_previousPoint, sourcePoint)) |
| | | 238 | | { |
| | 1 | 239 | | Fail(GridNavigationCorridorValidationStatus.CostOverflow); |
| | 1 | 240 | | return; |
| | | 241 | | } |
| | | 242 | | |
| | 2046 | 243 | | _previousPoint = sourcePoint; |
| | 2046 | 244 | | if (outgoingPortal.FaceKind == VoxelContactFaceKind.Horizontal) |
| | | 245 | | { |
| | 4 | 246 | | portalWaypoints[_portalWaypointCount++] = targetPoint; |
| | 4 | 247 | | if (!TryAccumulateDistance(_previousPoint, targetPoint)) |
| | | 248 | | { |
| | 1 | 249 | | Fail(GridNavigationCorridorValidationStatus.CostOverflow); |
| | 1 | 250 | | return; |
| | | 251 | | } |
| | | 252 | | |
| | 3 | 253 | | _previousPoint = targetPoint; |
| | | 254 | | } |
| | | 255 | | |
| | 2045 | 256 | | _selectedPortal = outgoingPortal; |
| | 2045 | 257 | | _hasCurrentPortal = true; |
| | 2045 | 258 | | _portalIndex++; |
| | 2045 | 259 | | if (_portalIndex == _cellCount - 1) |
| | 1030 | 260 | | _stage = ValidationStage.ExitAnchor; |
| | 2045 | 261 | | } |
| | | 262 | | |
| | | 263 | | private void ValidateExitAnchor(ReadOnlySpan<GridCellPrism> orderedCells) |
| | | 264 | | { |
| | 1030 | 265 | | if (!TryAccumulateDistance(_previousPoint, _exitAnchor)) |
| | | 266 | | { |
| | 1 | 267 | | Fail(GridNavigationCorridorValidationStatus.CostOverflow); |
| | 1 | 268 | | return; |
| | | 269 | | } |
| | | 270 | | |
| | 1029 | 271 | | if (!GridCellGeometry.IsNavigationBodySegmentValid( |
| | 1029 | 272 | | orderedCells[_cellCount - 1], |
| | 1029 | 273 | | _previousPoint, |
| | 1029 | 274 | | _exitAnchor, |
| | 1029 | 275 | | _radiusClearance, |
| | 1029 | 276 | | _heightClearance, |
| | 1029 | 277 | | _selectedPortal, |
| | 1029 | 278 | | default, |
| | 1029 | 279 | | GridNavigationBodySegmentEndpointAllowance.None)) |
| | | 280 | | { |
| | 3 | 281 | | Fail(GridNavigationCorridorValidationStatus.Invalid); |
| | 3 | 282 | | return; |
| | | 283 | | } |
| | | 284 | | |
| | 1026 | 285 | | _status = GridNavigationCorridorValidationStatus.Complete; |
| | 1026 | 286 | | } |
| | | 287 | | |
| | | 288 | | private GridNavigationCorridorValidationStatus Fail( |
| | | 289 | | GridNavigationCorridorValidationStatus status) |
| | | 290 | | { |
| | 26 | 291 | | _portalWaypointCount = 0; |
| | 26 | 292 | | _geometricCost = default; |
| | 26 | 293 | | _hasCurrentPortal = false; |
| | 26 | 294 | | _status = status; |
| | 26 | 295 | | return status; |
| | | 296 | | } |
| | | 297 | | |
| | | 298 | | private bool TryAccumulateDistance(Vector3d start, Vector3d end) |
| | | 299 | | { |
| | 3081 | 300 | | return Vector3d.TryGetDistance(start, end, out Fixed64 distance) |
| | 3081 | 301 | | && Fixed64.TryAdd(_geometricCost, distance, out _geometricCost); |
| | | 302 | | } |
| | | 303 | | |
| | | 304 | | } |