< Summary

Class:Itinero.Routing.IRouterOneToOneExtensions
Assembly:Itinero
File(s):/home/runner/work/routing2/routing2/src/Itinero/Routing/IRouterOneToOneExtensions.cs
Covered lines:66
Uncovered lines:11
Coverable lines:77
Total lines:183
Line coverage:85.7% (66 of 77)
Covered branches:18
Total branches:26
Branch coverage:69.2% (18 of 26)
Tag:284_38118444584

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
PathAsync()75%8100%
CalculateAsync()100%2100%
CalculateAsync(...)100%10%
CalculateAsync()100%2100%
SearchAsync()75%4100%
<SearchAsync()50%462.5%
BuildBalancedPotential(...)100%2100%

File(s)

/home/runner/work/routing2/routing2/src/Itinero/Routing/IRouterOneToOneExtensions.cs

#LineLine coverage
 1using System;
 2using System.Linq;
 3using System.Threading;
 4using System.Threading.Tasks;
 5using Itinero.Geo;
 6using Itinero.Network;
 7using Itinero.Profiles;
 8using Itinero.Routes;
 9using Itinero.Routes.Paths;
 10using Itinero.Routing.Flavours.Dijkstra;
 11using Itinero.Routing.Flavours.Dijkstra.Bidirectional;
 12using Itinero.Snapping;
 13
 14namespace Itinero.Routing;
 15
 16/// <summary>
 17/// One to one extensions.
 18/// </summary>
 19public static class IRouterOneToOneExtensions
 20{
 21    /// <summary>
 22    /// Calculates the path.
 23    /// </summary>
 24    /// <param name="oneToOneRouter">The router.</param>
 25    /// <param name="cancellationToken"></param>
 26    /// <returns>The path.</returns>
 27    public static async Task<Result<Path>> PathAsync(this IRouterOneToOne oneToOneRouter,
 28        CancellationToken cancellationToken = default)
 7029    {
 7030        if (oneToOneRouter.Source.direction == null && oneToOneRouter.Target.direction == null &&
 7031            (!oneToOneRouter.Settings.Profile.TurnCostFactorEnabled ||
 7032             oneToOneRouter.Settings.BidirectionalWithTurnCosts))
 6533        {
 6534            return await oneToOneRouter.CalculateAsync(oneToOneRouter.Source.sp, oneToOneRouter.Target.sp,
 6535                cancellationToken);
 36        }
 37
 538        return (await oneToOneRouter.CalculateAsync([oneToOneRouter.Source], [oneToOneRouter.Target]))[0][0];
 7039    }
 40
 41    /// <summary>
 42    /// Calculates the route.
 43    /// </summary>
 44    /// <param name="oneToOneRouter">The router.</param>
 45    /// <param name="cancellationToken"></param>
 46    /// <returns>The route.</returns>
 47    public static async Task<Result<Route>> CalculateAsync(this IRouterOneToOne oneToOneRouter,
 48        CancellationToken cancellationToken = default)
 3149    {
 3150        var path = await oneToOneRouter.PathAsync(cancellationToken);
 3151        if (path.IsError)
 1552        {
 1553            return new Result<Route>(path.ErrorMessage);
 54        }
 55
 1656        return oneToOneRouter.Settings.RouteBuilder.Build(oneToOneRouter.Network, oneToOneRouter.Settings.Profile,
 1657            path.Value);
 3158    }
 59
 60    /// <summary>
 61    /// Calculates the weights.
 62    /// </summary>
 63    /// <param name="oneToOneWeightRouter">The router.</param>
 64    /// <returns>The weight</returns>
 65    public static Task<Result<double?>> CalculateAsync(this IRouterWeights<IRouterOneToOne> oneToOneWeightRouter)
 066    {
 067        return Task.FromResult(new Result<double?>("Not implemented"));
 68
 69        // var profileHandler = oneToOneWeightRouter.Router.Network.GetCostFunctionFor(
 70        //     oneToOneWeightRouter.Router.Settings.Profile);
 71        // return oneToOneWeightRouter.Router.Path().Weight(profileHandler.GetForwardWeight);
 072    }
 73
 74    internal static async Task<Result<Path>> CalculateAsync(this IRouterOneToOne oneToOneRouter,
 75        SnapPoint source, SnapPoint target, CancellationToken cancellationToken)
 6576    {
 6577        var resolved = await oneToOneRouter.Network.SearchAsync(oneToOneRouter.Settings, source, target,
 6578            pruneFactor: 1.0, onForwardRelaxed: null, onBackwardRelaxed: null, cancellationToken);
 7579        if (resolved.IsError) return new Result<Path>(resolved.ErrorMessage);
 80
 5581        return resolved.Value.Path;
 6582    }
 83
 84    /// <summary>
 85    /// Routes between two points already snapped, optionally relaxing past the best meeting so a
 86    /// caller can map the region around the route out of the same search.
 87    /// </summary>
 88    /// <param name="pruneFactor">How far past the best meeting to go. 1.0 is an ordinary route.</param>
 89    /// <returns>The route, or an error if the two points are not connected.</returns>
 90    /// <remarks>
 91    /// <see cref="ResolvedRoute.Settled"/> comes back 0: this engine does not count it, only
 92    /// <see cref="ReachabilityRouter"/> does.
 93    /// </remarks>
 94    internal static async Task<Result<ResolvedRoute>> SearchAsync(
 95        this RoutingNetwork network,
 96        RoutingSettings settings,
 97        SnapPoint source,
 98        SnapPoint target,
 99        double pruneFactor,
 100        RelaxedCallback? onForwardRelaxed,
 101        RelaxedCallback? onBackwardRelaxed,
 102        CancellationToken cancellationToken)
 72103    {
 72104        var profile = settings.Profile;
 72105        var costFunction = network.GetCostFunctionFor(profile);
 72106        if (settings.CostFunctionWrapper != null) costFunction = settings.CostFunctionWrapper(costFunction);
 107
 72108        var maxBox = settings.MaxBoxFor(network, [source, target]);
 109
 72110        var isMainN = network.GetIsMainNFunc(profile);
 72111        var potential = BuildBalancedPotential(network, profile, source, target);
 112        // Rented for this call, returned in the finally.
 72113        var search = SearchPool<BidirectionalDijkstra>.Rent();
 114        Path? result;
 115        double cost;
 116        try
 72117        {
 72118            (result, cost) = await search.RunAsync(network, source, target, costFunction,
 72119                async v =>
 1650120                {
 1650121                    if (!network.UsageNotifier.IsVertexDataReady(network, v))
 0122                    {
 0123                        await network.UsageNotifier.NotifyVertex(network, v, cancellationToken);
 0124                    }
 1650125                    if (cancellationToken.IsCancellationRequested) return false;
 1650126                    return CheckMaxDistance(v);
 1650127                }, cancellationToken: cancellationToken, isMainN: isMainN, potential: potential,
 72128                pruneFactor: pruneFactor, onForwardRelaxed: onForwardRelaxed,
 72129                onBackwardRelaxed: onBackwardRelaxed);
 72130        }
 131        finally
 72132        {
 72133            SearchPool<BidirectionalDijkstra>.Return(search);
 72134        }
 135
 82136        if (result == null) return new Result<ResolvedRoute>("Path not found");
 137
 62138        return new ResolvedRoute(result, cost, source, target, costFunction, costFunction, 0);
 139
 140        bool CheckMaxDistance(VertexId v)
 1650141        {
 3300142            if (maxBox == null) return false;
 143
 0144            var vertex = network.GetVertex(v);
 0145            if (!maxBox.Value.Overlaps(vertex))
 0146            {
 0147                return true;
 148            }
 149
 0150            return false;
 1650151        }
 72152    }
 153    /// <summary>
 154    /// p(v) = (h_target(v) - h_source(v)) / 2, in centimetres; the backward half negates
 155    /// it so the two sum to zero. Null when the profile declares no bound. Halving is what
 156    /// makes them consistent, and why each half gets half the goal-direction of a one-way A*.
 157    /// </summary>
 158    /// <remarks>
 159    /// Internal rather than private because <see cref="ReachabilityRouter"/> needs the same
 160    /// potential. Two routers computing goal direction slightly differently would make their
 161    /// search costs incomparable, and comparing them is the whole point of having both.
 162    /// </remarks>
 163    internal static HeuristicFunc? BuildBalancedPotential(RoutingNetwork network, Profile profile,
 164        SnapPoint source, SnapPoint target)
 101165    {
 101166        var minFactor = profile.MinFactor;
 201167        if (minFactor == 0) return null;
 168
 1169        var sourceLocation = source.LocationOnNetwork(network);
 1170        var targetLocation = target.LocationOnNetwork(network);
 171
 1172        return (longitude, latitude) =>
 24173        {
 24174            var from = (longitude, latitude, (float?)null);
 1175
 1176            // 1% margin: DistanceEstimateInMeter can slightly overestimate east-west.
 24177            var toTarget = from.DistanceEstimateInMeter(targetLocation) * 0.99 * 100.0 * minFactor;
 24178            var toSource = from.DistanceEstimateInMeter(sourceLocation) * 0.99 * 100.0 * minFactor;
 24179            return (toTarget - toSource) / 2.0;
 25180        };
 101181    }
 182
 183}