From 8813023cd66fff8194e4ea49f1bdc16286f8dd65 Mon Sep 17 00:00:00 2001 From: glax Date: Thu, 6 Apr 2023 14:27:25 +0200 Subject: [PATCH] Created seperate PathNode class for Json Serialization --- API/Program.cs | 3 ++- OSMDatastructure/OsmNode.cs | 4 ++-- Pathfinding/PathNode.cs | 32 ++++++++++++++++++++++++++++++++ Pathfinding/Pathfinder.cs | 18 ++++++++---------- 4 files changed, 44 insertions(+), 13 deletions(-) create mode 100644 Pathfinding/PathNode.cs diff --git a/API/Program.cs b/API/Program.cs index 1d1e1a0..be3804a 100644 --- a/API/Program.cs +++ b/API/Program.cs @@ -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 path = Pathfinding.Pathfinder.CustomAStar("D:/stuttgart-regbez-latest", new Coordinates(latStart, lonStart), new Coordinates(latEnd, lonEnd), + List path = Pathfinder.CustomAStar("D:/stuttgart-regbez-latest", new Coordinates(latStart, lonStart), new Coordinates(latEnd, lonEnd), Tag.SpeedType.car); return path; } diff --git a/OSMDatastructure/OsmNode.cs b/OSMDatastructure/OsmNode.cs index e2d4642..ebf0676 100644 --- a/OSMDatastructure/OsmNode.cs +++ b/OSMDatastructure/OsmNode.cs @@ -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] diff --git a/Pathfinding/PathNode.cs b/Pathfinding/PathNode.cs new file mode 100644 index 0000000..b5af0b0 --- /dev/null +++ b/Pathfinding/PathNode.cs @@ -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; + } +} \ No newline at end of file diff --git a/Pathfinding/Pathfinder.cs b/Pathfinding/Pathfinder.cs index 030e49d..495e66b 100644 --- a/Pathfinding/Pathfinder.cs +++ b/Pathfinding/Pathfinder.cs @@ -6,30 +6,29 @@ namespace Pathfinding; public static class Pathfinder { - public static List CustomAStar(string workingDir, Coordinates start, Coordinates goal, Tag.SpeedType vehicle) + public static List 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(); + return new List(); OsmNode? startNode = ClosestNodeToCoordinates(start, startRegion, vehicle, ref regionManager); OsmNode? goalNode = ClosestNodeToCoordinates(goal, goalRegion, vehicle, ref regionManager); if (startNode == null || goalNode == null) - return new List(); + return new List(); PriorityQueue 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 path = new(); + List 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;