Compare commits
No commits in common. "2c5ab070a2895449b7e0d0e94b58d3c733b8bcb4" and "82d2de1537cae7884783eaeddf3d51e245d27ee4" have entirely different histories.
2c5ab070a2
...
82d2de1537
@ -6,7 +6,7 @@ namespace OSMDatastructure;
|
||||
[Serializable]
|
||||
public class Region
|
||||
{
|
||||
[NonSerialized]public const float RegionSize = 0.025f;
|
||||
[NonSerialized]public const float RegionSize = 0.1f;
|
||||
public readonly HashSet<OsmNode> nodes = new();
|
||||
public ulong regionHash { get; }
|
||||
public TagManager tagManager { get; }
|
||||
|
@ -14,23 +14,27 @@ public class Pathfinder
|
||||
if (startRegion is null || goalRegion is null)
|
||||
return new List<OsmNode>();
|
||||
|
||||
OsmNode? startNode = ClosestNodeToCoordinates(start, startRegion, vehicle);
|
||||
OsmNode? goalNode = ClosestNodeToCoordinates(goal, goalRegion, vehicle);
|
||||
OsmNode? startNode = ClosestNodeToCoordinates(start, startRegion);
|
||||
OsmNode? goalNode = ClosestNodeToCoordinates(goal, goalRegion);
|
||||
if (startNode == null || goalNode == null)
|
||||
return new List<OsmNode>();
|
||||
|
||||
PriorityQueue<OsmNode, double> toVisit = new();
|
||||
toVisit.Enqueue(startNode, 0);
|
||||
startNode.currentPathWeight = 0;
|
||||
startNode.currentPathLength = 0;
|
||||
startNode.directDistanceToGoal = Utils.DistanceBetween(startNode, goalNode);
|
||||
OsmNode closestNodeToGoal = startNode;
|
||||
List<OsmNode> toVisit = new() { startNode };
|
||||
OsmNode closestNodeToGoal = toVisit.First();
|
||||
closestNodeToGoal.currentPathWeight = 0;
|
||||
closestNodeToGoal.currentPathLength = 0;
|
||||
bool stop = false;
|
||||
|
||||
while (toVisit.Count > 0 && !stop)
|
||||
{
|
||||
closestNodeToGoal = toVisit.Dequeue();
|
||||
Console.WriteLine($"{toVisit.Count:000} {closestNodeToGoal.directDistanceToGoal:#.00} current:{closestNodeToGoal}");
|
||||
//Console.WriteLine("toVisit-length: {0}", toVisit.Count);
|
||||
closestNodeToGoal = toVisit.First();
|
||||
foreach (OsmNode node in toVisit.Where(node => node.directDistanceToGoal.Equals(Double.MaxValue)))
|
||||
{
|
||||
node.directDistanceToGoal = Utils.DistanceBetween(node, goalNode);
|
||||
}
|
||||
|
||||
closestNodeToGoal = toVisit.OrderBy(node => node.directDistanceToGoal).First();
|
||||
|
||||
foreach (OsmEdge edge in closestNodeToGoal.edges)
|
||||
{
|
||||
@ -45,20 +49,19 @@ public class Pathfinder
|
||||
neighbor.currentPathWeight = newPotentialWeight;
|
||||
neighbor.currentPathLength = closestNodeToGoal.currentPathLength + Utils.DistanceBetween(closestNodeToGoal, neighbor);
|
||||
|
||||
if (neighbor.Equals(goalNode) || closestNodeToGoal.directDistanceToGoal < 250)
|
||||
if (neighbor.Equals(goalNode))
|
||||
stop = true;
|
||||
else
|
||||
{
|
||||
neighbor.directDistanceToGoal = Utils.DistanceBetween(neighbor, goalNode);
|
||||
toVisit.Enqueue(neighbor, neighbor.directDistanceToGoal);
|
||||
}
|
||||
toVisit.Add(neighbor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
toVisit.Remove(closestNodeToGoal);
|
||||
}
|
||||
|
||||
List<OsmNode> path = new();
|
||||
OsmNode? currentNode = closestNodeToGoal;
|
||||
OsmNode? currentNode = goalNode;
|
||||
while (currentNode != null && !currentNode.Equals(startNode))
|
||||
{
|
||||
path.Add(currentNode);
|
||||
@ -70,33 +73,14 @@ public class Pathfinder
|
||||
return path;
|
||||
}
|
||||
|
||||
private static OsmNode? ClosestNodeToCoordinates(Coordinates coordinates, Region region, Tag.SpeedType vehicle)
|
||||
private static OsmNode? ClosestNodeToCoordinates(Coordinates coordinates, Region region)
|
||||
{
|
||||
OsmNode? closest = null;
|
||||
double distance = double.MaxValue;
|
||||
foreach (OsmNode node in region.nodes)
|
||||
{
|
||||
bool hasConnection = false;
|
||||
foreach (OsmEdge edge in node.edges)
|
||||
{
|
||||
byte speed = 0;
|
||||
switch (vehicle)
|
||||
{
|
||||
case Tag.SpeedType.road:
|
||||
case Tag.SpeedType.car:
|
||||
speed = Tag.defaultSpeedCar[(Tag.WayType)region.tagManager.GetTag(edge.wayId, Tag.TagType.highway)!];
|
||||
break;
|
||||
case Tag.SpeedType.pedestrian:
|
||||
speed = Tag.defaultSpeedPedestrian[
|
||||
(Tag.WayType)region.tagManager.GetTag(edge.wayId, Tag.TagType.highway)!];
|
||||
break;
|
||||
}
|
||||
|
||||
if (speed != 0)
|
||||
hasConnection = true;
|
||||
}
|
||||
double nodeDistance = Utils.DistanceBetween(node, coordinates);
|
||||
if (nodeDistance < distance && hasConnection)
|
||||
if (nodeDistance < distance)
|
||||
{
|
||||
closest = node;
|
||||
distance = nodeDistance;
|
||||
|
@ -43,7 +43,6 @@ namespace OSMImporter
|
||||
|
||||
private Region? LoadRegion(ulong id)
|
||||
{
|
||||
Console.WriteLine($"Load Region {id}");
|
||||
return Region.FromId(workingDirectory, id);
|
||||
}
|
||||
|
||||
|
@ -28,7 +28,7 @@ public class ConsoleWriter : TextWriter
|
||||
public override void WriteLine(string? text)
|
||||
{
|
||||
DateTime now = DateTime.Now;
|
||||
string dateTimeString = $"[{now.ToUniversalTime():u} ({Math.Floor((now - execStart).TotalMilliseconds):###,###,###})]";
|
||||
string dateTimeString = $"[{now.ToUniversalTime():u} ({Math.Floor((now - execStart).TotalMilliseconds):####00000})]";
|
||||
if(text is not null)
|
||||
OnWriteLine?.Invoke(this, new ConsoleWriterEventArgs($"{dateTimeString} {text}"));
|
||||
stdOut.WriteLine($"{dateTimeString} {text}");
|
||||
|
@ -251,16 +251,14 @@ public class RegionConverter
|
||||
throw new FileNotFoundException("Region does not exist");
|
||||
|
||||
#pragma warning disable SYSLIB0011
|
||||
string waysPath = Path.Join(folderPath, regionHash.ToString(), RegionConverter.WaysFileName);
|
||||
if(File.Exists(waysPath))
|
||||
using (FileStream wayFileStream = new FileStream(waysPath, FileMode.Open))
|
||||
using (FileStream wayFileStream = new FileStream(Path.Join(folderPath, regionHash.ToString(), RegionConverter.WaysFileName), FileMode.Open))
|
||||
{
|
||||
while (wayFileStream.Position < wayFileStream.Length)
|
||||
{
|
||||
while (wayFileStream.Position < wayFileStream.Length)
|
||||
{
|
||||
OsmEdge deserializedEdge = (OsmEdge)bFormatter.Deserialize(wayFileStream);
|
||||
ways.Add(deserializedEdge);
|
||||
}
|
||||
OsmEdge deserializedEdge = (OsmEdge)bFormatter.Deserialize(wayFileStream);
|
||||
ways.Add(deserializedEdge);
|
||||
}
|
||||
}
|
||||
|
||||
using (FileStream nodeFileStream = new FileStream(Path.Join(folderPath, regionHash.ToString(), RegionConverter.NodesFileName), FileMode.Open))
|
||||
{
|
||||
@ -271,18 +269,16 @@ public class RegionConverter
|
||||
}
|
||||
}
|
||||
|
||||
string tagsPath = Path.Join(folderPath, regionHash.ToString(), RegionConverter.TagsFileName);
|
||||
if(File.Exists(tagsPath))
|
||||
using (FileStream tagsFileStream = new FileStream(tagsPath, FileMode.Open))
|
||||
using (FileStream tagsFileStream = new FileStream(Path.Join(folderPath, regionHash.ToString(), RegionConverter.TagsFileName), FileMode.Open))
|
||||
{
|
||||
while (tagsFileStream.Position < tagsFileStream.Length)
|
||||
{
|
||||
while (tagsFileStream.Position < tagsFileStream.Length)
|
||||
{
|
||||
TagManager tm = (TagManager)bFormatter.Deserialize(tagsFileStream);
|
||||
ulong id = (ulong)tm.wayTagSets.First()!.Value.First(tag => tag.key == Tag.TagType.id)!.value;
|
||||
foreach(Tag tag in tm.wayTagSets.First()!.Value)
|
||||
newRegion.tagManager.AddTag(id, tag);
|
||||
}
|
||||
TagManager tm = (TagManager)bFormatter.Deserialize(tagsFileStream);
|
||||
ulong id = (ulong)tm.wayTagSets.First()!.Value.First(tag => tag.key == Tag.TagType.id)!.value;
|
||||
foreach(Tag tag in tm.wayTagSets.First()!.Value)
|
||||
newRegion.tagManager.AddTag(id, tag);
|
||||
}
|
||||
}
|
||||
#pragma warning restore SYSLIB0011
|
||||
|
||||
return new ValueTuple<Region, HashSet<OsmEdge>>(newRegion, ways);
|
||||
|
@ -18,15 +18,19 @@ public class Server
|
||||
Console.WriteLine("Loaded");
|
||||
|
||||
|
||||
Coordinates start = new Coordinates(48.793319f, 9.832102f);
|
||||
Coordinates finish = new Coordinates(48.840728f, 10.067603f);
|
||||
DateTime startTime = DateTime.Now;
|
||||
OsmNode[] path = Pathfinder.CustomAStar("D:/stuttgart-regbez-latest", start, finish, Tag.SpeedType.car).ToArray();
|
||||
TimeSpan duration = DateTime.Now - startTime;
|
||||
Console.WriteLine($"Took {duration.TotalMilliseconds}ms ({duration:g})");
|
||||
Coordinates start = new Coordinates(48.7933989f, 9.8301467f);
|
||||
Coordinates finish = new Coordinates(48.7906258f, 9.8355983f);
|
||||
OsmNode[] path = Pathfinder.CustomAStar("D:/map", start, finish, Tag.SpeedType.pedestrian).ToArray();
|
||||
Console.WriteLine("{0}\n", path[0].ToString());
|
||||
for (int i = 0; i < path.Length - 1; i++)
|
||||
{
|
||||
Console.WriteLine(path[i]);
|
||||
OsmNode n1 = path[i];
|
||||
OsmNode n2 = path[i + 1];
|
||||
OsmEdge? e = n1.GetEdgeToNode(n2);
|
||||
if(e != null)
|
||||
Console.WriteLine("{0}\n{1}", e.ToString(), n2.ToString());
|
||||
else
|
||||
Console.WriteLine("NO EDGE\n{0}", n2.ToString());
|
||||
}
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user