| | | 1 | | using Itinero.Routes.Paths; |
| | | 2 | | |
| | | 3 | | namespace Itinero.MapMatching; |
| | | 4 | | |
| | | 5 | | internal 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) |
| | 49 | 14 | | { |
| | 49 | 15 | | if (path1.Last.edge.LocalId == path2.First.edge.LocalId && |
| | 49 | 16 | | path1.Last.edge.TileId == path2.First.edge.TileId && |
| | 49 | 17 | | path1.Last.direction != path2.First.direction) |
| | 29 | 18 | | { |
| | 29 | 19 | | return path1.Offset2 + path2.Offset1 == ushort.MaxValue; |
| | | 20 | | } |
| | | 21 | | |
| | 20 | 22 | | return false; |
| | 49 | 23 | | } |
| | | 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) |
| | 36 | 32 | | { |
| | 36 | 33 | | if (path1.IsUTurn(path2)) |
| | 24 | 34 | | { |
| | 24 | 35 | | path1 = path1.Clone(); |
| | 24 | 36 | | path2 = path2.Clone(); |
| | 24 | 37 | | } |
| | | 38 | | else |
| | 12 | 39 | | { |
| | 12 | 40 | | return null; |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | do |
| | 29 | 44 | | { |
| | 29 | 45 | | if (path2.Count == 1 && |
| | 29 | 46 | | path2.Offset2 < ushort.MaxValue) |
| | 14 | 47 | | { |
| | 14 | 48 | | path1.Offset2 = (ushort)(ushort.MaxValue - path2.Offset2); |
| | 14 | 49 | | return path1; |
| | | 50 | | } |
| | 15 | 51 | | path1.RemoveLast(); |
| | 15 | 52 | | path2.RemoveFirst(); |
| | | 53 | | |
| | 17 | 54 | | if (path1.Count == 0) return null; |
| | 13 | 55 | | if (path2.Count == 0) return path1; |
| | 26 | 56 | | } while (path1.IsUTurn(path2)); |
| | | 57 | | |
| | 8 | 58 | | if (path1.IsNext(path2)) |
| | 8 | 59 | | { |
| | 8 | 60 | | return new[] { path1, path2 }.Merge(); |
| | | 61 | | } |
| | | 62 | | |
| | 0 | 63 | | return null; |
| | 36 | 64 | | } |
| | | 65 | | |
| | | 66 | | public static Path? TryMergeOverlap(this Path path1, Path path2) |
| | 0 | 67 | | { |
| | 0 | 68 | | path2 = path2.Clone(); |
| | 0 | 69 | | while (path1.Last.edge.LocalId != path2.First.edge.LocalId || |
| | 0 | 70 | | path1.Last.edge.TileId != path2.First.edge.TileId) |
| | 0 | 71 | | { |
| | 0 | 72 | | path2.RemoveFirst(); |
| | 0 | 73 | | } |
| | | 74 | | |
| | 0 | 75 | | if (path2.Count > 0) |
| | 0 | 76 | | { |
| | 0 | 77 | | if (path2.Count > 1) |
| | 0 | 78 | | { |
| | 0 | 79 | | path2.RemoveFirst(); |
| | 0 | 80 | | path1.Offset2 = ushort.MaxValue; |
| | 0 | 81 | | return new[] { path1, path2 }.Merge(); |
| | | 82 | | } |
| | | 83 | | else |
| | 0 | 84 | | { |
| | 0 | 85 | | path1 = path1.Clone(); |
| | 0 | 86 | | path1.Offset2 = path2.Offset2; |
| | 0 | 87 | | return path1; |
| | | 88 | | } |
| | | 89 | | } |
| | | 90 | | |
| | 0 | 91 | | return null; |
| | 0 | 92 | | } |
| | | 93 | | } |