| | 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 | |
|
| | 10 | | namespace Itinero.Routing; |
| | 11 | |
|
| | 12 | | /// <summary> |
| | 13 | | /// Many to one extensions. |
| | 14 | | /// </summary> |
| | 15 | | public static class IRouterManyToOneExtensions |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Calculates the paths. |
| | 19 | | /// </summary> |
| | 20 | | /// <param name="routerOneToMany">The router.</param> |
| | 21 | | /// <param name="cancellationToken"></param> |
| | 22 | | /// <returns>The paths.</returns> |
| | 23 | | public static async Task<IReadOnlyList<Result<Path>>> Paths(this IRouterManyToOne routerOneToMany, CancellationToken |
| 0 | 24 | | { |
| 0 | 25 | | var sources = routerOneToMany.Sources; |
| 0 | 26 | | var target = routerOneToMany.Target; |
| | 27 | |
|
| 0 | 28 | | if (target.direction.HasValue || |
| 0 | 29 | | !sources.TryToUndirected(out var sourcesUndirected)) |
| 0 | 30 | | { |
| 0 | 31 | | var routes = await routerOneToMany.CalculateAsync( |
| 0 | 32 | | sources, new[] { target }); |
| 0 | 33 | | if (routes == null) |
| 0 | 34 | | { |
| 0 | 35 | | throw new Exception("Could not calculate routes."); |
| | 36 | | } |
| | 37 | |
|
| 0 | 38 | | var manyToOne = new Result<Path>[sources.Count]; |
| 0 | 39 | | for (var s = 0; s < manyToOne.Length; s++) |
| 0 | 40 | | { |
| 0 | 41 | | manyToOne[s] = routes[s][0]; |
| 0 | 42 | | } |
| | 43 | |
|
| 0 | 44 | | return manyToOne; |
| | 45 | | } |
| | 46 | | else |
| 0 | 47 | | { |
| 0 | 48 | | var routes = await routerOneToMany.CalculateAsync(sourcesUndirected, new[] { target.sp }, cancellationToken) |
| 0 | 49 | | if (routes == null) |
| 0 | 50 | | { |
| 0 | 51 | | throw new Exception("Could not calculate routes."); |
| | 52 | | } |
| | 53 | |
|
| 0 | 54 | | var manyToOne = new Result<Path>[sources.Count]; |
| 0 | 55 | | for (var s = 0; s < manyToOne.Length; s++) |
| 0 | 56 | | { |
| 0 | 57 | | manyToOne[s] = routes[s][0]; |
| 0 | 58 | | } |
| | 59 | |
|
| 0 | 60 | | return manyToOne; |
| | 61 | | } |
| 0 | 62 | | } |
| | 63 | |
|
| | 64 | | /// <summary> |
| | 65 | | /// Calculates the routes. |
| | 66 | | /// </summary> |
| | 67 | | /// <param name="routerManyToOne">The router.</param> |
| | 68 | | /// <param name="cancellationToken"></param> |
| | 69 | | /// <returns>The routes.</returns> |
| | 70 | | public static async Task<IReadOnlyList<Result<Route>>> Calculate(this IRouterManyToOne routerManyToOne, Cancellation |
| 0 | 71 | | { |
| 0 | 72 | | return (await routerManyToOne.Paths(cancellationToken)).Select(x => routerManyToOne.Settings.RouteBuilder.Build( |
| 0 | 73 | | routerManyToOne.Settings.Profile, x)).ToArray(); |
| 0 | 74 | | } |
| | 75 | |
|
| | 76 | | /// <summary> |
| | 77 | | /// Calculates the weights. |
| | 78 | | /// </summary> |
| | 79 | | /// <param name="routerManyToOne">The router.</param> |
| | 80 | | /// <returns>The weights.</returns> |
| | 81 | | public static Task<Result<IReadOnlyList<double?>>> Calculate(this IRouterWeights<IRouterManyToOne> routerManyToOne) |
| 0 | 82 | | { |
| 0 | 83 | | return Task.FromResult(new Result<IReadOnlyList<double?>>("Not implemented")); |
| | 84 | | // |
| | 85 | | // var profileHandler = routerManyToOne.Router.Network.GetCostFunctionFor( |
| | 86 | | // routerManyToOne.Router.Settings.Profile); |
| | 87 | | // return routerManyToOne.Router.Paths().Select(x => x.Weight(profileHandler.GetForwardWeight)).ToArray(); |
| 0 | 88 | | } |
| | 89 | | } |