| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using Itinero.Data; |
| | | 5 | | using Itinero.Network.Tiles.Standalone.Global; |
| | | 6 | | using Itinero.Network.Writer; |
| | | 7 | | |
| | | 8 | | namespace Itinero.Network.Tiles.Standalone.Writer; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Extension methods related to writing standalone tiles to a network. |
| | | 12 | | /// </summary> |
| | | 13 | | public static class RoutingNetworkWriterExtensions |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Adds a tile in the form of a standalone tile to the network. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <param name="writer">The writer to write to.</param> |
| | | 19 | | /// <param name="tile">The tile to add.</param> |
| | | 20 | | /// <param name="globalIdSet">The global id set.</param> |
| | | 21 | | public static void AddStandaloneTile(this RoutingNetworkWriter writer, StandaloneNetworkTile tile, |
| | | 22 | | GlobalNetworkManager globalIdSet) |
| | 0 | 23 | | { |
| | | 24 | | // add the tile without boundary crossings. |
| | 0 | 25 | | writer.AddTile(tile.NetworkTile); |
| | | 26 | | |
| | | 27 | | // register all GlobalEdgeId → EdgeId mappings from the tile's internal edges. |
| | 0 | 28 | | var tileEnumerator = new NetworkTileEnumerator(); |
| | 0 | 29 | | tileEnumerator.MoveTo(tile.NetworkTile); |
| | 0 | 30 | | var tileId = tile.TileId; |
| | 0 | 31 | | for (uint v = 0; v < tile.NetworkTile.VertexCount; v++) |
| | 0 | 32 | | { |
| | 0 | 33 | | var vertexId = new VertexId(tileId, v); |
| | 0 | 34 | | if (!tileEnumerator.MoveTo(vertexId)) continue; |
| | | 35 | | |
| | 0 | 36 | | while (tileEnumerator.MoveNext()) |
| | 0 | 37 | | { |
| | | 38 | | // only process forward edges to avoid double registration. |
| | 0 | 39 | | if (!tileEnumerator.Forward) continue; |
| | | 40 | | |
| | 0 | 41 | | var globalEdgeId = tileEnumerator.GlobalEdgeId; |
| | 0 | 42 | | if (globalEdgeId != null) |
| | 0 | 43 | | { |
| | 0 | 44 | | globalIdSet.EdgeIdSet.Set(globalEdgeId.Value, tileEnumerator.EdgeId); |
| | 0 | 45 | | } |
| | 0 | 46 | | } |
| | 0 | 47 | | } |
| | | 48 | | |
| | | 49 | | // process boundary crossings. |
| | 0 | 50 | | foreach (var (isIncoming, globalEdgeId, vertex, attributes, edgeTypeId) in tile.GetBoundaryCrossings()) |
| | 0 | 51 | | { |
| | 0 | 52 | | if (globalIdSet.PendingBoundaryCrossings.TryGetValue(globalEdgeId, out var pending)) |
| | 0 | 53 | | { |
| | | 54 | | // match found - the other tile already loaded its half, create the boundary edge. |
| | | 55 | | EdgeId newEdge; |
| | 0 | 56 | | if (isIncoming) |
| | 0 | 57 | | { |
| | | 58 | | // isIncoming=true: vertex is at way tail, pending.vertex is at way head. |
| | 0 | 59 | | var length = writer.ComputeEdgeLength(vertex, pending.vertex); |
| | 0 | 60 | | newEdge = writer.AddEdge(vertex, pending.vertex, null, attributes, edgeTypeId, length, globalEdgeId) |
| | 0 | 61 | | } |
| | | 62 | | else |
| | 0 | 63 | | { |
| | | 64 | | // isIncoming=false: vertex is at way head, pending.vertex is at way tail. |
| | 0 | 65 | | var length = writer.ComputeEdgeLength(pending.vertex, vertex); |
| | 0 | 66 | | newEdge = writer.AddEdge(pending.vertex, vertex, null, attributes, edgeTypeId, length, globalEdgeId) |
| | 0 | 67 | | } |
| | | 68 | | |
| | | 69 | | // register boundary edge's GlobalEdgeId → EdgeId mapping. |
| | 0 | 70 | | globalIdSet.EdgeIdSet.Set(globalEdgeId, newEdge); |
| | 0 | 71 | | globalIdSet.PendingBoundaryCrossings.Remove(globalEdgeId); |
| | 0 | 72 | | } |
| | | 73 | | else |
| | 0 | 74 | | { |
| | | 75 | | // no match yet - store as pending (materialize attributes). |
| | 0 | 76 | | globalIdSet.PendingBoundaryCrossings[globalEdgeId] = |
| | 0 | 77 | | (vertex, attributes.ToArray(), edgeTypeId, isIncoming); |
| | 0 | 78 | | } |
| | 0 | 79 | | } |
| | | 80 | | |
| | | 81 | | // resolve global restrictions from this tile. |
| | 0 | 82 | | foreach (var (edges, isProhibitory, turnCostTypeId, restrictionAttributes) in tile.GetGlobalRestrictions()) |
| | 0 | 83 | | { |
| | 0 | 84 | | var globalRestriction = new GlobalRestriction( |
| | 0 | 85 | | edges.Select(e => e.globalEdgeId), |
| | 0 | 86 | | isProhibitory, |
| | 0 | 87 | | restrictionAttributes.ToArray()); |
| | | 88 | | |
| | 0 | 89 | | if (!TryResolveRestriction(globalRestriction, globalIdSet, writer)) |
| | 0 | 90 | | { |
| | 0 | 91 | | globalIdSet.PendingRestrictions.Add(globalRestriction); |
| | 0 | 92 | | } |
| | 0 | 93 | | } |
| | | 94 | | |
| | | 95 | | // retry pending restrictions with newly available edges. |
| | 0 | 96 | | for (var i = globalIdSet.PendingRestrictions.Count - 1; i >= 0; i--) |
| | 0 | 97 | | { |
| | 0 | 98 | | if (TryResolveRestriction(globalIdSet.PendingRestrictions[i], globalIdSet, writer)) |
| | 0 | 99 | | { |
| | 0 | 100 | | globalIdSet.PendingRestrictions.RemoveAt(i); |
| | 0 | 101 | | } |
| | 0 | 102 | | } |
| | 0 | 103 | | } |
| | | 104 | | |
| | | 105 | | private static bool TryResolveRestriction(GlobalRestriction globalRestriction, |
| | | 106 | | GlobalNetworkManager globalIdSet, RoutingNetworkWriter writer) |
| | 0 | 107 | | { |
| | | 108 | | // try to resolve all GlobalEdgeIds to EdgeIds. |
| | 0 | 109 | | if (!globalRestriction.TryBuildNetworkRestriction(GetEdge, out var networkRestriction)) |
| | 0 | 110 | | return false; |
| | | 111 | | |
| | 0 | 112 | | if (networkRestriction!.Count < 2) return true; |
| | | 113 | | |
| | | 114 | | // get last edge and determine turn cost vertex. |
| | 0 | 115 | | var last = networkRestriction[^1]; |
| | 0 | 116 | | var edgeEnumerator = writer.GetEdgeEnumerator(); |
| | 0 | 117 | | if (!edgeEnumerator.MoveTo(last.edge, last.forward)) |
| | 0 | 118 | | return false; |
| | 0 | 119 | | var turnCostVertex = edgeEnumerator.Tail; |
| | | 120 | | |
| | 0 | 121 | | var secondToLast = networkRestriction[^2]; |
| | | 122 | | |
| | 0 | 123 | | if (networkRestriction.IsProhibitory) |
| | 0 | 124 | | { |
| | | 125 | | // prohibitory: add a single cost entry forbidding this specific turn. |
| | 0 | 126 | | var costs = new uint[,] { { 0, 1 }, { 0, 0 } }; |
| | 0 | 127 | | writer.AddTurnCosts(turnCostVertex, networkRestriction.Attributes, |
| | 0 | 128 | | [secondToLast.edge, last.edge], costs, |
| | 0 | 129 | | networkRestriction.Take(networkRestriction.Count - 2).Select(x => x.edge)); |
| | | 130 | | |
| | 0 | 131 | | } |
| | | 132 | | else |
| | 0 | 133 | | { |
| | | 134 | | // mandatory: add cost for every *other* edge at the vertex. |
| | 0 | 135 | | if (!edgeEnumerator.MoveTo(secondToLast.edge, secondToLast.forward)) |
| | 0 | 136 | | return false; |
| | 0 | 137 | | var to = edgeEnumerator.Head; |
| | | 138 | | |
| | 0 | 139 | | edgeEnumerator.MoveTo(to); |
| | 0 | 140 | | while (edgeEnumerator.MoveNext()) |
| | 0 | 141 | | { |
| | 0 | 142 | | if (edgeEnumerator.EdgeId == secondToLast.edge || |
| | 0 | 143 | | edgeEnumerator.EdgeId == last.edge) continue; |
| | | 144 | | |
| | 0 | 145 | | var costs = new uint[,] { { 0, 1 }, { 0, 0 } }; |
| | 0 | 146 | | writer.AddTurnCosts(turnCostVertex, networkRestriction.Attributes, |
| | 0 | 147 | | [secondToLast.edge, edgeEnumerator.EdgeId], costs, |
| | 0 | 148 | | networkRestriction.Take(networkRestriction.Count - 2).Select(x => x.edge)); |
| | 0 | 149 | | } |
| | 0 | 150 | | } |
| | | 151 | | |
| | 0 | 152 | | return true; |
| | | 153 | | |
| | | 154 | | (EdgeId edge, bool forward)? GetEdge(GlobalEdgeId geid) |
| | 0 | 155 | | { |
| | 0 | 156 | | if (globalIdSet.EdgeIdSet.TryGet(geid, out var edgeId)) |
| | 0 | 157 | | return (edgeId, true); |
| | 0 | 158 | | if (globalIdSet.EdgeIdSet.TryGet(geid.GetInverted(), out edgeId)) |
| | 0 | 159 | | return (edgeId, false); |
| | | 160 | | |
| | | 161 | | // exact match not found — search for a subsection sharing the |
| | | 162 | | // endpoint closest to the restricted vertex. |
| | | 163 | | // for tail->head: keep head stable, search tail from head-1 toward 0 |
| | | 164 | | // for inverted head->tail: keep tail stable, search head from tail+1 toward max |
| | 0 | 165 | | if (geid.Tail < geid.Head) |
| | 0 | 166 | | { |
| | | 167 | | // forward direction: head is the restricted vertex, search toward it |
| | 0 | 168 | | for (var t = geid.Head - 1; t > geid.Tail; t--) |
| | 0 | 169 | | { |
| | 0 | 170 | | var sub = GlobalEdgeId.Create(geid.EdgeId, t, geid.Head); |
| | 0 | 171 | | if (globalIdSet.EdgeIdSet.TryGet(sub, out edgeId)) |
| | 0 | 172 | | return (edgeId, true); |
| | 0 | 173 | | if (globalIdSet.EdgeIdSet.TryGet(sub.GetInverted(), out edgeId)) |
| | 0 | 174 | | return (edgeId, false); |
| | 0 | 175 | | } |
| | 0 | 176 | | } |
| | | 177 | | else |
| | 0 | 178 | | { |
| | | 179 | | // reversed direction: tail is the restricted vertex, search away from it |
| | 0 | 180 | | for (var h = geid.Tail - 1; h > geid.Head; h--) |
| | 0 | 181 | | { |
| | 0 | 182 | | var sub = GlobalEdgeId.Create(geid.EdgeId, geid.Tail, h); |
| | 0 | 183 | | if (globalIdSet.EdgeIdSet.TryGet(sub, out edgeId)) |
| | 0 | 184 | | return (edgeId, true); |
| | 0 | 185 | | if (globalIdSet.EdgeIdSet.TryGet(sub.GetInverted(), out edgeId)) |
| | 0 | 186 | | return (edgeId, false); |
| | 0 | 187 | | } |
| | 0 | 188 | | } |
| | | 189 | | |
| | 0 | 190 | | return null; |
| | 0 | 191 | | } |
| | 0 | 192 | | } |
| | | 193 | | } |