| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationMaterializedComponentWork.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 GridForge.Grids; |
| | | 9 | | using GridForge.Spatial; |
| | | 10 | | |
| | | 11 | | namespace Trailblazer.Pathing; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Captures exact components incident to newly materialized medium states, then delegates the |
| | | 15 | | /// bounded rebuild to the ordinary component builder. |
| | | 16 | | /// </summary> |
| | | 17 | | internal sealed class NavigationMaterializedComponentWork |
| | | 18 | | { |
| | | 19 | | internal const long BaseRetainedBytes = 3_856L; |
| | | 20 | | |
| | | 21 | | private readonly NavigationWorldGraph _candidate; |
| | | 22 | | private readonly GridWorld? _world; |
| | | 23 | | private readonly NavigationGridBaselineCapture[]? _baselineCaptures; |
| | | 24 | | private readonly int[]? _affectedMapOrdinals; |
| | | 25 | | private readonly int _affectedMapCount; |
| | | 26 | | private readonly GridEventInfo[]? _events; |
| | | 27 | | private readonly int _eventCount; |
| | | 28 | | private readonly NavigationOperationCandidate? _transitionCandidate; |
| | | 29 | | private readonly NavigationSurfaceComponentKeySet _initialAffectedKeys; |
| | | 30 | | private readonly NavigationCellAddressSet _initialSeeds; |
| | | 31 | | private readonly int _initialAffectedMemberCount; |
| | | 32 | | private NavigationSurfaceComponentKeySet _changedStates; |
| | 2900 | 33 | | private PersistentStringMap<bool> _seamDiscoveryMaps = PersistentStringMap<bool>.Empty; |
| | 2900 | 34 | | private PersistentStringMap<bool> _transitionChangedMaps = PersistentStringMap<bool>.Empty; |
| | | 35 | | private NavigationTransitionRefreshWork? _transitionRefresh; |
| | | 36 | | private NavigationWorldGraph? _transitionGraph; |
| | | 37 | | private NavigationAutomaticSeamRefreshWork? _seamRefresh; |
| | 2900 | 38 | | private NavigationSurfaceComponentKeySet _affectedKeys = |
| | 2900 | 39 | | NavigationSurfaceComponentKeySet.Empty; |
| | 2900 | 40 | | private NavigationCellAddressSet _seeds = NavigationCellAddressSet.Empty; |
| | | 41 | | private NavigationSurfaceComponentBuildWork? _build; |
| | | 42 | | private NavigationWorldGraph? _componentGraph; |
| | | 43 | | private NavigationSurfaceComponentKeySet.Enumerator _changedStateEnumerator; |
| | | 44 | | private NavigationSurfaceComponentKey _state; |
| | | 45 | | private NavigationCellAddress? _pendingNeighbor; |
| | | 46 | | private NavigationSurfaceEdgeEnumerator _outgoing; |
| | | 47 | | private NavigationIncomingSurfaceEdgeEnumerator _incoming; |
| | | 48 | | private NavigationAutomaticSeamIndex.EndpointEnumerator _volumeSeams; |
| | | 49 | | private NavigationMediumStateRef _volumeSource; |
| | | 50 | | private int _stateOrdinal; |
| | | 51 | | private int _statePhase; |
| | | 52 | | private int _primaryOrdinal; |
| | | 53 | | private int _primaryCount; |
| | | 54 | | private int _affectedMemberCount; |
| | | 55 | | private int _frontAffectedOrdinal; |
| | | 56 | | private int _frontStateOrdinal; |
| | | 57 | | private int _frontEventOrdinal; |
| | 2900 | 58 | | private long _seamRevision = -1; |
| | | 59 | | private NavigationSurfaceComponentKeySet.Enumerator _frontStateEnumerator; |
| | | 60 | | private bool _frontMapActive; |
| | | 61 | | private bool _frontComplete; |
| | | 62 | | private bool _hasNoChanges; |
| | | 63 | | private bool _outgoingComplete; |
| | | 64 | | private bool _requiresSnapshotRestart; |
| | | 65 | | |
| | 2900 | 66 | | internal NavigationMaterializedComponentWork( |
| | 2900 | 67 | | NavigationWorldGraph candidate, |
| | 2900 | 68 | | NavigationSurfaceComponentKeySet changedStates, |
| | 2900 | 69 | | NavigationSurfaceComponentKeySet affectedKeys, |
| | 2900 | 70 | | NavigationCellAddressSet seeds, |
| | 2900 | 71 | | int affectedMemberCount, |
| | 2900 | 72 | | GridWorld? world, |
| | 2900 | 73 | | NavigationGridBaselineCapture[]? baselineCaptures, |
| | 2900 | 74 | | int[]? affectedMapOrdinals, |
| | 2900 | 75 | | int affectedMapCount, |
| | 2900 | 76 | | GridEventInfo[]? events, |
| | 2900 | 77 | | int eventCount, |
| | 2900 | 78 | | NavigationOperationCandidate? transitionCandidate = null) |
| | | 79 | | { |
| | 2900 | 80 | | _candidate = candidate; |
| | 2900 | 81 | | _world = world; |
| | 2900 | 82 | | _baselineCaptures = baselineCaptures; |
| | 2900 | 83 | | _affectedMapOrdinals = affectedMapOrdinals; |
| | 2900 | 84 | | _affectedMapCount = affectedMapCount; |
| | 2900 | 85 | | _events = events; |
| | 2900 | 86 | | _eventCount = eventCount; |
| | 2900 | 87 | | _transitionCandidate = transitionCandidate; |
| | 2900 | 88 | | _changedStates = changedStates; |
| | 2900 | 89 | | _affectedKeys = affectedKeys; |
| | 2900 | 90 | | _seeds = seeds; |
| | 2900 | 91 | | _affectedMemberCount = affectedMemberCount; |
| | 2900 | 92 | | _initialAffectedKeys = affectedKeys; |
| | 2900 | 93 | | _initialSeeds = seeds; |
| | 2900 | 94 | | _initialAffectedMemberCount = affectedMemberCount; |
| | 2900 | 95 | | _frontComplete = world == null; |
| | 2900 | 96 | | if (_frontComplete) |
| | | 97 | | { |
| | 1051 | 98 | | _changedStateEnumerator = changedStates.GetEnumerator(); |
| | 1051 | 99 | | _componentGraph = candidate; |
| | 1051 | 100 | | _hasNoChanges = changedStates.Count == 0 |
| | 1051 | 101 | | && affectedKeys.Count == 0 |
| | 1051 | 102 | | && seeds.Count == 0; |
| | | 103 | | } |
| | 2900 | 104 | | } |
| | | 105 | | |
| | 71 | 106 | | internal bool IsComplete => IsLifecycleComplete( |
| | 71 | 107 | | _frontComplete, |
| | 71 | 108 | | _transitionRefresh?.IsComplete, |
| | 71 | 109 | | _hasNoChanges, |
| | 71 | 110 | | _seamRefresh?.IsComplete, |
| | 71 | 111 | | _build?.IsComplete); |
| | | 112 | | |
| | | 113 | | internal static bool IsLifecycleComplete( |
| | | 114 | | bool frontComplete, |
| | | 115 | | bool? transitionRefreshComplete, |
| | | 116 | | bool hasNoChanges, |
| | | 117 | | bool? seamRefreshComplete, |
| | 78 | 118 | | bool? buildComplete) => frontComplete |
| | 78 | 119 | | && transitionRefreshComplete != false |
| | 78 | 120 | | && (hasNoChanges |
| | 78 | 121 | | || (seamRefreshComplete != false && buildComplete == true)); |
| | | 122 | | |
| | | 123 | | internal NavigationWorldGraph Result |
| | | 124 | | { |
| | | 125 | | get |
| | | 126 | | { |
| | | 127 | | System.Diagnostics.Debug.Assert(_componentGraph != null, |
| | | 128 | | "Completed materialized work owns an explicit component graph."); |
| | 2827 | 129 | | return _build == null |
| | 2827 | 130 | | ? _componentGraph! |
| | 2827 | 131 | | : _componentGraph!.WithSurfaceComponents(_build.Result); |
| | | 132 | | } |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | internal bool RevalidateForPublication() |
| | | 136 | | { |
| | 2037 | 137 | | if (_seamRefresh == null) |
| | 2014 | 138 | | return true; |
| | 23 | 139 | | long revision = _seamRefresh.Revision; |
| | 23 | 140 | | if (_seamRefresh.RevalidateForPublication()) |
| | 22 | 141 | | return true; |
| | | 142 | | System.Diagnostics.Debug.Assert(_seamRefresh.Revision != revision, |
| | | 143 | | "Failed seam revalidation resets the refresh and advances its revision."); |
| | 1 | 144 | | ResetComponentState(); |
| | 1 | 145 | | _requiresSnapshotRestart = true; |
| | 1 | 146 | | return false; |
| | | 147 | | } |
| | | 148 | | |
| | 7447 | 149 | | internal bool RequiresSnapshotRestart => _requiresSnapshotRestart; |
| | | 150 | | |
| | 13 | 151 | | internal int RetainedEventCount => _eventCount; |
| | | 152 | | |
| | 13209 | 153 | | internal long RetainedBytes => checked( |
| | 13209 | 154 | | BaseRetainedBytes |
| | 13209 | 155 | | + _candidate.RetainedBytes |
| | 13209 | 156 | | + (_transitionGraph == null || ReferenceEquals(_transitionGraph, _candidate) |
| | 13209 | 157 | | ? 0L |
| | 13209 | 158 | | : NavigationWorldGraph.BaseRetainedBytes) |
| | 13209 | 159 | | + (_componentGraph == null |
| | 13209 | 160 | | || ReferenceEquals(_componentGraph, _candidate) |
| | 13209 | 161 | | || ReferenceEquals(_componentGraph, _transitionGraph) |
| | 13209 | 162 | | ? 0L |
| | 13209 | 163 | | : NavigationWorldGraph.BaseRetainedBytes) |
| | 13209 | 164 | | + _changedStates.RetainedBytes |
| | 13209 | 165 | | + (_seamRefresh == null ? _seamDiscoveryMaps.RetainedBytes : 0L) |
| | 13209 | 166 | | + (_world == null ? 0L : _transitionChangedMaps.RetainedBytes) |
| | 13209 | 167 | | + (_transitionRefresh?.RetainedBytes ?? 0L) |
| | 13209 | 168 | | + _affectedKeys.RetainedBytes |
| | 13209 | 169 | | + (ReferenceEquals(_initialAffectedKeys, _affectedKeys) |
| | 13209 | 170 | | ? 0L |
| | 13209 | 171 | | : _initialAffectedKeys.RetainedBytes) |
| | 13209 | 172 | | + _seeds.RetainedBytes |
| | 13209 | 173 | | + (ReferenceEquals(_initialSeeds, _seeds) ? 0L : _initialSeeds.RetainedBytes) |
| | 13209 | 174 | | + (_seamRefresh?.RetainedBytes ?? 0L) |
| | 13209 | 175 | | + (_build?.RetainedBytes ?? 0L)); |
| | | 176 | | |
| | 13460 | 177 | | internal int PersistentPageCount => checked( |
| | 13460 | 178 | | 1 |
| | 13460 | 179 | | + _candidate.PersistentPageCount |
| | 13460 | 180 | | + _changedStates.PersistentPageCount |
| | 13460 | 181 | | + (_seamRefresh == null ? 1 + _seamDiscoveryMaps.PersistentNodeCount : 0) |
| | 13460 | 182 | | + (_world == null ? 0 : 1 + _transitionChangedMaps.PersistentNodeCount) |
| | 13460 | 183 | | + (_transitionRefresh?.PersistentPageCount ?? 0) |
| | 13460 | 184 | | + _affectedKeys.PersistentPageCount |
| | 13460 | 185 | | + (ReferenceEquals(_initialAffectedKeys, _affectedKeys) |
| | 13460 | 186 | | ? 0 |
| | 13460 | 187 | | : _initialAffectedKeys.PersistentPageCount) |
| | 13460 | 188 | | + _seeds.PersistentPageCount |
| | 13460 | 189 | | + (ReferenceEquals(_initialSeeds, _seeds) |
| | 13460 | 190 | | ? 0 |
| | 13460 | 191 | | : _initialSeeds.PersistentPageCount) |
| | 13460 | 192 | | + (_seamRefresh?.PersistentPageCount ?? 0) |
| | 13460 | 193 | | + (_build?.PersistentPageCount ?? 0)); |
| | | 194 | | |
| | | 195 | | internal bool Advance(MaintenanceWorkMeter meter) |
| | | 196 | | { |
| | 7755 | 197 | | if (!_frontComplete && !AdvanceFront(meter)) |
| | 268 | 198 | | return false; |
| | 7487 | 199 | | if (_world != null) |
| | | 200 | | { |
| | 5333 | 201 | | _transitionRefresh ??= new NavigationTransitionRefreshWork( |
| | 5333 | 202 | | _candidate, |
| | 5333 | 203 | | _candidate, |
| | 5333 | 204 | | _transitionCandidate, |
| | 5333 | 205 | | _transitionChangedMaps, |
| | 5333 | 206 | | rebuildRules: false, |
| | 5333 | 207 | | _candidate.GraphVersion); |
| | 5333 | 208 | | if (!_transitionRefresh.Advance(meter)) |
| | 749 | 209 | | return false; |
| | 4584 | 210 | | if (_transitionGraph == null) |
| | | 211 | | { |
| | 1846 | 212 | | _transitionGraph = _candidate.WithTransitionPublication( |
| | 1846 | 213 | | _transitionRefresh.Pages, |
| | 1846 | 214 | | _transitionRefresh.Rules); |
| | | 215 | | } |
| | 4584 | 216 | | if (_seamRefresh == null) |
| | 4432 | 217 | | _componentGraph = _transitionGraph; |
| | | 218 | | } |
| | 6738 | 219 | | if (_hasNoChanges) |
| | 1989 | 220 | | return true; |
| | 4749 | 221 | | if (_seamRefresh != null) |
| | | 222 | | { |
| | 152 | 223 | | if (_seamRevision != _seamRefresh.Revision) |
| | 22 | 224 | | ResetComponentState(); |
| | 400 | 225 | | while (!_seamRefresh.IsComplete) |
| | | 226 | | { |
| | 264 | 227 | | NavigationAutomaticSeamRefreshWork.SeamAdvanceStatus seamStatus = |
| | 264 | 228 | | _seamRefresh.AdvanceOne(meter); |
| | 264 | 229 | | if (_seamRevision != _seamRefresh.Revision) |
| | | 230 | | { |
| | 1 | 231 | | ResetComponentState(); |
| | 1 | 232 | | _requiresSnapshotRestart = true; |
| | 1 | 233 | | return false; |
| | | 234 | | } |
| | 263 | 235 | | if (seamStatus == NavigationAutomaticSeamRefreshWork.SeamAdvanceStatus.Blocked) |
| | 15 | 236 | | return false; |
| | | 237 | | } |
| | 136 | 238 | | if (_componentGraph == null) |
| | | 239 | | { |
| | 21 | 240 | | _seamRevision = _seamRefresh.Revision; |
| | | 241 | | System.Diagnostics.Debug.Assert(_transitionGraph != null, |
| | | 242 | | "Seam refresh follows transition publication for materialized work."); |
| | 21 | 243 | | _componentGraph = _transitionGraph! |
| | 21 | 244 | | .WithAutomaticSeams(_seamRefresh.Result); |
| | | 245 | | } |
| | | 246 | | } |
| | 4733 | 247 | | if (_build != null) |
| | 2382 | 248 | | return _build.Advance(meter); |
| | 2351 | 249 | | if (!AdvanceAffectedCapture(meter)) |
| | 1259 | 250 | | return false; |
| | 1092 | 251 | | _build = new NavigationSurfaceComponentBuildWork( |
| | 1092 | 252 | | _componentGraph!, |
| | 1092 | 253 | | _candidate, |
| | 1092 | 254 | | _affectedKeys, |
| | 1092 | 255 | | _seeds, |
| | 1092 | 256 | | checked(_affectedMemberCount + _seeds.Count)); |
| | 1092 | 257 | | return _build.Advance(meter); |
| | | 258 | | } |
| | | 259 | | |
| | | 260 | | private bool AdvanceFront(MaintenanceWorkMeter meter) |
| | | 261 | | { |
| | 8280 | 262 | | while (_frontAffectedOrdinal < _affectedMapCount) |
| | | 263 | | { |
| | 6402 | 264 | | int mapOrdinal = _affectedMapOrdinals![_frontAffectedOrdinal]; |
| | 6402 | 265 | | NavigationGridBaselineCapture capture = _baselineCaptures![mapOrdinal]; |
| | 6402 | 266 | | NavigationSurfaceComponentKeySet? states = capture.StructuralChangedStates; |
| | 6402 | 267 | | if (!_frontMapActive) |
| | | 268 | | { |
| | 1606 | 269 | | if (!meter.TryConsumeComponentNodes(1)) |
| | 211 | 270 | | return false; |
| | 1395 | 271 | | string changedMapId = _candidate.GetInstance(mapOrdinal).MapId; |
| | 1395 | 272 | | _transitionChangedMaps = _transitionChangedMaps.Set(changedMapId, true); |
| | 1395 | 273 | | if (capture.DefaultPhysicalAddressSetChanged) |
| | | 274 | | { |
| | 24 | 275 | | _seamDiscoveryMaps = _seamDiscoveryMaps.Set( |
| | 24 | 276 | | _candidate.GetInstance(mapOrdinal).MapId, |
| | 24 | 277 | | true); |
| | | 278 | | } |
| | 1395 | 279 | | _frontStateOrdinal = 0; |
| | 1395 | 280 | | _frontStateEnumerator = states?.GetEnumerator() ?? default; |
| | 1395 | 281 | | _frontMapActive = true; |
| | | 282 | | } |
| | 6191 | 283 | | if (states != null && _frontStateOrdinal < states.Count) |
| | | 284 | | { |
| | 78 | 285 | | if (!meter.TryConsumeDependencyEntries(1)) |
| | 1 | 286 | | return false; |
| | 77 | 287 | | _frontStateEnumerator.MoveNext(); |
| | 77 | 288 | | _changedStates = _changedStates.Add(_frontStateEnumerator.Current); |
| | 77 | 289 | | _frontStateOrdinal++; |
| | 77 | 290 | | continue; |
| | | 291 | | } |
| | 6113 | 292 | | GridNavigationBaseline? baseline = states == null ? capture.Baseline : null; |
| | 6113 | 293 | | if (baseline != null && _frontStateOrdinal < baseline.VoxelStates.Length) |
| | | 294 | | { |
| | 4719 | 295 | | if (!meter.TryConsumeDependencyEntries(1)) |
| | 25 | 296 | | return false; |
| | 4694 | 297 | | AddChangedMedia( |
| | 4694 | 298 | | _candidate.GetInstance(mapOrdinal), |
| | 4694 | 299 | | baseline.VoxelStates[_frontStateOrdinal++].VoxelIndex); |
| | 4694 | 300 | | continue; |
| | | 301 | | } |
| | 1394 | 302 | | _frontMapActive = false; |
| | 1394 | 303 | | _frontAffectedOrdinal++; |
| | | 304 | | } |
| | | 305 | | |
| | 2925 | 306 | | while (_frontEventOrdinal < _eventCount) |
| | | 307 | | { |
| | 1078 | 308 | | if (!meter.TryConsumeDependencyEntries(1)) |
| | 31 | 309 | | return false; |
| | 1047 | 310 | | GridEventInfo eventInfo = _events![_frontEventOrdinal++]; |
| | 1047 | 311 | | if (!_candidate.TryGetMapId( |
| | 1047 | 312 | | eventInfo.Configuration.ToGridKey(), |
| | 1047 | 313 | | out string mapId)) |
| | | 314 | | continue; |
| | 115 | 315 | | bool foundMap = _candidate.TryGetMap(mapId, out NavigationMapInstance next); |
| | | 316 | | System.Diagnostics.Debug.Assert(foundMap, |
| | | 317 | | "The immutable configuration index and map directory share one candidate graph."); |
| | 115 | 318 | | _transitionChangedMaps = _transitionChangedMaps.Set(mapId, true); |
| | 115 | 319 | | if (eventInfo.HasVoxelState) |
| | 80 | 320 | | AddChangedMedia(next, eventInfo.VoxelIndex); |
| | | 321 | | } |
| | | 322 | | |
| | 1847 | 323 | | _frontComplete = true; |
| | 1847 | 324 | | if (_changedStates.Count == 0) |
| | | 325 | | { |
| | 1804 | 326 | | _componentGraph = _candidate; |
| | 1804 | 327 | | _hasNoChanges = true; |
| | 1804 | 328 | | return true; |
| | | 329 | | } |
| | 43 | 330 | | _seamRefresh = _seamDiscoveryMaps.Count == 0 |
| | 43 | 331 | | ? null |
| | 43 | 332 | | : new NavigationAutomaticSeamRefreshWork( |
| | 43 | 333 | | _world!, |
| | 43 | 334 | | _candidate, |
| | 43 | 335 | | _candidate, |
| | 43 | 336 | | _seamDiscoveryMaps); |
| | 43 | 337 | | _changedStateEnumerator = _changedStates.GetEnumerator(); |
| | 43 | 338 | | return true; |
| | | 339 | | } |
| | | 340 | | |
| | | 341 | | private void AddChangedMedia(NavigationMapInstance next, VoxelIndex index) |
| | | 342 | | { |
| | 4774 | 343 | | var address = new NavigationCellAddress(next.MapId, index); |
| | 4774 | 344 | | TraversalMedia nextMedia = next.GetEffectiveMedia(index); |
| | 4774 | 345 | | for (TraversalMedium medium = TraversalMedium.Solid; |
| | 19096 | 346 | | medium <= TraversalMedium.Liquid; |
| | 14322 | 347 | | medium++) |
| | | 348 | | { |
| | 14322 | 349 | | TraversalMedia bit = (TraversalMedia)NavigationMediumSlots<byte>.GetBit(medium); |
| | 14322 | 350 | | bool priorHasState = _candidate.SurfaceComponents.TryGet(address, medium, out _); |
| | 14322 | 351 | | bool nextHasState = (nextMedia & bit) != 0; |
| | 14322 | 352 | | if (priorHasState != nextHasState) |
| | | 353 | | { |
| | 1199 | 354 | | _changedStates = _changedStates.Add( |
| | 1199 | 355 | | new NavigationSurfaceComponentKey(address, medium)); |
| | | 356 | | } |
| | | 357 | | } |
| | 4774 | 358 | | } |
| | | 359 | | |
| | | 360 | | private bool AdvanceAffectedCapture(MaintenanceWorkMeter meter) |
| | | 361 | | { |
| | 35262 | 362 | | while (_stateOrdinal < _changedStates.Count || _statePhase != 0) |
| | | 363 | | { |
| | 34170 | 364 | | if (_pendingNeighbor.HasValue) |
| | | 365 | | { |
| | 9146 | 366 | | if (!CapturePriorComponent(_pendingNeighbor.Value, _state.Medium, meter)) |
| | 1 | 367 | | return false; |
| | 9145 | 368 | | _pendingNeighbor = null; |
| | 9145 | 369 | | continue; |
| | | 370 | | } |
| | 25024 | 371 | | if (_statePhase == 0) |
| | | 372 | | { |
| | 3296 | 373 | | if (!meter.TryConsumeComponentNodes(1)) |
| | 1230 | 374 | | return false; |
| | 2066 | 375 | | _changedStateEnumerator.MoveNext(); |
| | 2066 | 376 | | _state = _changedStateEnumerator.Current; |
| | 2066 | 377 | | _stateOrdinal++; |
| | 2066 | 378 | | _statePhase = 1; |
| | | 379 | | } |
| | 23794 | 380 | | if (_statePhase == 1) |
| | | 381 | | { |
| | 2067 | 382 | | if (!_seeds.Contains(_state.Representative)) |
| | | 383 | | { |
| | 1271 | 384 | | if (!meter.TryConsumeDependencyEntries(1)) |
| | 1 | 385 | | return false; |
| | 1270 | 386 | | _seeds = _seeds.Add(_state.Representative); |
| | | 387 | | } |
| | 2066 | 388 | | _pendingNeighbor = _state.Representative; |
| | 2066 | 389 | | _statePhase = 2; |
| | 2066 | 390 | | continue; |
| | | 391 | | } |
| | 21727 | 392 | | if (_statePhase == 2) |
| | | 393 | | { |
| | 2066 | 394 | | if (_state.Medium == TraversalMedium.Solid) |
| | | 395 | | { |
| | 1255 | 396 | | if (!_componentGraph!.TryGetStructuralMediumStateRef( |
| | 1255 | 397 | | _state.Representative, |
| | 1255 | 398 | | TraversalMedium.Solid, |
| | 1255 | 399 | | out NavigationMediumStateRef solidState)) |
| | | 400 | | { |
| | 2 | 401 | | CompleteState(); |
| | 2 | 402 | | continue; |
| | | 403 | | } |
| | 1253 | 404 | | NavigationNodeRef node = solidState.Node; |
| | 1253 | 405 | | _outgoing = _componentGraph.EnumerateStructuralSurfaceEdges(node); |
| | 1253 | 406 | | _incoming = _componentGraph.EnumerateIncomingStructuralSurfaceEdges(node); |
| | 1253 | 407 | | _outgoingComplete = false; |
| | | 408 | | } |
| | | 409 | | else |
| | | 410 | | { |
| | 811 | 411 | | if (!_componentGraph!.TryGetStructuralMediumStateRef( |
| | 811 | 412 | | _state.Representative, |
| | 811 | 413 | | _state.Medium, |
| | 811 | 414 | | out _volumeSource)) |
| | | 415 | | { |
| | 12 | 416 | | CompleteState(); |
| | 12 | 417 | | continue; |
| | | 418 | | } |
| | 799 | 419 | | _primaryOrdinal = 0; |
| | 799 | 420 | | _primaryCount = _componentGraph.GetPrimaryDirectionCount(_volumeSource.Node); |
| | 799 | 421 | | _volumeSeams = _componentGraph.AutomaticSeams.GetActiveEndpointEnumerator( |
| | 799 | 422 | | _state.Representative); |
| | | 423 | | } |
| | 2052 | 424 | | _statePhase = 3; |
| | | 425 | | } |
| | 21713 | 426 | | if (_state.Medium == TraversalMedium.Solid) |
| | | 427 | | { |
| | 18717 | 428 | | if (!AdvanceSolidAdjacency(meter)) |
| | 15 | 429 | | return false; |
| | | 430 | | } |
| | 2996 | 431 | | else if (!AdvanceVolumeAdjacency(meter)) |
| | | 432 | | { |
| | 12 | 433 | | return false; |
| | | 434 | | } |
| | | 435 | | } |
| | 1092 | 436 | | return true; |
| | | 437 | | } |
| | | 438 | | |
| | | 439 | | private bool AdvanceSolidAdjacency(MaintenanceWorkMeter meter) |
| | | 440 | | { |
| | 18717 | 441 | | int remaining = meter.RemainingSurfaceComponentEdges; |
| | 18717 | 442 | | if (!_outgoingComplete) |
| | | 443 | | { |
| | 7523 | 444 | | NavigationSurfaceEdgeAdvanceStatus status = _outgoing.AdvanceOne( |
| | 7523 | 445 | | meter, |
| | 7523 | 446 | | ref remaining); |
| | 7523 | 447 | | if (status == NavigationSurfaceEdgeAdvanceStatus.Blocked) |
| | 6 | 448 | | return false; |
| | 7517 | 449 | | if (status == NavigationSurfaceEdgeAdvanceStatus.Pending) |
| | 3817 | 450 | | return true; |
| | 3700 | 451 | | if (status == NavigationSurfaceEdgeAdvanceStatus.Edge) |
| | | 452 | | { |
| | 2447 | 453 | | bool found = _componentGraph!.TryGetNodeAddress( |
| | 2447 | 454 | | _outgoing.Current.Target, |
| | 2447 | 455 | | out NavigationCellAddress address); |
| | | 456 | | System.Diagnostics.Debug.Assert(found, |
| | | 457 | | "A published outgoing surface edge targets a node in the same graph."); |
| | 2447 | 458 | | _pendingNeighbor = address; |
| | 2447 | 459 | | return true; |
| | | 460 | | } |
| | 1253 | 461 | | _outgoingComplete = true; |
| | | 462 | | } |
| | 12447 | 463 | | NavigationSurfaceEdgeAdvanceStatus incoming = _incoming.AdvanceOne( |
| | 12447 | 464 | | meter, |
| | 12447 | 465 | | ref remaining); |
| | 12447 | 466 | | if (incoming == NavigationSurfaceEdgeAdvanceStatus.Blocked) |
| | 9 | 467 | | return false; |
| | 12438 | 468 | | if (incoming == NavigationSurfaceEdgeAdvanceStatus.Pending) |
| | 8738 | 469 | | return true; |
| | 3700 | 470 | | if (incoming == NavigationSurfaceEdgeAdvanceStatus.Edge) |
| | | 471 | | { |
| | 2447 | 472 | | bool found = _componentGraph!.TryGetNodeAddress( |
| | 2447 | 473 | | _incoming.Current.Predecessor, |
| | 2447 | 474 | | out NavigationCellAddress address); |
| | | 475 | | System.Diagnostics.Debug.Assert(found, |
| | | 476 | | "A published incoming surface edge originates at a node in the same graph."); |
| | 2447 | 477 | | _pendingNeighbor = address; |
| | 2447 | 478 | | return true; |
| | | 479 | | } |
| | 1253 | 480 | | CompleteState(); |
| | 1253 | 481 | | return true; |
| | | 482 | | } |
| | | 483 | | |
| | | 484 | | private bool AdvanceVolumeAdjacency(MaintenanceWorkMeter meter) |
| | | 485 | | { |
| | 5996 | 486 | | while (_primaryOrdinal < _primaryCount) |
| | | 487 | | { |
| | 5148 | 488 | | if (!meter.TryConsumeSurfaceComponentEdges(1)) |
| | 10 | 489 | | return false; |
| | 5138 | 490 | | if (_componentGraph!.TryGetStructuralPrimaryMediumNeighbor( |
| | 5138 | 491 | | _volumeSource, |
| | 5138 | 492 | | _primaryOrdinal++, |
| | 5138 | 493 | | out NavigationMediumStateRef neighbor) |
| | 5138 | 494 | | && _componentGraph.TryGetNodeAddress( |
| | 5138 | 495 | | neighbor.Node, |
| | 5138 | 496 | | out NavigationCellAddress address)) |
| | | 497 | | { |
| | 2138 | 498 | | _pendingNeighbor = address; |
| | 2138 | 499 | | return true; |
| | | 500 | | } |
| | | 501 | | } |
| | 861 | 502 | | while (meter.RemainingSurfaceComponentEdges > 0) |
| | | 503 | | { |
| | 859 | 504 | | if (!_volumeSeams.MoveNext()) |
| | | 505 | | { |
| | 799 | 506 | | CompleteState(); |
| | 799 | 507 | | return true; |
| | | 508 | | } |
| | 60 | 509 | | meter.TryConsumeSurfaceComponentEdges(1); |
| | 60 | 510 | | NavigationCellAddress destination = _volumeSeams.Current.Destination; |
| | 60 | 511 | | if (_componentGraph!.TryGetStructuralMediumStateRef( |
| | 60 | 512 | | destination, |
| | 60 | 513 | | _state.Medium, |
| | 60 | 514 | | out _)) |
| | | 515 | | { |
| | 47 | 516 | | _pendingNeighbor = destination; |
| | 47 | 517 | | return true; |
| | | 518 | | } |
| | | 519 | | } |
| | 2 | 520 | | return false; |
| | | 521 | | } |
| | | 522 | | |
| | | 523 | | private bool CapturePriorComponent( |
| | | 524 | | NavigationCellAddress address, |
| | | 525 | | TraversalMedium medium, |
| | | 526 | | MaintenanceWorkMeter meter) |
| | | 527 | | { |
| | 9146 | 528 | | if (!_candidate.SurfaceComponents.TryGet( |
| | 9146 | 529 | | address, |
| | 9146 | 530 | | medium, |
| | 9146 | 531 | | out NavigationSurfaceComponent component) |
| | 9146 | 532 | | || _affectedKeys.Contains(component.Key)) |
| | | 533 | | { |
| | 9131 | 534 | | return true; |
| | | 535 | | } |
| | 15 | 536 | | if (!meter.TryConsumeDependencyEntries(1)) |
| | 1 | 537 | | return false; |
| | 14 | 538 | | _affectedKeys = _affectedKeys.Add(component.Key); |
| | 14 | 539 | | _affectedMemberCount = checked(_affectedMemberCount + component.Members.Count); |
| | 14 | 540 | | return true; |
| | | 541 | | } |
| | | 542 | | |
| | | 543 | | private void CompleteState() |
| | | 544 | | { |
| | 2090 | 545 | | _statePhase = 0; |
| | 2090 | 546 | | _pendingNeighbor = null; |
| | 2090 | 547 | | _outgoing = default; |
| | 2090 | 548 | | _incoming = default; |
| | 2090 | 549 | | _volumeSeams = default; |
| | 2090 | 550 | | _volumeSource = default; |
| | 2090 | 551 | | _primaryOrdinal = 0; |
| | 2090 | 552 | | _primaryCount = 0; |
| | 2090 | 553 | | _outgoingComplete = false; |
| | 2090 | 554 | | } |
| | | 555 | | |
| | | 556 | | private void ResetComponentState() |
| | | 557 | | { |
| | | 558 | | System.Diagnostics.Debug.Assert(_seamRefresh != null, |
| | | 559 | | "Only seam revision changes restart component discovery."); |
| | 24 | 560 | | _affectedKeys = _initialAffectedKeys; |
| | 24 | 561 | | _seeds = _initialSeeds; |
| | 24 | 562 | | _affectedMemberCount = _initialAffectedMemberCount; |
| | 24 | 563 | | _changedStateEnumerator = _changedStates.GetEnumerator(); |
| | 24 | 564 | | _state = default; |
| | 24 | 565 | | _stateOrdinal = 0; |
| | 24 | 566 | | _statePhase = 0; |
| | 24 | 567 | | _pendingNeighbor = null; |
| | 24 | 568 | | _build = null; |
| | 24 | 569 | | _componentGraph = null; |
| | 24 | 570 | | _seamRevision = _seamRefresh!.Revision; |
| | 24 | 571 | | CompleteState(); |
| | 24 | 572 | | } |
| | | 573 | | } |