From 42e915ee05976da67eeb6e2496561984a671555e Mon Sep 17 00:00:00 2001 From: glax Date: Fri, 21 Apr 2023 11:40:26 +0200 Subject: [PATCH] Added distance and duration to PathResult. --- Pathfinding/PathResult.cs | 6 +++++- Pathfinding/Pathfinder.cs | 15 +++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/Pathfinding/PathResult.cs b/Pathfinding/PathResult.cs index a0ebd63..4200071 100644 --- a/Pathfinding/PathResult.cs +++ b/Pathfinding/PathResult.cs @@ -6,11 +6,15 @@ public class PathResult { [JsonInclude]public TimeSpan calcTime; [JsonInclude]public List pathNodes; + [JsonInclude]public double distance; + [JsonInclude]public double weight; [JsonConstructor] - public PathResult(TimeSpan calcTime, List pathNodes) + public PathResult(TimeSpan calcTime, List pathNodes, double distance, double weight) { this.calcTime = calcTime; this.pathNodes = pathNodes; + this.distance = distance; + this.weight = weight; } } \ No newline at end of file diff --git a/Pathfinding/Pathfinder.cs b/Pathfinding/Pathfinder.cs index f469344..52d2cbf 100644 --- a/Pathfinding/Pathfinder.cs +++ b/Pathfinding/Pathfinder.cs @@ -36,7 +36,7 @@ public class Pathfinder OsmNode? goalNode = regionManager.ClosestNodeToCoordinates(goalCoordinates, _speedType); if (startNode is null || goalNode is null) { - pathResult = new(DateTime.Now - startCalc, new List()); + pathResult = new(DateTime.Now - startCalc, new List(),0 ,0); return this; } @@ -52,7 +52,7 @@ public class Pathfinder { TimeSpan calcTime = DateTime.Now - startCalc; pathResult = GetPath(goalNode, calcTime); - Console.WriteLine($"Path found. {calcTime} PathLength {pathResult.pathNodes.Count} VisitedNodes {gScore.Count}"); + Console.WriteLine($"Path found. {calcTime} PathLength {pathResult.pathNodes.Count} VisitedNodes {gScore.Count} Distance {pathResult.distance} Duration {pathResult.weight}"); return this; } @@ -78,7 +78,7 @@ public class Pathfinder } } - pathResult = new(DateTime.Now - startCalc, new List()); + pathResult = new(DateTime.Now - startCalc, new List(),0 ,0); return this; } @@ -94,6 +94,8 @@ public class Pathfinder { 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); @@ -102,12 +104,17 @@ public class Pathfinder 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); + return new PathResult(calcFinished, path, retDistance, retDistance / (weight / path.Count)); } private double Weight(OsmNode currentNode, OsmNode neighborNode, OsmEdge edge, double angleWeightFactor)