151 lines
5.7 KiB
C#
151 lines
5.7 KiB
C#
using OSMDatastructure;
|
|
using OSMDatastructure.Graph;
|
|
using WayType = OSMDatastructure.Tag.WayType;
|
|
|
|
namespace Pathfinding;
|
|
|
|
public static partial class Pathfinder
|
|
{
|
|
|
|
private static ValueTuple<OsmNode?, OsmNode?> SetupNodes(Coordinates startCoordinates, Coordinates goalCoordinates, RegionManager regionManager, Tag.SpeedType vehicle)
|
|
{
|
|
ValueTuple<OsmNode?, OsmNode?> retTuple = new();
|
|
retTuple.Item1 = regionManager.ClosestNodeToCoordinates(startCoordinates, vehicle);
|
|
retTuple.Item2 = regionManager.ClosestNodeToCoordinates(goalCoordinates, vehicle);
|
|
if (retTuple.Item1 is null || retTuple.Item2 is null)
|
|
return retTuple;
|
|
retTuple.Item1.currentPathWeight = 0;
|
|
retTuple.Item1.currentPathLength = 0;
|
|
retTuple.Item1.directDistanceToGoal = Utils.DistanceBetween(retTuple.Item1, retTuple.Item2);
|
|
return retTuple;
|
|
}
|
|
|
|
private static double EdgeWeight(OsmNode node1, OsmEdge edge, Tag.SpeedType vehicle, RegionManager regionManager)
|
|
{
|
|
OsmNode? node2 = regionManager.GetNode(edge.neighborId, edge.neighborRegion);
|
|
if (node2 is null)
|
|
return double.MaxValue;
|
|
double distance = Utils.DistanceBetween(node1, node2);
|
|
byte speed = regionManager.GetSpeedForEdge(node1, edge.wayId, vehicle);
|
|
return speed is 0 ? double.MaxValue : distance / speed;
|
|
}
|
|
|
|
//TODO when refs are imported use same road
|
|
private static double GetPriority(OsmNode current, OsmEdge edge, Tag.SpeedType vehicle, RegionManager regionManager)
|
|
{
|
|
if (vehicle == Tag.SpeedType.any)
|
|
return 1;
|
|
const double roadPriorityFactor = 1;
|
|
const double roadSpeedFactor = 0.0001;
|
|
Region r = regionManager.GetRegion(current.coordinates)!;
|
|
|
|
double roadPriority = GetPriorityVehicleRoad(edge, vehicle, r) * roadPriorityFactor;
|
|
double roadSpeed = regionManager.GetSpeedForEdge(current, edge.wayId, vehicle) * roadSpeedFactor;
|
|
|
|
if (vehicle == Tag.SpeedType.pedestrian)
|
|
return current.directDistanceToGoal / roadPriority;
|
|
|
|
|
|
if (vehicle == Tag.SpeedType.car)
|
|
return current.directDistanceToGoal / roadPriority;
|
|
|
|
return double.MaxValue;
|
|
}
|
|
|
|
private static double GetPriorityVehicleRoad(OsmEdge edge, Tag.SpeedType vehicle, Region region)
|
|
{
|
|
if (vehicle == Tag.SpeedType.any)
|
|
return 1;
|
|
WayType? wayType = (WayType?)region.tagManager.GetTag(edge.wayId, Tag.TagType.highway);
|
|
if(wayType is null)
|
|
return double.MaxValue;
|
|
if (vehicle == Tag.SpeedType.car)
|
|
{
|
|
switch (wayType)
|
|
{
|
|
case WayType.motorway:
|
|
case WayType.motorway_link:
|
|
case WayType.motorroad:
|
|
case WayType.trunk:
|
|
case WayType.trunk_link:
|
|
case WayType.primary:
|
|
case WayType.primary_link:
|
|
return 7;
|
|
case WayType.secondary:
|
|
case WayType.secondary_link:
|
|
case WayType.tertiary:
|
|
case WayType.tertiary_link:
|
|
return 4;
|
|
case WayType.unclassified:
|
|
case WayType.residential:
|
|
case WayType.road:
|
|
case WayType.living_street:
|
|
case WayType.service:
|
|
case WayType.track:
|
|
return 2;
|
|
default:
|
|
return 1;
|
|
}
|
|
}
|
|
if (vehicle == Tag.SpeedType.pedestrian)
|
|
{
|
|
switch (wayType)
|
|
{
|
|
case WayType.pedestrian:
|
|
case WayType.corridor:
|
|
case WayType.footway:
|
|
case WayType.path:
|
|
case WayType.steps:
|
|
case WayType.residential:
|
|
case WayType.living_street:
|
|
return 10;
|
|
case WayType.service:
|
|
case WayType.cycleway:
|
|
case WayType.bridleway:
|
|
case WayType.road:
|
|
case WayType.track:
|
|
case WayType.unclassified:
|
|
return 5;
|
|
case WayType.tertiary:
|
|
case WayType.tertiary_link:
|
|
case WayType.escape:
|
|
return 2;
|
|
default:
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
return 100;
|
|
|
|
}
|
|
|
|
private static List<PathNode> GetRouteFromCalc(OsmNode goalNode, RegionManager regionManager)
|
|
{
|
|
List<PathNode> path = new();
|
|
OsmNode? currentNode = goalNode;
|
|
while (currentNode is not null)
|
|
{
|
|
HashSet<Tag>? tags = null;
|
|
double pathDistanceDelta = 0;
|
|
double pathWeightDelta = 0;
|
|
double directDistanceDelta = 0;
|
|
if (currentNode.previousPathNode is not null)
|
|
{
|
|
OsmEdge edge = currentNode.previousPathNode!.edges.First(e => e.neighborId.Equals(currentNode.nodeId));
|
|
tags = regionManager.GetRegion(currentNode.coordinates)!.tagManager.GetTagsForWayId(edge.wayId);
|
|
pathDistanceDelta = currentNode.currentPathLength - currentNode.previousPathNode.currentPathLength;
|
|
pathWeightDelta = currentNode.currentPathWeight - currentNode.previousPathNode.currentPathWeight;
|
|
directDistanceDelta =
|
|
currentNode.directDistanceToGoal - currentNode.previousPathNode.directDistanceToGoal;
|
|
}
|
|
|
|
PathNode? pn = PathNode.FromOsmNode(currentNode, tags, pathDistanceDelta, pathWeightDelta, directDistanceDelta);
|
|
if(pn is not null)
|
|
path.Add(pn!);
|
|
currentNode = currentNode.previousPathNode;
|
|
}
|
|
|
|
path.Reverse();
|
|
return path;
|
|
}
|
|
} |