diff --git a/Config.cs b/Config.cs new file mode 100644 index 0000000..f5166de --- /dev/null +++ b/Config.cs @@ -0,0 +1,69 @@ +using Newtonsoft.Json; +using System.IO; +using System.Runtime.Serialization; + +namespace RiverTrace +{ + [DataContract] + class ConfigData + { + [DataMember] + public int zoom; + + [DataMember] + public double lat1; + [DataMember] + public double lon1; + [DataMember] + public double lat2; + [DataMember] + public double lon2; + + [DataMember] + public int iterationCount; + [DataMember] + public double sampleWidthScale; + [DataMember] + public double sampleLengthScale; + [DataMember] + public double shoreContrast; + [DataMember] + public double maxDifference; + + [DataMember] + public bool debug; + } + + class Config + { + public static ConfigData Data; + private static string fileName; + + static Config() + { + fileName = "config.json"; + Data = new ConfigData + { + zoom = 15, + lat1 = 64.9035637, + lon1 = 52.2209239, + lat2 = 64.9032122, + lon2 = 52.2213061, + iterationCount = 1000, + sampleWidthScale = 1.7, + sampleLengthScale = 0.7, + shoreContrast = 10.0, + maxDifference = 28.0, + debug = false + }; + if (File.Exists(fileName)) + Data = JsonConvert.DeserializeObject(File.ReadAllText(fileName)); + } + + public static void Write() + { + File.WriteAllText(fileName, + JsonConvert.SerializeObject(Data, Formatting.Indented)); + } + } +} diff --git a/FodyWeavers.xml b/FodyWeavers.xml new file mode 100644 index 0000000..2e6d4a7 --- /dev/null +++ b/FodyWeavers.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/Program.cs b/Program.cs index e07e4da..751bdfc 100644 --- a/Program.cs +++ b/Program.cs @@ -8,17 +8,14 @@ using System.Reflection; using System.Threading; using System.Threading.Tasks; -using System.Windows.Media.Imaging; namespace RiverTrace { class Program { - private const bool debug = true; private Cie1976Comparison cie; private int sampleWidth; private int sampleLength; - private double maxDifference; private TileMap tileMap; Program() @@ -26,18 +23,9 @@ class Program string exeDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); Directory.SetCurrentDirectory(exeDir); - /*Trace( - 64.9035637, 52.2209239, - 64.9032122, 52.2213061, - 15);*/ - /*Trace( - 62.85685, 83.55849, - 62.85723, 83.55906, - 15);*/ - Trace( - 65.5238753, 79.3499377, - 65.5239887, 79.3501845, - 16); + Config.Write(); + + Trace(); } void WriteOsm(List result, int zoom) @@ -82,8 +70,6 @@ double GetColorDifference(Color c1, Color c2) void CalcSampleDimensions(Vector startPoint, Vector direction) { - double shoreContrast = 10.0; - int pickCount = 5; Vector pickPoint1 = startPoint; double[] riverHalfWidth = new double[2]; @@ -99,7 +85,7 @@ void CalcSampleDimensions(Vector startPoint, Vector direction) pickPoint2 += sideDirs[j]; Color checkColor = tileMap.GetPixel(pickPoint2.X, pickPoint2.Y); double diff = GetColorDifference(refColor, checkColor); - if (diff > shoreContrast) + if (diff > Config.Data.shoreContrast) break; riverHalfWidth[j] += 1.0; } @@ -109,8 +95,8 @@ void CalcSampleDimensions(Vector startPoint, Vector direction) riverHalfWidth[0] /= pickCount; riverHalfWidth[1] /= pickCount; double riverWidth = riverHalfWidth[0] + riverHalfWidth[1] + 1.0; - sampleWidth = Math.Max((int)Math.Ceiling(riverWidth * 1.7), 5); - sampleLength = Math.Max((int)Math.Ceiling(riverWidth * 2.0), 3); + sampleWidth = Math.Max((int)Math.Ceiling(riverWidth * Config.Data.sampleWidthScale), 5); + sampleLength = Math.Max((int)Math.Ceiling(riverWidth * Config.Data.sampleLengthScale), 3); } SimpleBitmap GetSample(Vector origin, Vector direction) @@ -195,20 +181,20 @@ SimpleBitmap GetAvgSample(SimpleBitmap s1, SimpleBitmap s2) return sample; } - void Trace(double lat1, double lon1, double lat2, double lon2, int zoom) + void Trace() { Stopwatch sw = new Stopwatch(); sw.Start(); cie = new Cie1976Comparison(); - tileMap = new TileMap(zoom); + tileMap = new TileMap(Config.Data.zoom); var way = new List(); Vector p1 = new Vector(); Vector p2 = new Vector(); - Projection.DegToPix(lat1, lon1, zoom, out p1.X, out p1.Y); - Projection.DegToPix(lat2, lon2, zoom, out p2.X, out p2.Y); + Projection.DegToPix(Config.Data.lat1, Config.Data.lon1, Config.Data.zoom, out p1.X, out p1.Y); + Projection.DegToPix(Config.Data.lat2, Config.Data.lon2, Config.Data.zoom, out p2.X, out p2.Y); way.Add(p1); Vector lastDirection = p2 - p1; @@ -216,8 +202,6 @@ void Trace(double lat1, double lon1, double lat2, double lon2, int zoom) CalcSampleDimensions(p1, lastDirection); - maxDifference = 28.0; - List samples = new List(); SimpleBitmap firstSample = GetSample(p1, lastDirection); SimpleBitmap avgSample = firstSample; @@ -226,9 +210,7 @@ void Trace(double lat1, double lon1, double lat2, double lon2, int zoom) samples.Add(firstSample); double totalDiff = 0.0; - int iterationCount = 1710; - - for (int i = 0; i < iterationCount; i++) + for (int i = 0; i < Config.Data.iterationCount; i++) { SimpleBitmap bestSample; double bestDiff; @@ -239,7 +221,7 @@ void Trace(double lat1, double lon1, double lat2, double lon2, int zoom) GetBestAngle(lastPoint, lastDirection, avgSample, bestAngle - 4.0, bestAngle + 4.0, 1.0, out bestSample, out bestDiff, out bestVector, out bestAngle); - if (bestDiff > maxDifference) + if (bestDiff > Config.Data.maxDifference) break; totalDiff += bestDiff; @@ -254,9 +236,9 @@ void Trace(double lat1, double lon1, double lat2, double lon2, int zoom) sw.Stop(); way.Reverse(); - WriteOsm(way, zoom); + WriteOsm(way, Config.Data.zoom); - if (debug) + if (Config.Data.debug) { SimpleBitmap sampleChain = new SimpleBitmap( sampleWidth, sampleLength * samples.Count); diff --git a/RiverTrace.csproj b/RiverTrace.csproj index 6e8ca69..01e7ada 100644 --- a/RiverTrace.csproj +++ b/RiverTrace.csproj @@ -11,6 +11,7 @@ RiverTrace v4.5 512 + 18af9fa7 AnyCPU @@ -36,9 +37,14 @@ packages\ColorMine.1.1.3.0\lib\ColorMine.dll True + + packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll + True + + @@ -50,6 +56,7 @@ + @@ -61,7 +68,17 @@ + + + + + + + This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + +