Added distance and duration to PathResult.

This commit is contained in:
glax 2023-04-21 11:40:26 +02:00
parent 7d769a064f
commit 42e915ee05
2 changed files with 16 additions and 5 deletions

View File

@ -6,11 +6,15 @@ public class PathResult
{
[JsonInclude]public TimeSpan calcTime;
[JsonInclude]public List<PathNode> pathNodes;
[JsonInclude]public double distance;
[JsonInclude]public double weight;
[JsonConstructor]
public PathResult(TimeSpan calcTime, List<PathNode> pathNodes)
public PathResult(TimeSpan calcTime, List<PathNode> pathNodes, double distance, double weight)
{
this.calcTime = calcTime;
this.pathNodes = pathNodes;
this.distance = distance;
this.weight = weight;
}
}

View File

@ -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<PathNode>());
pathResult = new(DateTime.Now - startCalc, new List<PathNode>(),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<PathNode>());
pathResult = new(DateTime.Now - startCalc, new List<PathNode>(),0 ,0);
return this;
}
@ -94,6 +94,8 @@ public class Pathfinder
{
List<PathNode> 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)