Created seperate PathNode class for Json Serialization
This commit is contained in:
32
Pathfinding/PathNode.cs
Normal file
32
Pathfinding/PathNode.cs
Normal file
@ -0,0 +1,32 @@
|
||||
using System.Text.Json.Serialization;
|
||||
using OSMDatastructure.Graph;
|
||||
|
||||
namespace Pathfinding;
|
||||
|
||||
public class PathNode : OsmNode
|
||||
{
|
||||
[JsonInclude]public new double currentPathWeight = double.MaxValue;
|
||||
[JsonInclude]public new double currentPathLength = double.MaxValue;
|
||||
[JsonInclude]public new double directDistanceToGoal = double.MaxValue;
|
||||
|
||||
public PathNode(ulong nodeId, float lat, float lon) : base(nodeId, lat, lon)
|
||||
{
|
||||
}
|
||||
|
||||
public PathNode(ulong nodeId, Coordinates coordinates) : base(nodeId, coordinates)
|
||||
{
|
||||
}
|
||||
|
||||
public static PathNode? FromOsmNode(OsmNode? node)
|
||||
{
|
||||
if (node is null)
|
||||
return null;
|
||||
PathNode retNode = new PathNode(node.nodeId, node.coordinates)
|
||||
{
|
||||
currentPathLength = node.currentPathLength,
|
||||
currentPathWeight = node.currentPathWeight,
|
||||
directDistanceToGoal = node.directDistanceToGoal
|
||||
};
|
||||
return retNode;
|
||||
}
|
||||
}
|
@ -6,30 +6,29 @@ namespace Pathfinding;
|
||||
|
||||
public static class Pathfinder
|
||||
{
|
||||
public static List<OsmNode> CustomAStar(string workingDir, Coordinates start, Coordinates goal, Tag.SpeedType vehicle)
|
||||
public static List<PathNode> CustomAStar(string workingDir, Coordinates start, Coordinates goal, Tag.SpeedType vehicle)
|
||||
{
|
||||
RegionManager regionManager = new RegionManager(workingDir);
|
||||
Region? startRegion = regionManager.GetRegion(start);
|
||||
Region? goalRegion = regionManager.GetRegion(goal);
|
||||
if (startRegion is null || goalRegion is null)
|
||||
return new List<OsmNode>();
|
||||
return new List<PathNode>();
|
||||
|
||||
OsmNode? startNode = ClosestNodeToCoordinates(start, startRegion, vehicle, ref regionManager);
|
||||
OsmNode? goalNode = ClosestNodeToCoordinates(goal, goalRegion, vehicle, ref regionManager);
|
||||
if (startNode == null || goalNode == null)
|
||||
return new List<OsmNode>();
|
||||
return new List<PathNode>();
|
||||
|
||||
PriorityQueue<OsmNode, double> toVisit = new();
|
||||
toVisit.Enqueue(startNode, 0);
|
||||
startNode.currentPathWeight = 0;
|
||||
startNode.currentPathLength = 0;
|
||||
startNode.directDistanceToGoal = Utils.DistanceBetween(startNode, goalNode);
|
||||
OsmNode closestNodeToGoal = startNode;
|
||||
bool stop = false;
|
||||
|
||||
while (toVisit.Count > 0)
|
||||
{
|
||||
closestNodeToGoal = toVisit.Dequeue();
|
||||
OsmNode closestNodeToGoal = toVisit.Dequeue();
|
||||
Console.WriteLine($"{toVisit.Count:000} {closestNodeToGoal.directDistanceToGoal:#.00} current:{closestNodeToGoal}");
|
||||
|
||||
foreach (OsmEdge edge in closestNodeToGoal.edges)
|
||||
@ -44,6 +43,7 @@ public static class Pathfinder
|
||||
neighbor.previousPathNode = closestNodeToGoal;
|
||||
neighbor.currentPathWeight = newPotentialWeight;
|
||||
neighbor.currentPathLength = closestNodeToGoal.currentPathLength + Utils.DistanceBetween(closestNodeToGoal, neighbor);
|
||||
neighbor.directDistanceToGoal = Utils.DistanceBetween(neighbor, goalNode);
|
||||
|
||||
if (neighbor.Equals(goalNode) || closestNodeToGoal.directDistanceToGoal < 10)
|
||||
{
|
||||
@ -52,7 +52,6 @@ public static class Pathfinder
|
||||
}
|
||||
else if(!stop)
|
||||
{
|
||||
neighbor.directDistanceToGoal = Utils.DistanceBetween(neighbor, goalNode);
|
||||
toVisit.Enqueue(neighbor, neighbor.directDistanceToGoal);
|
||||
}
|
||||
}
|
||||
@ -60,14 +59,13 @@ public static class Pathfinder
|
||||
}
|
||||
}
|
||||
|
||||
List<OsmNode> path = new();
|
||||
List<PathNode> path = new();
|
||||
OsmNode? currentNode = goalNode;
|
||||
while (currentNode != null && !currentNode.Equals(startNode))
|
||||
while (currentNode is not null)
|
||||
{
|
||||
path.Add(currentNode);
|
||||
path.Add(PathNode.FromOsmNode(currentNode)!);
|
||||
currentNode = currentNode.previousPathNode;
|
||||
}
|
||||
path.Add(startNode);
|
||||
path.Reverse();
|
||||
|
||||
return path;
|
||||
|
Reference in New Issue
Block a user