From dd374307613099d4fa000f8b9df468e6be4ea4db Mon Sep 17 00:00:00 2001 From: glax Date: Fri, 21 Apr 2023 13:41:01 +0200 Subject: [PATCH] Moved RoadPriority Function to RegionManager --- Pathfinding/Pathfinder.cs | 138 ++++++++++------------------------- Pathfinding/RegionManager.cs | 65 +++++++++++++++++ 2 files changed, 103 insertions(+), 100 deletions(-) diff --git a/Pathfinding/Pathfinder.cs b/Pathfinding/Pathfinder.cs index 52d2cbf..7f9692b 100644 --- a/Pathfinding/Pathfinder.cs +++ b/Pathfinding/Pathfinder.cs @@ -81,42 +81,7 @@ public class Pathfinder pathResult = new(DateTime.Now - startCalc, new List(),0 ,0); return this; } - - public void SaveResult(string path) - { - FileStream fs = new (path, FileMode.CreateNew); - JsonSerializer.Serialize(fs, pathResult, JsonSerializerOptions.Default); - fs.Dispose(); - Console.WriteLine($"Saved result to {path}"); - } - - private PathResult GetPath(OsmNode goalNode, TimeSpan calcFinished) - { - List path = new(); - OsmNode currentNode = goalNode; - double retDistance = 0; - double weight = 0; - while (_cameFromDict!.ContainsKey(_cameFromDict[currentNode])) - { - OsmEdge? currentEdge = _cameFromDict[currentNode].edges.First(edge => edge.neighborId == currentNode.nodeId); - HashSet? tags = - regionManager.GetRegion(currentNode.coordinates)!.tagManager.GetTagsForWayId(currentEdge.wayId); - PathNode? newNode = PathNode.FromOsmNode(currentNode, tags); - if(newNode is not null) - path.Add(newNode); - - double distance = Utils.DistanceBetween(currentNode, _cameFromDict[currentNode]); - retDistance += distance; - weight += regionManager.GetSpeedForEdge(_cameFromDict[currentNode], currentEdge.wayId, _speedType); - - currentNode = _cameFromDict[currentNode]; - } - - path.Reverse(); - - return new PathResult(calcFinished, path, retDistance, retDistance / (weight / path.Count)); - } - + private double Weight(OsmNode currentNode, OsmNode neighborNode, OsmEdge edge, double angleWeightFactor) { double distance = Utils.DistanceBetween(currentNode, neighborNode); @@ -131,13 +96,13 @@ public class Pathfinder double nodeAngle = v1.Angle(v2); angle = ((180 - nodeAngle) / 180) * angleWeightFactor; } - double prio = GetPriorityVehicleRoad(edge, regionManager.GetRegion(currentNode.coordinates)!); + double prio = regionManager.GetPriorityForVehicle(_speedType,edge, regionManager.GetRegion(currentNode.coordinates)!); return distance / (1 + speed + angle + prio); } private double Heuristic(OsmNode currentNode, OsmNode neighborNode, OsmNode goalNode, OsmEdge edge, double roadPriorityFactor, double junctionFactor, double sameRoadFactor, double nodeAngleFactor) { - double roadPriority = GetPriorityVehicleRoad(edge, regionManager.GetRegion(currentNode.coordinates)!) * roadPriorityFactor; + double roadPriority = regionManager.GetPriorityForVehicle(_speedType, edge, regionManager.GetRegion(currentNode.coordinates)!) * roadPriorityFactor; if (roadPriority == 0) return double.MaxValue; @@ -174,6 +139,41 @@ public class Pathfinder return Utils.DistanceBetween(neighborNode, goalNode) / (roadPriority + sameRoadName + junctionCount + angle); } + public void SaveResult(string path) + { + FileStream fs = new (path, FileMode.CreateNew); + JsonSerializer.Serialize(fs, pathResult, JsonSerializerOptions.Default); + fs.Dispose(); + Console.WriteLine($"Saved result to {path}"); + } + + private PathResult GetPath(OsmNode goalNode, TimeSpan calcFinished) + { + List path = new(); + OsmNode currentNode = goalNode; + double retDistance = 0; + double weight = 0; + while (_cameFromDict!.ContainsKey(_cameFromDict[currentNode])) + { + OsmEdge? currentEdge = _cameFromDict[currentNode].edges.First(edge => edge.neighborId == currentNode.nodeId); + HashSet? tags = + regionManager.GetRegion(currentNode.coordinates)!.tagManager.GetTagsForWayId(currentEdge.wayId); + PathNode? newNode = PathNode.FromOsmNode(currentNode, tags); + if(newNode is not null) + path.Add(newNode); + + double distance = Utils.DistanceBetween(currentNode, _cameFromDict[currentNode]); + retDistance += distance; + weight += regionManager.GetSpeedForEdge(_cameFromDict[currentNode], currentEdge.wayId, _speedType); + + currentNode = _cameFromDict[currentNode]; + } + + path.Reverse(); + + return new PathResult(calcFinished, path, retDistance, retDistance / (weight / path.Count)); + } + private class Vector { public float x, y; @@ -205,66 +205,4 @@ public class Pathfinder return angle; } } - - private double GetPriorityVehicleRoad(OsmEdge edge, Region region) - { - if (_speedType == SpeedType.any) - return 1; - WayType? wayType = (WayType?)region.tagManager.GetTag(edge.wayId, TagType.highway); - if(wayType is null) - return 0; - if (_speedType == SpeedType.car) - { - switch (wayType) - { - case WayType.motorway: - case WayType.motorway_link: - case WayType.motorroad: - return 17; - case WayType.trunk: - case WayType.trunk_link: - case WayType.primary: - case WayType.primary_link: - return 10; - case WayType.secondary: - case WayType.secondary_link: - return 7; - case WayType.tertiary: - case WayType.tertiary_link: - return 5; - case WayType.unclassified: - case WayType.residential: - case WayType.road: - case WayType.living_street: - return 2; - } - } - if (_speedType == 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; - } - } - - return 0; - } } \ No newline at end of file diff --git a/Pathfinding/RegionManager.cs b/Pathfinding/RegionManager.cs index 55e9022..1b3febf 100644 --- a/Pathfinding/RegionManager.cs +++ b/Pathfinding/RegionManager.cs @@ -1,6 +1,8 @@ using System.Text.Json; using OSMDatastructure; using OSMDatastructure.Graph; +using SpeedType = OSMDatastructure.Tag.SpeedType; +using WayType = OSMDatastructure.Tag.WayType; namespace Pathfinding { @@ -128,5 +130,68 @@ namespace Pathfinding return 0; } } + + public double GetPriorityForVehicle(SpeedType speedType, OsmEdge edge, Region region) + { + if (speedType == SpeedType.any) + return 1; + WayType? wayType = (WayType?)region.tagManager.GetTag(edge.wayId, Tag.TagType.highway); + if(wayType is null) + return 0; + if (speedType == SpeedType.car) + { + switch (wayType) + { + case WayType.motorway: + case WayType.motorway_link: + case WayType.motorroad: + return 17; + case WayType.trunk: + case WayType.trunk_link: + case WayType.primary: + case WayType.primary_link: + return 10; + case WayType.secondary: + case WayType.secondary_link: + return 7; + case WayType.tertiary: + case WayType.tertiary_link: + return 5; + case WayType.unclassified: + case WayType.residential: + case WayType.road: + case WayType.living_street: + return 2; + } + } + + if (speedType == 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; + } + } + + return 0; + } } } \ No newline at end of file