| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.IO; |
| | 4 | | using Itinero.Network; |
| | 5 | | using Itinero.Network.Enumerators.Edges; |
| | 6 | |
|
| | 7 | | namespace Itinero.Routes.Paths; |
| | 8 | |
|
| | 9 | | /// <summary> |
| | 10 | | /// Extensions for path. |
| | 11 | | /// </summary> |
| | 12 | | public static class PathExtensions |
| | 13 | | { |
| | 14 | | /// <summary> |
| | 15 | | /// Calculates the weight of the path given the weight function. |
| | 16 | | /// </summary> |
| | 17 | | /// <param name="path">The path.</param> |
| | 18 | | /// <param name="getWeight">The weight function.</param> |
| | 19 | | /// <returns>The total weight.</returns> |
| | 20 | | public static double? Weight(this Result<Path> path, Func<RoutingNetworkEdgeEnumerator, double> getWeight) |
| 0 | 21 | | { |
| 0 | 22 | | if (path.IsError) |
| 0 | 23 | | { |
| 0 | 24 | | return null; |
| | 25 | | } |
| | 26 | |
|
| 0 | 27 | | return path.Value.Weight(getWeight); |
| 0 | 28 | | } |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// Calculates the weight of the path given the weight function. |
| | 32 | | /// </summary> |
| | 33 | | /// <param name="path">The path.</param> |
| | 34 | | /// <param name="getWeight">The weight function.</param> |
| | 35 | | /// <returns>The total weight.</returns> |
| | 36 | | public static double Weight(this Path path, Func<RoutingNetworkEdgeEnumerator, double> getWeight) |
| 0 | 37 | | { |
| 0 | 38 | | var weight = 0.0; |
| | 39 | |
|
| 0 | 40 | | var edgeEnumerator = path.RoutingNetwork.GetEdgeEnumerator(); |
| 0 | 41 | | foreach (var (edge, direction, offset1, offset2) in path) |
| 0 | 42 | | { |
| 0 | 43 | | if (!edgeEnumerator.MoveTo(edge, direction)) |
| 0 | 44 | | { |
| 0 | 45 | | throw new InvalidDataException("An edge in the path is not found!"); |
| | 46 | | } |
| | 47 | |
|
| 0 | 48 | | var edgeWeight = getWeight(edgeEnumerator); |
| 0 | 49 | | if (offset1 != 0 || offset2 != ushort.MaxValue) |
| 0 | 50 | | { |
| 0 | 51 | | edgeWeight *= (double)(offset2 - offset1) / ushort.MaxValue; |
| 0 | 52 | | } |
| | 53 | |
|
| 0 | 54 | | weight += edgeWeight; |
| 0 | 55 | | } |
| | 56 | |
|
| 0 | 57 | | return weight; |
| 0 | 58 | | } |
| | 59 | |
|
| | 60 | | /// <summary> |
| | 61 | | /// Calculates the length in meters of this path. |
| | 62 | | /// </summary> |
| | 63 | | /// <param name="path">The path.</param> |
| | 64 | | /// <returns>The length in meters.</returns> |
| | 65 | | /// <exception cref="InvalidDataException"></exception> |
| | 66 | | public static double LengthInMeters(this Path path) |
| 0 | 67 | | { |
| 0 | 68 | | var weight = 0.0; |
| | 69 | |
|
| 0 | 70 | | var edgeEnumerator = path.RoutingNetwork.GetEdgeEnumerator(); |
| 0 | 71 | | foreach (var (edge, direction, offset1, offset2) in path) |
| 0 | 72 | | { |
| 0 | 73 | | if (!edgeEnumerator.MoveTo(edge, direction)) |
| 0 | 74 | | { |
| 0 | 75 | | throw new InvalidDataException("An edge in the path is not found!"); |
| | 76 | | } |
| | 77 | |
|
| 0 | 78 | | var edgeWeight = edgeEnumerator.EdgeLength(); |
| 0 | 79 | | if (offset1 != 0 || offset2 != ushort.MaxValue) |
| 0 | 80 | | { |
| 0 | 81 | | edgeWeight *= (double)(offset2 - offset1) / ushort.MaxValue; |
| 0 | 82 | | } |
| | 83 | |
|
| 0 | 84 | | weight += edgeWeight; |
| 0 | 85 | | } |
| | 86 | |
|
| 0 | 87 | | return weight; |
| 0 | 88 | | } |
| | 89 | |
|
| | 90 | | /// <summary> |
| | 91 | | /// Returns true if the path is next. |
| | 92 | | /// </summary> |
| | 93 | | /// <param name="path">The path.</param> |
| | 94 | | /// <param name="next">The next path.</param> |
| | 95 | | /// <returns>True if the next path</returns> |
| | 96 | | public static bool IsNext(this Path path, Path next) |
| 0 | 97 | | { |
| 0 | 98 | | var last = path.Last; |
| 0 | 99 | | var first = next.First; |
| | 100 | |
|
| | 101 | | // check if the same edge and if the offsets match. |
| 0 | 102 | | if (last.edge == first.edge && |
| 0 | 103 | | last.direction == first.direction) |
| 0 | 104 | | { |
| 0 | 105 | | return path.Offset2 == next.Offset1; |
| | 106 | | } |
| | 107 | |
|
| | 108 | | // check if the same vertices at the end. |
| 0 | 109 | | if (next.Offset1 != 0 || |
| 0 | 110 | | path.Offset2 != ushort.MaxValue) |
| 0 | 111 | | { |
| 0 | 112 | | return false; |
| | 113 | | } |
| | 114 | |
|
| 0 | 115 | | var edgeEnumerator = path.RoutingNetwork.GetEdgeEnumerator(); |
| 0 | 116 | | edgeEnumerator.MoveTo(last.edge, last.direction); |
| 0 | 117 | | var lastVertex = edgeEnumerator.Head; |
| 0 | 118 | | edgeEnumerator.MoveTo(first.edge, first.direction); |
| 0 | 119 | | var firstVertex = edgeEnumerator.Tail; |
| | 120 | |
|
| 0 | 121 | | return lastVertex == firstVertex; |
| 0 | 122 | | } |
| | 123 | |
|
| | 124 | | /// <summary> |
| | 125 | | /// Merges the paths. |
| | 126 | | /// </summary> |
| | 127 | | /// <param name="path">The first part.</param> |
| | 128 | | /// <param name="paths">The next parts.</param> |
| | 129 | | /// <returns>The merged path.</returns> |
| | 130 | | public static Path? Merge(this Path path, params Path[] paths) |
| 0 | 131 | | { |
| | 132 | | IEnumerable<Path> Enumerate() |
| 0 | 133 | | { |
| 0 | 134 | | yield return path; |
| | 135 | |
|
| 0 | 136 | | foreach (var p in paths) |
| 0 | 137 | | { |
| 0 | 138 | | yield return p; |
| 0 | 139 | | } |
| 0 | 140 | | } |
| | 141 | |
|
| 0 | 142 | | return Enumerate().Merge(); |
| 0 | 143 | | } |
| | 144 | |
|
| | 145 | | /// <summary> |
| | 146 | | /// Merges the paths. |
| | 147 | | /// </summary> |
| | 148 | | /// <param name="paths">The paths.</param> |
| | 149 | | /// <returns>The merged path.</returns> |
| | 150 | | public static Path? Merge(this IEnumerable<Path> paths) |
| 0 | 151 | | { |
| 0 | 152 | | Path? merged = null; |
| 0 | 153 | | RoutingNetworkEdgeEnumerator? enumerator = null; |
| 0 | 154 | | foreach (var path in paths) |
| 0 | 155 | | { |
| 0 | 156 | | merged ??= new Path(path.RoutingNetwork); |
| 0 | 157 | | enumerator ??= path.RoutingNetwork.GetEdgeEnumerator(); |
| | 158 | |
|
| 0 | 159 | | if (merged.Count == 0) |
| 0 | 160 | | { |
| 0 | 161 | | merged.Offset1 = path.Offset1; |
| 0 | 162 | | } |
| 0 | 163 | | else if (!merged.IsNext(path)) |
| 0 | 164 | | { |
| 0 | 165 | | throw new InvalidDataException( |
| 0 | 166 | | $"Paths cannot be concatenated."); |
| | 167 | | } |
| | 168 | |
|
| 0 | 169 | | var isFirst = true; |
| 0 | 170 | | foreach (var (edge, direction, _, _) in path) |
| 0 | 171 | | { |
| 0 | 172 | | if (isFirst) |
| 0 | 173 | | { |
| 0 | 174 | | isFirst = false; |
| 0 | 175 | | if (merged.Count > 0 && |
| 0 | 176 | | merged.Last.edge == edge && |
| 0 | 177 | | merged.Last.direction == direction) |
| 0 | 178 | | { |
| | 179 | | // first edge may be the same edge as |
| | 180 | | // previous path, don't add it again. |
| 0 | 181 | | continue; |
| | 182 | | } |
| 0 | 183 | | } |
| | 184 | |
|
| 0 | 185 | | merged.Append(edge, direction); |
| 0 | 186 | | } |
| | 187 | |
|
| 0 | 188 | | merged.Offset2 = path.Offset2; |
| 0 | 189 | | } |
| | 190 | |
|
| 0 | 191 | | return merged; |
| 0 | 192 | | } |
| | 193 | |
|
| | 194 | | public static void Append(this Path path, IEnumerable<(EdgeId edge, bool forward, ushort offset1, ushort offset2)> o |
| 2 | 195 | | { |
| 2 | 196 | | if (path.Offset2 != ushort.MaxValue) |
| 0 | 197 | | throw new Exception("Cannot append another hop to a path not ending at a vertex"); |
| | 198 | |
|
| 10 | 199 | | foreach (var hop in other) |
| 2 | 200 | | { |
| 2 | 201 | | path.Append(hop.edge, hop.forward); |
| | 202 | |
|
| 2 | 203 | | path.Offset2 = hop.offset2; |
| 2 | 204 | | } |
| 2 | 205 | | } |
| | 206 | |
|
| | 207 | | /// <summary> |
| | 208 | | /// Removes the first and/or last edge if they are not part of the path via the offset properties. |
| | 209 | | /// </summary> |
| | 210 | | /// <param name="path">The path.</param> |
| | 211 | | public static void Trim(this Path path) |
| 8 | 212 | | { |
| 8 | 213 | | if (path.Count <= 1) |
| 2 | 214 | | { |
| 2 | 215 | | return; |
| | 216 | | } |
| | 217 | |
|
| 6 | 218 | | if (path.Offset1 == ushort.MaxValue) |
| 4 | 219 | | { |
| 4 | 220 | | path.RemoveFirst(); |
| 4 | 221 | | } |
| | 222 | |
|
| 6 | 223 | | if (path.Count <= 1) |
| 1 | 224 | | { |
| 1 | 225 | | return; |
| | 226 | | } |
| | 227 | |
|
| 5 | 228 | | if (path.Offset2 == 0) |
| 4 | 229 | | { |
| 4 | 230 | | path.RemoveLast(); |
| 4 | 231 | | } |
| 8 | 232 | | } |
| | 233 | |
|
| | 234 | | public static bool HasLength(this Path path) |
| 0 | 235 | | { |
| 0 | 236 | | if (path.Count > 1) return true; |
| | 237 | |
|
| 0 | 238 | | return path.Offset1 != path.Offset2; |
| 0 | 239 | | } |
| | 240 | |
|
| | 241 | | public static IEnumerable<(EdgeId edge, bool forward, ushort offset1, ushort offset2)> InvertDirection( |
| | 242 | | this Path path) |
| 7 | 243 | | { |
| 7 | 244 | | var pathOffset1 = (ushort)(ushort.MaxValue - path.Offset2); |
| 7 | 245 | | var pathOffset2 = (ushort)(ushort.MaxValue - path.Offset1); |
| | 246 | |
|
| 34 | 247 | | for (var i = 0; i < path.Count; i++) |
| 10 | 248 | | { |
| 10 | 249 | | var s = path[path.Count - i - 1]; |
| | 250 | |
|
| 10 | 251 | | ushort offset1 = 0; |
| 10 | 252 | | ushort offset2 = ushort.MaxValue; |
| 17 | 253 | | if (i == 0) offset1 = pathOffset1; |
| 17 | 254 | | if (i == path.Count - 1) offset2 = pathOffset2; |
| | 255 | |
|
| 10 | 256 | | yield return (s.edge, !s.forward, offset1, offset2); |
| 10 | 257 | | } |
| 7 | 258 | | } |
| | 259 | | } |