Compare commits

..

No commits in common. "fe0ccd0fca955fa04052ea9765290ca012c00ab3" and "72b5511c2654ef2437ecb02ee7946e9b23b4a44a" have entirely different histories.

5 changed files with 33 additions and 93 deletions

View File

@ -1,7 +1,4 @@
using System.Text.Json.Serialization;
using OSMDatastructure.Graph; using OSMDatastructure.Graph;
using OSMImporter;
using Pathfinding;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
@ -18,19 +15,12 @@ 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) =>
{ {
ValueTuple<TimeSpan, List<PathNode>> result = Pathfinder.CustomAStar("D:/stuttgart-regbez-latest", new Coordinates(latStart, lonStart), new Coordinates(latEnd, lonEnd), List<OsmNode> path = Pathfinding.Pathfinder.CustomAStar("D:/stuttgart-regbez-latest", new Coordinates(latStart, lonStart), new Coordinates(latEnd, lonEnd),
Tag.SpeedType.car); Tag.SpeedType.car);
PathResult pathResult = new PathResult(result.Item1, result.Item2); return path;
return pathResult;
} }
); );
app.MapGet("/getClosestNode", (float lat, float lon) =>
{
RegionManager regionManager = new RegionManager("D:/stuttgart-regbez-latest");
return Pathfinder.ClosestNodeToCoordinates(new Coordinates(lat, lon), Tag.SpeedType.car, ref regionManager);
});
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment())
{ {
@ -44,20 +34,4 @@ 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;
}
}

View File

@ -12,8 +12,8 @@ public class OsmNode
public Coordinates coordinates { get; } public Coordinates coordinates { get; }
[JsonIgnore][NonSerialized]public OsmNode? previousPathNode = null; [JsonIgnore][NonSerialized]public OsmNode? previousPathNode = null;
[JsonIgnore][NonSerialized]public double currentPathWeight = double.MaxValue; [JsonInclude][NonSerialized]public double currentPathWeight = double.MaxValue;
[JsonIgnore][NonSerialized]public double currentPathLength = double.MaxValue; [JsonInclude][NonSerialized]public double currentPathLength = double.MaxValue;
[JsonIgnore][NonSerialized]public double directDistanceToGoal = double.MaxValue; [JsonIgnore][NonSerialized]public double directDistanceToGoal = double.MaxValue;
[OnDeserialized] [OnDeserialized]

View File

@ -1,32 +0,0 @@
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;
}
}

View File

