50 lines
2.0 KiB
C#
50 lines
2.0 KiB
C#
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, Tag.SpeedType.any);
|
|
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 currentNode = toVisit.Dequeue();
|
|
|
|
foreach (OsmEdge edge in currentNode.edges)
|
|
{
|
|
OsmNode? neighbor = regionManager.GetNode(edge.neighborId, edge.neighborRegion);
|
|
if (neighbor is not null)
|
|
{
|
|
if (Math.Abs(neighbor.directDistanceToGoal - double.MaxValue) < 1)
|
|
neighbor.directDistanceToGoal = Utils.DistanceBetween(neighbor, goalNode);
|
|
double newPotentialLength = currentNode.currentPathLength + Utils.DistanceBetween(currentNode, neighbor);
|
|
if (newPotentialLength < neighbor.currentPathLength)
|
|
{
|
|
neighbor.previousPathNode = currentNode;
|
|
neighbor.currentPathLength = newPotentialLength;
|
|
|
|
if(neighbor.Equals(goalNode))
|
|
return GetRouteFromCalc(goalNode, regionManager);
|
|
if (!toVisit.UnorderedItems.Any(item => item.Element.Equals(neighbor)) && !stop)
|
|
{
|
|
toVisit.Enqueue(neighbor, neighbor.directDistanceToGoal);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return GetRouteFromCalc(goalNode, regionManager);
|
|
}
|
|
} |