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