| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Threading; |
| | 5 | | using System.Threading.Tasks; |
| | 6 | | using Itinero.Routes; |
| | 7 | | using Itinero.Routes.Builders; |
| | 8 | | using Itinero.Routes.Paths; |
| | 9 | | using Itinero.Snapping; |
| | 10 | |
|
| | 11 | | namespace Itinero.Routing; |
| | 12 | |
|
| | 13 | | /// <summary> |
| | 14 | | /// Contains extensions methods for the one to many router. |
| | 15 | | /// </summary> |
| | 16 | | public static class IRouterOneToManyExtensions |
| | 17 | | { |
| | 18 | | /// <summary> |
| | 19 | | /// Calculates the paths. |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="routerOneToMany">The router.</param> |
| | 22 | | /// <param name="cancellationToken"></param> |
| | 23 | | /// <returns>The paths.</returns> |
| | 24 | | public static async Task<IReadOnlyList<Result<Path>>> Paths(this IRouterOneToMany routerOneToMany, CancellationToken |
| 0 | 25 | | { |
| 0 | 26 | | var source = routerOneToMany.Source; |
| 0 | 27 | | var targets = routerOneToMany.Targets; |
| | 28 | |
|
| 0 | 29 | | if (source.direction.HasValue || |
| 0 | 30 | | !targets.TryToUndirected(out var targetsUndirected)) |
| 0 | 31 | | { |
| 0 | 32 | | var paths = await routerOneToMany.CalculateAsync( |
| 0 | 33 | | new (SnapPoint snapPoint, bool? direction)[] { source }, targets); |
| | 34 | |
|
| 0 | 35 | | if (paths == null) |
| 0 | 36 | | { |
| 0 | 37 | | throw new Exception("Could not calculate routes."); |
| | 38 | | } |
| | 39 | |
|
| 0 | 40 | | if (paths.Count < 1) |
| 0 | 41 | | { |
| 0 | 42 | | throw new Exception("Could not calculate routes."); |
| | 43 | | } |
| | 44 | |
|
| 0 | 45 | | return paths[0]; |
| | 46 | | } |
| | 47 | | else |
| 0 | 48 | | { |
| 0 | 49 | | var paths = await routerOneToMany.CalculateAsync(new[] { source.sp }, targetsUndirected, cancellationToken); |
| 0 | 50 | | if (paths == null) |
| 0 | 51 | | { |
| 0 | 52 | | throw new Exception("Could not calculate routes."); |
| | 53 | | } |
| | 54 | |
|
| 0 | 55 | | if (paths.Count < 1) |
| 0 | 56 | | { |
| 0 | 57 | | throw new Exception("Could not calculate routes."); |
| | 58 | | } |
| | 59 | |
|
| 0 | 60 | | return paths[0]; |
| | 61 | | } |
| 0 | 62 | | } |
| | 63 | |
|
| | 64 | | /// <summary> |
| | 65 | | /// Calculates the routes. |
| | 66 | | /// </summary> |
| | 67 | | /// <param name="routerOneToMany">The router.</param> |
| | 68 | | /// <param name="cancellationToken"></param> |
| | 69 | | /// <returns>The routes.</returns> |
| | 70 | | public static async Task<IReadOnlyList<Result<Route>>> Calculate(this IRouterOneToMany routerOneToMany, Cancellation |
| 0 | 71 | | { |
| 0 | 72 | | return (await routerOneToMany.Paths(cancellationToken)).Select(x => routerOneToMany.Settings.RouteBuilder.Build( |
| 0 | 73 | | routerOneToMany.Settings.Profile, x)).ToArray(); |
| 0 | 74 | | } |
| | 75 | |
|
| | 76 | | /// <summary> |
| | 77 | | /// Calculates the weights. |
| | 78 | | /// </summary> |
| | 79 | | /// <param name="routerOneToMany">The router.</param> |
| | 80 | | /// <returns>The weights.</returns> |
| | 81 | | public static Task<Result<IReadOnlyList<double?>>> Calculate(this IRouterWeights<IRouterOneToMany> routerOneToMany) |
| 0 | 82 | | { |
| 0 | 83 | | return Task.FromResult(new Result<IReadOnlyList<double?>>("Not implemented")); |
| | 84 | |
|
| | 85 | | // var profileHandler = routerOneToMany.Router.Network.GetCostFunctionFor( |
| | 86 | | // routerOneToMany.Router.Settings.Profile); |
| | 87 | | // return routerOneToMany.Router.Paths().Select(x => x.Weight(profileHandler.GetForwardWeight)).ToArray(); |
| 0 | 88 | | } |
| | 89 | | } |