| | 1 | | using System; |
| | 2 | | using System.Linq; |
| | 3 | | using System.Threading; |
| | 4 | | using System.Threading.Tasks; |
| | 5 | | using Itinero.Geo; |
| | 6 | | using Itinero.Network; |
| | 7 | | using Itinero.Routes; |
| | 8 | | using Itinero.Routes.Paths; |
| | 9 | | using Itinero.Routing.Flavours.Dijkstra.Bidirectional; |
| | 10 | | using Itinero.Snapping; |
| | 11 | |
|
| | 12 | | namespace Itinero.Routing; |
| | 13 | |
|
| | 14 | | /// <summary> |
| | 15 | | /// One to one extensions. |
| | 16 | | /// </summary> |
| | 17 | | public static class IRouterOneToOneExtensions |
| | 18 | | { |
| | 19 | | /// <summary> |
| | 20 | | /// Calculates the path. |
| | 21 | | /// </summary> |
| | 22 | | /// <param name="oneToOneRouter">The router.</param> |
| | 23 | | /// <param name="cancellationToken"></param> |
| | 24 | | /// <returns>The path.</returns> |
| | 25 | | public static async Task<Result<Path>> PathAsync(this IRouterOneToOne oneToOneRouter, |
| | 26 | | CancellationToken cancellationToken = default) |
| 7 | 27 | | { |
| 7 | 28 | | if (oneToOneRouter.Source.direction == null && oneToOneRouter.Target.direction == null) |
| 7 | 29 | | { |
| 7 | 30 | | return await oneToOneRouter.CalculateAsync(oneToOneRouter.Source.sp, oneToOneRouter.Target.sp, |
| 7 | 31 | | cancellationToken); |
| | 32 | | } |
| | 33 | |
|
| 0 | 34 | | return (await oneToOneRouter.CalculateAsync([oneToOneRouter.Source], [oneToOneRouter.Target]))[0][0]; |
| 7 | 35 | | } |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Calculates the route. |
| | 39 | | /// </summary> |
| | 40 | | /// <param name="oneToOneRouter">The router.</param> |
| | 41 | | /// <param name="cancellationToken"></param> |
| | 42 | | /// <returns>The route.</returns> |
| | 43 | | public static async Task<Result<Route>> CalculateAsync(this IRouterOneToOne oneToOneRouter, |
| | 44 | | CancellationToken cancellationToken = default) |
| 7 | 45 | | { |
| 7 | 46 | | var path = await oneToOneRouter.PathAsync(cancellationToken); |
| 7 | 47 | | if (path.IsError) |
| 0 | 48 | | { |
| 0 | 49 | | return new Result<Route>(path.ErrorMessage); |
| | 50 | | } |
| | 51 | |
|
| 7 | 52 | | return oneToOneRouter.Settings.RouteBuilder.Build(oneToOneRouter.Network, oneToOneRouter.Settings.Profile, |
| 7 | 53 | | path.Value); |
| 7 | 54 | | } |
| | 55 | |
|
| | 56 | | /// <summary> |
| | 57 | | /// Calculates the weights. |
| | 58 | | /// </summary> |
| | 59 | | /// <param name="oneToOneWeightRouter">The router.</param> |
| | 60 | | /// <returns>The weight</returns> |
| | 61 | | public static Task<Result<double?>> CalculateAsync(this IRouterWeights<IRouterOneToOne> oneToOneWeightRouter) |
| 0 | 62 | | { |
| 0 | 63 | | return Task.FromResult(new Result<double?>("Not implemented")); |
| | 64 | |
|
| | 65 | | // var profileHandler = oneToOneWeightRouter.Router.Network.GetCostFunctionFor( |
| | 66 | | // oneToOneWeightRouter.Router.Settings.Profile); |
| | 67 | | // return oneToOneWeightRouter.Router.Path().Weight(profileHandler.GetForwardWeight); |
| 0 | 68 | | } |
| | 69 | |
|
| | 70 | | internal static async Task<Result<Path>> CalculateAsync(this IRouterOneToOne oneToOneRouter, |
| | 71 | | SnapPoint source, SnapPoint target, CancellationToken cancellationToken) |
| 7 | 72 | | { |
| 7 | 73 | | var settings = oneToOneRouter.Settings; |
| 7 | 74 | | var routingNetwork = oneToOneRouter.Network; |
| | 75 | |
|
| 7 | 76 | | var profile = settings.Profile; |
| 7 | 77 | | var costFunction = routingNetwork.GetCostFunctionFor(profile); |
| | 78 | |
|
| 7 | 79 | | var maxBox = settings.MaxBoxFor(routingNetwork, [source, target]); |
| | 80 | |
|
| 7 | 81 | | var bidirectionalDijkstra = BidirectionalDijkstra.ForNetwork(routingNetwork); |
| | 82 | |
|
| 7 | 83 | | var (result, _) = await bidirectionalDijkstra.RunAsync(source, target, costFunction, async v => |
| 30 | 84 | | { |
| 30 | 85 | | await routingNetwork.UsageNotifier.NotifyVertex(routingNetwork, v, cancellationToken); |
| 30 | 86 | | if (cancellationToken.IsCancellationRequested) return false; |
| 30 | 87 | | return CheckMaxDistance(v); |
| 37 | 88 | | }, cancellationToken: cancellationToken); |
| | 89 | |
|
| 7 | 90 | | if (result == null) return new Result<Path>("Path not found"); |
| | 91 | |
|
| 7 | 92 | | return result; |
| | 93 | |
|
| | 94 | | bool CheckMaxDistance(VertexId v) |
| 30 | 95 | | { |
| 30 | 96 | | if (routingNetwork == null) throw new Exception("Router cannot be null here."); |
| 60 | 97 | | if (maxBox == null) return false; |
| | 98 | |
|
| 0 | 99 | | var vertex = routingNetwork.GetVertex(v); |
| 0 | 100 | | if (!maxBox.Value.Overlaps(vertex)) |
| 0 | 101 | | { |
| 0 | 102 | | return true; |
| | 103 | | } |
| | 104 | |
|
| 0 | 105 | | return false; |
| 30 | 106 | | } |
| 7 | 107 | | } |
| | 108 | | } |