| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Threading; |
| | 4 | | using System.Threading.Tasks; |
| | 5 | | using Itinero.Network.Enumerators.Edges; |
| | 6 | | using Itinero.Network.Tiles; |
| | 7 | | using Itinero.Profiles; |
| | 8 | | using Itinero.Routing.Costs; |
| | 9 | | using Itinero.Routing.DataStructures; |
| | 10 | |
|
| | 11 | | namespace Itinero.Network.Search.Islands; |
| | 12 | |
|
| | 13 | | internal class IslandBuilder |
| | 14 | | { |
| | 15 | | public static async Task BuildForTileAsync(RoutingNetwork network, Profile profile, uint tileId, CancellationToken c |
| 0 | 16 | | { |
| 0 | 17 | | if (cancellationToken.IsCancellationRequested) return; |
| 0 | 18 | | var islands = network.IslandManager.GetIslandsFor(profile); |
| | 19 | |
|
| 0 | 20 | | var tile = network.GetTileForRead(tileId); |
| 0 | 21 | | if (tile == null) return; |
| | 22 | |
|
| 0 | 23 | | var enumerator = new NetworkTileEnumerator(); |
| 0 | 24 | | enumerator.MoveTo(tile); |
| | 25 | |
|
| 0 | 26 | | var labels = new IslandLabels(network.IslandManager.MaxIslandSize); |
| 0 | 27 | | var costFunction = network.GetCostFunctionFor(profile); |
| 0 | 28 | | var vertex = new VertexId(tileId, 0); |
| 0 | 29 | | while (enumerator.MoveTo(vertex)) |
| 0 | 30 | | { |
| 0 | 31 | | while (enumerator.MoveNext()) |
| 0 | 32 | | { |
| 0 | 33 | | if (cancellationToken.IsCancellationRequested) return; |
| | 34 | |
|
| 0 | 35 | | var onIsland = await IsOnIslandAsync(network, labels, costFunction, enumerator.EdgeId, |
| 0 | 36 | | IsOnIslandAlready, cancellationToken); |
| 0 | 37 | | if (onIsland == null) continue; // edge cannot be traversed by profile. |
| 0 | 38 | | if (!onIsland.Value) continue; // edge is not on island. |
| | 39 | |
|
| 0 | 40 | | islands.SetEdgeOnIsland(enumerator.EdgeId); |
| 0 | 41 | | } |
| | 42 | |
|
| 0 | 43 | | vertex = new VertexId(tileId, vertex.LocalId + 1); |
| 0 | 44 | | } |
| | 45 | |
|
| 0 | 46 | | if (cancellationToken.IsCancellationRequested) return; |
| 0 | 47 | | islands.SetTileDone(tileId); |
| 0 | 48 | | return; |
| | 49 | |
|
| | 50 | | bool? IsOnIslandAlready(IEdgeEnumerator e) |
| 0 | 51 | | { |
| 0 | 52 | | return islands.IsEdgeOnIsland(e, tileId); |
| 0 | 53 | | } |
| 0 | 54 | | } |
| | 55 | |
|
| | 56 | | internal static async Task<bool?> IsOnIslandAsync(RoutingNetwork network, IslandLabels labels, |
| | 57 | | ICostFunction costFunction, EdgeId edgeId, |
| | 58 | | Func<IEdgeEnumerator, bool?>? isOnIslandAlready = null, CancellationToken cancellationToken = default) |
| 24 | 59 | | { |
| | 60 | | // to check if an edge is on an island we do the following: |
| | 61 | | // - verify it can be used as an origin by searching forward and finding a big connected island. |
| | 62 | | // - verify it can be used as a destination by searching backward and finding a big connected island. |
| | 63 | |
|
| | 64 | | // the following assumptions are used: |
| | 65 | | // - two bidirectional edges that share a vertex are always on the same island. |
| | 66 | | // - an island bigger than a given threshold is considered connected. |
| | 67 | | // - onedirectional edges are always their own islands until they occur in a loop. |
| | 68 | |
|
| 24 | 69 | | var edgeEnumerator = network.GetEdgeEnumerator(); |
| 24 | 70 | | edgeEnumerator.MoveTo(edgeId, true); |
| 24 | 71 | | var costEnumerator = network.GetEdgeEnumerator(); |
| 24 | 72 | | var canForward = costFunction.GetIslandBuilderCost(edgeEnumerator); |
| 24 | 73 | | costEnumerator.MoveTo(edgeId, false); |
| 24 | 74 | | var canBackward = costFunction.GetIslandBuilderCost(costEnumerator); |
| | 75 | |
|
| | 76 | | // test if the edge can be traversed, if not return undefined value. |
| 24 | 77 | | if (!canForward && !canBackward) return null; |
| | 78 | |
|
| | 79 | | // see if the edge already has a label. |
| 24 | 80 | | if (!labels.TryGetWithDetails(edgeId, out var rootIsland)) |
| 19 | 81 | | { |
| | 82 | | // check if the neighbour has a status already we can use. |
| 19 | 83 | | var onIslandAlready = isOnIslandAlready?.Invoke(edgeEnumerator); |
| 19 | 84 | | if (onIslandAlready != null) |
| 2 | 85 | | { |
| 2 | 86 | | if (onIslandAlready.Value) |
| 0 | 87 | | { |
| 0 | 88 | | rootIsland = labels.AddNew(edgeId, false); |
| 0 | 89 | | } |
| | 90 | | else |
| 2 | 91 | | { |
| 2 | 92 | | rootIsland = labels.AddTo(IslandLabels.NotAnIslandLabel, edgeId); |
| 2 | 93 | | } |
| 2 | 94 | | } |
| | 95 | | else |
| 17 | 96 | | { |
| | 97 | | // create the root island. |
| 17 | 98 | | rootIsland = labels.AddNew(edgeId); |
| 17 | 99 | | } |
| 19 | 100 | | } |
| | 101 | |
|
| 24 | 102 | | if (rootIsland.size >= network.IslandManager.MaxIslandSize) |
| 7 | 103 | | { |
| 7 | 104 | | if (rootIsland.label != IslandLabels.NotAnIslandLabel) |
| 0 | 105 | | throw new Exception("A large island without the not-an-island label should not exist"); |
| 7 | 106 | | return false; |
| | 107 | | } |
| | 108 | |
|
| 17 | 109 | | if (rootIsland.final) return true; // the island can never ever get bigger anymore. |
| | 110 | |
|
| | 111 | | // a search in two parts: |
| | 112 | | // - a search space of routes going towards the edge, the destination heap. |
| | 113 | | // - a search space of routes going away from the edge, the origin heap. |
| | 114 | | // when either of the searches stops before the island is final the edge is on an island. |
| | 115 | |
|
| 17 | 116 | | var originHeap = new BinaryHeap<(EdgeId id, bool forward)>(); |
| 17 | 117 | | var originVisits = new HashSet<(EdgeId id, bool forward)>(); |
| 17 | 118 | | var originHasNonIsland = false; |
| 17 | 119 | | var destinationHeap = new BinaryHeap<(EdgeId id, bool forward)>(); |
| 17 | 120 | | var destinationVisits = new HashSet<(EdgeId id, bool forward)>(); |
| 17 | 121 | | var destinationHasNonIsland = false; |
| 17 | 122 | | if (canForward) |
| 17 | 123 | | { |
| 17 | 124 | | originHeap.Push((edgeId, true), 1); |
| 17 | 125 | | destinationHeap.Push((edgeId, true), 1); |
| 17 | 126 | | } |
| 17 | 127 | | if (canBackward) |
| 11 | 128 | | { |
| 11 | 129 | | originHeap.Push((edgeId, false), 1); |
| 11 | 130 | | destinationHeap.Push((edgeId, false), 1); |
| 11 | 131 | | } |
| | 132 | |
|
| 37 | 133 | | while (originHeap.Count > 0 || destinationHeap.Count > 0) |
| 29 | 134 | | { |
| 29 | 135 | | while (true) |
| 29 | 136 | | { |
| 29 | 137 | | if (cancellationToken.IsCancellationRequested) return null; |
| | 138 | |
|
| 30 | 139 | | if (destinationHeap.Count == 0) break; |
| 28 | 140 | | var currentEdge = destinationHeap.Pop(out var hops); |
| 28 | 141 | | if (!destinationVisits.Add(currentEdge)) continue; |
| | 142 | |
|
| 28 | 143 | | if (!edgeEnumerator.MoveTo(currentEdge.id, currentEdge.forward)) |
| 0 | 144 | | throw new Exception("Enumeration attempted to an edge that does not exist"); |
| | 145 | | #if DEBUG |
| | 146 | | // TODO: if we queue the tail vertex then we can avoid this move. |
| 28 | 147 | | var currentCanMove = costFunction.GetIslandBuilderCost(edgeEnumerator); |
| 28 | 148 | | if (!currentCanMove) |
| 0 | 149 | | throw new Exception( |
| 0 | 150 | | "Queued edge always has to be traversable in the opposite queued direction in towards search"); |
| | 151 | | #endif |
| | 152 | |
|
| | 153 | | // enumerate the neighbours at the tail and propagate labels if a |
| | 154 | | // move is possible neighbour -> tail -> current. |
| | 155 | | // TODO: queue previous edges too to be able to handle more complex restrictions. |
| 28 | 156 | | var previousEdges = |
| 28 | 157 | | new (EdgeId edgeId, byte? turn)[] { (edgeEnumerator.EdgeId, edgeEnumerator.TailOrder) }; |
| | 158 | |
|
| | 159 | | // notify usage of vertex before loading neighbours. |
| 28 | 160 | | await network.UsageNotifier.NotifyVertex(network, edgeEnumerator.Tail, cancellationToken); |
| 28 | 161 | | if (cancellationToken.IsCancellationRequested) return null; |
| | 162 | |
|
| | 163 | | // enumerate the neighbours and see if the label propagates. |
| 28 | 164 | | if (!edgeEnumerator.MoveTo(edgeEnumerator.Tail)) throw new Exception("Vertex does not exist"); |
| 67 | 165 | | while (edgeEnumerator.MoveNext()) |
| 44 | 166 | | { |
| 72 | 167 | | if (edgeEnumerator.EdgeId == currentEdge.id) continue; |
| | 168 | |
|
| | 169 | | // see the turn neighbour -> tail -> current is possible. |
| | 170 | | // this means we need to check the neighbour in it's current backward direction and current in forwa |
| 16 | 171 | | var canMove = costFunction.GetIslandBuilderCost(edgeEnumerator, false, previousEdges); |
| 18 | 172 | | if (!canMove) continue; |
| | 173 | |
|
| | 174 | | // get label for the current edge, we get it here because it could have changed during neighbour pro |
| 14 | 175 | | if (!labels.TryGetWithDetails(currentEdge.id, out var currentLabelDetails)) |
| 0 | 176 | | throw new Exception("Current should already have been assigned a label"); |
| | 177 | |
|
| | 178 | | // get neighbour label, if it exists already. |
| 14 | 179 | | var neighbourLabelDetails = labels.GetOrCreateLabel(edgeEnumerator, isOnIslandAlready); |
| | 180 | |
|
| | 181 | | // a connection can be made, the path neighbour -> current is possible. |
| 14 | 182 | | var madeConnection = labels.ConnectTo(neighbourLabelDetails.label, currentLabelDetails.label); |
| 14 | 183 | | if (!madeConnection) |
| 13 | 184 | | { |
| | 185 | | // check if there is a bidirectional link. |
| 13 | 186 | | var canMoveOtherDirection = |
| 13 | 187 | | costFunction.GetIslandBuilderCost(edgeEnumerator, true, previousEdges); |
| 13 | 188 | | if (canMoveOtherDirection) |
| 9 | 189 | | { |
| 9 | 190 | | labels.ConnectTo(currentLabelDetails.label, neighbourLabelDetails.label); |
| 9 | 191 | | madeConnection = true; |
| 9 | 192 | | } |
| 13 | 193 | | } |
| | 194 | |
|
| 14 | 195 | | if (madeConnection) |
| 10 | 196 | | { |
| | 197 | | // check root island again, it could have grown. |
| 10 | 198 | | if (labels.TryGetWithDetails(edgeId, out rootIsland)) |
| 10 | 199 | | { |
| 10 | 200 | | if (rootIsland.size >= network.IslandManager.MaxIslandSize) |
| 5 | 201 | | { |
| 5 | 202 | | if (rootIsland.label != IslandLabels.NotAnIslandLabel) |
| 0 | 203 | | throw new Exception( |
| 0 | 204 | | "A large island without the not-an-island label should not exist"); |
| 5 | 205 | | return false; |
| | 206 | | } |
| | 207 | |
|
| 5 | 208 | | if (rootIsland.final) |
| 0 | 209 | | return true; // the island can never ever get bigger anymore. |
| 5 | 210 | | } |
| | 211 | |
|
| | 212 | | // if not, get the neighbour label again, it is now different. |
| 5 | 213 | | if (!labels.TryGetWithDetails(edgeEnumerator.EdgeId, out neighbourLabelDetails)) |
| 0 | 214 | | throw new Exception("This should always exist at this point"); |
| 5 | 215 | | } |
| | 216 | |
|
| 9 | 217 | | if (neighbourLabelDetails.final) |
| 3 | 218 | | { |
| 3 | 219 | | originHasNonIsland = originHasNonIsland || |
| 3 | 220 | | neighbourLabelDetails.label == IslandLabels.NotAnIslandLabel; |
| | 221 | |
|
| | 222 | | // if the neighbour already has a final label there are only two possibilities here: |
| | 223 | | // - this neighbour is an island, a connection with a non-island will never be made, no need to |
| | 224 | | // - this neighbour is not an island, a connection with a non-island edge was made, no need to s |
| 3 | 225 | | continue; |
| | 226 | | } |
| | 227 | |
|
| | 228 | | // add the neighbour to the queue, but in the opposite direction as currently enumerated. |
| 6 | 229 | | var neighbourHops = hops + 1; |
| 6 | 230 | | destinationHeap.Push((edgeEnumerator.EdgeId, !edgeEnumerator.Forward), |
| 6 | 231 | | neighbourHops); |
| 6 | 232 | | } |
| | 233 | |
|
| 23 | 234 | | break; |
| | 235 | | } |
| | 236 | |
|
| | 237 | | // do away. |
| 24 | 238 | | while (true) |
| 24 | 239 | | { |
| 24 | 240 | | if (cancellationToken.IsCancellationRequested) return null; |
| | 241 | |
|
| 24 | 242 | | if (originHeap.Count == 0) break; |
| 24 | 243 | | var currentEdge = originHeap.Pop(out var hops); |
| 24 | 244 | | if (!originVisits.Add(currentEdge)) continue; |
| | 245 | |
|
| 24 | 246 | | if (!edgeEnumerator.MoveTo(currentEdge.id, currentEdge.forward)) |
| 0 | 247 | | throw new Exception("Enumeration attempted to an edge that does not exist"); |
| | 248 | | #if DEBUG |
| | 249 | | // TODO: if we queue the tail vertex then we can avoid this move. |
| 24 | 250 | | var currentCanMove = costFunction.GetIslandBuilderCost(edgeEnumerator); |
| 24 | 251 | | if (!currentCanMove) |
| 0 | 252 | | throw new Exception( |
| 0 | 253 | | "Queued edge always has to be traversable in the opposite queued direction in towards search"); |
| | 254 | | #endif |
| | 255 | |
|
| | 256 | | // enumerate the neighbours at the tail and propagate labels if a |
| | 257 | | // move is possible current -> head -> neighbour. |
| | 258 | | // TODO: queue previous edges too to be able to handle more complex restrictions. |
| 24 | 259 | | var previousEdges = |
| 24 | 260 | | new (EdgeId edgeId, byte? turn)[] { (edgeEnumerator.EdgeId, edgeEnumerator.HeadOrder) }; |
| | 261 | |
|
| | 262 | | // notify usage of vertex before loading neighbours. |
| 24 | 263 | | await network.UsageNotifier.NotifyVertex(network, edgeEnumerator.Head, cancellationToken); |
| 24 | 264 | | if (cancellationToken.IsCancellationRequested) return null; |
| | 265 | |
|
| | 266 | | // enumerate the neighbours and see if the label propagates. |
| 24 | 267 | | if (!edgeEnumerator.MoveTo(edgeEnumerator.Head)) throw new Exception("Vertex does not exist"); |
| 54 | 268 | | while (edgeEnumerator.MoveNext()) |
| 34 | 269 | | { |
| 54 | 270 | | if (edgeEnumerator.EdgeId == currentEdge.id) continue; |
| | 271 | |
|
| | 272 | | // see the turn current -> head -> neighbour is possible. |
| 14 | 273 | | var canMove = costFunction.GetIslandBuilderCost(edgeEnumerator, true, previousEdges); |
| 19 | 274 | | if (!canMove) continue; |
| | 275 | |
|
| | 276 | | // get label for the current edge, we get it here because it could have changed during neighbour pro |
| 9 | 277 | | if (!labels.TryGetWithDetails(currentEdge.id, out var currentLabelDetails)) |
| 0 | 278 | | throw new Exception("Current should already have been assigned a label"); |
| | 279 | |
|
| | 280 | | // get neighbour label, if it exists already. |
| 9 | 281 | | var neighbourLabelDetails = labels.GetOrCreateLabel(edgeEnumerator, isOnIslandAlready); |
| | 282 | |
|
| | 283 | | // a connection can be made, the path current -> neighbour is possible. |
| 9 | 284 | | var madeConnection = labels.ConnectTo(currentLabelDetails.label, neighbourLabelDetails.label); |
| 9 | 285 | | if (!madeConnection) |
| 9 | 286 | | { |
| | 287 | | // check if there is a bidirectional link. |
| 9 | 288 | | var canMoveOtherDirection = |
| 9 | 289 | | costFunction.GetIslandBuilderCost(edgeEnumerator, false, previousEdges); |
| 9 | 290 | | if (canMoveOtherDirection) |
| 7 | 291 | | { |
| 7 | 292 | | labels.ConnectTo(neighbourLabelDetails.label, currentLabelDetails.label); |
| 7 | 293 | | madeConnection = true; |
| 7 | 294 | | } |
| 9 | 295 | | } |
| | 296 | |
|
| 9 | 297 | | if (madeConnection) |
| 7 | 298 | | { |
| | 299 | | // check root island again, it could have grown. |
| 7 | 300 | | if (labels.TryGetWithDetails(edgeId, out rootIsland)) |
| 7 | 301 | | { |
| 7 | 302 | | if (rootIsland.size >= network.IslandManager.MaxIslandSize) |
| 4 | 303 | | { |
| 4 | 304 | | if (rootIsland.label != IslandLabels.NotAnIslandLabel) |
| 0 | 305 | | throw new Exception( |
| 0 | 306 | | "A large island without the not-an-island label should not exist"); |
| 4 | 307 | | return false; |
| | 308 | | } |
| | 309 | |
|
| 3 | 310 | | if (rootIsland.final) |
| 0 | 311 | | return true; // the island can never ever get bigger anymore. |
| 3 | 312 | | } |
| | 313 | |
|
| | 314 | | // if not, get the neighbour label again, it is now different. |
| 3 | 315 | | if (!labels.TryGetWithDetails(edgeEnumerator.EdgeId, out neighbourLabelDetails)) |
| 0 | 316 | | throw new Exception("This should always exist at this point"); |
| 3 | 317 | | } |
| | 318 | |
|
| 5 | 319 | | if (neighbourLabelDetails.final) |
| 0 | 320 | | { |
| 0 | 321 | | destinationHasNonIsland = destinationHasNonIsland || |
| 0 | 322 | | neighbourLabelDetails.label == IslandLabels.NotAnIslandLabel; |
| | 323 | |
|
| | 324 | | // if the neighbour already has a final label there are only two possibilities here: |
| | 325 | | // - this neighbour is an island, a connection with a non-island will never be made, no need to |
| | 326 | | // - this neighbour is not an island, a connection with a non-island edge was made, no need to s |
| 0 | 327 | | continue; |
| | 328 | | } |
| | 329 | |
|
| | 330 | | // add the neighbour to the queue. |
| 5 | 331 | | var neighbourHops = hops + 1; |
| 5 | 332 | | originHeap.Push((edgeEnumerator.EdgeId, edgeEnumerator.Forward), |
| 5 | 333 | | neighbourHops); |
| 5 | 334 | | } |
| | 335 | |
|
| 20 | 336 | | break; |
| | 337 | | } |
| 20 | 338 | | } |
| | 339 | |
|
| 8 | 340 | | if (!labels.TryGetWithDetails(edgeId, out rootIsland)) throw new Exception("Root should have an island here"); |
| 8 | 341 | | if (rootIsland.size >= network.IslandManager.MaxIslandSize) |
| 0 | 342 | | throw new Exception("This should have been tested before"); |
| 8 | 343 | | if (rootIsland.final) throw new Exception("This should have been tested before"); |
| | 344 | |
|
| | 345 | | // island cannot get bigger anymore. |
| | 346 | | // it has also not reached the min size. |
| 8 | 347 | | labels.SetAsFinal(rootIsland.label); |
| 8 | 348 | | return true; |
| 24 | 349 | | } |
| | 350 | |
|
| | 351 | | // private static async Task WriteGeoJson(RoutingNetwork network, IslandLabels labels, EdgeId edge) |
| | 352 | | // { |
| | 353 | | // await File.WriteAllTextAsync($"island_labels_{edge.LocalId}_{edge.TileId}_{(long)(DateTime.UtcNow - DateTime. |
| | 354 | | // await labels.ToGeoJson(network)); |
| | 355 | | // } |
| | 356 | | } |