| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Threading; |
| | 4 | | using System.Threading.Tasks; |
| | 5 | | using Itinero.Geo; |
| | 6 | | using Itinero.Geo.Directions; |
| | 7 | | using Itinero.Network; |
| | 8 | | using Itinero.Routes.Paths; |
| | 9 | | using Itinero.Routing.Alternatives; |
| | 10 | | using Itinero.Routing.Costs; |
| | 11 | | using Itinero.Routing.Flavours.Dijkstra; |
| | 12 | | using Itinero.Snapping; |
| | 13 | |
|
| | 14 | | namespace Itinero.Routing; |
| | 15 | |
|
| | 16 | | /// <summary> |
| | 17 | | /// Contains extensions for the IRouter interface. |
| | 18 | | /// </summary> |
| | 19 | | public static class IRouterExtensions |
| | 20 | | { |
| | 21 | | /// <summary> |
| | 22 | | /// Configures the router to route from the given point. |
| | 23 | | /// </summary> |
| | 24 | | /// <param name="router">The router.</param> |
| | 25 | | /// <param name="snapPoint">The point to route from.</param> |
| | 26 | | /// <returns>A configured router.</returns> |
| | 27 | | public static IHasSource From(this IRouter router, SnapPoint snapPoint) |
| 7 | 28 | | { |
| 7 | 29 | | return router.From((snapPoint, (bool?)null)); |
| 7 | 30 | | } |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Configures the router to route from the given point. |
| | 34 | | /// </summary> |
| | 35 | | /// <param name="router">The router.</param> |
| | 36 | | /// <param name="directedSnapPoint">The point to route from.</param> |
| | 37 | | /// <returns>A configured router.</returns> |
| | 38 | | public static IHasSource From(this IRouter router, (SnapPoint snapPoint, bool? direction) directedSnapPoint) |
| 7 | 39 | | { |
| 7 | 40 | | return new Router(router.Network, router.Settings) |
| 7 | 41 | | { |
| 7 | 42 | | Source = directedSnapPoint |
| 7 | 43 | | }; |
| 7 | 44 | | } |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// Configures the router to route from the given point. |
| | 48 | | /// </summary> |
| | 49 | | /// <param name="router">The router.</param> |
| | 50 | | /// <param name="directedSnapPoint">The point to route from.</param> |
| | 51 | | /// <returns>A configured router.</returns> |
| | 52 | | public static IHasSource From(this IRouter router, |
| | 53 | | (SnapPoint snapPoint, DirectionEnum? direction) directedSnapPoint) |
| 0 | 54 | | { |
| 0 | 55 | | return router.From(directedSnapPoint.ToDirected(router.Network)); |
| 0 | 56 | | } |
| | 57 | |
|
| | 58 | | /// <summary> |
| | 59 | | /// Configures the router to route from the given point. |
| | 60 | | /// </summary> |
| | 61 | | /// <param name="router">The router.</param> |
| | 62 | | /// <param name="snapPoints">The points to route from.</param> |
| | 63 | | /// <returns>A configured router.</returns> |
| | 64 | | public static IHasSources From(this IRouter router, IReadOnlyList<SnapPoint> snapPoints) |
| 0 | 65 | | { |
| 0 | 66 | | return router.From(snapPoints.ToDirected()); |
| 0 | 67 | | } |
| | 68 | |
|
| | 69 | | /// <summary> |
| | 70 | | /// Configures the router to route from the given point. |
| | 71 | | /// </summary> |
| | 72 | | /// <param name="router">The router.</param> |
| | 73 | | /// <param name="directedSnapPoints">The points to route from.</param> |
| | 74 | | /// <returns>A configured router.</returns> |
| | 75 | | public static IHasSources From(this IRouter router, |
| | 76 | | IReadOnlyList<(SnapPoint snapPoint, DirectionEnum? direction)> directedSnapPoints) |
| 0 | 77 | | { |
| 0 | 78 | | return router.From(directedSnapPoints.ToDirected(router.Network)); |
| 0 | 79 | | } |
| | 80 | |
|
| | 81 | | /// <summary> |
| | 82 | | /// Configures the router to route from the given point. |
| | 83 | | /// </summary> |
| | 84 | | /// <param name="router">The router.</param> |
| | 85 | | /// <param name="directedSnapPoints">The points to route from.</param> |
| | 86 | | /// <returns>A configured router.</returns> |
| | 87 | | public static IHasSources From(this IRouter router, |
| | 88 | | IReadOnlyList<(SnapPoint snapPoint, bool? direction)> directedSnapPoints) |
| 0 | 89 | | { |
| 0 | 90 | | return new Router(router.Network, router.Settings) |
| 0 | 91 | | { |
| 0 | 92 | | Sources = directedSnapPoints |
| 0 | 93 | | }; |
| 0 | 94 | | } |
| | 95 | |
|
| | 96 | | internal static async Task<IReadOnlyList<IReadOnlyList<Result<Path>>>> CalculateAsync(this IRouter manyToManyRouter, |
| | 97 | | IReadOnlyList<SnapPoint> sources, IReadOnlyList<SnapPoint> targets, CancellationToken cancellationToken) |
| 7 | 98 | | { |
| 7 | 99 | | var settings = manyToManyRouter.Settings; |
| 7 | 100 | | var routingNetwork = manyToManyRouter.Network; |
| | 101 | |
|
| 7 | 102 | | var profile = settings.Profile; |
| 7 | 103 | | var costFunction = routingNetwork.GetCostFunctionFor(profile); |
| | 104 | |
|
| 7 | 105 | | var maxBox = settings.MaxBoxFor(routingNetwork, sources); |
| | 106 | |
|
| | 107 | | bool CheckMaxDistance(VertexId v) |
| 15 | 108 | | { |
| 15 | 109 | | if (maxBox == null) |
| 15 | 110 | | { |
| 15 | 111 | | return false; |
| | 112 | | } |
| | 113 | |
|
| 0 | 114 | | if (routingNetwork == null) |
| 0 | 115 | | { |
| 0 | 116 | | throw new Exception("Router cannot be null here."); |
| | 117 | | } |
| | 118 | |
|
| 0 | 119 | | var vertex = routingNetwork.GetVertex(v); |
| 0 | 120 | | if (!maxBox.Value.Overlaps(vertex)) |
| 0 | 121 | | { |
| 0 | 122 | | return true; |
| | 123 | | } |
| | 124 | |
|
| 0 | 125 | | return false; |
| 15 | 126 | | } |
| | 127 | |
|
| 7 | 128 | | var results = new IReadOnlyList<Result<Path>>[sources.Count]; |
| 28 | 129 | | for (var s = 0; s < sources.Count; s++) |
| 7 | 130 | | { |
| 7 | 131 | | var source = sources[s]; |
| 7 | 132 | | var pathsAndCosts = await Flavours.Dijkstra.EdgeBased.Dijkstra.Default.RunAsync(routingNetwork, source, targ |
| 7 | 133 | | costFunction.GetDijkstraWeightFunc(), |
| 7 | 134 | | async v => |
| 15 | 135 | | { |
| 15 | 136 | | await routingNetwork.UsageNotifier.NotifyVertex(routingNetwork, v.vertexId, cancellationToken); |
| 15 | 137 | | return CheckMaxDistance(v.vertexId); |
| 22 | 138 | | }); |
| | 139 | |
|
| 7 | 140 | | var sourceResults = new Result<Path>[pathsAndCosts.Length]; |
| 28 | 141 | | for (var r = 0; r < sourceResults.Length; r++) |
| 7 | 142 | | { |
| 7 | 143 | | var (path, _) = pathsAndCosts[r]; |
| 7 | 144 | | if (path == null) |
| 0 | 145 | | { |
| 0 | 146 | | sourceResults[r] = new Result<Path>("Path not found!"); |
| 0 | 147 | | } |
| | 148 | | else |
| 7 | 149 | | { |
| 7 | 150 | | sourceResults[r] = path; |
| 7 | 151 | | } |
| 7 | 152 | | } |
| | 153 | |
|
| 7 | 154 | | results[s] = sourceResults; |
| 7 | 155 | | } |
| | 156 | |
|
| 7 | 157 | | return results; |
| 7 | 158 | | } |
| | 159 | |
|
| | 160 | | internal static async Task<IReadOnlyList<IReadOnlyList<Result<Path>>>> CalculateAsync(this IRouter manyToManyRouter, |
| | 161 | | IReadOnlyList<(SnapPoint snapPoint, bool? direction)> sources, |
| | 162 | | IReadOnlyList<(SnapPoint snapPoint, bool? direction)> targets) |
| 0 | 163 | | { |
| 0 | 164 | | var settings = manyToManyRouter.Settings; |
| 0 | 165 | | var routerDb = manyToManyRouter.Network; |
| | 166 | |
|
| 0 | 167 | | var profile = settings.Profile; |
| 0 | 168 | | var costFunction = routerDb.GetCostFunctionFor(profile); |
| | 169 | |
|
| 0 | 170 | | var maxBox = settings.MaxBoxFor(routerDb, sources); |
| | 171 | |
|
| | 172 | | bool CheckMaxDistance(VertexId v) |
| 0 | 173 | | { |
| 0 | 174 | | if (maxBox == null) |
| 0 | 175 | | { |
| 0 | 176 | | return false; |
| | 177 | | } |
| | 178 | |
|
| 0 | 179 | | if (routerDb == null) |
| 0 | 180 | | { |
| 0 | 181 | | throw new Exception("Router cannot be null here."); |
| | 182 | | } |
| | 183 | |
|
| 0 | 184 | | var vertex = routerDb.GetVertex(v); |
| 0 | 185 | | if (!maxBox.Value.Overlaps(vertex)) |
| 0 | 186 | | { |
| 0 | 187 | | return true; |
| | 188 | | } |
| | 189 | |
|
| 0 | 190 | | return false; |
| 0 | 191 | | } |
| | 192 | |
|
| 0 | 193 | | var results = new IReadOnlyList<Result<Path>>[sources.Count]; |
| 0 | 194 | | for (var s = 0; s < sources.Count; s++) |
| 0 | 195 | | { |
| 0 | 196 | | var source = sources[s]; |
| 0 | 197 | | var paths = await Flavours.Dijkstra.EdgeBased.Dijkstra.Default.RunAsync(routerDb, source, targets, |
| 0 | 198 | | costFunction.GetDijkstraWeightFunc(), |
| 0 | 199 | | async e => |
| 0 | 200 | | { |
| 0 | 201 | | await routerDb.UsageNotifier.NotifyVertex(routerDb, e.vertexId); |
| 0 | 202 | | return CheckMaxDistance(e.vertexId); |
| 0 | 203 | | }); |
| | 204 | |
|
| 0 | 205 | | var sourceResults = new Result<Path>[paths.Length]; |
| 0 | 206 | | for (var r = 0; r < sourceResults.Length; r++) |
| 0 | 207 | | { |
| 0 | 208 | | var (path, _) = paths[r]; |
| 0 | 209 | | if (path == null) |
| 0 | 210 | | { |
| 0 | 211 | | sourceResults[r] = new Result<Path>("Routes not found!"); |
| 0 | 212 | | } |
| | 213 | | else |
| 0 | 214 | | { |
| 0 | 215 | | sourceResults[r] = path; |
| 0 | 216 | | } |
| 0 | 217 | | } |
| | 218 | |
|
| 0 | 219 | | results[s] = sourceResults; |
| 0 | 220 | | } |
| | 221 | |
|
| 0 | 222 | | return results; |
| 0 | 223 | | } |
| | 224 | | } |