// a function for displaying the normals in an STL file public static void draw_stl_normals(GLWindow g, STLSurf s) { // draw triangle normas foreach (Geo.Tri t in s.tris) { // from STL file Geo.Point p1 = new Geo.Point(t.p[0].x, t.p[0].y, t.p[0].z); Geo.Point p2 = new Geo.Point(t.p[0].x + t.n.x, t.p[0].y + t.n.y, t.p[0].z + t.n.z); GeoLine l = new GeoLine(p1, p2); l.color = System.Drawing.Color.Green; g.addGeom(l); // calculate yourself: Vector v1 = new Vector(t.p[0].x - t.p[1].x, t.p[0].y - t.p[1].y, t.p[0].z - t.p[1].z); Vector v2 = new Vector(t.p[0].x - t.p[2].x, t.p[0].y - t.p[2].y, t.p[0].z - t.p[2].z); Vector n; n = v1.Cross(v2); // the normal is in the direction of the cross product between the edge vectors n = (1 / n.Length()) * n; // normalize to length==1 p1 = new Geo.Point(t.p[0].x, t.p[0].y, t.p[0].z); p2 = new Geo.Point(t.p[0].x + n.x, t.p[0].y + n.y, t.p[0].z + n.z); l = new GeoLine(p1, p2); l.color = System.Drawing.Color.Blue; g.addGeom(l); } }
private void openSTLToolStripMenuItem_Click(object sender, EventArgs e) { System.Console.WriteLine("opening STL"); System.IO.StreamReader rdr = file_open(); STLSurf s = null; if (rdr != null) { s = STL.Load(rdr); } if (s != null) { addGeom(s); } else { System.Console.WriteLine("loading STL file failed. no geometry created."); } }
static public STLSurf Load(System.IO.StreamReader fs) { // Here's where autodetection will be provided for loading binary stl files and by wrapping STLA(scii) and STLB(inary) // but for now as only one format is provided, not bothering with it, we need to define a object format for handling file loading/saving // System.IO.StreamReader fs = new System.IO.StreamReader(FileName); STLSurf surf = new STLSurf(); int state = 0; int counter = 0; string[] data; Geo.Tri triangle = new Geo.Tri(); Vector normal = new Vector(); System.Globalization.CultureInfo locale = new System.Globalization.CultureInfo("en-GB"); int n_triangles = 0; while (!fs.EndOfStream) { data = fs.ReadLine().TrimStart(' ').Split(' '); switch (state) { case 0: if (data[0].Equals("solid")) { surf.name = data[1]; state = 1; // continue readingx } break; case 1: if (data[0].Equals("facet")) { normal.x = double.Parse(data[2], locale); normal.y = double.Parse(data[3], locale); normal.z = double.Parse(data[4], locale); triangle = new Geo.Tri(normal); counter = 0; state = 2; } break; case 2: if (data[0].Equals("vertex")) { if (counter <= 2) { triangle.p[counter].x = double.Parse(data[1], locale); triangle.p[counter].y = double.Parse(data[2], locale); triangle.p[counter].z = double.Parse(data[3], locale); // System.Console.WriteLine("STLReader: added point" + triangle.p[counter]); // System.Console.ReadKey(); counter++; } } else if (data[0].Equals("endfacet")) { if (counter == 3) { surf.AddTriangle(triangle); n_triangles += 1; } state = 1; } break; } } fs.Close(); System.Console.WriteLine("STLReader: read {0} triangles!", n_triangles); return(surf); }
public static void stlmachine(GLWindow g, STLSurf s) { List<Geo.Point> pointlist=new List<Geo.Point>(); // seems to work... // foreach (Geo.Tri t in s.tris) // System.Console.WriteLine("loop1 triangles " + t); // System.Console.ReadKey(); // recalculate normal data // create bounding box data foreach (Geo.Tri t in s.tris) { t.recalc_normals(); // FIXME why don't new values stick?? t.calc_bbox(); // FIXME: why doen't bb-data 'stick' ?? } /* // FIXME: if we check bb-data here it is gone!!(??) foreach (Geo.Tri t in s.tris) { System.Console.WriteLine("loop2 triangles " + t); System.Console.WriteLine("loop2 direct maxx" + t.bb.maxx + " minx:" + t.bb.minx); } System.Console.ReadKey(); */ // find bounding box (this should probably be done in the STLSurf class?) double minx = 0, maxx = 10, miny = 0, maxy = 10; // generate XY pattern (a general zigzag-strategy, needed also for pocketing) double Nx=50; double Ny=50; double dx=(maxx-minx)/(double)(Nx-1); double dy = (maxy - miny) / (double)(Ny-1); double x = minx; for (int n = 0; n < Nx; n++) { if (n%2==0) { double y = miny; for (int m = 0; m < Ny; m++) { pointlist.Add(new Geo.Point(x,y,5)); // System.Console.WriteLine("x:"+x+" y:"+y); y += dy; // System.Console.ReadKey(); } } else { double y = maxy; for (int m = 0; m < Ny; m++) { pointlist.Add(new Geo.Point(x,y,5)); //System.Console.WriteLine("x:" + x + " y:" + y); y -= dy; //System.Console.ReadKey(); } } x += dx; } // drop cutter (i.e. add z-data) double R=1,r=0.2; Cutter cu = new Cutter(R,r); List<Geo.Point> drop_points = new List<Geo.Point>(); double redundant = 0; double checks = 0; foreach (Geo.Point p in pointlist) { double? v1 = null,v2=null,v3=null,z_new=null,f=null,e1=null,e2=null,e3=null; List<double> zlist = new List<double>(); foreach (Geo.Tri t in s.tris) { checks++; t.calc_bbox(); // why do we have to re-calculate bb-data here?? //System.Console.WriteLine("testing triangle" + t); if (t.bb.minx > (p.x + cu.R)) { redundant++; continue; } else if (t.bb.maxx < (p.x - cu.R)) { redundant++; continue; } if (t.bb.miny > (p.y + cu.R)) { redundant++; continue; } if (t.bb.maxy < (p.y - cu.R)) { redundant++; continue; } v1 = DropCutter.VertexTest(cu, p, t.p[0]); v2 = DropCutter.VertexTest(cu, p, t.p[1]); v3 = DropCutter.VertexTest(cu, p, t.p[2]); if (v2 != null) { zlist.Add((double)v2); } if (v1 != null) { zlist.Add((double)v1); } if (v3 != null) { zlist.Add((double)v3); } f = DropCutter.FacetTest(cu, p, t); if (f != null) { zlist.Add((double)f); } e1 = DropCutter.EdgeTest(cu, p, t.p[0], t.p[1]); e2 = DropCutter.EdgeTest(cu, p, t.p[1], t.p[2]); e3 = DropCutter.EdgeTest(cu, p, t.p[0], t.p[2]); if (e1 != null) zlist.Add((double)e1); if (e2 != null) zlist.Add((double)e2); if (e3 != null) zlist.Add((double)e3); /* if (zlist.Count > 1) { System.Console.Write("Before: "); foreach (double d in zlist) System.Console.Write(d.ToString() + " "); System.Console.Write("\n"); } */ zlist.Sort(); /* if (zlist.Count > 2) { System.Console.Write("After: "); foreach (double d in zlist) System.Console.Write(d.ToString() + " "); System.Console.Write("\n"); } */ // System.Console.Write("Sorted: "); // foreach (double d in zlist) // System.Console.Write(d.ToString() + " "); // System.Console.Write("\n"); if (zlist.Count > 0) z_new = zlist[zlist.Count-1]; /* if (zlist.Count > 1) System.Console.WriteLine("chosen: " + z_new); */ // System.Console.ReadKey(); } // end triangle loop if (z_new != null) { drop_points.Add(new Geo.Point(p.x, p.y, (double)z_new)); } } // end point-list loop System.Console.WriteLine("checked: "+ checks + " redundant: " + redundant); System.Console.WriteLine("relevant: "+(checks-redundant) + " ("+100*(double)(checks-redundant)/(double)checks+"%)"); // check to see that STL has not changed // display drop-points int i = 1; Geo.Point p0=new Geo.Point(); foreach (Geo.Point p in drop_points) { if (i == 1) // first move { p0 = new Geo.Point(p.x, p.y, 10); GeoLine l = new GeoLine(p0, p); l.color = System.Drawing.Color.Yellow; g.addGeom(l); p0 = p; } else // don't do anything for last move { GeoLine l = new GeoLine(p0, p); l.color = System.Drawing.Color.Magenta; g.addGeom(l); p0 = p; } i++; /* GeoPoint pg = new GeoPoint(p); pg.color = System.Drawing.Color.Aqua; g.addGeom(pg); */ } // display zigzag and points /* i = 1; foreach (Geo.Point p in pointlist) { if (i == 1) { p0 = new Geo.Point(p.x, p.y, 10); GeoLine l = new GeoLine(p0, p); l.color = System.Drawing.Color.Yellow; g.addGeom(l); p0 = p; } else { GeoLine l = new GeoLine(p0, p); l.color = System.Drawing.Color.Cyan; g.addGeom(l); p0 = p; } i++; } */ // dummy test: /* foreach (Geo.Tri t in s.tris) { GeoPoint p = new GeoPoint(t.p[0].x, t.p[0].y, t.p[0].z); pointlist.Add(p); } */ }
public static void stlmachine(STLSurf s, GeoCollection g) { List <Point> pointlist = new List <Point>(); // seems to work... // foreach (Geo.Tri t in s.tris) // System.Console.WriteLine("loop1 triangles " + t); // System.Console.ReadKey(); // recalculate normal data // create bounding box data foreach (Tri t in s.tris) { t.recalc_normals(); // FIXME why don't new values stick?? t.calc_bbox(); // FIXME: why doen't bb-data 'stick' ?? } /* * // FIXME: if we check bb-data here it is gone!!(??) * foreach (Geo.Tri t in s.tris) * { * System.Console.WriteLine("loop2 triangles " + t); * System.Console.WriteLine("loop2 direct maxx" + t.bb.maxx + " minx:" + t.bb.minx); * } * System.Console.ReadKey(); */ // find bounding box (this should probably be done in the STLSurf class?) double minx = 0, maxx = 10, miny = 0, maxy = 10; // generate XY pattern (a general zigzag-strategy, needed also for pocketing) // store in a list called pointlist double Nx = 30; double Ny = 40; double dx = (maxx - minx) / (double)(Nx - 1); double dy = (maxy - miny) / (double)(Ny - 1); double x = minx; for (int n = 0; n < Nx; n++) { if (n % 2 == 0) { double y = miny; for (int m = 0; m < Ny; m++) { pointlist.Add(new Point(x, y, 5)); // System.Console.WriteLine("x:"+x+" y:"+y); y += dy; // go forward in the y-axis direction // System.Console.ReadKey(); } } else { double y = maxy; for (int m = 0; m < Ny; m++) { pointlist.Add(new Point(x, y, 5)); //System.Console.WriteLine("x:" + x + " y:" + y); y -= dy; // go backward in the y-axis direction //System.Console.ReadKey(); } } x += dx; } // drop cutter (i.e. add z-data) double R = 1, r = 0.2; // this is the cutter definition Cutter cu = new Cutter(R, r); List <Point> drop_points = new List <Point>(); double redundant = 0; // number of unneccesary calls to drop-cutter double checks = 0; // number of relevant calls // build the kd-tree Stopwatch st = new Stopwatch(); Console.WriteLine("Building kd-tree. Stopwatch start"); st.Start(); kd_node root; root = kdtree.build_kdtree(s.tris); st.Stop(); Console.WriteLine("Elapsed = {0}", st.Elapsed.ToString()); // FIXME: these calls to drop-cutter are independent of each other // thus the points could/should be divided into many subsets // and each subset is processed by a seprarate thread // this should give a substantial speedup on multi-core cpus Console.WriteLine("Running drop-cutter. Stopwatch start"); st.Start(); foreach (Point p in pointlist) // loop through each point { double?v1 = null, v2 = null, v3 = null, z_new = null, f = null, e1 = null, e2 = null, e3 = null; // store the possible z-values in this list // the highest one of these should be chosen in the end List <double> zlist = new List <double>(); // find triangles under cutter using kd-tree int mode = 1; List <Tri> tris_to_search = new List <Tri>(); if (mode == 0) { tris_to_search = s.tris; } else if (mode == 1) { kdtree.search_kdtree(tris_to_search, p, cu, root); } //Console.WriteLine("searching {0} tris",tris_to_search.Count); //Console.ReadKey(); // loop through each triangle foreach (Tri t in tris_to_search) { checks++; t.calc_bbox(); // FIXME: why do we have to re-calculate bb-data here?? //System.Console.WriteLine("testing triangle" + t); // here are four ways the triangle bounding box can be // outside the cutter bounding box // redundant could be used to test the performance of bucketing/kd-tree if (t.bb.minx > (p.x + cu.R)) { redundant++; continue; } else if (t.bb.maxx < (p.x - cu.R)) { redundant++; continue; } if (t.bb.miny > (p.y + cu.R)) { redundant++; continue; } if (t.bb.maxy < (p.y - cu.R)) { redundant++; continue; } // test cutter against each vertex v1 = DropCutter.VertexTest(cu, p, t.p[0]); v2 = DropCutter.VertexTest(cu, p, t.p[1]); v3 = DropCutter.VertexTest(cu, p, t.p[2]); if (v2 != null) { zlist.Add((double)v2); } if (v1 != null) { zlist.Add((double)v1); } if (v3 != null) { zlist.Add((double)v3); } // test cutter against facet f = DropCutter.FacetTest(cu, p, t); if (f != null) { zlist.Add((double)f); } // test cutter against each edge e1 = DropCutter.EdgeTest(cu, p, t.p[0], t.p[1]); e2 = DropCutter.EdgeTest(cu, p, t.p[1], t.p[2]); e3 = DropCutter.EdgeTest(cu, p, t.p[0], t.p[2]); if (e1 != null) { zlist.Add((double)e1); } if (e2 != null) { zlist.Add((double)e2); } if (e3 != null) { zlist.Add((double)e3); } // now we have some suggestions for z in zlist // by sorting it we get the highest one at the end of the list zlist.Sort(); // if there's anything in the list, return the last element if (zlist.Count > 0) { z_new = zlist[zlist.Count - 1]; } } // end triangle loop // we've gone through all triangles for this XY-location // if we found a z-value, let's add the valid cutter location // to a list drop_points if (z_new != null) { drop_points.Add(new Point(p.x, p.y, (double)z_new)); } } // end point-list loop st.Stop(); Console.WriteLine("Elapsed = {0}", st.Elapsed.ToString()); // print some statistics: System.Console.WriteLine("checked: " + checks + " redundant: " + redundant); double fraction = (100 * (double)(checks - redundant) / (double)checks); System.Console.WriteLine("relevant: " + (checks - redundant) + " (" + fraction.ToString("N3") + "%)"); // FIXME: now a toolpath object should be created // that has rapids/feeds according to the points calculated above int i = 1; Point p0 = new Point(); // this is needed so we get decimal points, not commas System.Globalization.CultureInfo glob = new System.Globalization.CultureInfo("en-GB"); Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-GB"); foreach (Point p in drop_points) { if (i == 1) // first move { p0 = new Point(p.x, p.y, 12); camtest.outfile.WriteLine("Cylinder"); camtest.outfile.WriteLine("{0},{1},{2}", p0.x.ToString("0.000", glob), p0.y.ToString("0.000", glob), p0.z.ToString("0.000", glob)); camtest.outfile.WriteLine("{0}", 0.01.ToString("0.000", glob)); camtest.outfile.WriteLine("{0},{1},{2}", p.x.ToString("0.000", glob), p.y.ToString("0.000", glob), p.z.ToString("0.000", glob)); Line l = new Line(p0, p); g.add(l); // ADD geometry to toolpath p0 = p; } else { camtest.outfile.WriteLine("Cylinder"); camtest.outfile.WriteLine("{0},{1},{2}", p0.x.ToString("0.000", glob), p0.y.ToString("0.000", glob), p0.z.ToString("0.000", glob)); camtest.outfile.WriteLine("{0}", 0.01.ToString("0.000", glob)); camtest.outfile.WriteLine("{0},{1},{2}", p.x.ToString("0.000", glob), p.y.ToString("0.000", glob), p.z.ToString("0.000", glob)); Line l = new Line(p0, p); g.add(l); // ADD geometry to toolpath p0 = p; } i++; } }
public static void stlmachine(STLSurf s, GeoCollection g) { List<Point> pointlist=new List<Point>(); // seems to work... // foreach (Geo.Tri t in s.tris) // System.Console.WriteLine("loop1 triangles " + t); // System.Console.ReadKey(); // recalculate normal data // create bounding box data foreach (Tri t in s.tris) { t.recalc_normals(); // FIXME why don't new values stick?? t.calc_bbox(); // FIXME: why doen't bb-data 'stick' ?? } /* // FIXME: if we check bb-data here it is gone!!(??) foreach (Geo.Tri t in s.tris) { System.Console.WriteLine("loop2 triangles " + t); System.Console.WriteLine("loop2 direct maxx" + t.bb.maxx + " minx:" + t.bb.minx); } System.Console.ReadKey(); */ // find bounding box (this should probably be done in the STLSurf class?) double minx = 0, maxx = 10, miny = 0, maxy = 10; // generate XY pattern (a general zigzag-strategy, needed also for pocketing) // store in a list called pointlist double Nx=30; double Ny=40; double dx=(maxx-minx)/(double)(Nx-1); double dy = (maxy - miny) / (double)(Ny-1); double x = minx; for (int n = 0; n < Nx; n++) { if (n%2==0) { double y = miny; for (int m = 0; m < Ny; m++) { pointlist.Add(new Point(x,y,5)); // System.Console.WriteLine("x:"+x+" y:"+y); y += dy; // go forward in the y-axis direction // System.Console.ReadKey(); } } else { double y = maxy; for (int m = 0; m < Ny; m++) { pointlist.Add(new Point(x,y,5)); //System.Console.WriteLine("x:" + x + " y:" + y); y -= dy; // go backward in the y-axis direction //System.Console.ReadKey(); } } x += dx; } // drop cutter (i.e. add z-data) double R=1,r=0.2; // this is the cutter definition Cutter cu = new Cutter(R,r); List<Point> drop_points = new List<Point>(); double redundant = 0; // number of unneccesary calls to drop-cutter double checks = 0; // number of relevant calls // build the kd-tree Stopwatch st = new Stopwatch(); Console.WriteLine("Building kd-tree. Stopwatch start"); st.Start(); kd_node root; root = kdtree.build_kdtree(s.tris); st.Stop(); Console.WriteLine("Elapsed = {0}", st.Elapsed.ToString()); // FIXME: these calls to drop-cutter are independent of each other // thus the points could/should be divided into many subsets // and each subset is processed by a seprarate thread // this should give a substantial speedup on multi-core cpus Console.WriteLine("Running drop-cutter. Stopwatch start"); st.Start(); foreach (Point p in pointlist) // loop through each point { double? v1 = null,v2=null,v3=null,z_new=null,f=null,e1=null,e2=null,e3=null; // store the possible z-values in this list // the highest one of these should be chosen in the end List<double> zlist = new List<double>(); // find triangles under cutter using kd-tree int mode = 1; List<Tri> tris_to_search = new List<Tri>(); if (mode == 0) { tris_to_search = s.tris; } else if (mode == 1) { kdtree.search_kdtree(tris_to_search, p, cu, root); } //Console.WriteLine("searching {0} tris",tris_to_search.Count); //Console.ReadKey(); // loop through each triangle foreach (Tri t in tris_to_search) { checks++; t.calc_bbox(); // FIXME: why do we have to re-calculate bb-data here?? //System.Console.WriteLine("testing triangle" + t); // here are four ways the triangle bounding box can be // outside the cutter bounding box // redundant could be used to test the performance of bucketing/kd-tree if (t.bb.minx > (p.x + cu.R)) { redundant++; continue; } else if (t.bb.maxx < (p.x - cu.R)) { redundant++; continue; } if (t.bb.miny > (p.y + cu.R)) { redundant++; continue; } if (t.bb.maxy < (p.y - cu.R)) { redundant++; continue; } // test cutter against each vertex v1 = DropCutter.VertexTest(cu, p, t.p[0]); v2 = DropCutter.VertexTest(cu, p, t.p[1]); v3 = DropCutter.VertexTest(cu, p, t.p[2]); if (v2 != null) { zlist.Add((double)v2); } if (v1 != null) { zlist.Add((double)v1); } if (v3 != null) { zlist.Add((double)v3); } // test cutter against facet f = DropCutter.FacetTest(cu, p, t); if (f != null) { zlist.Add((double)f); } // test cutter against each edge e1 = DropCutter.EdgeTest(cu, p, t.p[0], t.p[1]); e2 = DropCutter.EdgeTest(cu, p, t.p[1], t.p[2]); e3 = DropCutter.EdgeTest(cu, p, t.p[0], t.p[2]); if (e1 != null) zlist.Add((double)e1); if (e2 != null) zlist.Add((double)e2); if (e3 != null) zlist.Add((double)e3); // now we have some suggestions for z in zlist // by sorting it we get the highest one at the end of the list zlist.Sort(); // if there's anything in the list, return the last element if (zlist.Count > 0) z_new = zlist[zlist.Count-1]; } // end triangle loop // we've gone through all triangles for this XY-location // if we found a z-value, let's add the valid cutter location // to a list drop_points if (z_new != null) { drop_points.Add(new Point(p.x, p.y, (double)z_new)); } } // end point-list loop st.Stop(); Console.WriteLine("Elapsed = {0}", st.Elapsed.ToString()); // print some statistics: System.Console.WriteLine("checked: "+ checks + " redundant: " + redundant); double fraction=(100*(double)(checks-redundant)/(double)checks); System.Console.WriteLine("relevant: "+(checks-redundant) + " ("+fraction.ToString("N3")+"%)"); // FIXME: now a toolpath object should be created // that has rapids/feeds according to the points calculated above int i = 1; Point p0=new Point(); // this is needed so we get decimal points, not commas System.Globalization.CultureInfo glob = new System.Globalization.CultureInfo("en-GB"); Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-GB"); foreach (Point p in drop_points) { if (i == 1) // first move { p0 = new Point(p.x, p.y, 12); camtest.outfile.WriteLine("Cylinder"); camtest.outfile.WriteLine("{0},{1},{2}", p0.x.ToString("0.000", glob), p0.y.ToString("0.000", glob), p0.z.ToString("0.000", glob)); camtest.outfile.WriteLine("{0}", 0.01.ToString("0.000", glob)); camtest.outfile.WriteLine("{0},{1},{2}", p.x.ToString("0.000", glob), p.y.ToString("0.000", glob), p.z.ToString("0.000", glob)); Line l = new Line(p0, p); g.add(l); // ADD geometry to toolpath p0 = p; } else { camtest.outfile.WriteLine("Cylinder"); camtest.outfile.WriteLine("{0},{1},{2}", p0.x.ToString("0.000", glob), p0.y.ToString("0.000", glob), p0.z.ToString("0.000", glob)); camtest.outfile.WriteLine("{0}", 0.01.ToString("0.000", glob)); camtest.outfile.WriteLine("{0},{1},{2}", p.x.ToString("0.000", glob), p.y.ToString("0.000", glob), p.z.ToString("0.000", glob)); Line l = new Line(p0, p); g.add(l); // ADD geometry to toolpath p0 = p; } i++; } }
static void Main(string[] args) { GeoCollection g = new GeoCollection(); System.Console.WriteLine("MonoCAM 2008 Mar 03"); // load an STL file System.String FileName = "Demo1.stl"; System.Console.WriteLine("opening STL file {0}", FileName); System.IO.StreamReader rdr = file_open(FileName); STLSurf s = null; if (rdr != null) { s = STL.Load(rdr); } if (s != null) { g.add(s); } else { System.Console.WriteLine("loading STL file failed. no geometry created."); } WriteGeoColl(g); // try a cam operation camtest.run(g); //WriteGeoColl(g); // test kd-tree // kdtree.spread(s.tris, cutdim.MINUS_X); //kdtree.PrintKdtree(root); /* * Cutter c = new Cutter(1.5, 0); * Point p = new Point(0, 0, 0); * List<Tri> tris = new List<Tri>(); * kdtree.search_kdtree(tris, p, c, root); * System.Console.WriteLine("found {0} triangles!", tris.Count); * System.Console.WriteLine("ns={0}",kdtree.ns); * if (tris.Count <= 10) * { * foreach (Tri t in tris) * { * Console.WriteLine("x: " + t.bb.minx + " / " + t.bb.maxx + " " + (p.x - c.R) + "to" + (p.x + c.R)); * Console.WriteLine("y: " + t.bb.miny + " / " + t.bb.maxy + " " + (p.y - c.R) + "to" + (p.y + c.R)); * } * } */ // display the kd_tree //kdtree.PrintKdtree(root); // wait for user to end program System.Console.WriteLine("Press any key to end"); System.Console.ReadKey(); }
public static void stlmachine(GLWindow g, STLSurf s) { List <Geo.Point> pointlist = new List <Geo.Point>(); // seems to work... // foreach (Geo.Tri t in s.tris) // System.Console.WriteLine("loop1 triangles " + t); // System.Console.ReadKey(); // recalculate normal data // create bounding box data foreach (Geo.Tri t in s.tris) { t.recalc_normals(); // FIXME why don't new values stick?? t.calc_bbox(); // FIXME: why doen't bb-data 'stick' ?? } /* * // FIXME: if we check bb-data here it is gone!!(??) * foreach (Geo.Tri t in s.tris) * { * System.Console.WriteLine("loop2 triangles " + t); * System.Console.WriteLine("loop2 direct maxx" + t.bb.maxx + " minx:" + t.bb.minx); * } * System.Console.ReadKey(); */ // find bounding box (this should probably be done in the STLSurf class?) double minx = 0, maxx = 10, miny = 0, maxy = 10; // generate XY pattern (a general zigzag-strategy, needed also for pocketing) double Nx = 50; double Ny = 50; double dx = (maxx - minx) / (double)(Nx - 1); double dy = (maxy - miny) / (double)(Ny - 1); double x = minx; for (int n = 0; n < Nx; n++) { if (n % 2 == 0) { double y = miny; for (int m = 0; m < Ny; m++) { pointlist.Add(new Geo.Point(x, y, 5)); // System.Console.WriteLine("x:"+x+" y:"+y); y += dy; // System.Console.ReadKey(); } } else { double y = maxy; for (int m = 0; m < Ny; m++) { pointlist.Add(new Geo.Point(x, y, 5)); //System.Console.WriteLine("x:" + x + " y:" + y); y -= dy; //System.Console.ReadKey(); } } x += dx; } // drop cutter (i.e. add z-data) double R = 1, r = 0.2; Cutter cu = new Cutter(R, r); List <Geo.Point> drop_points = new List <Geo.Point>(); double redundant = 0; double checks = 0; foreach (Geo.Point p in pointlist) { double? v1 = null, v2 = null, v3 = null, z_new = null, f = null, e1 = null, e2 = null, e3 = null; List <double> zlist = new List <double>(); foreach (Geo.Tri t in s.tris) { checks++; t.calc_bbox(); // why do we have to re-calculate bb-data here?? //System.Console.WriteLine("testing triangle" + t); if (t.bb.minx > (p.x + cu.R)) { redundant++; continue; } else if (t.bb.maxx < (p.x - cu.R)) { redundant++; continue; } if (t.bb.miny > (p.y + cu.R)) { redundant++; continue; } if (t.bb.maxy < (p.y - cu.R)) { redundant++; continue; } v1 = DropCutter.VertexTest(cu, p, t.p[0]); v2 = DropCutter.VertexTest(cu, p, t.p[1]); v3 = DropCutter.VertexTest(cu, p, t.p[2]); if (v2 != null) { zlist.Add((double)v2); } if (v1 != null) { zlist.Add((double)v1); } if (v3 != null) { zlist.Add((double)v3); } f = DropCutter.FacetTest(cu, p, t); if (f != null) { zlist.Add((double)f); } e1 = DropCutter.EdgeTest(cu, p, t.p[0], t.p[1]); e2 = DropCutter.EdgeTest(cu, p, t.p[1], t.p[2]); e3 = DropCutter.EdgeTest(cu, p, t.p[0], t.p[2]); if (e1 != null) { zlist.Add((double)e1); } if (e2 != null) { zlist.Add((double)e2); } if (e3 != null) { zlist.Add((double)e3); } /* * if (zlist.Count > 1) * { * System.Console.Write("Before: "); * foreach (double d in zlist) * System.Console.Write(d.ToString() + " "); * System.Console.Write("\n"); * } */ zlist.Sort(); /* * if (zlist.Count > 2) * { * System.Console.Write("After: "); * foreach (double d in zlist) * System.Console.Write(d.ToString() + " "); * System.Console.Write("\n"); * } */ // System.Console.Write("Sorted: "); // foreach (double d in zlist) // System.Console.Write(d.ToString() + " "); // System.Console.Write("\n"); if (zlist.Count > 0) { z_new = zlist[zlist.Count - 1]; } /* * if (zlist.Count > 1) * System.Console.WriteLine("chosen: " + z_new); */ // System.Console.ReadKey(); } // end triangle loop if (z_new != null) { drop_points.Add(new Geo.Point(p.x, p.y, (double)z_new)); } } // end point-list loop System.Console.WriteLine("checked: " + checks + " redundant: " + redundant); System.Console.WriteLine("relevant: " + (checks - redundant) + " (" + 100 * (double)(checks - redundant) / (double)checks + "%)"); // check to see that STL has not changed // display drop-points int i = 1; Geo.Point p0 = new Geo.Point(); foreach (Geo.Point p in drop_points) { if (i == 1) // first move { p0 = new Geo.Point(p.x, p.y, 10); GeoLine l = new GeoLine(p0, p); l.color = System.Drawing.Color.Yellow; g.addGeom(l); p0 = p; } else // don't do anything for last move { GeoLine l = new GeoLine(p0, p); l.color = System.Drawing.Color.Magenta; g.addGeom(l); p0 = p; } i++; /* * GeoPoint pg = new GeoPoint(p); * pg.color = System.Drawing.Color.Aqua; * g.addGeom(pg); */ } // display zigzag and points /* * i = 1; * foreach (Geo.Point p in pointlist) * { * if (i == 1) * { * p0 = new Geo.Point(p.x, p.y, 10); * GeoLine l = new GeoLine(p0, p); * l.color = System.Drawing.Color.Yellow; * g.addGeom(l); * p0 = p; * } * else * { * GeoLine l = new GeoLine(p0, p); * l.color = System.Drawing.Color.Cyan; * g.addGeom(l); * p0 = p; * } * i++; * } */ // dummy test: /* * foreach (Geo.Tri t in s.tris) * { * GeoPoint p = new GeoPoint(t.p[0].x, t.p[0].y, t.p[0].z); * pointlist.Add(p); * } */ }
public static STLSurf Load(System.IO.StreamReader fs) { // Here's where autodetection will be provided for loading binary stl files and by wrapping STLA(scii) and STLB(inary) // but for now as only one format is provided, not bothering with it, we need to define a object format for handling file loading/saving // System.IO.StreamReader fs = new System.IO.StreamReader(FileName); STLSurf surf = new STLSurf(); int state = 0; int counter = 0; string[] data; Geo.Tri triangle = new Geo.Tri(); Vector normal = new Vector(); System.Globalization.CultureInfo locale = new System.Globalization.CultureInfo("en-GB"); int n_triangles=0; while ( !fs.EndOfStream ) { data = fs.ReadLine().TrimStart(' ').Split(' '); switch (state) { case 0: if (data[0].Equals("solid")) { surf.name = data[1]; state = 1; // continue readingx } break; case 1: if (data[0].Equals("facet")) { normal.x = double.Parse(data[2], locale); normal.y = double.Parse(data[3], locale); normal.z = double.Parse(data[4], locale); triangle = new Geo.Tri(normal); counter = 0; state = 2; } break; case 2: if (data[0].Equals("vertex")) { if ( counter <= 2 ) { triangle.p[counter].x = double.Parse(data[1], locale); triangle.p[counter].y = double.Parse(data[2], locale); triangle.p[counter].z = double.Parse(data[3], locale); // System.Console.WriteLine("STLReader: added point" + triangle.p[counter]); // System.Console.ReadKey(); counter++; } } else if (data[0].Equals("endfacet")) { if (counter == 3) { surf.AddTriangle(triangle); n_triangles += 1; } state = 1; } break; } } fs.Close(); System.Console.WriteLine("STLReader: read {0} triangles!",n_triangles); return (surf); }
public static STLSurf Load(System.IO.StreamReader fs) { STLSurf surf = new STLSurf(); int state = 0; int counter = 0; string[] data; Tri triangle = new Tri(); Vector normal = new Vector(); System.Globalization.CultureInfo locale = new System.Globalization.CultureInfo("en-GB"); int n_triangles=0; while ( !fs.EndOfStream ) { data = fs.ReadLine().TrimStart(' ').Split(' '); switch (state) { case 0: if (data[0].Equals("solid")) { state = 1; // continue readingx } break; case 1: if (data[0].Equals("facet")) { normal.x = double.Parse(data[2], locale); normal.y = double.Parse(data[3], locale); normal.z = double.Parse(data[4], locale); triangle = new Tri(normal); counter = 0; state = 2; } break; case 2: if (data[0].Equals("vertex")) { if ( counter <= 2 ) { triangle.p[counter].x = double.Parse(data[1], locale); triangle.p[counter].y = double.Parse(data[2], locale); triangle.p[counter].z = double.Parse(data[3], locale); // System.Console.WriteLine("STLReader: added point" + triangle.p[counter]); // System.Console.ReadKey(); counter++; } } else if (data[0].Equals("endfacet")) { if (counter == 3) { surf.AddTriangle(triangle); n_triangles += 1; } state = 1; } break; } } fs.Close(); System.Console.WriteLine("STLReader: read {0} triangles!",n_triangles); return (surf); }
static public STLSurf Load(System.IO.StreamReader fs) { STLSurf surf = new STLSurf(); int state = 0; int counter = 0; string[] data; Tri triangle = new Tri(); Vector normal = new Vector(); System.Globalization.CultureInfo locale = new System.Globalization.CultureInfo("en-GB"); int n_triangles = 0; while (!fs.EndOfStream) { data = fs.ReadLine().TrimStart(' ').Split(' '); switch (state) { case 0: if (data[0].Equals("solid")) { state = 1; // continue readingx } break; case 1: if (data[0].Equals("facet")) { normal.x = double.Parse(data[2], locale); normal.y = double.Parse(data[3], locale); normal.z = double.Parse(data[4], locale); triangle = new Tri(normal); counter = 0; state = 2; } break; case 2: if (data[0].Equals("vertex")) { if (counter <= 2) { triangle.p[counter].x = double.Parse(data[1], locale); triangle.p[counter].y = double.Parse(data[2], locale); triangle.p[counter].z = double.Parse(data[3], locale); // System.Console.WriteLine("STLReader: added point" + triangle.p[counter]); // System.Console.ReadKey(); counter++; } } else if (data[0].Equals("endfacet")) { if (counter == 3) { surf.AddTriangle(triangle); n_triangles += 1; } state = 1; } break; } } fs.Close(); System.Console.WriteLine("STLReader: read {0} triangles!", n_triangles); return(surf); }