< Summary

Class:Itinero.MapMatching.PathExtensions
Assembly:Itinero.MapMatching
File(s):/home/runner/work/routing2/routing2/src/Itinero.MapMatching/PathExtensions.cs
Covered lines:31
Uncovered lines:21
Coverable lines:52
Total lines:93
Line coverage:59.6% (31 of 52)
Covered branches:18
Total branches:28
Branch coverage:64.2% (18 of 28)
Tag:251_23667616543

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
IsUTurn(...)100%6100%
TryMergeAsUTurn(...)85.71%1495.83%
TryMergeOverlap(...)0%80%

File(s)

/home/runner/work/routing2/routing2/src/Itinero.MapMatching/PathExtensions.cs

#LineLine coverage
 1using Itinero.Routes.Paths;
 2
 3namespace Itinero.MapMatching;
 4
 5internal static class PathExtensions
 6{
 7    /// <summary>
 8    /// The transition from path1 to path2 represents a u-turn.
 9    /// </summary>
 10    /// <param name="path1">The first path.</param>
 11    /// <param name="path2">The second path.</param>
 12    /// <returns>True if a u-turn, false otherwise.</returns>
 13    public static bool IsUTurn(this Path path1, Path path2)
 4914    {
 4915        if (path1.Last.edge.LocalId == path2.First.edge.LocalId &&
 4916            path1.Last.edge.TileId == path2.First.edge.TileId &&
 4917            path1.Last.direction != path2.First.direction)
 2918        {
 2919            return path1.Offset2 + path2.Offset1 == ushort.MaxValue;
 20        }
 21
 2022        return false;
 4923    }
 24
 25    /// <summary>
 26    /// Tries to merge the two paths by removing the last few edges if they represent a u-turn.
 27    /// </summary>
 28    /// <param name="path1">The first path.</param>
 29    /// <param name="path2">The second path.</param>
 30    /// <returns>A path without u-turns or null if merging is not possible.</returns>
 31    public static Path? TryMergeAsUTurn(this Path path1, Path path2)
 3632    {
 3633        if (path1.IsUTurn(path2))
 2434        {
 2435            path1 = path1.Clone();
 2436            path2 = path2.Clone();
 2437        }
 38        else
 1239        {
 1240            return null;
 41        }
 42
 43        do
 2944        {
 2945            if (path2.Count == 1 &&
 2946                path2.Offset2 < ushort.MaxValue)
 1447            {
 1448                path1.Offset2 = (ushort)(ushort.MaxValue - path2.Offset2);
 1449                return path1;
 50            }
 1551            path1.RemoveLast();
 1552            path2.RemoveFirst();
 53
 1754            if (path1.Count == 0) return null;
 1355            if (path2.Count == 0) return path1;
 2656        } while (path1.IsUTurn(path2));
 57
 858        if (path1.IsNext(path2))
 859        {
 860            return new[] { path1, path2 }.Merge();
 61        }
 62
 063        return null;
 3664    }
 65
 66    public static Path? TryMergeOverlap(this Path path1, Path path2)
 067    {
 068        path2 = path2.Clone();
 069        while (path1.Last.edge.LocalId != path2.First.edge.LocalId ||
 070               path1.Last.edge.TileId != path2.First.edge.TileId)
 071        {
 072            path2.RemoveFirst();
 073        }
 74
 075        if (path2.Count > 0)
 076        {
 077            if (path2.Count > 1)
 078            {
 079                path2.RemoveFirst();
 080                path1.Offset2 = ushort.MaxValue;
 081                return new[] { path1, path2 }.Merge();
 82            }
 83            else
 084            {
 085                path1 = path1.Clone();
 086                path1.Offset2 = path2.Offset2;
 087                return path1;
 88            }
 89        }
 90
 091        return null;
 092    }
 93}