From feb9b70e503a19074e0f9b337a0806489d17c7fd Mon Sep 17 00:00:00 2001 From: glax Date: Sun, 9 Apr 2023 16:47:30 +0200 Subject: [PATCH] Added Pathfinding Time --- Pathfinding/Pathfinder_Time.cs | 65 ++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 Pathfinding/Pathfinder_Time.cs diff --git a/Pathfinding/Pathfinder_Time.cs b/Pathfinding/Pathfinder_Time.cs new file mode 100644 index 0000000..bea0adb --- /dev/null +++ b/Pathfinding/Pathfinder_Time.cs @@ -0,0 +1,65 @@ +using OSMDatastructure.Graph; +using Utils = OSMDatastructure.Utils; + +namespace Pathfinding; + +public static partial class Pathfinder +{ + public static List AStarTime(string workingDir, Coordinates start, + Coordinates goal, Tag.SpeedType vehicle) + { + RegionManager regionManager = new (workingDir); + ValueTuple startAndEndNode = SetupNodes(start, goal, regionManager); + if (startAndEndNode.Item1 is null || startAndEndNode.Item2 is null) + return new List(); + OsmNode goalNode = startAndEndNode.Item2!; + + PriorityQueue toVisit = new(); + toVisit.Enqueue(startAndEndNode.Item1, 0); + bool stop = false; + + while (toVisit.Count > 0) + { + OsmNode closestNodeToGoal = toVisit.Dequeue(); + + foreach (OsmEdge edge in closestNodeToGoal.edges.Where( + edge => regionManager.TestValidConnectionForType(closestNodeToGoal, edge, vehicle))) + { + OsmNode? neighbor = regionManager.GetNode(edge.neighborId, edge.neighborRegion); + if (neighbor is not null) + { + double newPotentialWeight = closestNodeToGoal.currentPathLength + + EdgeWeight(closestNodeToGoal, neighbor, edge.wayId, vehicle, + ref regionManager); + if (newPotentialWeight < neighbor.currentPathWeight) + { + neighbor.previousPathNode = closestNodeToGoal; + neighbor.currentPathLength = closestNodeToGoal.currentPathLength + Utils.DistanceBetween(closestNodeToGoal, neighbor); + neighbor.currentPathWeight = newPotentialWeight; + neighbor.directDistanceToGoal = Utils.DistanceBetween(neighbor, goalNode); + + if (neighbor.Equals(goalNode)) + { + stop = true; + } + else if(!stop) + { + toVisit.Enqueue(neighbor, neighbor.directDistanceToGoal); + } + } + } + } + } + + List path = new(); + OsmNode? currentNode = goalNode; + while (currentNode is not null) + { + path.Add(PathNode.FromOsmNode(currentNode)!); + currentNode = currentNode.previousPathNode; + } + path.Reverse(); + + return path; + } +} \ No newline at end of file