static void FindLimitTriBuddha(string file, int w, int h) { string cache = "limits_tri.cache"; if (File.Exists(cache) && !Settings.Test_Run) { string[] lines = File.ReadAllLines(cache); int i = 0; for (int x = 0; x < 3; x++) { for (int y = 0; y < 3; y++) { Helpers.TriLimit[x, y] = UInt64.Parse(lines[i]); i++; } } } else { using (var indent = MyConsole.Indent("Finding limits")) { LoaderHelper helper = null; if (file == null) { helper = new LoaderHelper(); } Dictionary<UInt64, long> counts = new Dictionary<UInt64, long>(); UInt64 total = 0; while (true) { ComplexData data = null; if (helper != null) { data = helper.GetData(); } else { if (file != null) { data = ComplexData.LoadFile(file, w, h); file = null; } } if (data == null) { break; } for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { var cur = data.Level[x, y]; if (!counts.ContainsKey(cur)) { counts.Add(cur, 1); } else { counts[cur]++; } total++; } } } for (int level = 0; level < 3; level++) { MyConsole.WriteLine("Working on level " + (level + 1)); UInt64 running = 0; bool target1Set = false; bool target2Set = false; bool target3Set = false; UInt64 target1 = 0; UInt64 target2 = 0; UInt64 target3 = 0; List<UInt64> list = new List<ulong>(counts.Keys); list.Sort(); double limit1 = 0; double limit2 = 0; double limit3 = 0; limit1 = 0.5; limit2 = 0.998; limit3 = 1.0; foreach (var key in list) { running += (UInt64)counts[key]; if (!target1Set) { if ((((double)running) / ((double)total)) > limit1) { target1 = key; target1Set = true; } } if (!target2Set) { if ((((double)running) / ((double)total)) > limit2) { target2 = key; target2Set = true; } } if (!target3Set) { if ((((double)running) / ((double)total)) > limit3) { target3 = key; target3Set = true; } } } if (!target3Set) { target3 = list[list.Count - 1]; } Helpers.TriLimit[level, 0] = target1; Helpers.TriLimit[level, 1] = target2; Helpers.TriLimit[level, 2] = target3; } StringBuilder sb = new StringBuilder(); for (int x = 0; x < 3; x++) { for (int y = 0; y < 3; y++) { sb.AppendLine(Helpers.TriLimit[x, y].ToString()); } } File.WriteAllText(cache, sb.ToString()); } } }
static void FindLimit(string file, int w, int h) { string cache = "limits.cache"; if (Helpers.Mode == Helpers.Modes.TriBuddha) { cache = "tri-limits.cache"; } if (File.Exists(cache) && !Settings.Test_Run) { string[] lines = File.ReadAllLines(cache); int i = 0; for (int x = 0; x < 3; x++) { for (int y = 0; y < 3; y++) { Helpers.Limits[x, y] = double.Parse(lines[i]); i++; } } for (int x = 0; x < 3; x++) { for (int y = 0; y < 32768; y++) { Helpers.Limits2[x, y] = double.Parse(lines[i]); i++; } } } else { using (var indent = MyConsole.Indent("Finding limits")) { Dictionary<long, long>[] counts = new Dictionary<long, long>[3]; long[] totalPixels = new long[3]; LoaderHelper helper = null; int levels = 0; for (int i = 0; i < 3; i++) { counts[i] = new Dictionary<long, long>(); totalPixels[i] = 0; } if (file == null) { helper = new LoaderHelper(); } while (true) { double[,] res = null; double[,] ims = null; double[,] other = null; UInt64[,] height = null; UInt64[,] height2 = null; UInt64[,] height3 = null; int tileX = 0; int tileY = 0; ComplexData data = null; if (helper != null) { data = helper.GetData(ref tileX, ref tileY, ref res, ref ims, ref other, ref height, ref height2, ref height3); } else { if (file != null) { data = ComplexData.LoadFile(file, w, h); res = data.Real; ims = data.Imaginary; other = data.Other; height = data.Level; height2 = data.Level2; height3 = data.Level3; file = null; } } if (data == null) { break; } if (height2 != null) { levels = 3; for (int i = 0; i < 3; i++) { ulong[,] cur = null; switch (i) { case 0: cur = height; break; case 1: cur = height2; break; case 2: cur = height3; break; } for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { if (UsePixel(x, y, tileX, tileY)) { totalPixels[i]++; long abs = (long)cur[x, y]; long temp = 0; if (counts[i].TryGetValue(abs, out temp)) { counts[i][abs] = temp + 1; } else { counts[i].Add(abs, 1); } } } } } } else { levels = 1; for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { totalPixels[0]++; Complex cur = new Complex(res[x, y], ims[x, y]); long abs = (long)cur.Abs; if (counts[0].ContainsKey(abs)) { counts[0][abs]++; } else { counts[0].Add(abs, 1); } } } } } for (int i = 0; i < levels; i++) { List<long> keys = new List<long>(counts[i].Keys); keys.Sort(); long curPixels = 0; long limit1 = -1; long limit2 = -1; long limit3 = -1; foreach (long key in keys) { curPixels += counts[i][key]; double perc = ((double)curPixels) / ((double)totalPixels[i]); if (limit1 == -1) { if (perc >= 0.995) { limit1 = key; } } if (limit2 == -1) { if (perc >= 0.9999) { limit2 = key; } } if (limit3 == -1) { if (perc >= 0.9999) { limit3 = key; } } } double tot = (0.9) * (((double)totalPixels[i]) * 1.0); double stepPixels = (1.0 / (32768.0)) * (tot); double checkPixels = stepPixels + (totalPixels[i] - tot); curPixels = 0; int x = 0; foreach (long key in keys) { curPixels += counts[i][key]; while (curPixels >= checkPixels) { Helpers.Limits2[i, x] = key; checkPixels += stepPixels; x++; if (x == 32768) { break; } } if (x == 32768) { break; } } Helpers.Limits[i, 0] = limit1; Helpers.Limits[i, 1] = limit2; Helpers.Limits[i, 2] = limit3; } StringBuilder sb2 = new StringBuilder(); for (int x = 0; x < 3; x++) { for (int y = 0; y < 3; y++) { sb2.AppendLine(Helpers.Limits[x, y].ToString()); } } for (int x = 0; x < 3; x++) { for (int y = 0; y < 32768; y++) { sb2.AppendLine(Helpers.Limits2[x, y].ToString()); } } File.WriteAllText(cache, sb2.ToString()); } } }
static void FindLimit(string file, int w, int h) { string cache = "limits.cache"; if (File.Exists(cache) && !Settings.Test_Run) { string[] lines = File.ReadAllLines(cache); Helpers.Limit1 = double.Parse(lines[0]); Helpers.Limit2 = double.Parse(lines[1]); } else { using (var indent = MyConsole.Indent("Finding limits")) { Dictionary<long, long> counts = new Dictionary<long, long>(); List<string> inCache = new List<string>(); long totalPixels = 0; LoaderHelper helper = null; if (file == null) { helper = new LoaderHelper(); } while (true) { double[,] res = null; double[,] ims = null; double[,] other = null; UInt64[,] height = null; int tileX = 0; int tileY = 0; ComplexData data = null; if (helper != null) { data = helper.GetData(ref tileX, ref tileY, ref res, ref ims, ref other, ref height); } else { if (file != null) { data = ComplexData.LoadFile(file, w, h); res = data.Real; ims = data.Imaginary; other = data.Other; height = data.Level; file = null; } } if (data == null) { break; } for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { totalPixels++; Complex cur = new Complex(res[x, y], ims[x, y]); long abs = (long)cur.Abs; if (counts.ContainsKey(abs)) { counts[abs]++; } else { counts.Add(abs, 1); } } } } List<long> keys = new List<long>(counts.Keys); keys.Sort(); long curPixels = 0; long limit1 = -1; long limit2 = -1; foreach (long key in keys) { curPixels += counts[key]; double perc = ((double)curPixels) / ((double)totalPixels); if (limit1 == -1) { if (perc >= 0.995) { limit1 = key; } } if (limit2 == -1) { if (perc >= 0.9999) { limit2 = key; } } if (limit1 != -1 && limit2 != -1) { break; } } Helpers.Limit1 = limit1; Helpers.Limit2 = limit2; StringBuilder sb = new StringBuilder(); sb.AppendLine(Helpers.Limit1.ToString()); sb.AppendLine(Helpers.Limit2.ToString()); File.WriteAllText("limits.cache", sb.ToString()); } } }