2023-04-09 17:06:45 +02:00
|
|
|
using OSMDatastructure;
|
2023-04-09 16:47:30 +02:00
|
|
|
using OSMDatastructure.Graph;
|
|
|
|
using Utils = OSMDatastructure.Utils;
|
|
|
|
|
|
|
|
namespace Pathfinding;
|
|
|
|
|
|
|
|
public static partial class Pathfinder
|
|
|
|
{
|
|
|
|
public static List<PathNode> AStarTime(string workingDir, Coordinates start,
|
|
|
|
Coordinates goal, Tag.SpeedType vehicle)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2023-04-09 18:37:45 +02:00
|
|
|
OsmNode currentNode = toVisit.Dequeue();
|
2023-04-09 16:47:30 +02:00
|
|
|
|
2023-04-09 18:37:45 +02:00
|
|
|
foreach (OsmEdge edge in currentNode.edges.Where(
|
|
|
|
edge => regionManager.TestValidConnectionForType(currentNode, edge, vehicle)))
|
2023-04-09 16:47:30 +02:00
|
|
|
{
|
|
|
|
OsmNode? neighbor = regionManager.GetNode(edge.neighborId, edge.neighborRegion);
|
|
|
|
if (neighbor is not null)
|
|
|
|
{
|
2023-04-09 19:22:21 +02:00
|
|
|
if (Math.Abs(neighbor.directDistanceToGoal - double.MaxValue) < 1)
|
|
|
|
neighbor.directDistanceToGoal = Utils.DistanceBetween(neighbor, goalNode);
|
|
|
|
|
2023-04-09 18:37:45 +02:00
|
|
|
double newPotentialWeight = currentNode.currentPathWeight +
|
|
|
|
EdgeWeight(currentNode, edge, vehicle, regionManager);
|
2023-04-09 19:22:21 +02:00
|
|
|
|
2023-04-09 16:47:30 +02:00
|
|
|
if (newPotentialWeight < neighbor.currentPathWeight)
|
|
|
|
{
|
2023-04-09 18:37:45 +02:00
|
|
|
neighbor.previousPathNode = currentNode;
|
|
|
|
neighbor.currentPathLength = currentNode.currentPathLength + Utils.DistanceBetween(currentNode, neighbor);
|
2023-04-09 16:47:30 +02:00
|
|
|
neighbor.currentPathWeight = newPotentialWeight;
|
2023-04-09 19:22:21 +02:00
|
|
|
|
|
|
|
if(neighbor.Equals(goalNode))
|
|
|
|
return GetRouteFromCalc(goalNode, regionManager);
|
|
|
|
//stop = true;
|
|
|
|
if (!toVisit.UnorderedItems.Any(item => item.Element.Equals(neighbor)) && !stop)
|
2023-04-09 16:47:30 +02:00
|
|
|
{
|
2023-04-09 19:22:21 +02:00
|
|
|
toVisit.Enqueue(neighbor, neighbor.currentPathWeight + neighbor.directDistanceToGoal);
|
2023-04-09 16:47:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-09 17:38:57 +02:00
|
|
|
return GetRouteFromCalc(goalNode, regionManager);
|
2023-04-09 16:47:30 +02:00
|
|
|
}
|
|
|
|
}
|