Update Dependencies,

Introduced directional search
This commit is contained in:
glax 2024-07-23 16:42:03 +02:00
parent f8acf3c9eb
commit 0758ae1d86
4 changed files with 35 additions and 29 deletions

View File

@ -2,6 +2,7 @@
using Graph;
using Microsoft.Extensions.Logging;
using Graph.Utils;
using OSM_Graph.Enums;
using OSM_Regions;
namespace astar
@ -13,13 +14,13 @@ namespace astar
{
RegionLoader rl = new(regionSize, importFolderPath, logger: logger);
Graph graph = Spiral(rl, startLat, startLon, regionSize);
Graph endRegion = Spiral(rl, endLat, endLon, regionSize);
graph.ConcatGraph(endRegion);
KeyValuePair<ulong, Node> startNode = graph.ClosestNodeToCoordinates(startLat, startLon, car);
startNode.Value.PreviousIsFromStart = true;
startNode.Value.PreviousNodeId = startNode.Key;
startNode.Value.Distance = 0f;
Graph endRegion = Spiral(rl, endLat, endLon, regionSize);
graph.ConcatGraph(endRegion);
KeyValuePair<ulong, Node> endNode = graph.ClosestNodeToCoordinates(endLat, endLon, car);
endNode.Value.PreviousIsFromStart = false;
endNode.Value.PreviousNodeId = endNode.Key;
@ -41,20 +42,25 @@ namespace astar
{
ulong currentNodeStartId = toVisitStart.Dequeue();
Node currentNodeStart = graph.Nodes[currentNodeStartId];
foreach ((ulong neighborId, ulong wayId) in currentNodeStart.Neighbors)
foreach ((ulong neighborId, KeyValuePair<ulong, bool> wayId) in currentNodeStart.Neighbors)
{
if (!graph.ContainsNode(neighborId))
graph.ConcatGraph(Graph.FromGraph(rl.LoadRegionFromNodeId(neighborId)));
if (!graph.ContainsWay(wayId))
if (!graph.ContainsWay(wayId.Key))
{
foreach (global::Graph.Graph? g in rl.LoadRegionsFromWayId(wayId))
foreach (global::Graph.Graph? g in rl.LoadRegionsFromWayId(wayId.Key))
graph.ConcatGraph(Graph.FromGraph(g));
}
Way way = graph.Ways[wayId];
OSM_Graph.Way way = graph.Ways[wayId.Key];
byte speed = SpeedHelper.GetSpeed(way, car);
if(speed < 1)
continue;
if(wayId.Value && way.GetDirection() == WayDirection.Forwards && car)
continue;
if(!wayId.Value && way.GetDirection() == WayDirection.Backwards && car)
continue;
Node neighborNode = graph.Nodes[neighborId];
if (neighborNode.PreviousIsFromStart is false)//Check if we found the opposite End
@ -73,20 +79,26 @@ namespace astar
ulong currentNodeEndId = toVisitEnd.Dequeue();
Node currentNodeEnd = graph.Nodes[currentNodeEndId];
foreach ((ulong neighborId, ulong wayId) in currentNodeEnd.Neighbors)
foreach ((ulong neighborId, KeyValuePair<ulong, bool> wayId) in currentNodeEnd.Neighbors)
{
if (!graph.ContainsNode(neighborId))
graph.ConcatGraph(Graph.FromGraph(rl.LoadRegionFromNodeId(neighborId)));
if (!graph.ContainsWay(wayId))
if (!graph.ContainsWay(wayId.Key))
{
foreach (global::Graph.Graph? g in rl.LoadRegionsFromWayId(wayId))
foreach (global::Graph.Graph? g in rl.LoadRegionsFromWayId(wayId.Key))
graph.ConcatGraph(Graph.FromGraph(g));
}
Way way = graph.Ways[wayId];
OSM_Graph.Way way = graph.Ways[wayId.Key];
byte speed = SpeedHelper.GetSpeed(way, car);
if(speed < 1)
continue;
if(wayId.Value && way.GetDirection() == WayDirection.Backwards && car)
continue;
if(!wayId.Value && way.GetDirection() == WayDirection.Forwards && car)
continue;
Node neighborNode = graph.Nodes[neighborId];
if (neighborNode.PreviousIsFromStart is true)//Check if we found the opposite End

View File

@ -6,7 +6,7 @@ namespace astar;
public class Graph
{
public readonly Dictionary<ulong, Node> Nodes = new();
public readonly Dictionary<ulong, Way> Ways = new ();
public readonly Dictionary<ulong, OSM_Graph.Way> Ways = new ();
public static Graph? FromGraph(global::Graph.Graph? graph)
{
@ -16,7 +16,7 @@ public class Graph
foreach ((ulong id, global::Graph.Node? node) in graph.Nodes)
ret.Nodes.Add(id, Node.FromGraphNode(node));
foreach ((ulong id, Way? way) in graph.Ways)
ret.Ways.Add(id, way);
ret.Ways.Add(id, new OSM_Graph.Way(id, way.Tags, new()));
return ret;
}
@ -26,7 +26,7 @@ public class Graph
return;
foreach ((ulong id, Node n) in graph.Nodes)
this.Nodes.TryAdd(id, n);
foreach ((ulong id, Way w) in graph.Ways)
foreach ((ulong id, OSM_Graph.Way w) in graph.Ways)
this.Ways.TryAdd(id, w);
}
@ -40,7 +40,7 @@ public class Graph
return Nodes.ContainsKey(nodeId);
}
public bool ContainsWay(Way way)
public bool ContainsWay(OSM_Graph.Way way)
{
return Ways.ContainsValue(way);
}
@ -52,7 +52,7 @@ public class Graph
public KeyValuePair<ulong, Node> ClosestNodeToCoordinates(float lat, float lon, bool car = true)
{
return Nodes.Where(n => n.Value.Neighbors.Values.Any(wayId => SpeedHelper.GetSpeed(Ways[wayId], car) > 0)).MinBy(n => n.Value.DistanceTo(lat, lon));
return Nodes.Where(n => n.Value.Neighbors.Values.Any(way => SpeedHelper.GetSpeed(Ways[way.Key], car) > 0)).MinBy(n => n.Value.DistanceTo(lat, lon));
}
public override string ToString()

View File

@ -1,6 +1,6 @@
namespace astar;
public class Node(float lat, float lon, Dictionary<ulong, ulong>? neighbors = null) : global::Graph.Node(lat, lon, neighbors)
public class Node(float lat, float lon, Dictionary<ulong, KeyValuePair<ulong, bool>>? neighbors = null) : global::Graph.Node(lat, lon, neighbors)
{
public ulong? PreviousNodeId = null;
public float? Distance = null;

View File

@ -1,23 +1,17 @@
using Graph;
using OSM_Graph.Enums;
namespace astar.PathingHelper;
internal static class SpeedHelper
{
public static byte GetSpeed(Way way, bool car = true)
public static byte GetSpeed(OSM_Graph.Way way, bool car = true)
{
if (!way.Tags.TryGetValue("highway", out string? highwayTypeStr))
return 0;
if (!Enum.TryParse(highwayTypeStr, out HighwayType highwayType))
return 0;
byte speed = car ? SpeedCar[highwayType] : SpeedPedestrian[highwayType];
if (speed < 1)
return speed;
if(!way.Tags.TryGetValue("maxspeed", out string? maxSpeedStr))
return speed;
if (!byte.TryParse(maxSpeedStr, out speed))
return speed;
return speed;
byte maxspeed = way.GetMaxSpeed();
if (maxspeed != 0)
return maxspeed;
HighwayType highwayType = way.GetHighwayType();
return car ? SpeedCar[highwayType] : SpeedPedestrian[highwayType];
}
public static byte GetMaxSpeed(bool car = true)