2023-04-09 16:17:15 +02:00
|
|
|
using OSMDatastructure;
|
|
|
|
using OSMDatastructure.Graph;
|
|
|
|
|
|
|
|
namespace Pathfinding;
|
|
|
|
|
|
|
|
public static partial class Pathfinder
|
|
|
|
{
|
|
|
|
public static List<PathNode> AStarDistance(string workingDir, Coordinates start,
|
|
|
|
Coordinates goal)
|
|
|
|
{
|
|
|
|
RegionManager regionManager = new (workingDir);
|
|
|
|
ValueTuple<OsmNode?, OsmNode?> startAndEndNode = SetupNodes(start, goal, regionManager);
|
|
|
|
if (startAndEndNode.Item1 is null || startAndEndNode.Item2 is null)
|
|
|
|
return new List<PathNode>();
|
|
|
|
OsmNode goalNode = startAndEndNode.Item2!;
|
|
|
|
|
|
|
|
PriorityQueue<OsmNode, double> toVisit = new();
|
|
|
|
toVisit.Enqueue(startAndEndNode.Item1, 0);
|
|
|
|
bool stop = false;
|
|
|
|
|
|
|
|
while (toVisit.Count > 0)
|
|
|
|
{
|
|
|
|
OsmNode closestNodeToGoal = toVisit.Dequeue();
|
|
|
|
|
|
|
|
foreach (OsmEdge edge in closestNodeToGoal.edges)
|
|
|
|
{
|
|
|
|
OsmNode? neighbor = regionManager.GetNode(edge.neighborId, edge.neighborRegion);
|
|
|
|
if (neighbor is not null)
|
|
|
|
{
|
2023-04-09 16:24:43 +02:00
|
|
|
double newPotentialLength = closestNodeToGoal.currentPathLength + Utils.DistanceBetween(closestNodeToGoal, neighbor);
|
|
|
|
if (newPotentialLength < neighbor.currentPathLength)
|
2023-04-09 16:17:15 +02:00
|
|
|
{
|
|
|
|
neighbor.previousPathNode = closestNodeToGoal;
|
2023-04-09 16:24:43 +02:00
|
|
|
neighbor.currentPathLength = newPotentialLength;
|
2023-04-09 16:17:15 +02:00
|
|
|
neighbor.directDistanceToGoal = Utils.DistanceBetween(neighbor, goalNode);
|
|
|
|
|
|
|
|
if (neighbor.Equals(goalNode))
|
|
|
|
{
|
|
|
|
stop = true;
|
|
|
|
}
|
|
|
|
else if(!stop)
|
|
|
|
{
|
|
|
|
toVisit.Enqueue(neighbor, neighbor.directDistanceToGoal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-09 17:38:57 +02:00
|
|
|
return GetRouteFromCalc(goalNode, regionManager);
|
2023-04-09 16:17:15 +02:00
|
|
|
}
|
|
|
|
}
|