From 3350d8e3b908c6c62d5592feb55c1305a8d2c975 Mon Sep 17 00:00:00 2001 From: C9Glax <13404778+C9Glax@users.noreply.github.com> Date: Sun, 5 Feb 2023 20:02:22 +0100 Subject: [PATCH] Added GetNode(Coordinates coordinates) Added GetNode(ulong id) Instead of returning null in LoadRegion now throw an exception when file is not found. Added some Method descriptors --- OSMDatastructure/Region.cs | 14 +++++++++-- Pathfinding/RegionManager.cs | 49 ++++++++++++++++++++++++++++++------ 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/OSMDatastructure/Region.cs b/OSMDatastructure/Region.cs index a1a5c88..5cab5d6 100644 --- a/OSMDatastructure/Region.cs +++ b/OSMDatastructure/Region.cs @@ -2,7 +2,7 @@ namespace OSMDatastructure { public class Region { - public const float regionSize = 0.01f; + public const float regionSize = 0.01f; //For Splitting private readonly Dictionary _nodesInRegion = new(); public ulong regionHash { get; } @@ -36,6 +36,16 @@ namespace OSMDatastructure { return this._nodesInRegion.ContainsKey(id) ? this._nodesInRegion[id] : null; } - + + public Node? GetNode(Coordinates coordinates) + { + foreach (Node node in this._nodesInRegion.Values) + { + if (node.Equals(coordinates)) + return node; + } + + return null; + } } } \ No newline at end of file diff --git a/Pathfinding/RegionManager.cs b/Pathfinding/RegionManager.cs index ba76f80..ae58c0b 100644 --- a/Pathfinding/RegionManager.cs +++ b/Pathfinding/RegionManager.cs @@ -11,16 +11,21 @@ namespace OSMImporter { this.workingDirectory = workingDirectory; } - - public Region? GetRegion(Coordinates coordinates) + + /// + /// Checks wether the Region is already loaded and returns the Region, or tries to load the Region from file in workingDirectory + /// + /// Coordinates of the Region (or within the Region) to load + /// The Region at the specified Coordinates containing Nodes and Connections + /// If the Regionfile can not be found. + public Region GetRegion(Coordinates coordinates) { if(this._regions.ContainsKey(coordinates.GetRegionHash())) return this._regions[coordinates.GetRegionHash()]; else { - Region? loadedRegion = LoadRegion(coordinates); - if(loadedRegion != null) - this._regions.Add(loadedRegion.regionHash, value: loadedRegion); + Region loadedRegion = LoadRegion(coordinates); + this._regions.Add(loadedRegion.regionHash, value: loadedRegion); return loadedRegion; } } @@ -30,20 +35,48 @@ namespace OSMImporter return this._regions.Values.ToArray(); } - private Region? LoadRegion(Coordinates coordinates) + /// + /// + /// + /// Coordinates of the Region (or within the Region) to load + /// The Region at the specified Coordinates containing Nodes and Connections + /// If the Regionfile can not be found. + private Region LoadRegion(Coordinates coordinates) { string fullPath = Path.Combine(workingDirectory, coordinates.GetRegionHash().ToString()); Console.WriteLine(fullPath); if (!File.Exists(fullPath)) - return null; + { + throw new FileNotFoundException(string.Format("Region does not exist: {0}", fullPath)); + } FileStream fileStream = new FileStream(fullPath, FileMode.Open); byte[] regionBytes = new byte[fileStream.Length]; - fileStream.Read(regionBytes, 0, regionBytes.Length); + int _ = fileStream.Read(regionBytes, 0, regionBytes.Length); fileStream.Close(); return ByteConverter.ToRegion(regionBytes); } + + public Node? GetNode(ulong id) + { + foreach (Region region in this._regions.Values) + { + Node? node = region.GetNode(id); + if (node != null) + return node; + } + + return null; + } + + public Node? GetNode(Coordinates coordinates) + { + Region? regionWithNode = GetRegion(coordinates); + if (regionWithNode == null) + return null;//TODO null handling + return regionWithNode.GetNode(coordinates); + } } } \ No newline at end of file