Created seperate PathNode class for Json Serialization
This commit is contained in:
parent
72b5511c26
commit
8813023cd6
@ -1,4 +1,5 @@
|
||||
using OSMDatastructure.Graph;
|
||||
using Pathfinding;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
@ -15,7 +16,7 @@ var app = builder.Build();
|
||||
|
||||
app.MapGet("/getRoute", (float latStart, float lonStart, float latEnd, float lonEnd) =>
|
||||
{
|
||||
List<OsmNode> path = Pathfinding.Pathfinder.CustomAStar("D:/stuttgart-regbez-latest", new Coordinates(latStart, lonStart), new Coordinates(latEnd, lonEnd),
|
||||
List<PathNode> path = Pathfinder.CustomAStar("D:/stuttgart-regbez-latest", new Coordinates(latStart, lonStart), new Coordinates(latEnd, lonEnd),
|
||||
Tag.SpeedType.car);
|
||||
return path;
|
||||
}
|
||||
|
@ -12,8 +12,8 @@ public class OsmNode
|
||||
public Coordinates coordinates { get; }
|
||||
|
||||
[JsonIgnore][NonSerialized]public OsmNode? previousPathNode = null;
|
||||
[JsonInclude][NonSerialized]public double currentPathWeight = double.MaxValue;
|
||||
[JsonInclude][NonSerialized]public double currentPathLength = double.MaxValue;
|
||||
[JsonIgnore][NonSerialized]public double currentPathWeight = double.MaxValue;
|
||||
[JsonIgnore][NonSerialized]public double currentPathLength = double.MaxValue;
|
||||
[JsonIgnore][NonSerialized]public double directDistanceToGoal = double.MaxValue;
|
||||
|
||||
[OnDeserialized]
|
||||
|
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;
|
||||
|
Loading…
Reference in New Issue
Block a user