| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using System.Linq; |
| | | 3 | | using Itinero.Network.Enumerators.Edges; |
| | | 4 | | using Itinero.Routing.Costs; |
| | | 5 | | |
| | | 6 | | namespace Itinero.Network.Search.Reachability; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// The local-access region around an endpoint: the edges a route may move through freely before it |
| | | 10 | | /// is genuinely on the network. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <remarks> |
| | | 13 | | /// Reconstructs, lazily and around one endpoint, the class island detection precomputed per tile: |
| | | 14 | | /// local-access edges plus the untagged segments enclosed by them. |
| | | 15 | | /// </remarks> |
| | | 16 | | internal static class LocalPocket |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Members are treated as local-access by the search; perimeter edges are the exits. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <remarks> |
| | | 22 | | /// Abandoned means the region outgrew its budget, so it is not a pocket and the caller should |
| | | 23 | | /// behave as though none was found. |
| | | 24 | | /// </remarks> |
| | | 25 | | internal sealed class Result |
| | | 26 | | { |
| | 35 | 27 | | public required HashSet<EdgeId> Members { get; init; } |
| | 23 | 28 | | public required HashSet<EdgeId> Perimeter { get; init; } |
| | 28 | 29 | | public required bool Abandoned { get; init; } |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Composes the pocket around <paramref name="seeds"/>, in the half's own direction. |
| | | 34 | | /// </summary> |
| | | 35 | | /// <param name="seeds"> |
| | | 36 | | /// The candidate edge, or the settled set of a half that stalled — already proved closed, so it |
| | | 37 | | /// costs nothing to start from. |
| | | 38 | | /// </param> |
| | | 39 | | /// <param name="asOrigin">True for a half leaving an origin, false for one arriving.</param> |
| | | 40 | | /// <param name="componentAllowance"> |
| | | 41 | | /// How far an N component may be followed before it counts as main network. Exceeding it means |
| | | 42 | | /// "not absorbed", which is the safe direction: the strict rule then applies. |
| | | 43 | | /// </param> |
| | | 44 | | /// <param name="budget">Total members allowed before the region is too big to be a pocket.</param> |
| | | 45 | | public static Result Compose( |
| | | 46 | | RoutingNetwork network, |
| | | 47 | | ICostFunction costFunction, |
| | | 48 | | IEnumerable<EdgeId> seeds, |
| | | 49 | | bool asOrigin, |
| | | 50 | | int componentAllowance, |
| | | 51 | | int budget) |
| | 15 | 52 | | { |
| | 15 | 53 | | var members = new HashSet<EdgeId>(seeds); |
| | 15 | 54 | | var perimeter = new HashSet<EdgeId>(); |
| | | 55 | | |
| | | 56 | | // Edges in a component that outgrew its allowance. Main network is one component, so the |
| | | 57 | | // first seed to discover it answers for every later seed inside it. |
| | 15 | 58 | | var knownMain = new HashSet<EdgeId>(); |
| | | 59 | | |
| | 15 | 60 | | var changed = true; |
| | 58 | 61 | | while (changed) |
| | 44 | 62 | | { |
| | 44 | 63 | | if (members.Count > budget) |
| | 1 | 64 | | { |
| | 1 | 65 | | return new Result { Members = members, Perimeter = perimeter, Abandoned = true }; |
| | | 66 | | } |
| | | 67 | | |
| | 43 | 68 | | changed = false; |
| | | 69 | | |
| | | 70 | | // (a) Grow over local-access edges, and collect the N edges we end up against. |
| | | 71 | | // Materialised: the loop below adds to members, and Reachable enumerates it lazily. |
| | 43 | 72 | | var nSeeds = new List<EdgeId>(); |
| | 445 | 73 | | foreach (var edge in Reachable(network, costFunction, members, asOrigin).ToList()) |
| | 158 | 74 | | { |
| | 275 | 75 | | if (members.Contains(edge.edge) || perimeter.Contains(edge.edge)) continue; |
| | | 76 | | |
| | 41 | 77 | | if (edge.localAccess) |
| | 25 | 78 | | { |
| | 25 | 79 | | members.Add(edge.edge); |
| | 25 | 80 | | changed = true; |
| | 25 | 81 | | } |
| | | 82 | | else |
| | 16 | 83 | | { |
| | 16 | 84 | | nSeeds.Add(edge.edge); |
| | 16 | 85 | | } |
| | 41 | 86 | | } |
| | | 87 | | |
| | 67 | 88 | | if (changed) continue; |
| | | 89 | | |
| | | 90 | | // (b) One pass over the N edges against the pocket, sharing knownMain so main network |
| | | 91 | | // is discovered once rather than per seed. |
| | 85 | 92 | | foreach (var seed in nSeeds) |
| | 14 | 93 | | { |
| | 14 | 94 | | if (members.Contains(seed) || perimeter.Contains(seed)) continue; |
| | | 95 | | |
| | 14 | 96 | | if (knownMain.Contains(seed)) |
| | 0 | 97 | | { |
| | 0 | 98 | | perimeter.Add(seed); |
| | 0 | 99 | | continue; |
| | | 100 | | } |
| | | 101 | | |
| | 14 | 102 | | var (component, escaped) = NonLocalComponent( |
| | 14 | 103 | | network, costFunction, seed, asOrigin, componentAllowance, knownMain); |
| | | 104 | | |
| | 14 | 105 | | if (escaped) |
| | 9 | 106 | | { |
| | 9 | 107 | | knownMain.UnionWith(component); |
| | 9 | 108 | | perimeter.Add(seed); |
| | 9 | 109 | | } |
| | | 110 | | else |
| | 5 | 111 | | { |
| | 5 | 112 | | members.UnionWith(component); |
| | 5 | 113 | | changed = true; |
| | 5 | 114 | | } |
| | 14 | 115 | | } |
| | 19 | 116 | | } |
| | | 117 | | |
| | 14 | 118 | | return new Result { Members = members, Perimeter = perimeter, Abandoned = false }; |
| | 15 | 119 | | } |
| | | 120 | | |
| | | 121 | | /// The edges reachable in one step from any member, in the half's direction. |
| | | 122 | | private static IEnumerable<(EdgeId edge, bool localAccess)> Reachable( |
| | | 123 | | RoutingNetwork network, ICostFunction costFunction, HashSet<EdgeId> from, bool asOrigin) |
| | 43 | 124 | | { |
| | 43 | 125 | | var probe = network.GetEdgeEnumerator(); |
| | 43 | 126 | | var seen = new HashSet<EdgeId>(); |
| | | 127 | | |
| | 387 | 128 | | foreach (var member in from) |
| | 129 | 129 | | { |
| | 903 | 130 | | foreach (var vertex in EndsOf(probe, member, costFunction, asOrigin)) |
| | 258 | 131 | | { |
| | 258 | 132 | | if (!probe.MoveTo(vertex)) continue; |
| | | 133 | | |
| | 738 | 134 | | while (probe.MoveNext()) |
| | 480 | 135 | | { |
| | 738 | 136 | | if (probe.EdgeId == member) continue; |
| | 285 | 137 | | if (!seen.Add(probe.EdgeId)) continue; |
| | | 138 | | |
| | 159 | 139 | | var (canAccess, _, localAccess, cost, _) = |
| | 159 | 140 | | costFunction.Get(probe, tailToHead: asOrigin); |
| | 160 | 141 | | if (!canAccess || cost <= 0) continue; |
| | | 142 | | |
| | 158 | 143 | | yield return (probe.EdgeId, localAccess); |
| | 158 | 144 | | } |
| | 258 | 145 | | } |
| | 129 | 146 | | } |
| | 43 | 147 | | } |
| | | 148 | | |
| | | 149 | | /// <summary> |
| | | 150 | | /// The component of <paramref name="seed"/> over non-local edges only. |
| | | 151 | | /// </summary> |
| | | 152 | | /// <returns> |
| | | 153 | | /// The edges found, and whether it escaped — outgrew the allowance or ran into known main |
| | | 154 | | /// network, either of which means it is not enclosed. |
| | | 155 | | /// </returns> |
| | | 156 | | private static (HashSet<EdgeId> component, bool escaped) NonLocalComponent( |
| | | 157 | | RoutingNetwork network, ICostFunction costFunction, EdgeId seed, bool asOrigin, |
| | | 158 | | int allowance, HashSet<EdgeId> knownMain) |
| | 14 | 159 | | { |
| | 14 | 160 | | var probe = network.GetEdgeEnumerator(); |
| | 14 | 161 | | var component = new HashSet<EdgeId> { seed }; |
| | 14 | 162 | | var queue = new Queue<EdgeId>(); |
| | 14 | 163 | | queue.Enqueue(seed); |
| | | 164 | | |
| | 14 | 165 | | var expansions = 0; |
| | 86 | 166 | | while (queue.Count > 0) |
| | 81 | 167 | | { |
| | 90 | 168 | | if (++expansions > allowance) return (component, true); |
| | | 169 | | |
| | 72 | 170 | | var edge = queue.Dequeue(); |
| | 72 | 171 | | if (knownMain.Contains(edge)) return (component, true); |
| | | 172 | | |
| | 488 | 173 | | foreach (var vertex in EndsOf(probe, edge, costFunction, asOrigin)) |
| | 136 | 174 | | { |
| | 136 | 175 | | if (!probe.MoveTo(vertex)) continue; |
| | | 176 | | |
| | 408 | 177 | | while (probe.MoveNext()) |
| | 272 | 178 | | { |
| | 408 | 179 | | if (probe.EdgeId == edge) continue; |
| | | 180 | | |
| | 136 | 181 | | var (canAccess, _, localAccess, cost, _) = |
| | 136 | 182 | | costFunction.Get(probe, tailToHead: asOrigin); |
| | 136 | 183 | | if (!canAccess || cost <= 0) continue; |
| | | 184 | | |
| | | 185 | | // Local edges bound the component rather than extending it. |
| | 154 | 186 | | if (localAccess) continue; |
| | 169 | 187 | | if (!component.Add(probe.EdgeId)) continue; |
| | | 188 | | |
| | 67 | 189 | | queue.Enqueue(probe.EdgeId); |
| | 67 | 190 | | } |
| | 136 | 191 | | } |
| | 72 | 192 | | } |
| | | 193 | | |
| | 5 | 194 | | return (component, false); |
| | 14 | 195 | | } |
| | | 196 | | |
| | | 197 | | /// The vertices an edge can be left by, in the half's direction. |
| | | 198 | | private static List<VertexId> EndsOf(RoutingNetworkEdgeEnumerator probe, EdgeId edge, |
| | | 199 | | ICostFunction costFunction, bool asOrigin) |
| | 201 | 200 | | { |
| | 201 | 201 | | var ends = new List<VertexId>(2); |
| | 1407 | 202 | | foreach (var forward in new[] { true, false }) |
| | 402 | 203 | | { |
| | 402 | 204 | | if (!probe.MoveTo(edge, forward)) continue; |
| | | 205 | | |
| | 402 | 206 | | var (canAccess, _, _, cost, _) = costFunction.Get(probe, tailToHead: asOrigin); |
| | 410 | 207 | | if (!canAccess || cost <= 0) continue; |
| | | 208 | | |
| | 394 | 209 | | ends.Add(probe.Head); |
| | 394 | 210 | | } |
| | | 211 | | |
| | 201 | 212 | | return ends; |
| | 201 | 213 | | } |
| | | 214 | | } |