| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using System.Linq; |
| | | 3 | | using System.Threading; |
| | | 4 | | using System.Threading.Tasks; |
| | | 5 | | using Itinero.Routes; |
| | | 6 | | using Itinero.Routes.Paths; |
| | | 7 | | using Itinero.Routing.Corridors; |
| | | 8 | | |
| | | 9 | | namespace Itinero.Routing.Alternatives; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// One to one with alternatives extensions. |
| | | 13 | | /// </summary> |
| | | 14 | | public static class IRouterOneToOneWithAlternativesExtensions |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Calculates the fastest path and its alternatives. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <returns> |
| | | 20 | | /// The fastest path first, then the alternatives cheapest-first. Never empty unless the |
| | | 21 | | /// endpoints are not connected, in which case an error comes back. |
| | | 22 | | /// </returns> |
| | | 23 | | /// <remarks> |
| | | 24 | | /// One search: the alternatives come out of the cost ellipse around the fastest route, which |
| | | 25 | | /// the route's own search maps on the way past. |
| | | 26 | | /// </remarks> |
| | | 27 | | public static async Task<Result<IReadOnlyList<Path>>> PathsAsync( |
| | | 28 | | this IRouterOneToOneWithAlternatives withAlternatives, |
| | | 29 | | CancellationToken cancellationToken = default) |
| | 8 | 30 | | { |
| | 8 | 31 | | if (withAlternatives.Source.direction != null || withAlternatives.Target.direction != null) |
| | 1 | 32 | | { |
| | 1 | 33 | | return new Result<IReadOnlyList<Path>>( |
| | 1 | 34 | | "Alternatives are not supported for directed endpoints"); |
| | | 35 | | } |
| | | 36 | | |
| | 7 | 37 | | var settings = withAlternatives.AlternativeRouteSettings; |
| | 7 | 38 | | var network = withAlternatives.Network; |
| | 7 | 39 | | var source = withAlternatives.Source.sp; |
| | 7 | 40 | | var target = withAlternatives.Target.sp; |
| | | 41 | | |
| | | 42 | | // The budget IS the cost constraint, so every path in the corridor satisfies it by |
| | | 43 | | // construction and only overlap and the detour bound are left to apply. |
| | 7 | 44 | | var searched = await network.RouteCorridorAsync(withAlternatives.Settings, source, target, |
| | 7 | 45 | | settings.MaxCostFactor, cancellationToken); |
| | 7 | 46 | | if (searched.IsError) return new Result<IReadOnlyList<Path>>(searched.ErrorMessage); |
| | | 47 | | |
| | 7 | 48 | | var (route, corridor) = searched.Value; |
| | 7 | 49 | | var paths = new List<Path>(settings.MaxNumberOfAlternativeRoutes) { route.Path }; |
| | 8 | 50 | | if (settings.MaxNumberOfAlternativeRoutes <= 1) return paths; |
| | | 51 | | |
| | 6 | 52 | | paths.AddRange(network.AlternativesFor(route, corridor, settings.MaxOverlap, |
| | 6 | 53 | | settings.MaxNumberOfAlternativeRoutes - 1, settings.LocalDetourFactor, |
| | 6 | 54 | | cancellationToken)); |
| | | 55 | | |
| | 6 | 56 | | return paths; |
| | 8 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Calculates the fastest route and its alternatives. |
| | | 61 | | /// </summary> |
| | | 62 | | /// <returns>The fastest route first, then the alternatives cheapest-first.</returns> |
| | | 63 | | public static async Task<Result<IReadOnlyList<Route>>> CalculateAsync( |
| | | 64 | | this IRouterOneToOneWithAlternatives withAlternatives, |
| | | 65 | | CancellationToken cancellationToken = default) |
| | 1 | 66 | | { |
| | 1 | 67 | | var paths = await withAlternatives.PathsAsync(cancellationToken); |
| | 1 | 68 | | if (paths.IsError) return new Result<IReadOnlyList<Route>>(paths.ErrorMessage); |
| | | 69 | | |
| | 3 | 70 | | var routes = paths.Value.Select(path => withAlternatives.Settings.RouteBuilder.Build( |
| | 3 | 71 | | withAlternatives.Network, withAlternatives.Settings.Profile, path).Value).ToList(); |
| | | 72 | | |
| | 1 | 73 | | return new Result<IReadOnlyList<Route>>(routes); |
| | 1 | 74 | | } |
| | | 75 | | } |