@ -6,30 +6,31 @@ namespace Pathfinding;
public static class Pathfinder public static class Pathfinder
{ {
public static ValueTuple<TimeSpan, List<PathNode>> CustomAStar(string workingDir, Coordinates start, Coordinates goal, Tag.SpeedType vehicle) public static List<OsmNode> CustomAStar(string workingDir, Coordinates start, Coordinates goal, Tag.SpeedType vehicle)
{ {
DateTime startTime = DateTime.Now; RegionManager regionManager = new RegionManager(workingDir);
TimeSpan calcTime; Region? startRegion = regionManager.GetRegion(start);
RegionManager regionManager = new (workingDir); Region? goalRegion = regionManager.GetRegion(goal);
OsmNode? startNode = ClosestNodeToCoordinates(start, vehicle, ref regionManager); if (startRegion is null || goalRegion is null)
OsmNode? goalNode = ClosestNodeToCoordinates(goal, vehicle, ref regionManager); return new List<OsmNode>();
OsmNode? startNode = ClosestNodeToCoordinates(start, startRegion, vehicle, ref regionManager);
OsmNode? goalNode = ClosestNodeToCoordinates(goal, goalRegion, vehicle, ref regionManager);
if (startNode == null || goalNode == null) if (startNode == null || goalNode == null)
{ return new List<OsmNode>();
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);
startNode.currentPathWeight = 0; startNode.currentPathWeight = 0;
startNode.currentPathLength = 0; startNode.currentPathLength = 0;
startNode.directDistanceToGoal = Utils.DistanceBetween(startNode, goalNode); startNode.directDistanceToGoal = Utils.DistanceBetween(startNode, goalNode);
OsmNode closestNodeToGoal = startNode;
bool stop = false; bool stop = false;
while (toVisit.Count > 0) while (toVisit.Count > 0)
{ {
OsmNode closestNodeToGoal = toVisit.Dequeue(); 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)
{ {
@ -43,7 +44,6 @@ public static class Pathfinder
neighbor.previousPathNode = closestNodeToGoal; neighbor.previousPathNode = closestNodeToGoal;
neighbor.currentPathWeight = newPotentialWeight; neighbor.currentPathWeight = newPotentialWeight;
neighbor.currentPathLength = closestNodeToGoal.currentPathLength + Utils.DistanceBetween(closestNodeToGoal, neighbor); neighbor.currentPathLength = closestNodeToGoal.currentPathLength + Utils.DistanceBetween(closestNodeToGoal, neighbor);
neighbor.directDistanceToGoal = Utils.DistanceBetween(neighbor, goalNode);
if (neighbor.Equals(goalNode) || closestNodeToGoal.directDistanceToGoal < 10) if (neighbor.Equals(goalNode) || closestNodeToGoal.directDistanceToGoal < 10)
{ {
@ -52,6 +52,7 @@ public static class Pathfinder
} }
else if(!stop) else if(!stop)
{ {
neighbor.directDistanceToGoal = Utils.DistanceBetween(neighbor, goalNode);
toVisit.Enqueue(neighbor, neighbor.directDistanceToGoal); toVisit.Enqueue(neighbor, neighbor.directDistanceToGoal);
} }
} }
@ -59,26 +60,23 @@ public static class Pathfinder
} }
} }
List<PathNode> path = new(); List<OsmNode> path = new();
OsmNode? currentNode = goalNode; OsmNode? currentNode = goalNode;
while (currentNode is not null) while (currentNode != null && !currentNode.Equals(startNode))
{ {
path.Add(PathNode.FromOsmNode(currentNode)!); path.Add(currentNode);
currentNode = currentNode.previousPathNode; currentNode = currentNode.previousPathNode;
} }
path.Add(startNode);
path.Reverse(); path.Reverse();
calcTime = DateTime.Now - startTime; return path;
return new ValueTuple<TimeSpan, List<PathNode>>(calcTime, path);
} }
public static OsmNode? ClosestNodeToCoordinates(Coordinates coordinates, Tag.SpeedType vehicle, ref RegionManager regionManager) private static OsmNode? ClosestNodeToCoordinates(Coordinates coordinates, Region region, Tag.SpeedType vehicle, ref RegionManager regionManager)
{ {
OsmNode? closest = null; OsmNode? closest = null;
double distance = double.MaxValue; double distance = double.MaxValue;
Region? region = regionManager.GetRegion(coordinates);
if (region is null)
return null;
foreach (OsmNode node in region.nodes) foreach (OsmNode node in region.nodes)
{ {
bool hasConnectionUsingVehicle = false; bool hasConnectionUsingVehicle = false;

View File

@ -13,21 +13,21 @@ public class Server
Console.SetOut(newConsole); Console.SetOut(newConsole);
Console.SetError(newConsole); Console.SetError(newConsole);
RegionConverter.ConvertXMLToRegions("D:/stuttgart-regbez-latest.osm", "D:/stuttgart-regbez-latest"); //RegionConverter.ConvertXMLToRegions("D:/stuttgart-regbez-latest.osm", "D:/stuttgart-regbez-latest");
//RegionConverter.ConvertXMLToRegions("D:/map.osm", "D:/map"); //RegionConverter.ConvertXMLToRegions("D:/map.osm", "D:/map");
//RegionConverter.ConvertXMLToRegions("D:/germany-latest.osm", "D:/germany-latest"); Console.WriteLine("Loaded");
/*
Coordinates start = new Coordinates(48.794567f, 9.820625f); Coordinates start = new Coordinates(48.793319f, 9.832102f);
Coordinates finish = new Coordinates(48.79593f, 9.824013f); Coordinates finish = new Coordinates(48.8407888f, 10.0676020f);
DateTime startTime = DateTime.Now; DateTime startTime = DateTime.Now;
OsmNode[] path = Pathfinder.CustomAStar("D:/map", start, finish, Tag.SpeedType.car).ToArray(); OsmNode[] path = Pathfinder.CustomAStar("D:/stuttgart-regbez-latest", start, finish, Tag.SpeedType.car).ToArray();
TimeSpan duration = DateTime.Now - startTime; TimeSpan duration = DateTime.Now - startTime;
Console.WriteLine($"Took {duration.TotalMilliseconds}ms ({duration:g})"); Console.WriteLine($"Took {duration.TotalMilliseconds}ms ({duration:g})");
for (int i = 0; i < path.Length - 1; i++) for (int i = 0; i < path.Length - 1; i++)
{ {
Console.WriteLine(path[i]); Console.WriteLine(path[i]);
} }
Console.WriteLine();*/ Console.WriteLine();
} }
} }