/* * list have the ID of pixels * pring pixels itself * construct points * draw curve passing by them */ public void draw_line(List <int> l, graph g, System.Windows.Forms.PictureBox pictureBox1) { assign(l); Point[] points = new Point[orginal_path.Count + l.Count]; // draw main path int i = 0; for (; i < orginal_path.Count; i++) { int tid = orginal_path[i]; pixel tmp1 = helper.un_flatten(tid, g.width); Point tmp2 = new Point(tmp1.y, tmp1.x); // col , row points[i] = tmp2; } // draw tmp temporary path for (int j = 0; j < tmp_path.Count; j++) { int tid = tmp_path[j]; pixel tmp1 = helper.un_flatten(tid, g.width); Point tmp2 = new Point(tmp1.y, tmp1.x); // col , row points[i++] = tmp2; } if (points.Length >= 2) // draw the whole curve { pictureBox1.Refresh(); MainForm.gr.DrawCurve(MainForm.p, points); } }
private void button3_Click(object sender, EventArgs e) { pictureBox1.Refresh(); // current int tmp_id = g.get_cur_anchor_point(); pixel tmp_pixel1 = helper.un_flatten(tmp_id, g.width); // very first tmp_id = g.get_first_anchor_point(); pixel tmp_pixel2 = helper.un_flatten(tmp_id, g.width); List <int> last_path = g.dijkstra(tmp_pixel1.y, tmp_pixel1.x, tmp_pixel2.y, tmp_pixel2.x); // col , row // no more live wires mov = false; ok = false; draw_cutter.conc(last_path); draw_cutter.draw_line(new List <int>(), g, pictureBox1); }
private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { // mouse if (mov && ok) { g.set_free_point(e.X, e.Y); pixel tmp_pixel = helper.un_flatten(g.get_cur_anchor_point(), g.width); //List<int> path = g.dijkstra(g.get_cur_anchor_point() , g.get_cur_free_point()); Stopwatch ss = new Stopwatch(); ss.Start(); List <int> path = g.dijkstra(tmp_pixel.y, tmp_pixel.x, e.X, e.Y); // col , row ->>> my new version of dijkstra ss.Stop(); TimeSpan t = ss.Elapsed; double x = t.Milliseconds; textBox3.Text = x.ToString(); draw_cutter.draw_line(path, g, pictureBox1); } }
private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { // some anchor point x = e.X; // x- cordinate (column) y = e.Y; // y- cordinate (row) // first anchor point if (mov == false) { //int tmp = g.id[new pixel(y, x)]; // what is it's use ????????????????????? g.set_first_anchor_point(x, y); mov = true; // Indicate that we have anchor point and we are able to start drawing } //current anchor point else { g.set_cur_anchor_point(x, y); // exchange with prv pixel tmp_pixel = helper.un_flatten(g.get_prv_anchor_point(), g.width); List <int> strong_path = g.dijkstra(tmp_pixel.y, tmp_pixel.x, x, y); // my new version of dijkstra draw_cutter.conc(strong_path); //draw_cutter.draw_line(new List<int>() , g.rid , pictureBox1); } }
private void button2_Click(object sender, EventArgs e) { if (!constructed) { // first time ever g.g = new List <Tuple <int, double> > [g.size]; // 0 based id for (int i = 0; i < g.size; i++) { g.g[i] = new List <Tuple <int, double> >(); } g.build(ImageMatrix); constructed = true; } // shortest path between two nodes // input is ID's //int from = int.Parse(textBox1.Text.ToString()); //int to = int.Parse(textBox2.Text.ToString()); // input is pixles Stopwatch s = new Stopwatch(); s.Start(); pixel ffrom = new pixel(1, 2); // row , col pixel tto = new pixel(10, 10); int from = helper.flatten(ffrom.y, ffrom.x, g.width); int to = helper.flatten(tto.y, tto.x, g.width); // hard coded input from = 1695577; to = 2055619; ffrom = helper.un_flatten(from, g.width); tto = helper.un_flatten(to, g.width); List <int> path = g.dijkstra(from, to); FileStream f = new FileStream("shortest_path.txt", FileMode.Create); StreamWriter sw = new StreamWriter(f, Encoding.UTF8, 65536); // bigger buffer string tmp = ""; tmp = "The Shortest path from Node " + from + " at(" + ffrom.y + ", " + ffrom.x + ") to Node " + to + " at(" + tto.y + ", " + tto.x + ")"; // col , row sw.WriteLine(tmp); tmp = "Format: (node_index, x, y)"; sw.WriteLine(tmp); pixel tmp_pixel = new pixel(); for (int i = 0; i < path.Count; i++) { tmp_pixel = helper.un_flatten(path[i], g.width); tmp = "{ X = " + tmp_pixel.y + ",Y = " + tmp_pixel.x + "}," + tmp_pixel.y + "," + tmp_pixel.x + ")"; sw.WriteLine(tmp); } s.Stop(); tmp = "Path construction took: " + (s.ElapsedMilliseconds / (double)1000).ToString() + " seconds."; sw.WriteLine(tmp); /* * mile stone 1 * sw.WriteLine(path.Count); * * pixel tmp_pixel = helper.un_flatten(from , g.width); * // first line * sw.Write(" The Shortest path from Node "); * sw.Write(from); * sw.Write("at"); * sw.Write(" position "); * sw.Write(tmp_pixel.x); * sw.Write(" "); * sw.WriteLine(tmp_pixel.y); * * // second line * tmp_pixel = helper.un_flatten(to, g.width); * sw.Write(" The Shortest path to Node "); * sw.Write(to); * sw.Write("at"); * sw.Write(" position "); * sw.Write(tmp_pixel.x); * sw.Write(" "); * sw.WriteLine(tmp_pixel.y); * * // path it self * for (int i=0; i<path.Count; i++) * { * tmp_pixel = helper.un_flatten(path[i] , g.width); * sw.Write("Node "); * sw.Write("{X="); * sw.Write(tmp_pixel.y); * sw.Write(",Y="); * sw.Write(tmp_pixel.x); * sw.Write("} "); * sw.Write(" at position x "); * sw.Write(tmp_pixel.y); * sw.Write(" at position y "); * sw.WriteLine(tmp_pixel.x); * }*/ sw.Close(); f.Close(); }
public List <int> dijkstra(int xf, int yf, int xt, int yt) //col , row ->> source , destination { // Data holders double inf = helper.infinty(); int [] path = new int[size + 2]; double [] dis = new double[size + 2]; Priority_Queue q = new Priority_Queue(10); int node = helper.flatten(xf, yf, width); // col , row int to = helper.flatten(xt, yt, width); //col , row //initialize for (int i = 0; i <= size; i++) { dis[i] = inf; } dis[node] = 0; path[node] = -1; q.insert(node, 0); while (!q.empty()) { // pick clossest node = q.top().id; double dnode = q.top().w; // delete q.pop(); pixel tmp_pixel = helper.un_flatten(node, width); // update the node that I relax from xf = tmp_pixel.y; // col yf = tmp_pixel.x; // row if (node == to) { break; } if (dis[node] < dnode) { continue; } List <Tuple <int, double> > neighbours = new List <Tuple <int, double> >(); // build neghbours 4 connectivity Vector2D v = ImageOperations.CalculatePixelEnergies(xf, yf, MainForm.ImageMatrix); int tmp_id; // hold my child that I will add double w; if (helper.valid(xf, yf + 1)) // bellow { w = v.Y; w = (double)1 / w; w = helper.remove_infinty(w); tmp_id = helper.flatten(xf, yf + 1, width); neighbours.Add(Tuple.Create(tmp_id, w)); } if (helper.valid(xf + 1, yf)) // to the right { w = v.X; w = (double)1 / w; w = helper.remove_infinty(w); tmp_id = helper.flatten(xf + 1, yf, width); neighbours.Add(Tuple.Create(tmp_id, w)); } if (helper.valid(xf, yf - 1)) // above me { v = ImageOperations.CalculatePixelEnergies(xf, yf - 1, MainForm.ImageMatrix); w = v.Y; w = (double)1 / w; w = helper.remove_infinty(w); tmp_id = helper.flatten(xf, yf - 1, width); neighbours.Add(Tuple.Create(tmp_id, w)); } if (helper.valid(xf - 1, yf)) // to the left { v = ImageOperations.CalculatePixelEnergies(xf - 1, yf, MainForm.ImageMatrix); w = v.X; // energy w = (double)1 / w; // weight w = helper.remove_infinty(w); tmp_id = helper.flatten(xf - 1, yf, width); neighbours.Add(Tuple.Create(tmp_id, w)); } // relax for (int i = 0; i < neighbours.Count; i++) { int child = neighbours[i].Item1; double cost = neighbours[i].Item2; if (dis[child] > dnode + cost) { dis[child] = dnode + cost; path[child] = node; // your current parent q.insert(child, dis[child]); } } } // build the path , we are always sure that we will reach our distination List <int> shp = new List <int>(); shp.Add(to); while (path[to] != -1) { shp.Add(path[to]); to = path[to]; } shp.Reverse(); return(shp); }