| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Text; |
| | 5 | | using Itinero.Network; |
| | 6 | | using Itinero.Network.Enumerators.Edges; |
| | 7 | |
|
| | 8 | | namespace Itinero.Routes.Paths; |
| | 9 | |
|
| | 10 | | /// <summary> |
| | 11 | | /// Represents a path in a graph as a collection of edges. |
| | 12 | | /// </summary> |
| | 13 | | public sealed class Path : IEnumerable<(EdgeId edge, bool forward, ushort offset1, ushort offset2)> |
| | 14 | | { |
| | 15 | | private readonly List<(EdgeId edge, bool forward)> _edges; |
| | 16 | | private readonly RoutingNetworkEdgeEnumerator _edgeEnumerator; |
| | 17 | | private readonly RoutingNetwork _network; |
| | 18 | |
|
| 61 | 19 | | public Path(RoutingNetwork network) |
| 61 | 20 | | { |
| 61 | 21 | | _network = network; |
| 61 | 22 | | _edgeEnumerator = network.GetEdgeEnumerator(); |
| | 23 | |
|
| 61 | 24 | | _edges = new List<(EdgeId edge, bool forward)>(); |
| 61 | 25 | | } |
| | 26 | |
|
| 0 | 27 | | internal Path(Path pathToClone) |
| 0 | 28 | | { |
| 0 | 29 | | _network = pathToClone._network; |
| 0 | 30 | | _edgeEnumerator = _network.GetEdgeEnumerator(); |
| | 31 | |
|
| 0 | 32 | | _edges = new List<(EdgeId edge, bool forward)>(pathToClone._edges); |
| | 33 | |
|
| 0 | 34 | | this.Offset1 = pathToClone.Offset1; |
| 0 | 35 | | this.Offset2 = pathToClone.Offset2; |
| 0 | 36 | | } |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// Gets the network. |
| | 40 | | /// </summary> |
| 0 | 41 | | public RoutingNetwork RoutingNetwork => _network; |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Gets the offset at the start of the path. |
| | 45 | | /// </summary> |
| | 46 | | /// <remarks> |
| | 47 | | /// This is independent of the the direction of the first edge: |
| | 48 | | /// - 0 : means the edge is fully included. |
| | 49 | | /// - max : means the edge is not included. |
| | 50 | | /// </remarks> |
| 222 | 51 | | public ushort Offset1 { get; set; } = 0; |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// Gets the offset at the end of the path, relative to the direction of the edge. |
| | 55 | | /// </summary> |
| | 56 | | /// <remarks> |
| | 57 | | /// This is independent of the the direction of the last edge: |
| | 58 | | /// - 0 : means the edge is not included. |
| | 59 | | /// - max : means the edge is fully included. |
| | 60 | | /// </remarks> |
| 221 | 61 | | public ushort Offset2 { get; set; } = ushort.MaxValue; |
| | 62 | |
|
| | 63 | | /// <summary> |
| | 64 | | /// Gets the first edge. |
| | 65 | | /// </summary> |
| 44 | 66 | | public (EdgeId edge, bool direction) First => _edges[0]; |
| | 67 | |
|
| | 68 | | /// <summary> |
| | 69 | | /// Gets the last edge. |
| | 70 | | /// </summary> |
| 40 | 71 | | public (EdgeId edge, bool direction) Last => _edges[this.Count - 1]; |
| | 72 | |
|
| | 73 | | /// <summary> |
| | 74 | | /// Remove the first edge. |
| | 75 | | /// </summary> |
| | 76 | | /// <exception cref="InvalidOperationException"></exception> |
| | 77 | | public void RemoveFirst() |
| 4 | 78 | | { |
| 4 | 79 | | if (_edges.Count == 0) |
| 0 | 80 | | { |
| 0 | 81 | | throw new InvalidOperationException("Cannot remove first from an already empty path."); |
| | 82 | | } |
| | 83 | |
|
| 4 | 84 | | _edges.RemoveAt(0); |
| 4 | 85 | | this.Offset1 = 0; |
| 4 | 86 | | } |
| | 87 | |
|
| | 88 | | /// <summary> |
| | 89 | | /// Remove the last edge. |
| | 90 | | /// </summary> |
| | 91 | | /// <exception cref="InvalidOperationException"></exception> |
| | 92 | | public void RemoveLast() |
| 4 | 93 | | { |
| 4 | 94 | | if (_edges.Count == 0) |
| 0 | 95 | | { |
| 0 | 96 | | throw new InvalidOperationException("Cannot remove last from an already empty path."); |
| | 97 | | } |
| | 98 | |
|
| 4 | 99 | | _edges.RemoveAt(_edges.Count - 1); |
| 4 | 100 | | this.Offset2 = ushort.MaxValue; |
| 4 | 101 | | } |
| | 102 | |
|
| | 103 | | /// <summary> |
| | 104 | | /// Returns the number of edges. |
| | 105 | | /// </summary> |
| 413 | 106 | | public int Count => _edges.Count; |
| | 107 | |
|
| | 108 | | /// <summary> |
| | 109 | | /// Appends the given edge and calculates the proper direction. |
| | 110 | | /// </summary> |
| | 111 | | /// <param name="edge">The edge.</param> |
| | 112 | | /// <param name="forward">The direction of the edge.</param> |
| | 113 | | public void Append(EdgeId edge, bool forward) |
| 33 | 114 | | { |
| 33 | 115 | | if (!_edgeEnumerator.MoveTo(edge, forward)) throw new Exception($"Edge does not exist."); |
| | 116 | |
|
| | 117 | | #if DEBUG |
| 33 | 118 | | if (_edges.Count > 0) |
| 13 | 119 | | { |
| 13 | 120 | | var edgeEnumerator = _network.GetEdgeEnumerator(); |
| 13 | 121 | | if (!edgeEnumerator.MoveTo(edge, forward)) throw new Exception($"Edge does not exist."); |
| 13 | 122 | | var tail = edgeEnumerator.Tail; |
| 13 | 123 | | var previous = _edges[^1]; |
| 26 | 124 | | if (!edgeEnumerator.MoveTo(previous.edge, previous.forward)) throw new Exception($"Edge does not exist."); ; |
| 13 | 125 | | if (edgeEnumerator.Head != tail) |
| 0 | 126 | | throw new Exception("Cannot append edge, previous edge head does not match tail"); |
| 13 | 127 | | } |
| | 128 | | #endif |
| | 129 | |
|
| 33 | 130 | | this.AppendInternal(edge, forward); |
| 33 | 131 | | } |
| | 132 | |
|
| | 133 | | /// <summary> |
| | 134 | | /// Prepends the given edge and calculates the proper direction. |
| | 135 | | /// </summary> |
| | 136 | | /// <param name="edge">The edge.</param> |
| | 137 | | /// <param name="forward">The direction of the edge.</param> |
| | 138 | | public void Prepend(EdgeId edge, bool forward) |
| 76 | 139 | | { |
| 76 | 140 | | if (!_edgeEnumerator.MoveTo(edge, forward)) throw new Exception($"Edge does not exist."); |
| | 141 | |
|
| | 142 | | #if DEBUG |
| 76 | 143 | | if (_edges.Count > 0) |
| 36 | 144 | | { |
| 36 | 145 | | var edgeEnumerator = _network.GetEdgeEnumerator(); |
| 36 | 146 | | if (!edgeEnumerator.MoveTo(edge, forward)) throw new Exception($"Edge does not exist."); |
| 36 | 147 | | var head = edgeEnumerator.Head; |
| 36 | 148 | | var next = _edges[0]; |
| 36 | 149 | | if (!edgeEnumerator.MoveTo(next.edge, next.forward)) throw new Exception($"Edge does not exist."); |
| 36 | 150 | | if (edgeEnumerator.Tail != head) |
| 0 | 151 | | throw new Exception("Cannot append edge, next edge tail does not match head"); |
| 36 | 152 | | } |
| | 153 | | #endif |
| | 154 | |
|
| 76 | 155 | | this.PrependInternal(edge, forward); |
| 76 | 156 | | } |
| | 157 | |
|
| | 158 | | /// <summary> |
| | 159 | | /// Returns a copy of this path. |
| | 160 | | /// </summary> |
| | 161 | | /// <returns>A copy.</returns> |
| | 162 | | public Path Clone() |
| 0 | 163 | | { |
| 0 | 164 | | return new Path(this); |
| 0 | 165 | | } |
| | 166 | |
|
| | 167 | | internal void AppendInternal(EdgeId edge, bool forward) |
| 33 | 168 | | { |
| 33 | 169 | | _edges.Add((edge, forward)); |
| 33 | 170 | | } |
| | 171 | |
|
| | 172 | | internal void PrependInternal(EdgeId edge, bool forward) |
| 76 | 173 | | { |
| 76 | 174 | | _edges.Insert(0, (edge, forward)); |
| 76 | 175 | | } |
| | 176 | |
|
| | 177 | | public IEnumerator<(EdgeId edge, bool forward, ushort offset1, ushort offset2)> GetEnumerator() |
| 78 | 178 | | { |
| 400 | 179 | | for (var i = 0; i < this.Count; i++) |
| 125 | 180 | | { |
| 125 | 181 | | var edge = _edges[i]; |
| 125 | 182 | | var offset1 = (ushort)0; |
| 125 | 183 | | var offset2 = ushort.MaxValue; |
| | 184 | |
|
| 125 | 185 | | if (i == 0) |
| 78 | 186 | | { |
| 78 | 187 | | offset1 = this.Offset1; |
| 78 | 188 | | } |
| | 189 | |
|
| 125 | 190 | | if (i == this.Count - 1) |
| 78 | 191 | | { |
| 78 | 192 | | offset2 = this.Offset2; |
| 78 | 193 | | } |
| | 194 | |
|
| 125 | 195 | | yield return (edge.edge, edge.forward, offset1, offset2); |
| 122 | 196 | | } |
| 75 | 197 | | } |
| | 198 | |
|
| | 199 | | IEnumerator IEnumerable.GetEnumerator() |
| 0 | 200 | | { |
| 0 | 201 | | return this.GetEnumerator(); |
| 0 | 202 | | } |
| | 203 | |
|
| | 204 | | /// <summary> |
| | 205 | | /// Returns a description of this path. |
| | 206 | | /// </summary> |
| | 207 | | public override string ToString() |
| 0 | 208 | | { |
| | 209 | | // 1 edge without offsets: [0]->21543F->[4] |
| | 210 | | // 1 edge with offsets: [0]->10%-21543F-20%->[4] |
| 0 | 211 | | var builder = new StringBuilder(); |
| | 212 | |
|
| 0 | 213 | | if (_edges.Count > 0) |
| 0 | 214 | | { // there is a first edge. |
| 0 | 215 | | var first = _edges[0]; |
| 0 | 216 | | _edgeEnumerator.MoveTo(first.edge, first.forward); |
| 0 | 217 | | builder.Append($"[{_edgeEnumerator.Tail}]"); |
| 0 | 218 | | builder.Append("->"); |
| 0 | 219 | | if (this.Offset1 != 0) |
| 0 | 220 | | { |
| 0 | 221 | | builder.Append(OffsetPer(this.Offset1)); |
| 0 | 222 | | builder.Append("-"); |
| 0 | 223 | | } |
| | 224 | |
|
| 0 | 225 | | builder.Append($"{first.edge}"); |
| 0 | 226 | | builder.Append(first.forward ? "F" : "B"); |
| | 227 | |
|
| 0 | 228 | | if (_edges.Count == 1) |
| 0 | 229 | | { |
| 0 | 230 | | if ((first.forward && this.Offset2 != ushort.MaxValue) || |
| 0 | 231 | | (!first.forward && this.Offset2 != 0)) |
| 0 | 232 | | { |
| 0 | 233 | | builder.Append("-"); |
| 0 | 234 | | builder.Append(OffsetPer(this.Offset2)); |
| 0 | 235 | | } |
| | 236 | |
|
| 0 | 237 | | builder.Append("->"); |
| 0 | 238 | | builder.Append($"[{_edgeEnumerator.Head}]"); |
| 0 | 239 | | return builder.ToString(); |
| | 240 | | } |
| | 241 | |
|
| 0 | 242 | | builder.Append("->"); |
| 0 | 243 | | builder.Append($"[{_edgeEnumerator.Head}]"); |
| 0 | 244 | | } |
| | 245 | |
|
| 0 | 246 | | for (var e = 1; e < _edges.Count - 1; e++) |
| 0 | 247 | | { |
| 0 | 248 | | var edgeAndDirection = _edges[e]; |
| 0 | 249 | | _edgeEnumerator.MoveTo(edgeAndDirection.edge, edgeAndDirection.forward); |
| 0 | 250 | | builder.Append("->"); |
| 0 | 251 | | builder.Append($"{edgeAndDirection.edge}"); |
| 0 | 252 | | builder.Append(edgeAndDirection.forward ? "F" : "B"); |
| 0 | 253 | | builder.Append("->"); |
| 0 | 254 | | builder.Append($"[{_edgeEnumerator.Head}]"); |
| 0 | 255 | | } |
| | 256 | |
|
| 0 | 257 | | if (_edges.Count > 0) |
| 0 | 258 | | { // there is a last edge. |
| 0 | 259 | | var last = _edges[^1]; |
| 0 | 260 | | builder.Append("->"); |
| 0 | 261 | | builder.Append($"{last.edge}"); |
| 0 | 262 | | builder.Append(last.forward ? "F" : "B"); |
| 0 | 263 | | if (this.Offset2 != ushort.MaxValue) |
| 0 | 264 | | { |
| 0 | 265 | | builder.Append("-"); |
| 0 | 266 | | builder.Append(OffsetPer(this.Offset2)); |
| 0 | 267 | | } |
| | 268 | |
|
| 0 | 269 | | _edgeEnumerator.MoveTo(last.edge, last.forward); |
| 0 | 270 | | builder.Append("->"); |
| 0 | 271 | | builder.Append($"[{_edgeEnumerator.Head}]"); |
| 0 | 272 | | return builder.ToString(); |
| | 273 | | } |
| | 274 | |
|
| 0 | 275 | | return builder.ToString(); |
| | 276 | |
|
| | 277 | | // Declare a local function. |
| | 278 | | static string OffsetPer(ushort offset) |
| 0 | 279 | | { |
| 0 | 280 | | return $"{(double)offset / ushort.MaxValue * 100:F1}%"; |
| 0 | 281 | | } |
| 0 | 282 | | } |
| | 283 | | } |