Example #1
0
        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++;
            }
        }
Example #2
0
        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);
             * }
             */
        }