| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using System.Threading; |
| | | 3 | | using System.Threading.Tasks; |
| | | 4 | | using Itinero.Network.Enumerators.Edges; |
| | | 5 | | using Itinero.Network.Tiles; |
| | | 6 | | using Itinero.Profiles; |
| | | 7 | | using Itinero.Routing.Costs; |
| | | 8 | | |
| | | 9 | | namespace Itinero.Network.Search.Islands; |
| | | 10 | | |
| | | 11 | | internal class IslandBuilder |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Resolves whether an edge is on an island. Keeps expanding until definitive. |
| | | 15 | | /// </summary> |
| | | 16 | | public static async Task<bool?> ResolveEdgeAsync(RoutingNetwork network, Profile profile, EdgeId edgeId, |
| | | 17 | | CancellationToken cancellationToken) |
| | 31 | 18 | | { |
| | 31 | 19 | | if (cancellationToken.IsCancellationRequested) return null; |
| | | 20 | | |
| | 31 | 21 | | var islands = network.IslandManager.GetIslandsFor(profile); |
| | 31 | 22 | | var dg = network.IslandManager.GetOrCreateDirectedGraph(profile); |
| | | 23 | | |
| | | 24 | | // quick checks. |
| | 42 | 25 | | if (islands.IsEdgeOnIsland(edgeId)) return true; |
| | 34 | 26 | | if (dg.IsNotIsland(edgeId)) return false; |
| | | 27 | | |
| | 6 | 28 | | var costFunction = network.GetCostFunctionFor(profile); |
| | 6 | 29 | | var edgeEnumerator = network.GetEdgeEnumerator(); |
| | 6 | 30 | | var maxIslandSize = network.IslandManager.MaxIslandSize; |
| | | 31 | | |
| | | 32 | | // process the edge. |
| | 6 | 33 | | ProcessEdge(dg, costFunction, edgeEnumerator, edgeId, islands, maxIslandSize); |
| | 6 | 34 | | if (!dg.IsInGraph(edgeId)) return null; // not traversable |
| | | 35 | | |
| | | 36 | | // expand outward from the edge's component until resolved. |
| | 6 | 37 | | var processedTiles = new HashSet<uint>(); |
| | 6 | 38 | | var tilesToProcess = new Queue<uint>(); |
| | | 39 | | |
| | | 40 | | // seed with tiles of vertices of the edge. |
| | 6 | 41 | | if (edgeEnumerator.MoveTo(edgeId, true)) |
| | 6 | 42 | | { |
| | 6 | 43 | | tilesToProcess.Enqueue(edgeEnumerator.Tail.TileId); |
| | 6 | 44 | | tilesToProcess.Enqueue(edgeEnumerator.Head.TileId); |
| | 6 | 45 | | } |
| | | 46 | | |
| | 12 | 47 | | while (tilesToProcess.Count > 0) |
| | 10 | 48 | | { |
| | 10 | 49 | | if (cancellationToken.IsCancellationRequested) return null; |
| | | 50 | | |
| | | 51 | | // check resolution. |
| | 10 | 52 | | if (islands.IsEdgeOnIsland(edgeId)) return true; |
| | 10 | 53 | | if (dg.IsNotIsland(edgeId)) return false; |
| | | 54 | | |
| | 10 | 55 | | var tileId = tilesToProcess.Dequeue(); |
| | 13 | 56 | | if (!processedTiles.Add(tileId)) continue; |
| | | 57 | | |
| | | 58 | | // load tile. |
| | 7 | 59 | | await network.UsageNotifier.NotifyVertex(network, new VertexId(tileId, 0), cancellationToken); |
| | 7 | 60 | | if (cancellationToken.IsCancellationRequested) return null; |
| | | 61 | | |
| | 7 | 62 | | var tile = network.GetTileForRead(tileId); |
| | 7 | 63 | | if (tile == null) continue; |
| | | 64 | | |
| | | 65 | | // process all edges in the tile. |
| | 7 | 66 | | var tileEnumerator = new NetworkTileEnumerator(); |
| | 7 | 67 | | tileEnumerator.MoveTo(tile); |
| | 7 | 68 | | var vertex = new VertexId(tileId, 0); |
| | 425 | 69 | | while (tileEnumerator.MoveTo(vertex)) |
| | 422 | 70 | | { |
| | 2021 | 71 | | while (tileEnumerator.MoveNext()) |
| | 1603 | 72 | | { |
| | 2389 | 73 | | if (!tileEnumerator.Forward) continue; |
| | 817 | 74 | | ProcessEdge(dg, costFunction, edgeEnumerator, tileEnumerator.EdgeId, islands, maxIslandSize); |
| | | 75 | | |
| | | 76 | | // early exit if resolved. |
| | 821 | 77 | | if (dg.IsNotIsland(edgeId)) return false; |
| | 813 | 78 | | } |
| | 418 | 79 | | vertex = new VertexId(tileId, vertex.LocalId + 1); |
| | 418 | 80 | | } |
| | 3 | 81 | | if (dg.IsNotIsland(edgeId)) return false; |
| | | 82 | | |
| | | 83 | | // try resolve with cycle detection. |
| | 3 | 84 | | var candidates = CollectComponentCandidates(dg, edgeId, islands); |
| | 3 | 85 | | TryResolve(dg, islands, candidates, maxIslandSize); |
| | | 86 | | |
| | 3 | 87 | | if (islands.IsEdgeOnIsland(edgeId)) return true; |
| | 3 | 88 | | if (dg.IsNotIsland(edgeId)) return false; |
| | | 89 | | |
| | | 90 | | // enqueue tiles of unresolved neighbors. |
| | 3 | 91 | | EnqueueNeighborTiles(dg, edgeId, edgeEnumerator, processedTiles, tilesToProcess); |
| | 3 | 92 | | } |
| | | 93 | | |
| | | 94 | | // final resolution attempt. |
| | 2 | 95 | | var finalCandidates = CollectComponentCandidates(dg, edgeId, islands); |
| | 2 | 96 | | TryResolve(dg, islands, finalCandidates, maxIslandSize); |
| | | 97 | | |
| | 2 | 98 | | if (islands.IsEdgeOnIsland(edgeId)) return true; |
| | 2 | 99 | | if (dg.IsNotIsland(edgeId)) return false; |
| | | 100 | | |
| | | 101 | | // no more tiles to expand and still unresolved. |
| | | 102 | | // if the component can't reach the sentinel, it's isolated → island. |
| | 2 | 103 | | return true; |
| | 31 | 104 | | } |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// Processes all edges in a tile into the DG. |
| | | 108 | | /// </summary> |
| | | 109 | | public static async Task BuildForTileAsync(RoutingNetwork network, Profile profile, uint tileId, |
| | | 110 | | CancellationToken cancellationToken) |
| | 11 | 111 | | { |
| | 11 | 112 | | if (cancellationToken.IsCancellationRequested) return; |
| | 11 | 113 | | var islands = network.IslandManager.GetIslandsFor(profile); |
| | | 114 | | |
| | 11 | 115 | | var tile = network.GetTileForRead(tileId); |
| | 11 | 116 | | if (tile == null) return; |
| | | 117 | | |
| | 11 | 118 | | var dg = network.IslandManager.GetOrCreateDirectedGraph(profile); |
| | 11 | 119 | | var costFunction = network.GetCostFunctionFor(profile); |
| | 11 | 120 | | var edgeEnumerator = network.GetEdgeEnumerator(); |
| | 11 | 121 | | var maxIslandSize = network.IslandManager.MaxIslandSize; |
| | | 122 | | |
| | 11 | 123 | | var tileEnumerator = new NetworkTileEnumerator(); |
| | 11 | 124 | | tileEnumerator.MoveTo(tile); |
| | 11 | 125 | | var vertex = new VertexId(tileId, 0); |
| | 49 | 126 | | while (tileEnumerator.MoveTo(vertex)) |
| | 38 | 127 | | { |
| | 92 | 128 | | while (tileEnumerator.MoveNext()) |
| | 54 | 129 | | { |
| | 54 | 130 | | if (cancellationToken.IsCancellationRequested) return; |
| | 81 | 131 | | if (!tileEnumerator.Forward) continue; |
| | 27 | 132 | | ProcessEdge(dg, costFunction, edgeEnumerator, tileEnumerator.EdgeId, islands, maxIslandSize); |
| | 27 | 133 | | } |
| | 38 | 134 | | vertex = new VertexId(tileId, vertex.LocalId + 1); |
| | 38 | 135 | | } |
| | | 136 | | |
| | | 137 | | // collect tile edges as candidates. |
| | 11 | 138 | | var candidates = new List<EdgeId>(); |
| | 11 | 139 | | tileEnumerator.MoveTo(tile); |
| | 11 | 140 | | vertex = new VertexId(tileId, 0); |
| | 49 | 141 | | while (tileEnumerator.MoveTo(vertex)) |
| | 38 | 142 | | { |
| | 92 | 143 | | while (tileEnumerator.MoveNext()) |
| | 54 | 144 | | { |
| | 81 | 145 | | if (!tileEnumerator.Forward) continue; |
| | 27 | 146 | | var eid = tileEnumerator.EdgeId; |
| | 27 | 147 | | if (dg.IsInGraph(eid) && !dg.IsNotIsland(eid) && !islands.IsEdgeOnIsland(eid)) |
| | 14 | 148 | | candidates.Add(eid); |
| | 27 | 149 | | } |
| | 38 | 150 | | vertex = new VertexId(tileId, vertex.LocalId + 1); |
| | 38 | 151 | | } |
| | | 152 | | |
| | 11 | 153 | | TryResolve(dg, islands, candidates, maxIslandSize); |
| | | 154 | | |
| | 11 | 155 | | if (candidates.Count == 0) |
| | 10 | 156 | | islands.SetTileDone(tileId); |
| | 11 | 157 | | } |
| | | 158 | | |
| | | 159 | | /// <summary> |
| | | 160 | | /// Processes a single edge into the DG. |
| | | 161 | | /// </summary> |
| | | 162 | | internal static void ProcessEdge(IslandDirectedGraph dg, |
| | | 163 | | ICostFunction costFunction, RoutingNetworkEdgeEnumerator edgeEnumerator, |
| | | 164 | | EdgeId edgeId, Islands islands, int maxIslandSize) |
| | 850 | 165 | | { |
| | 860 | 166 | | if (dg.IsProcessed(edgeId)) return; |
| | 846 | 167 | | if (dg.IsNotIsland(edgeId)) return; |
| | 834 | 168 | | if (islands.IsEdgeOnIsland(edgeId)) return; |
| | | 169 | | |
| | 834 | 170 | | if (!edgeEnumerator.MoveTo(edgeId, true)) return; |
| | 834 | 171 | | var canForward = costFunction.GetIslandBuilderCost(edgeEnumerator); |
| | 834 | 172 | | if (!edgeEnumerator.MoveTo(edgeId, false)) return; |
| | 834 | 173 | | var canBackward = costFunction.GetIslandBuilderCost(edgeEnumerator); |
| | | 174 | | |
| | 834 | 175 | | if (!canForward && !canBackward) return; |
| | | 176 | | |
| | 834 | 177 | | dg.AddVertex(edgeId); |
| | | 178 | | |
| | 4968 | 179 | | for (var pass = 0; pass < 2; pass++) |
| | 1660 | 180 | | { |
| | 1660 | 181 | | var forward = pass == 0; |
| | 1660 | 182 | | if (forward && !canForward) continue; |
| | 1666 | 183 | | if (!forward && !canBackward) continue; |
| | | 184 | | |
| | 1654 | 185 | | edgeEnumerator.MoveTo(edgeId, forward); |
| | 1654 | 186 | | var targetVertex = edgeEnumerator.Head; |
| | | 187 | | |
| | | 188 | | // Pre-position helper enumerators on edgeId in both traversal directions so |
| | | 189 | | // we can hand them to the two-enumerator GetIslandBuilderCost primitive: |
| | | 190 | | // edgeIdFrom: head = targetVertex (for canGoTo, where edgeId is the "from") |
| | | 191 | | // edgeIdTo: tail = targetVertex (for canComeFrom, where edgeId is the "to") |
| | 1654 | 192 | | var edgeIdFrom = edgeEnumerator.Network.GetEdgeEnumerator(); |
| | 1654 | 193 | | edgeIdFrom.MoveTo(edgeId, forward); |
| | 1654 | 194 | | var edgeIdTo = edgeEnumerator.Network.GetEdgeEnumerator(); |
| | 1654 | 195 | | edgeIdTo.MoveTo(edgeId, !forward); |
| | | 196 | | |
| | | 197 | | // Reusable probe positioned on the current neighbor in its |
| | | 198 | | // "arriving at targetVertex" direction (head = targetVertex), used as the |
| | | 199 | | // "from" side of canComeFrom. |
| | 1654 | 200 | | var neighborArriving = edgeEnumerator.Network.GetEdgeEnumerator(); |
| | | 201 | | |
| | 1654 | 202 | | if (!edgeEnumerator.MoveTo(targetVertex)) continue; |
| | | 203 | | |
| | 8006 | 204 | | while (edgeEnumerator.MoveNext()) |
| | 6362 | 205 | | { |
| | 8011 | 206 | | if (edgeEnumerator.EdgeId == edgeId) continue; |
| | | 207 | | |
| | 4713 | 208 | | var neighborId = edgeEnumerator.EdgeId; |
| | | 209 | | |
| | | 210 | | // skip known islands. |
| | 4713 | 211 | | if (islands.IsEdgeOnIsland(neighborId)) continue; |
| | | 212 | | |
| | | 213 | | // determine DG vertex for the neighbor. |
| | | 214 | | EdgeId neighborDgVertex; |
| | 4713 | 215 | | if (dg.IsNotIsland(neighborId)) |
| | 0 | 216 | | { |
| | 0 | 217 | | neighborDgVertex = IslandDirectedGraph.MainNetworkSentinel; |
| | 0 | 218 | | } |
| | | 219 | | else |
| | 4713 | 220 | | { |
| | 4713 | 221 | | dg.AddVertex(neighborId); |
| | 4713 | 222 | | neighborDgVertex = neighborId; |
| | 4713 | 223 | | } |
| | | 224 | | |
| | | 225 | | // canGoTo (edgeId → neighbor at the shared vertex): |
| | | 226 | | // from = edgeIdFrom (head = targetVertex) |
| | | 227 | | // to = edgeEnumerator on the neighbor (tail = targetVertex by iteration) |
| | 4713 | 228 | | var canGoTo = costFunction.GetIslandBuilderCost(edgeIdFrom, edgeEnumerator); |
| | 4713 | 229 | | if (canGoTo) |
| | 4708 | 230 | | { |
| | 4708 | 231 | | dg.AddDirectedLink(edgeId, neighborDgVertex); |
| | | 232 | | // bidirectional merge check: O(1). |
| | 4708 | 233 | | if (dg.HasDirectedLink(neighborDgVertex, edgeId)) |
| | 0 | 234 | | { |
| | 0 | 235 | | dg.Merge(edgeId, neighborDgVertex); |
| | 0 | 236 | | var newSize = dg.GetSize(dg.Find(edgeId)); |
| | 0 | 237 | | if (newSize >= maxIslandSize) |
| | 0 | 238 | | dg.CollapseToMainNetwork(edgeId); |
| | 0 | 239 | | } |
| | 4708 | 240 | | } |
| | | 241 | | |
| | | 242 | | // canComeFrom (neighbor → edgeId at the shared vertex): |
| | | 243 | | // from = neighbor in its "arriving at targetVertex" direction |
| | | 244 | | // (head = targetVertex, i.e. the opposite of iteration direction) |
| | | 245 | | // to = edgeIdTo (tail = targetVertex) |
| | 4713 | 246 | | neighborArriving.MoveTo(neighborId, !edgeEnumerator.Forward); |
| | 4713 | 247 | | var canComeFrom = costFunction.GetIslandBuilderCost(neighborArriving, edgeIdTo); |
| | 4713 | 248 | | if (canComeFrom) |
| | 4704 | 249 | | { |
| | 4704 | 250 | | dg.AddDirectedLink(neighborDgVertex, edgeId); |
| | | 251 | | // bidirectional merge check: O(1). |
| | 4704 | 252 | | if (dg.HasDirectedLink(edgeId, neighborDgVertex)) |
| | 1028 | 253 | | { |
| | 1028 | 254 | | dg.Merge(edgeId, neighborDgVertex); |
| | 1028 | 255 | | var newSize = dg.GetSize(dg.Find(edgeId)); |
| | 1028 | 256 | | if (newSize >= maxIslandSize) |
| | 10 | 257 | | dg.CollapseToMainNetwork(edgeId); |
| | 1028 | 258 | | } |
| | 4704 | 259 | | } |
| | | 260 | | |
| | 4723 | 261 | | if (dg.IsNotIsland(edgeId)) break; |
| | | 262 | | |
| | | 263 | | // re-enumerate to continue. |
| | 4703 | 264 | | edgeEnumerator.MoveTo(targetVertex); |
| | 11554 | 265 | | while (edgeEnumerator.MoveNext()) |
| | 11554 | 266 | | { |
| | 16257 | 267 | | if (edgeEnumerator.EdgeId == neighborId) break; |
| | 6851 | 268 | | } |
| | 4703 | 269 | | } |
| | | 270 | | |
| | 1664 | 271 | | if (dg.IsNotIsland(edgeId)) break; |
| | 1644 | 272 | | } |
| | | 273 | | |
| | 834 | 274 | | dg.SetProcessed(edgeId); |
| | 850 | 275 | | } |
| | | 276 | | |
| | | 277 | | /// <summary> |
| | | 278 | | /// Resolves candidates: dead-end pruning, cycle detection, reachability. |
| | | 279 | | /// </summary> |
| | | 280 | | private static void TryResolve(IslandDirectedGraph dg, Islands islands, |
| | | 281 | | List<EdgeId> candidates, int maxIslandSize) |
| | 16 | 282 | | { |
| | | 283 | | bool changed; |
| | | 284 | | do |
| | 24 | 285 | | { |
| | 24 | 286 | | changed = false; |
| | | 287 | | |
| | | 288 | | // 1. Prune dead-ends. |
| | 292 | 289 | | for (var i = candidates.Count - 1; i >= 0; i--) |
| | 122 | 290 | | { |
| | 122 | 291 | | var edgeId = candidates[i]; |
| | 122 | 292 | | if (!dg.IsInGraph(edgeId) || dg.IsNotIsland(edgeId)) |
| | 0 | 293 | | { |
| | 0 | 294 | | candidates.RemoveAt(i); |
| | 0 | 295 | | continue; |
| | | 296 | | } |
| | | 297 | | |
| | 122 | 298 | | if (dg.IsDeadEnd(edgeId)) |
| | 11 | 299 | | { |
| | 11 | 300 | | var members = dg.GetMembers(dg.Find(edgeId)); |
| | 11 | 301 | | if (members != null) |
| | 11 | 302 | | { |
| | 11 | 303 | | var copy = new List<EdgeId>(members); |
| | 55 | 304 | | foreach (var m in copy) |
| | 11 | 305 | | { |
| | 11 | 306 | | islands.SetEdgeOnIsland(m); |
| | 11 | 307 | | dg.RemoveEdge(m); |
| | 11 | 308 | | } |
| | 11 | 309 | | } |
| | 11 | 310 | | candidates.RemoveAt(i); |
| | 11 | 311 | | changed = true; |
| | 11 | 312 | | } |
| | 122 | 313 | | } |
| | | 314 | | |
| | | 315 | | // 2. Detect one-way cycles among remaining candidates (Tarjan's SCC). |
| | 24 | 316 | | if (candidates.Count >= 2) |
| | 6 | 317 | | { |
| | 6 | 318 | | if (dg.DetectAndMergeCycles(candidates)) |
| | 0 | 319 | | { |
| | 0 | 320 | | changed = true; |
| | | 321 | | // check if any merged component now exceeds MaxIslandSize. |
| | 0 | 322 | | for (var i = candidates.Count - 1; i >= 0; i--) |
| | 0 | 323 | | { |
| | 0 | 324 | | var edgeId = candidates[i]; |
| | 0 | 325 | | if (!dg.IsInGraph(edgeId) || dg.IsNotIsland(edgeId)) |
| | 0 | 326 | | { |
| | 0 | 327 | | candidates.RemoveAt(i); |
| | 0 | 328 | | continue; |
| | | 329 | | } |
| | | 330 | | |
| | 0 | 331 | | var root = dg.Find(edgeId); |
| | 0 | 332 | | if (dg.GetSize(root) >= maxIslandSize) |
| | 0 | 333 | | { |
| | 0 | 334 | | dg.CollapseToMainNetwork(root); |
| | 0 | 335 | | candidates.RemoveAt(i); |
| | 0 | 336 | | } |
| | 0 | 337 | | } |
| | 0 | 338 | | } |
| | 6 | 339 | | } |
| | | 340 | | |
| | | 341 | | // 3. Check if any remaining candidate can reach the sentinel both ways. |
| | 270 | 342 | | for (var i = candidates.Count - 1; i >= 0; i--) |
| | 111 | 343 | | { |
| | 111 | 344 | | var edgeId = candidates[i]; |
| | 111 | 345 | | if (!dg.IsInGraph(edgeId) || dg.IsNotIsland(edgeId)) |
| | 0 | 346 | | { |
| | 0 | 347 | | candidates.RemoveAt(i); |
| | 0 | 348 | | continue; |
| | | 349 | | } |
| | | 350 | | |
| | 111 | 351 | | if (dg.CanReachMainNetwork(edgeId)) |
| | 1 | 352 | | { |
| | 1 | 353 | | dg.CollapseToMainNetwork(edgeId); |
| | 1 | 354 | | candidates.RemoveAt(i); |
| | 1 | 355 | | changed = true; |
| | 1 | 356 | | } |
| | 111 | 357 | | } |
| | 48 | 358 | | } while (changed); |
| | 16 | 359 | | } |
| | | 360 | | |
| | | 361 | | private static List<EdgeId> CollectComponentCandidates(IslandDirectedGraph dg, EdgeId edgeId, Islands islands) |
| | 5 | 362 | | { |
| | 5 | 363 | | var candidates = new List<EdgeId>(); |
| | 5 | 364 | | if (!dg.IsInGraph(edgeId)) return candidates; |
| | | 365 | | |
| | 5 | 366 | | var members = dg.GetMembers(dg.Find(edgeId)); |
| | 5 | 367 | | if (members != null) |
| | 5 | 368 | | { |
| | 231 | 369 | | foreach (var m in members) |
| | 108 | 370 | | { |
| | 108 | 371 | | if (!dg.IsNotIsland(m) && !islands.IsEdgeOnIsland(m)) |
| | 108 | 372 | | candidates.Add(m); |
| | 108 | 373 | | } |
| | 5 | 374 | | } |
| | | 375 | | |
| | 5 | 376 | | return candidates; |
| | 5 | 377 | | } |
| | | 378 | | |
| | | 379 | | private static void EnqueueNeighborTiles(IslandDirectedGraph dg, EdgeId edgeId, |
| | | 380 | | RoutingNetworkEdgeEnumerator edgeEnumerator, |
| | | 381 | | HashSet<uint> processedTiles, Queue<uint> tilesToProcess) |
| | 3 | 382 | | { |
| | 3 | 383 | | if (!dg.IsInGraph(edgeId)) return; |
| | | 384 | | |
| | 3 | 385 | | var members = dg.GetMembers(dg.Find(edgeId)); |
| | 3 | 386 | | if (members == null) return; |
| | | 387 | | |
| | 217 | 388 | | foreach (var member in members) |
| | 104 | 389 | | { |
| | 104 | 390 | | if (!edgeEnumerator.MoveTo(member, true)) continue; |
| | 104 | 391 | | var t = edgeEnumerator.Tail.TileId; |
| | 104 | 392 | | var h = edgeEnumerator.Head.TileId; |
| | 119 | 393 | | if (!processedTiles.Contains(t)) tilesToProcess.Enqueue(t); |
| | 104 | 394 | | if (!processedTiles.Contains(h)) tilesToProcess.Enqueue(h); |
| | 104 | 395 | | } |
| | 3 | 396 | | } |
| | | 397 | | } |