CustomAStar now returns ValueTuple with calctime and path as result
This commit is contained in:
parent
9c53c67763
commit
dfc9ffeb2c
@ -1,3 +1,4 @@
|
|||||||
|
using System.Text.Json.Serialization;
|
||||||
using OSMDatastructure.Graph;
|
using OSMDatastructure.Graph;
|
||||||
using OSMImporter;
|
using OSMImporter;
|
||||||
using Pathfinding;
|
using Pathfinding;
|
||||||
@ -17,9 +18,10 @@ var app = builder.Build();
|
|||||||
|
|
||||||
app.MapGet("/getRoute", (float latStart, float lonStart, float latEnd, float lonEnd) =>
|
app.MapGet("/getRoute", (float latStart, float lonStart, float latEnd, float lonEnd) =>
|
||||||
{
|
{
|
||||||
List<PathNode> path = Pathfinder.CustomAStar("D:/stuttgart-regbez-latest", new Coordinates(latStart, lonStart), new Coordinates(latEnd, lonEnd),
|
ValueTuple<TimeSpan, List<PathNode>> result = Pathfinder.CustomAStar("D:/stuttgart-regbez-latest", new Coordinates(latStart, lonStart), new Coordinates(latEnd, lonEnd),
|
||||||
Tag.SpeedType.car);
|
Tag.SpeedType.car);
|
||||||
return path;
|
PathResult pathResult = new PathResult(result.Item1, result.Item2);
|
||||||
|
return pathResult;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -43,3 +45,19 @@ app.UseAuthorization();
|
|||||||
app.MapControllers();
|
app.MapControllers();
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
|
|
||||||
|
internal class PathResult
|
||||||
|
{
|
||||||
|
[JsonInclude]public TimeSpan calcTime;
|
||||||
|
[JsonInclude] public double pathWeight;
|
||||||
|
[JsonInclude] public double pathTravelDistance;
|
||||||
|
[JsonInclude]public List<PathNode> pathNodes;
|
||||||
|
|
||||||
|
public PathResult(TimeSpan calcTime, List<PathNode> pathNodes)
|
||||||
|
{
|
||||||
|
this.calcTime = calcTime;
|
||||||
|
this.pathNodes = pathNodes;
|
||||||
|
this.pathWeight = pathNodes.Last().currentPathWeight;
|
||||||
|
this.pathTravelDistance = pathNodes.Last().currentPathLength;
|
||||||
|
}
|
||||||
|
}
|
@ -6,13 +6,18 @@ namespace Pathfinding;
|
|||||||
|
|
||||||
public static class Pathfinder
|
public static class Pathfinder
|
||||||
{
|
{
|
||||||
public static List<PathNode> CustomAStar(string workingDir, Coordinates start, Coordinates goal, Tag.SpeedType vehicle)
|
public static ValueTuple<TimeSpan, List<PathNode>> CustomAStar(string workingDir, Coordinates start, Coordinates goal, Tag.SpeedType vehicle)
|
||||||
{
|
{
|
||||||
|
DateTime startTime = DateTime.Now;
|
||||||
|
TimeSpan calcTime;
|
||||||
RegionManager regionManager = new (workingDir);
|
RegionManager regionManager = new (workingDir);
|
||||||
OsmNode? startNode = ClosestNodeToCoordinates(start, vehicle, ref regionManager);
|
OsmNode? startNode = ClosestNodeToCoordinates(start, vehicle, ref regionManager);
|
||||||
OsmNode? goalNode = ClosestNodeToCoordinates(goal, vehicle, ref regionManager);
|
OsmNode? goalNode = ClosestNodeToCoordinates(goal, vehicle, ref regionManager);
|
||||||
if (startNode == null || goalNode == null)
|
if (startNode == null || goalNode == null)
|
||||||
return new List<PathNode>();
|
{
|
||||||
|
calcTime = DateTime.Now - startTime;
|
||||||
|
return new ValueTuple<TimeSpan, List<PathNode>>(calcTime, new List<PathNode>());
|
||||||
|
}
|
||||||
|
|
||||||
PriorityQueue<OsmNode, double> toVisit = new();
|
PriorityQueue<OsmNode, double> toVisit = new();
|
||||||
toVisit.Enqueue(startNode, 0);
|
toVisit.Enqueue(startNode, 0);
|
||||||
@ -24,7 +29,7 @@ public static class Pathfinder
|
|||||||
while (toVisit.Count > 0)
|
while (toVisit.Count > 0)
|
||||||
{
|
{
|
||||||
OsmNode closestNodeToGoal = toVisit.Dequeue();
|
OsmNode closestNodeToGoal = toVisit.Dequeue();
|
||||||
Console.WriteLine($"{toVisit.Count:000} {closestNodeToGoal.directDistanceToGoal:#.00} current:{closestNodeToGoal}");
|
//Console.WriteLine($"{toVisit.Count:000} {closestNodeToGoal.directDistanceToGoal:#.00} current:{closestNodeToGoal}");
|
||||||
|
|
||||||
foreach (OsmEdge edge in closestNodeToGoal.edges)
|
foreach (OsmEdge edge in closestNodeToGoal.edges)
|
||||||
{
|
{
|
||||||
@ -63,7 +68,8 @@ public static class Pathfinder
|
|||||||
}
|
}
|
||||||
path.Reverse();
|
path.Reverse();
|
||||||
|
|
||||||
return path;
|
calcTime = DateTime.Now - startTime;
|
||||||
|
return new ValueTuple<TimeSpan, List<PathNode>>(calcTime, path);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static OsmNode? ClosestNodeToCoordinates(Coordinates coordinates, Tag.SpeedType vehicle, ref RegionManager regionManager)
|
public static OsmNode? ClosestNodeToCoordinates(Coordinates coordinates, Tag.SpeedType vehicle, ref RegionManager regionManager)
|
||||||
|
Reference in New Issue
Block a user