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