| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Runtime.CompilerServices; |
| | | 5 | | using System.Threading; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | using Itinero.Network; |
| | | 8 | | using Itinero.Routes.Paths; |
| | | 9 | | using Itinero.Routing.DataStructures; |
| | | 10 | | using Itinero.Snapping; |
| | | 11 | | |
| | | 12 | | [assembly: InternalsVisibleTo("Itinero.Tests")] |
| | | 13 | | [assembly: InternalsVisibleTo("Itinero.Tests.Benchmarks")] |
| | | 14 | | [assembly: InternalsVisibleTo("Itinero.Tests.Functional")] |
| | | 15 | | |
| | | 16 | | namespace Itinero.Routing.Flavours.Dijkstra; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// An edge-based dijkstra implementation. |
| | | 20 | | /// |
| | | 21 | | /// When <c>isMainN</c> is supplied to <see cref="RunAsync"/>, the search is access-aware: |
| | | 22 | | /// each label carries a sticky <c>leftMain</c> bit that flips true on the first |
| | | 23 | | /// main → non-main transition, after which the search refuses to relax onto any |
| | | 24 | | /// edge classified as main-N. This enforces the "L-edge is fine iff it is the |
| | | 25 | | /// only route to destination/origin" rule structurally — main-N may only host |
| | | 26 | | /// a contiguous middle segment of the path. See the design note for the five |
| | | 27 | | /// valid path shapes this admits. |
| | | 28 | | /// |
| | | 29 | | /// When <c>isMainN</c> is null the search reduces to the classic edge-based |
| | | 30 | | /// Dijkstra: <c>leftMain</c> stays false everywhere and no relaxations are |
| | | 31 | | /// rejected on access grounds. |
| | | 32 | | /// </summary> |
| | | 33 | | internal class Dijkstra |
| | | 34 | | { |
| | 11571 | 35 | | private readonly PathTree _tree = new(); |
| | 11571 | 36 | | private readonly HashSet<(EdgeId edgeId, VertexId vertexId, bool leftMain)> _visits = new(); |
| | 11571 | 37 | | private readonly BinaryHeap<(uint pointer, EdgeId edgeId, VertexId vertexId, bool leftMain)> _heap = new(); |
| | | 38 | | |
| | | 39 | | public async Task<(Path? path, double cost)> RunAsync(RoutingNetwork network, SnapPoint source, |
| | | 40 | | SnapPoint target, |
| | | 41 | | DijkstraWeightFunc getDijkstraWeight, |
| | | 42 | | Func<(EdgeId edgeId, VertexId vertexId), Task<bool>>? settled = null, |
| | | 43 | | Func<(EdgeId edgeId, VertexId vertexId), Task<bool>>? queued = null, |
| | | 44 | | CancellationToken cancellationToken = default, |
| | | 45 | | IsMainNFunc? isMainN = null) |
| | 0 | 46 | | { |
| | 0 | 47 | | var paths = await this.RunAsync(network, (source, null), new[] { (target, (bool?)null) }, getDijkstraWeight, |
| | 0 | 48 | | settled, queued, cancellationToken, isMainN); |
| | | 49 | | |
| | 0 | 50 | | return paths.Length < 1 ? (null, double.MaxValue) : paths[0]; |
| | 0 | 51 | | } |
| | | 52 | | |
| | | 53 | | public async Task<(Path? path, double cost)> RunAsync(RoutingNetwork network, |
| | | 54 | | (SnapPoint sp, bool? direction) source, |
| | | 55 | | (SnapPoint sp, bool? direction) target, |
| | | 56 | | DijkstraWeightFunc getDijkstraWeight, |
| | | 57 | | Func<(EdgeId edgeId, VertexId vertexId), Task<bool>>? settled = null, |
| | | 58 | | Func<(EdgeId edgeId, VertexId vertexId), Task<bool>>? queued = null, |
| | | 59 | | CancellationToken cancellationToken = default, |
| | | 60 | | IsMainNFunc? isMainN = null) |
| | 22 | 61 | | { |
| | 22 | 62 | | var paths = await this.RunAsync(network, source, new[] { target }, getDijkstraWeight, settled, queued, cancellat |
| | | 63 | | |
| | 22 | 64 | | return paths.Length < 1 ? (null, double.MaxValue) : paths[0]; |
| | 22 | 65 | | } |
| | | 66 | | |
| | | 67 | | public async Task<(Path? path, double cost)[]> RunAsync(RoutingNetwork network, SnapPoint source, |
| | | 68 | | IReadOnlyList<SnapPoint> targets, |
| | | 69 | | DijkstraWeightFunc getDijkstraWeight, |
| | | 70 | | Func<(EdgeId edgeId, VertexId vertexId), Task<bool>>? settled = null, |
| | | 71 | | Func<(EdgeId edgeId, VertexId vertexId), Task<bool>>? queued = null, |
| | | 72 | | CancellationToken cancellationToken = default, |
| | | 73 | | IsMainNFunc? isMainN = null) |
| | 11542 | 74 | | { |
| | 11542 | 75 | | var directedTargets = new (SnapPoint sp, bool? direction)[targets.Count]; |
| | 402636 | 76 | | for (var i = 0; i < targets.Count; i++) |
| | 189776 | 77 | | { |
| | 189776 | 78 | | directedTargets[i] = (targets[i], null); |
| | 189776 | 79 | | } |
| | | 80 | | |
| | 11542 | 81 | | return await this.RunAsync(network, (source, null), directedTargets, |
| | 11542 | 82 | | getDijkstraWeight, settled, queued, cancellationToken, isMainN); |
| | 11542 | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// |
| | | 87 | | /// </summary> |
| | | 88 | | /// <param name="network"></param> |
| | | 89 | | /// <param name="source"></param> |
| | | 90 | | /// <param name="targets"></param> |
| | | 91 | | /// <param name="getDijkstraWeight"></param> |
| | | 92 | | /// <param name="settled">This Callback is called for every edge for which the minimal cost is known. If this callba |
| | | 93 | | /// <param name="queued">This callback is called before an edge is loaded. Should not be used to influence route pla |
| | | 94 | | /// <param name="isMainN">Optional access-aware predicate. When supplied the search tracks a |
| | | 95 | | /// sticky <c>leftMain</c> bit and rejects any relaxation back onto a main-N edge after the |
| | | 96 | | /// first main → non-main transition.</param> |
| | | 97 | | /// <returns></returns> |
| | | 98 | | /// <exception cref="Exception"></exception> |
| | | 99 | | public async Task<(Path? path, double cost)[]> RunAsync(RoutingNetwork network, |
| | | 100 | | (SnapPoint sp, bool? direction) source, |
| | | 101 | | IReadOnlyList<(SnapPoint sp, bool? direction)> targets, |
| | | 102 | | DijkstraWeightFunc getDijkstraWeight, |
| | | 103 | | Func<(EdgeId edgeId, VertexId vertexId), Task<bool>>? settled = null, |
| | | 104 | | Func<(EdgeId edgeId, VertexId vertexId), Task<bool>>? queued = null, |
| | | 105 | | CancellationToken cancellationToken = default, |
| | | 106 | | IsMainNFunc? isMainN = null) |
| | 11571 | 107 | | { |
| | | 108 | | static double GetWorst((uint pointer, double cost)[] targets) |
| | 133845 | 109 | | { |
| | 133845 | 110 | | var worst = 0d; |
| | 776532 | 111 | | for (var i = 0; i < targets.Length; i++) |
| | 383953 | 112 | | { |
| | 383953 | 113 | | if (!(targets[i].cost > worst)) |
| | 154873 | 114 | | { |
| | 154873 | 115 | | continue; |
| | | 116 | | } |
| | | 117 | | |
| | 229080 | 118 | | worst = targets[i].cost; |
| | 229080 | 119 | | if (worst >= double.MaxValue) |
| | 129532 | 120 | | { |
| | 129532 | 121 | | break; |
| | | 122 | | } |
| | 99548 | 123 | | } |
| | | 124 | | |
| | 133845 | 125 | | return worst; |
| | 133845 | 126 | | } |
| | | 127 | | |
| | 11571 | 128 | | var enumerator = network.GetEdgeEnumerator(); |
| | | 129 | | |
| | 11571 | 130 | | _tree.Clear(); |
| | 11571 | 131 | | _visits.Clear(); |
| | 11571 | 132 | | _heap.Clear(); |
| | | 133 | | |
| | | 134 | | // add sources. leftMain starts false on every source: the search has not yet |
| | | 135 | | // transitioned out of main-N (or has not yet entered, when source is non-main). |
| | 11571 | 136 | | var sourceForwardVisit = uint.MaxValue; |
| | 11571 | 137 | | if (source.Forward()) |
| | 11571 | 138 | | { |
| | | 139 | | // add forward. |
| | 11571 | 140 | | if (!enumerator.MoveTo(source.sp.EdgeId, true)) |
| | 0 | 141 | | { |
| | 0 | 142 | | throw new Exception($"Edge in source {source} not found!"); |
| | | 143 | | } |
| | | 144 | | |
| | 11571 | 145 | | var sourceFwd = getDijkstraWeight(enumerator, default(PreviousEdgeEnumerable)); |
| | 11571 | 146 | | if (sourceFwd.cost > 0) |
| | 11571 | 147 | | { |
| | | 148 | | // can traverse edge in the forward direction. |
| | 11571 | 149 | | var sourceOffsetCostForward = sourceFwd.cost * (1 - source.sp.OffsetFactor()); |
| | 11571 | 150 | | sourceForwardVisit = |
| | 11571 | 151 | | _tree.AddVisit(enumerator, leftMain: false, localAccess: sourceFwd.localAccess, uint.MaxValue); |
| | 11571 | 152 | | _heap.Push((sourceForwardVisit, enumerator.EdgeId, enumerator.Head, false), sourceOffsetCostForward); |
| | 11571 | 153 | | } |
| | 11571 | 154 | | } |
| | | 155 | | |
| | 11571 | 156 | | var sourceBackwardVisit = uint.MaxValue; |
| | 11571 | 157 | | if (source.Backward()) |
| | 11566 | 158 | | { |
| | | 159 | | // add backward. |
| | 11566 | 160 | | if (!enumerator.MoveTo(source.sp.EdgeId, false)) |
| | 0 | 161 | | { |
| | 0 | 162 | | throw new Exception($"Edge in source {source} not found!"); |
| | | 163 | | } |
| | | 164 | | |
| | 11566 | 165 | | var sourceBwd = getDijkstraWeight(enumerator, default(PreviousEdgeEnumerable)); |
| | 11566 | 166 | | if (sourceBwd.cost > 0) |
| | 8343 | 167 | | { |
| | | 168 | | // can traverse edge in the backward direction. |
| | 8343 | 169 | | var sourceOffsetCostBackward = sourceBwd.cost * source.sp.OffsetFactor(); |
| | 8343 | 170 | | sourceBackwardVisit = |
| | 8343 | 171 | | _tree.AddVisit(enumerator, leftMain: false, localAccess: sourceBwd.localAccess, uint.MaxValue); |
| | 8343 | 172 | | _heap.Push((sourceBackwardVisit, enumerator.EdgeId, enumerator.Head, false), sourceOffsetCostBackward); |
| | 8343 | 173 | | } |
| | 11566 | 174 | | } |
| | | 175 | | |
| | | 176 | | // add targets. |
| | 11571 | 177 | | var bestTargets = new (uint pointer, double cost)[targets.Count]; |
| | 11571 | 178 | | var targetsPerVertex = new Dictionary<VertexId, List<int>>(); |
| | 402764 | 179 | | for (var t = 0; t < targets.Count; t++) |
| | 189811 | 180 | | { |
| | 189811 | 181 | | bestTargets[t] = (uint.MaxValue, double.MaxValue); |
| | 189811 | 182 | | var target = targets[t]; |
| | | 183 | | |
| | 189811 | 184 | | if (target.Forward()) |
| | 189808 | 185 | | { |
| | | 186 | | // add forward. |
| | 189808 | 187 | | if (!enumerator.MoveTo(target.sp.EdgeId, true)) |
| | 0 | 188 | | { |
| | 0 | 189 | | throw new Exception($"Edge in target {target} not found!"); |
| | | 190 | | } |
| | | 191 | | |
| | 189808 | 192 | | var targetCostForward = getDijkstraWeight(enumerator, default(PreviousEdgeEnumerable)) |
| | 189808 | 193 | | .cost; |
| | 189808 | 194 | | if (targetCostForward > 0) |
| | 189808 | 195 | | { |
| | 189808 | 196 | | if (!targetsPerVertex.TryGetValue(enumerator.Tail, out var targetsAtVertex)) |
| | 110335 | 197 | | { |
| | 110335 | 198 | | targetsAtVertex = new List<int>(); |
| | 110335 | 199 | | targetsPerVertex[enumerator.Tail] = targetsAtVertex; |
| | 110335 | 200 | | } |
| | | 201 | | |
| | 189808 | 202 | | targetsAtVertex.Add(t); |
| | 189808 | 203 | | } |
| | 189808 | 204 | | } |
| | | 205 | | |
| | 189811 | 206 | | if (target.Backward()) |
| | 189809 | 207 | | { |
| | | 208 | | // add backward. |
| | 189809 | 209 | | if (!enumerator.MoveTo(target.sp.EdgeId, false)) |
| | 0 | 210 | | { |
| | 0 | 211 | | throw new Exception($"Edge in source {source} not found!"); |
| | | 212 | | } |
| | | 213 | | |
| | 189809 | 214 | | var targetCostBackward = |
| | 189809 | 215 | | getDijkstraWeight(enumerator, default(PreviousEdgeEnumerable)).cost; |
| | 189809 | 216 | | if (targetCostBackward > 0) |
| | 102031 | 217 | | { |
| | 102031 | 218 | | if (!targetsPerVertex.TryGetValue(enumerator.Tail, out var targetsAtVertex)) |
| | 68899 | 219 | | { |
| | 68899 | 220 | | targetsAtVertex = new List<int>(); |
| | 68899 | 221 | | targetsPerVertex[enumerator.Tail] = targetsAtVertex; |
| | 68899 | 222 | | } |
| | | 223 | | |
| | 102031 | 224 | | targetsAtVertex.Add(t); |
| | 102031 | 225 | | } |
| | 189809 | 226 | | } |
| | | 227 | | |
| | | 228 | | // consider paths 'within' a single edge. |
| | 189811 | 229 | | if (source.sp.EdgeId != target.sp.EdgeId) |
| | 180026 | 230 | | { |
| | 180026 | 231 | | continue; |
| | | 232 | | } |
| | | 233 | | |
| | 9785 | 234 | | if (source.sp.Offset == target.sp.Offset) |
| | 5092 | 235 | | { |
| | | 236 | | // source and target are identical. |
| | 5092 | 237 | | if (sourceForwardVisit != uint.MaxValue && |
| | 5092 | 238 | | target.Forward()) |
| | 5091 | 239 | | { |
| | 5091 | 240 | | bestTargets[t] = (sourceForwardVisit, 0); |
| | 5091 | 241 | | } |
| | 1 | 242 | | else if (sourceBackwardVisit != uint.MaxValue && |
| | 1 | 243 | | target.Backward()) |
| | 0 | 244 | | { |
| | 0 | 245 | | bestTargets[t] = (sourceForwardVisit, 0); |
| | 0 | 246 | | } |
| | 5092 | 247 | | } |
| | 4693 | 248 | | else if (source.sp.Offset < target.sp.Offset && |
| | 4693 | 249 | | source.Forward() && target.Forward()) |
| | 2865 | 250 | | { |
| | | 251 | | // the source is earlier in the direction of the edge |
| | | 252 | | // and the edge can be traversed in this direction. |
| | 2865 | 253 | | if (!enumerator.MoveTo(source.sp.EdgeId, true)) |
| | 0 | 254 | | { |
| | 0 | 255 | | throw new Exception($"Edge in source {source} not found!"); |
| | | 256 | | } |
| | | 257 | | |
| | 2865 | 258 | | var weight = getDijkstraWeight(enumerator, default(PreviousEdgeEnumerable)).cost * |
| | 2865 | 259 | | (target.sp.OffsetFactor() - source.sp.OffsetFactor()); |
| | 2865 | 260 | | bestTargets[t] = (sourceForwardVisit, weight); |
| | 2865 | 261 | | } |
| | 1828 | 262 | | else if (source.sp.Offset > target.sp.Offset && |
| | 1828 | 263 | | source.Backward() && target.Backward()) |
| | 1825 | 264 | | { |
| | | 265 | | // the source is earlier against the direction of the edge |
| | | 266 | | // and the edge can be traversed in this direction. |
| | 1825 | 267 | | if (!enumerator.MoveTo(source.sp.EdgeId, false)) |
| | 0 | 268 | | { |
| | 0 | 269 | | throw new Exception($"Edge in source {source} not found!"); |
| | | 270 | | } |
| | | 271 | | |
| | 1825 | 272 | | var weight = getDijkstraWeight(enumerator, default(PreviousEdgeEnumerable)).cost * |
| | 1825 | 273 | | (source.sp.OffsetFactor() - target.sp.OffsetFactor()); |
| | 1825 | 274 | | bestTargets[t] = (sourceBackwardVisit, weight); |
| | 1825 | 275 | | } |
| | 9785 | 276 | | } |
| | | 277 | | |
| | | 278 | | // update worst target cost. |
| | 11571 | 279 | | var worstTargetCost = GetWorst(bestTargets); |
| | | 280 | | |
| | | 281 | | // keep going until heap is empty. |
| | 313389 | 282 | | while (_heap.Count > 0) |
| | 306306 | 283 | | { |
| | 306306 | 284 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 285 | | |
| | | 286 | | // dequeue new visit. Dedup includes leftMain because the same (edge,vertex) |
| | | 287 | | // is reachable both with leftMain=false and leftMain=true; the former is |
| | | 288 | | // strictly more permissive but both can occur in the search. |
| | 306306 | 289 | | var currentEntry = _heap.Pop(out var currentCost); |
| | 394189 | 290 | | while (_visits.Contains((currentEntry.edgeId, currentEntry.vertexId, currentEntry.leftMain))) |
| | 89858 | 291 | | { |
| | | 292 | | // visited before, skip. |
| | 89858 | 293 | | if (_heap.Count == 0) |
| | 1975 | 294 | | { |
| | 1975 | 295 | | currentEntry = (uint.MaxValue, default, default, false); |
| | 1975 | 296 | | break; |
| | | 297 | | } |
| | | 298 | | |
| | 87883 | 299 | | currentEntry = _heap.Pop(out currentCost); |
| | 87883 | 300 | | } |
| | | 301 | | |
| | 306306 | 302 | | var currentPointer = currentEntry.pointer; |
| | 306306 | 303 | | if (currentPointer == uint.MaxValue) |
| | 1975 | 304 | | { |
| | 1975 | 305 | | break; |
| | | 306 | | } |
| | | 307 | | |
| | | 308 | | // only call GetVisitWithState after the visited check passes. |
| | 304331 | 309 | | var currentVisit = _tree.GetVisitWithState(currentPointer); |
| | | 310 | | |
| | | 311 | | // log visit. |
| | 304331 | 312 | | if (currentVisit.previousPointer != uint.MaxValue) |
| | 284971 | 313 | | { |
| | 284971 | 314 | | _visits.Add((currentEntry.edgeId, currentEntry.vertexId, currentEntry.leftMain)); |
| | 284971 | 315 | | } |
| | | 316 | | |
| | 304331 | 317 | | if (settled != null && await settled((currentEntry.edgeId, currentEntry.vertexId))) |
| | 51273 | 318 | | { |
| | | 319 | | // the best cost to this edge has already been found; current visit can not improve this anymore so we c |
| | 51273 | 320 | | continue; |
| | | 321 | | } |
| | | 322 | | |
| | | 323 | | // check if the search needs to stop. |
| | 253058 | 324 | | if (currentCost > worstTargetCost) |
| | 2513 | 325 | | { |
| | | 326 | | // impossible to improve on cost to any target. |
| | 2513 | 327 | | break; |
| | | 328 | | } |
| | | 329 | | |
| | | 330 | | // check neighbours. |
| | 250545 | 331 | | if (!enumerator.MoveTo(currentVisit.vertex)) |
| | 0 | 332 | | { |
| | | 333 | | // no edges, move on! |
| | 0 | 334 | | continue; |
| | | 335 | | } |
| | | 336 | | |
| | | 337 | | // check if this is a target. |
| | 250545 | 338 | | if (!targetsPerVertex.TryGetValue(currentVisit.vertex, out var targetsAtVertex)) |
| | 111428 | 339 | | { |
| | 111428 | 340 | | targetsAtVertex = null; |
| | 111428 | 341 | | } |
| | | 342 | | |
| | 1045507 | 343 | | while (enumerator.MoveNext()) |
| | 794962 | 344 | | { |
| | | 345 | | // filter out if u-turns or visits on the same edge. |
| | 794962 | 346 | | var neighbourEdge = enumerator.EdgeId; |
| | 794962 | 347 | | if (neighbourEdge == currentVisit.edge) |
| | 250545 | 348 | | { |
| | 250545 | 349 | | continue; |
| | | 350 | | } |
| | | 351 | | |
| | | 352 | | // gets the cost of the current edge. |
| | 544417 | 353 | | var (neighbourCost, turnCost, neighbourLocalAccess) = |
| | 544417 | 354 | | getDijkstraWeight(enumerator, new PreviousEdgeEnumerable(_tree, currentPointer)); |
| | 544417 | 355 | | if (neighbourCost is >= double.MaxValue or <= 0) |
| | 154190 | 356 | | { |
| | 154190 | 357 | | continue; |
| | | 358 | | } |
| | | 359 | | |
| | 390227 | 360 | | if (turnCost is >= double.MaxValue or < 0) |
| | 11 | 361 | | { |
| | 11 | 362 | | continue; |
| | | 363 | | } |
| | | 364 | | |
| | | 365 | | // Access-aware state-bit logic. When isMainN is null this block is a no-op: |
| | | 366 | | // newLeftMain stays false and no relaxation is rejected. |
| | 390216 | 367 | | var newLeftMain = false; |
| | 390216 | 368 | | if (isMainN != null) |
| | 390195 | 369 | | { |
| | 390195 | 370 | | var prevMain = IsMain(currentVisit.edge, currentVisit.localAccess, currentVisit.leftMain, isMainN); |
| | 390195 | 371 | | var neighbourMain = IsMain(neighbourEdge, neighbourLocalAccess, currentVisit.leftMain, isMainN); |
| | | 372 | | |
| | | 373 | | // Rule: once leftMain is true we may not relax onto a main-N edge again. |
| | | 374 | | // That would mean re-entering main-N after having departed, i.e. an L-edge |
| | | 375 | | // (or non-main-N pocket) used as through-traffic between two main-N segments. |
| | 390196 | 376 | | if (currentVisit.leftMain && neighbourMain) continue; |
| | | 377 | | |
| | | 378 | | // newLeftMain is sticky and flips on the first main-N → non-main-N transition. |
| | 390194 | 379 | | newLeftMain = currentVisit.leftMain || (prevMain && !neighbourMain); |
| | 390194 | 380 | | } |
| | | 381 | | |
| | | 382 | | // if the vertex has targets, check if this edge is a match. |
| | 390215 | 383 | | var neighbourPointer = uint.MaxValue; |
| | 390215 | 384 | | if (targetsAtVertex != null) |
| | 221010 | 385 | | { |
| | | 386 | | // only consider targets when found for the 'from' vertex. |
| | | 387 | | // and when this in not a u-turn. |
| | 1638960 | 388 | | foreach (var t in targetsAtVertex) |
| | 487965 | 389 | | { |
| | 487965 | 390 | | var target = targets[t]; |
| | 487965 | 391 | | if (target.sp.EdgeId != neighbourEdge) |
| | 297180 | 392 | | { |
| | 297180 | 393 | | continue; |
| | | 394 | | } |
| | | 395 | | |
| | | 396 | | // check directions. |
| | 190785 | 397 | | if (enumerator.Forward && !target.Forward()) |
| | 0 | 398 | | { |
| | 0 | 399 | | continue; |
| | | 400 | | } |
| | | 401 | | |
| | 190785 | 402 | | if (!enumerator.Forward && !target.Backward()) |
| | 0 | 403 | | { |
| | 0 | 404 | | continue; |
| | | 405 | | } |
| | | 406 | | |
| | | 407 | | // there is a target on this edge, calculate the cost. |
| | | 408 | | // calculate the cost from the 'from' vertex to the target. |
| | 190785 | 409 | | var targetCost = enumerator.Forward |
| | 190785 | 410 | | ? neighbourCost * target.sp.OffsetFactor() |
| | 190785 | 411 | | : neighbourCost * (1 - target.sp.OffsetFactor()); |
| | | 412 | | // this is the case where the target is on this edge |
| | | 413 | | // and there is a path to 'from' before. |
| | 190785 | 414 | | targetCost += currentCost; |
| | | 415 | | |
| | 190785 | 416 | | targetCost += turnCost; |
| | | 417 | | |
| | | 418 | | // if this is an improvement, use it! |
| | 190785 | 419 | | var targetBestCost = bestTargets[t].cost; |
| | 190785 | 420 | | if (!(targetCost < targetBestCost)) |
| | 68511 | 421 | | { |
| | 68511 | 422 | | continue; |
| | | 423 | | } |
| | | 424 | | |
| | | 425 | | // this is an improvement. |
| | 122274 | 426 | | neighbourPointer = _tree.AddVisit(enumerator, newLeftMain, neighbourLocalAccess, currentPointer) |
| | 122274 | 427 | | bestTargets[t] = (neighbourPointer, targetCost); |
| | | 428 | | |
| | | 429 | | // update worst. |
| | 122274 | 430 | | worstTargetCost = GetWorst(bestTargets); |
| | 122274 | 431 | | } |
| | 221010 | 432 | | } |
| | | 433 | | |
| | 390215 | 434 | | if (queued != null && |
| | 390215 | 435 | | await queued((enumerator.EdgeId, enumerator.Head))) |
| | 0 | 436 | | { |
| | | 437 | | // don't queue this edge if the queued function returns true. |
| | 0 | 438 | | continue; |
| | | 439 | | } |
| | | 440 | | |
| | | 441 | | // add visit if not added yet. |
| | 390215 | 442 | | if (neighbourPointer == uint.MaxValue) |
| | 267944 | 443 | | { |
| | 267944 | 444 | | neighbourPointer = |
| | 267944 | 445 | | _tree.AddVisit(enumerator, newLeftMain, neighbourLocalAccess, currentPointer); |
| | 267944 | 446 | | } |
| | | 447 | | |
| | | 448 | | // add visit to heap. |
| | 390215 | 449 | | _heap.Push((neighbourPointer, enumerator.EdgeId, enumerator.Head, newLeftMain), neighbourCost + currentC |
| | 390215 | 450 | | } |
| | 250545 | 451 | | } |
| | | 452 | | |
| | 11571 | 453 | | var paths = new (Path? path, double cost)[targets.Count]; |
| | 402764 | 454 | | for (var p = 0; p < paths.Length; p++) |
| | 189811 | 455 | | { |
| | 189811 | 456 | | var bestTarget = bestTargets[p]; |
| | 189811 | 457 | | if (bestTarget.pointer == uint.MaxValue) |
| | 59497 | 458 | | { |
| | 59497 | 459 | | paths[p] = (null, double.MaxValue); |
| | 59497 | 460 | | continue; |
| | | 461 | | } |
| | | 462 | | |
| | | 463 | | // build resulting path. |
| | 130314 | 464 | | var path = new Path(network); |
| | 130314 | 465 | | var visit = _tree.GetVisit(bestTarget.pointer); |
| | | 466 | | |
| | | 467 | | // path is at least two edges. |
| | 711685 | 468 | | while (true) |
| | 711685 | 469 | | { |
| | 711685 | 470 | | if (visit.previousPointer == uint.MaxValue) |
| | 130314 | 471 | | { |
| | 130314 | 472 | | enumerator.MoveTo(visit.edge); |
| | 130314 | 473 | | path.Prepend(visit.edge, visit.forward); |
| | 130314 | 474 | | break; |
| | | 475 | | } |
| | | 476 | | |
| | 581371 | 477 | | path.Prepend(visit.edge, visit.forward); |
| | 581371 | 478 | | visit = _tree.GetVisit(visit.previousPointer); |
| | 581371 | 479 | | } |
| | | 480 | | |
| | | 481 | | // add the offsets. |
| | 130314 | 482 | | var target = targets[p]; |
| | 130314 | 483 | | path.Offset1 = path.First.direction ? source.sp.Offset : (ushort)(ushort.MaxValue - source.sp.Offset); |
| | 130314 | 484 | | path.Offset2 = path.Last.direction |
| | 130314 | 485 | | ? target.sp.Offset |
| | 130314 | 486 | | : (ushort)(ushort.MaxValue - target.sp.Offset); |
| | | 487 | | |
| | 130314 | 488 | | paths[p] = (path, bestTarget.cost); |
| | 130314 | 489 | | } |
| | | 490 | | |
| | 11571 | 491 | | return paths; |
| | 11571 | 492 | | } |
| | | 493 | | |
| | | 494 | | /// <summary> |
| | | 495 | | /// Hybrid "is main-N?" predicate. Consults <paramref name="isMainN"/> first; when that |
| | | 496 | | /// returns null (the tile isn't classified yet) falls back to <c>!leftMain</c>. The fallback |
| | | 497 | | /// is path-dependent: while we still believe we are in main (<c>leftMain == false</c>) an |
| | | 498 | | /// unclassified edge is treated as main, so no spurious leftMain transition fires; once we |
| | | 499 | | /// have left main, an unclassified edge is treated as non-main, so no spurious re-entry |
| | | 500 | | /// rejection fires. |
| | | 501 | | /// </summary> |
| | | 502 | | private static bool IsMain(EdgeId edgeId, bool localAccess, bool leftMain, IsMainNFunc isMainN) |
| | 780390 | 503 | | { |
| | 780390 | 504 | | var verdict = isMainN(edgeId, localAccess); |
| | 780390 | 505 | | return verdict ?? !leftMain; |
| | 780390 | 506 | | } |
| | | 507 | | |
| | | 508 | | /// <summary> |
| | | 509 | | /// Gets a fresh Dijkstra instance for this call. Was previously a [ThreadStatic] |
| | | 510 | | /// reuse, but that's unsafe under async/await + ThreadPool reuse: thread A starts |
| | | 511 | | /// a routing call on instance D, awaits a callback, returns to the pool, picks up |
| | | 512 | | /// a second routing call — Default returns the same D, and now two routing calls |
| | | 513 | | /// mutate D's _heap/_visits/_tree concurrently → "concurrent update" crash. |
| | | 514 | | /// Allocating three small collections per call is cheap vs. the routing work. |
| | | 515 | | /// </summary> |
| | 11571 | 516 | | public static Dijkstra Default => new(); |
| | | 517 | | } |