< Summary

Class:Itinero.Routing.IRouterExtensions
Assembly:Itinero
File(s):/home/runner/work/routing2/routing2/src/Itinero/Routing/IRouterExtensions.cs
Covered lines:44
Uncovered lines:72
Coverable lines:116
Total lines:224
Line coverage:37.9% (44 of 116)
Covered branches:8
Total branches:28
Branch coverage:28.5% (8 of 28)
Tag:224_14471318300

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
From(...)100%1100%
From(...)100%1100%
From(...)100%10%
From(...)100%10%
From(...)100%10%
From(...)100%10%
CalculateAsync()87.5%889.65%
<CalculateAsync()100%1100%
CalculateAsync()0%80%
<CalculateAsync()100%10%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Threading;
 4using System.Threading.Tasks;
 5using Itinero.Geo;
 6using Itinero.Geo.Directions;
 7using Itinero.Network;
 8using Itinero.Routes.Paths;
 9using Itinero.Routing.Alternatives;
 10using Itinero.Routing.Costs;
 11using Itinero.Routing.Flavours.Dijkstra;
 12using Itinero.Snapping;
 13
 14namespace Itinero.Routing;
 15
 16/// <summary>
 17///     Contains extensions for the IRouter interface.
 18/// </summary>
 19public static class IRouterExtensions
 20{
 21    /// <summary>
 22    ///     Configures the router to route from the given point.
 23    /// </summary>
 24    /// <param name="router">The router.</param>
 25    /// <param name="snapPoint">The point to route from.</param>
 26    /// <returns>A configured router.</returns>
 27    public static IHasSource From(this IRouter router, SnapPoint snapPoint)
 728    {
 729        return router.From((snapPoint, (bool?)null));
 730    }
 31
 32    /// <summary>
 33    ///     Configures the router to route from the given point.
 34    /// </summary>
 35    /// <param name="router">The router.</param>
 36    /// <param name="directedSnapPoint">The point to route from.</param>
 37    /// <returns>A configured router.</returns>
 38    public static IHasSource From(this IRouter router, (SnapPoint snapPoint, bool? direction) directedSnapPoint)
 739    {
 740        return new Router(router.Network, router.Settings)
 741        {
 742            Source = directedSnapPoint
 743        };
 744    }
 45
 46    /// <summary>
 47    ///     Configures the router to route from the given point.
 48    /// </summary>
 49    /// <param name="router">The router.</param>
 50    /// <param name="directedSnapPoint">The point to route from.</param>
 51    /// <returns>A configured router.</returns>
 52    public static IHasSource From(this IRouter router,
 53        (SnapPoint snapPoint, DirectionEnum? direction) directedSnapPoint)
 054    {
 055        return router.From(directedSnapPoint.ToDirected(router.Network));
 056    }
 57
 58    /// <summary>
 59    ///     Configures the router to route from the given point.
 60    /// </summary>
 61    /// <param name="router">The router.</param>
 62    /// <param name="snapPoints">The points to route from.</param>
 63    /// <returns>A configured router.</returns>
 64    public static IHasSources From(this IRouter router, IReadOnlyList<SnapPoint> snapPoints)
 065    {
 066        return router.From(snapPoints.ToDirected());
 067    }
 68
 69    /// <summary>
 70    ///     Configures the router to route from the given point.
 71    /// </summary>
 72    /// <param name="router">The router.</param>
 73    /// <param name="directedSnapPoints">The points to route from.</param>
 74    /// <returns>A configured router.</returns>
 75    public static IHasSources From(this IRouter router,
 76        IReadOnlyList<(SnapPoint snapPoint, DirectionEnum? direction)> directedSnapPoints)
 077    {
 078        return router.From(directedSnapPoints.ToDirected(router.Network));
 079    }
 80
 81    /// <summary>
 82    ///     Configures the router to route from the given point.
 83    /// </summary>
 84    /// <param name="router">The router.</param>
 85    /// <param name="directedSnapPoints">The points to route from.</param>
 86    /// <returns>A configured router.</returns>
 87    public static IHasSources From(this IRouter router,
 88        IReadOnlyList<(SnapPoint snapPoint, bool? direction)> directedSnapPoints)
 089    {
 090        return new Router(router.Network, router.Settings)
 091        {
 092            Sources = directedSnapPoints
 093        };
 094    }
 95
 96    internal static async Task<IReadOnlyList<IReadOnlyList<Result<Path>>>> CalculateAsync(this IRouter manyToManyRouter,
 97        IReadOnlyList<SnapPoint> sources, IReadOnlyList<SnapPoint> targets, CancellationToken cancellationToken)
 798    {
 799        var settings = manyToManyRouter.Settings;
 7100        var routingNetwork = manyToManyRouter.Network;
 101
 7102        var profile = settings.Profile;
 7103        var costFunction = routingNetwork.GetCostFunctionFor(profile);
 104
 7105        var maxBox = settings.MaxBoxFor(routingNetwork, sources);
 106
 107        bool CheckMaxDistance(VertexId v)
 15108        {
 15109            if (maxBox == null)
 15110            {
 15111                return false;
 112            }
 113
 0114            if (routingNetwork == null)
 0115            {
 0116                throw new Exception("Router cannot be null here.");
 117            }
 118
 0119            var vertex = routingNetwork.GetVertex(v);
 0120            if (!maxBox.Value.Overlaps(vertex))
 0121            {
 0122                return true;
 123            }
 124
 0125            return false;
 15126        }
 127
 7128        var results = new IReadOnlyList<Result<Path>>[sources.Count];
 28129        for (var s = 0; s < sources.Count; s++)
 7130        {
 7131            var source = sources[s];
 7132            var pathsAndCosts = await Flavours.Dijkstra.EdgeBased.Dijkstra.Default.RunAsync(routingNetwork, source, targ
 7133                costFunction.GetDijkstraWeightFunc(),
 7134                async v =>
 15135                {
 15136                    await routingNetwork.UsageNotifier.NotifyVertex(routingNetwork, v.vertexId, cancellationToken);
 15137                    return CheckMaxDistance(v.vertexId);
 22138                });
 139
 7140            var sourceResults = new Result<Path>[pathsAndCosts.Length];
 28141            for (var r = 0; r < sourceResults.Length; r++)
 7142            {
 7143                var (path, _) = pathsAndCosts[r];
 7144                if (path == null)
 0145                {
 0146                    sourceResults[r] = new Result<Path>("Path not found!");
 0147                }
 148                else
 7149                {
 7150                    sourceResults[r] = path;
 7151                }
 7152            }
 153
 7154            results[s] = sourceResults;
 7155        }
 156
 7157        return results;
 7158    }
 159
 160    internal static async Task<IReadOnlyList<IReadOnlyList<Result<Path>>>> CalculateAsync(this IRouter manyToManyRouter,
 161        IReadOnlyList<(SnapPoint snapPoint, bool? direction)> sources,
 162        IReadOnlyList<(SnapPoint snapPoint, bool? direction)> targets)
 0163    {
 0164        var settings = manyToManyRouter.Settings;
 0165        var routerDb = manyToManyRouter.Network;
 166
 0167        var profile = settings.Profile;
 0168        var costFunction = routerDb.GetCostFunctionFor(profile);
 169
 0170        var maxBox = settings.MaxBoxFor(routerDb, sources);
 171
 172        bool CheckMaxDistance(VertexId v)
 0173        {
 0174            if (maxBox == null)
 0175            {
 0176                return false;
 177            }
 178
 0179            if (routerDb == null)
 0180            {
 0181                throw new Exception("Router cannot be null here.");
 182            }
 183
 0184            var vertex = routerDb.GetVertex(v);
 0185            if (!maxBox.Value.Overlaps(vertex))
 0186            {
 0187                return true;
 188            }
 189
 0190            return false;
 0191        }
 192
 0193        var results = new IReadOnlyList<Result<Path>>[sources.Count];
 0194        for (var s = 0; s < sources.Count; s++)
 0195        {
 0196            var source = sources[s];
 0197            var paths = await Flavours.Dijkstra.EdgeBased.Dijkstra.Default.RunAsync(routerDb, source, targets,
 0198                costFunction.GetDijkstraWeightFunc(),
 0199                async e =>
 0200                {
 0201                    await routerDb.UsageNotifier.NotifyVertex(routerDb, e.vertexId);
 0202                    return CheckMaxDistance(e.vertexId);
 0203                });
 204
 0205            var sourceResults = new Result<Path>[paths.Length];
 0206            for (var r = 0; r < sourceResults.Length; r++)
 0207            {
 0208                var (path, _) = paths[r];
 0209                if (path == null)
 0210                {
 0211                    sourceResults[r] = new Result<Path>("Routes not found!");
 0212                }
 213                else
 0214                {
 0215                    sourceResults[r] = path;
 0216                }
 0217            }
 218
 0219            results[s] = sourceResults;
 0220        }
 221
 0222        return results;
 0223    }
 224}