示例#1
0
        // 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);
            }
        }
示例#2
0
        public static bool isinside(Geo.Tri t, Geo.Point p)
        {
            // point in triangle test

            // a new Tri projected onto the xy plane:
            Geo.Point p1 = new Geo.Point(t.p[0].x, t.p[0].y, 0);
            Geo.Point p2 = new Geo.Point(t.p[1].x, t.p[1].y, 0);
            Geo.Point p3 = new Geo.Point(t.p[2].x, t.p[2].y, 0);
            Geo.Point pt = new Geo.Point(p.x, p.y, 0);

            bool b1 = isright(p1, p2, pt);
            bool b2 = isright(p3, p1, pt);
            bool b3 = isright(p2, p3, pt);

            if ((b1) && (b2) && (b3))
            {
                return true;
            }
            else if ((!b1) && (!b2) && (!b3))
            {
                return true;
            }
            else
            {
                return false;
            }

        } // end isinside()
示例#3
0
        public static double? VertexTest(Cutter c,Geo.Point e, Geo.Point p)
       {
           // c.R and c.r define the cutter
           // e.x and e.y is the xy-position of the cutter (e.z is ignored)
           // p is the vertex tested against

           // q is the distance along xy-plane from e to vertex
           double q = Math.Sqrt(Math.Pow(e.x - p.x, 2) + Math.Pow((e.y - p.y), 2));

           if (q > c.R)
           { 
               // vertex is outside cutter. no need to do anything!
               return null;
           }
           else if (q <= (c.R - c.r))
           { 
                // vertex is in the cylindical/flat part of the cutter
               return p.z;
           }
           else if ((q > (c.R - c.r)) && (q <= c.R))
           {
               // vertex is in the toroidal part of the cutter
               double h2 = Math.Sqrt(Math.Pow(c.r, 2) - Math.Pow((q - (c.R - c.r)), 2));
               double h1 = c.r - h2;
               return p.z - h1;
           }
           else
           {
               // SERIOUS ERROR, we should not be here!
               System.Console.WriteLine("DropCutter: VertexTest: ERROR!");
               return null;
           }

       } // end VertexTest
示例#4
0
        // 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);

            }
        }
示例#5
0
文件: Geo.cs 项目: dharani811/monocam
        public Vector Cross(Geo.Point p)
        {
            // cross product with point
            // point coordinates are treated as vector coordinates
            double xc = y * p.z - z * p.y;
            double yc = z * p.x - x * p.z;
            double zc = x * p.y - y * p.x;

            return(new Vector(xc, yc, zc));
        }
示例#6
0
        public Camera()
        {
            eye = new Geo.Point(0, 0, 0);
            cen = new Geo.Point(0, 0, 0);
            up  = new Vector(0, 0, 1);

            _theta = 0.1;
            _fi    = 1;
            _r     = 3;
            recalc();
        }
示例#7
0
        private void addRandomGeoLineToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Geo.Point p1 = new Geo.Point((double)RandomNumber(-10, 10), (double)RandomNumber(-10, 10), (double)RandomNumber(-10, 10));
            Geo.Point p2 = new Geo.Point((double)RandomNumber(-10, 10), (double)RandomNumber(-10, 10), (double)RandomNumber(-10, 10));
            GeoLine   l  = new GeoLine(p1, p2);

            l.color = System.Drawing.Color.Red;
            addGeom(l);
            // l.gengldata();
            // Renderer.MakeRenderList(ref l.gldata[0]);
            // dlist.Add((int)l.gldata[0].dlistID);
            // geom.add(l);
            // GLPanel.Refresh();
        }
示例#8
0
        // test for bounding box function
        public static void bbox_test(GLWindow g)
        {
            // draw a triangle
            Geo.Point p1 = new Geo.Point(0.1, 0.1, 0);
            Geo.Point p2 = new Geo.Point(0.8, 0.3, 0);
            Geo.Point p3 = new Geo.Point(0.2, 0.6, 0);
            GeoLine   l1 = new GeoLine(p1, p2);
            GeoLine   l2 = new GeoLine(p1, p3);
            GeoLine   l3 = new GeoLine(p3, p2);

            l1.color = System.Drawing.Color.RoyalBlue;
            l2.color = System.Drawing.Color.RoyalBlue;
            l3.color = System.Drawing.Color.RoyalBlue;
            g.addGeom(l1);
            g.addGeom(l2);
            g.addGeom(l3);

            // create triangle and calculate bounding box
            Geo.Tri t = new Geo.Tri(p1, p2, p3);
            t.calc_bbox();
            Geo.Point a  = new Geo.Point(t.bb.minx, t.bb.miny, 0);
            Geo.Point b  = new Geo.Point(t.bb.maxx, t.bb.miny, 0);
            Geo.Point c  = new Geo.Point(t.bb.maxx, t.bb.maxy, 0);
            Geo.Point d  = new Geo.Point(t.bb.minx, t.bb.maxy, 0);
            GeoLine   h1 = new GeoLine(a, b);
            GeoLine   h2 = new GeoLine(b, c);
            GeoLine   h3 = new GeoLine(c, d);
            GeoLine   h4 = new GeoLine(d, a);

            h1.color = System.Drawing.Color.Red;
            h2.color = System.Drawing.Color.Red;
            h3.color = System.Drawing.Color.Red;
            h4.color = System.Drawing.Color.Red;
            g.addGeom(h1);
            g.addGeom(h2);
            g.addGeom(h3);
            g.addGeom(h4);

            // check that the points are OK
            GeoPoint v1 = new GeoPoint(t.bb.minx, t.bb.miny, 0);

            v1.color = System.Drawing.Color.Green;
            g.addGeom(v1);
            GeoPoint v2 = new GeoPoint(t.bb.maxx, t.bb.maxy, 0);

            v2.color = System.Drawing.Color.Azure;
            g.addGeom(v2);
        }
示例#9
0
        // testing that the isinside function works OK
        public static void isinside_test(GLWindow g)
        {
            Random r = new Random();

            // generate lots of random points
            int             N      = 100;
            List <GeoPoint> points = new List <GeoPoint>();

            for (int n = 0; n < N; n++)
            {
                GeoPoint p = new GeoPoint(0.001 * (float)r.Next(0, 1000), 0.001 * (float)r.Next(0, 1000), 0);
                points.Add(p);
            }


            // draw a triangle
            Geo.Point p1 = new Geo.Point(0.1, 0.1, 0);
            Geo.Point p2 = new Geo.Point(0.8, 0.1, 0);
            Geo.Point p3 = new Geo.Point(0.2, 0.6, 0);
            GeoLine   l1 = new GeoLine(p1, p2);
            GeoLine   l2 = new GeoLine(p1, p3);
            GeoLine   l3 = new GeoLine(p3, p2);

            l1.color = System.Drawing.Color.RoyalBlue;
            l2.color = System.Drawing.Color.RoyalBlue;
            l3.color = System.Drawing.Color.RoyalBlue;
            g.addGeom(l1);
            g.addGeom(l2);
            g.addGeom(l3);
            // p.color = System.Drawing.Color.Aqua;

            // draw points
            Geo.Tri t = new Geo.Tri(p2, p3, p1);
            foreach (GeoPoint p in points)
            {
                if (DropCutter.isinside(t, p.p))
                {
                    p.color = System.Drawing.Color.Aqua;
                }
                else
                {
                    p.color = System.Drawing.Color.Red;
                }

                g.addGeom(p);
            }
        }
示例#10
0
        // test for bounding box function
        public static void bbox_test(GLWindow g)
        {
            // draw a triangle
            Geo.Point p1 = new Geo.Point(0.1, 0.1, 0);
            Geo.Point p2 = new Geo.Point(0.8, 0.3, 0);
            Geo.Point p3 = new Geo.Point(0.2, 0.6, 0);
            GeoLine l1 = new GeoLine(p1, p2);
            GeoLine l2 = new GeoLine(p1, p3);
            GeoLine l3 = new GeoLine(p3, p2);
            l1.color = System.Drawing.Color.RoyalBlue;
            l2.color = System.Drawing.Color.RoyalBlue;
            l3.color = System.Drawing.Color.RoyalBlue;
            g.addGeom(l1);
            g.addGeom(l2);
            g.addGeom(l3);

            // create triangle and calculate bounding box
            Geo.Tri t = new Geo.Tri(p1, p2, p3);
            t.calc_bbox();
            Geo.Point a = new Geo.Point(t.bb.minx, t.bb.miny, 0);
            Geo.Point b = new Geo.Point(t.bb.maxx, t.bb.miny, 0);
            Geo.Point c = new Geo.Point(t.bb.maxx, t.bb.maxy, 0);
            Geo.Point d = new Geo.Point(t.bb.minx, t.bb.maxy, 0);
            GeoLine h1 = new GeoLine(a, b);
            GeoLine h2 = new GeoLine(b, c);
            GeoLine h3 = new GeoLine(c, d);
            GeoLine h4 = new GeoLine(d, a);
            h1.color = System.Drawing.Color.Red;
            h2.color = System.Drawing.Color.Red;
            h3.color = System.Drawing.Color.Red;
            h4.color = System.Drawing.Color.Red;
            g.addGeom(h1);
            g.addGeom(h2);
            g.addGeom(h3);
            g.addGeom(h4);

            // check that the points are OK
            GeoPoint v1 = new GeoPoint(t.bb.minx, t.bb.miny, 0);
            v1.color = System.Drawing.Color.Green;
            g.addGeom(v1);
            GeoPoint v2 = new GeoPoint(t.bb.maxx, t.bb.maxy, 0);
            v2.color = System.Drawing.Color.Azure;
            g.addGeom(v2);
        }
示例#11
0
        } // end isinside()

        public static bool isright(Geo.Point p1, Geo.Point p2, Geo.Point p)
        {
            // is point p right of line through points p1 and p2 ?

            // this is an ugly way of doing a determinant
            // should be prettyfied sometime...
            double a1 = p2.x - p1.x;
            double a2 = p2.y - p1.y;
            double t1 = a2;
            double t2 = -a1;
            double b1 = p.x - p1.x;
            double b2 = p.y - p1.y;

            double t = t1 * b1 + t2 * b2;
            if (t > 0)
                return true;
            else
                return false;
        } // end isright()
示例#12
0
        } // end FacetTest

        public static double? EdgeTest(Cutter cu, Geo.Point e, Geo.Point p1, Geo.Point p2)
        { 
            // contact cutter against edge from p1 to p2

            // translate segment so that cutter is at (0,0)
            Geo.Point start = new Geo.Point(p1.x - e.x, p1.y - e.y, p1.z);
            Geo.Point end = new Geo.Point(p2.x - e.x, p2.y - e.y, p2.z);

            // find angle btw. segment and X-axis
            double dx = end.x - start.x;
            double dy = end.y - start.y;
            double alfa;
            if (dx != 0)
                alfa = Math.Atan(dy / dx);
            else
                alfa = Math.PI / 2;

            //alfa = -alfa;
            // rotation matrix for rotation around z-axis:
            // should probably implement a matrix class later

            // rotate by angle alfa
            // need copy of data that does not change as we go through each line:
            double sx = start.x, sy = start.y, ex = end.x, ey = end.y;
            start.x = sx * Math.Cos(alfa) + sy * Math.Sin(alfa);
            start.y = -sx * Math.Sin(alfa) + sy * Math.Cos(alfa);
            end.x = ex * Math.Cos(alfa) + ey * Math.Sin(alfa);
            end.y = -ex * Math.Sin(alfa) + ey * Math.Cos(alfa);


            // check if segment is below cutter
            
            if (start.y > 0)
            {   
                alfa = alfa+Math.PI;
                start.x = sx * Math.Cos(alfa) + sy * Math.Sin(alfa);
                start.y = -sx * Math.Sin(alfa) + sy * Math.Cos(alfa);
                end.x = ex * Math.Cos(alfa) + ey * Math.Sin(alfa);
                end.y = -ex * Math.Sin(alfa) + ey * Math.Cos(alfa);
            }

            if (Math.Abs(start.y-end.y)>0.0000001)
            {
                System.Console.WriteLine("EdgeTest ERROR! (start.y - end.y) = " +(start.y-end.y));
                return null;
            }

            double l = -start.y; // distance from cutter to edge
            if (l < 0)
                System.Console.WriteLine("EdgeTest ERROR! l<0 !");

 
                
            
            // System.Console.WriteLine("l=" + l+" start.y="+start.y+" end.y="+end.y);
            

            // now we have two different algorithms depending on the cutter:
            if (cu.r == 0)
            {
                // this is the flat endmill case
                // it is easier and faster than the general case, so we handle it separately
                if (l > cu.R) // edge is outside of the cutter
                    return null;
                else // we are inside the cutter
                {   // so calculate CC point
                    double xc1 = Math.Sqrt(Math.Pow(cu.R, 2) - Math.Pow(l, 2));
                    double xc2 = -xc1;
                    double zc1 = ((xc1 - start.x) / (end.x - start.x)) * (end.z - start.z) + start.z;
                    double zc2 = ((xc2 - start.x) / (end.x - start.x)) * (end.z - start.z) + start.z;

                    // choose the higher point
                    double zc,xc;
                    if (zc1 > zc2)
                    {
                        zc = zc1;
                        xc = xc1;
                    }
                    else
                    {
                        zc = zc2;
                        xc = xc2;
                    }

                    // now that we have a CC point, check if it's in the edge
                    if ((start.x > xc) && (xc < end.x))
                        return null;
                    else if ((end.x < xc) && (xc > start.x))
                        return null;
                    else
                        return zc;

                }
                // unreachable place (according to compiler)
            } // end of flat endmill (r=0) case
            else if (cu.r > 0)
            {
                // System.Console.WriteLine("edgetest r>0 case!");

                // this is the general case (r>0)   ball-nose or bull-nose (spherical or toroidal)
                // later a separate case for the ball-cutter might be added (for performance)

                double xd=0, w=0, h=0, xd1=0, xd2=0, xc=0 , ze=0, zc=0;

                if (l > cu.R) // edge is outside of the cutter
                    return null;
                else if (((cu.R-cu.r)<l)&&(l<=cu.R))
                {    // toroidal case
                    xd=0; // center of ellipse
                    w=Math.Sqrt(Math.Pow(cu.R,2)-Math.Pow(l,2)); // width of ellipse
                    h=Math.Sqrt(Math.Pow(cu.r,2)-Math.Pow((l-(cu.R-cu.r)),2)); // height of ellipse
                }
                else if ((cu.R-cu.r)>=l)
                {
                    // quarter ellipse case
                    xd1=Math.Sqrt( Math.Pow((cu.R-cu.r),2)-Math.Pow(l,2));
                    xd2=-xd1;
                    h=cu.r; // ellipse height
                    w=Math.Sqrt( Math.Pow(cu.R,2)-Math.Pow(l,2) )- Math.Sqrt( Math.Pow((cu.R-cu.r),2)-Math.Pow(l,2) ); // ellipse height
                }

                // now there is a special case where the theta calculation will fail if
                // the segment is horziontal, i.e. start.z==end.z  so we need to catch that here
                if (start.z==end.z)
                {
                    if ((cu.R-cu.r)<l) 
                    {
                        // half-ellipse case
                        xc=0;
                        h=Math.Sqrt(Math.Pow(cu.r,2)-Math.Pow((l-(cu.R-cu.r)),2));
                        ze = start.z + h - cu.r;
                    }
                    else if ((cu.R - cu.r) > l)
                    {
                        // quarter ellipse case
                        xc = 0;
                        ze = start.z;
                    }

                    // now we have a CC point
                    // so we need to check if the CC point is in the edge
                    if (isinrange(start.x, end.x, xc))
                        return ze;
                    else
                        return null;

                } // end horizontal edge special case


                // now the general case where the theta calculation works
                double theta = Math.Atan( h*(start.x-end.x)/(w*(start.z-end.z))  );

                // based on this calculate the CC point
                if (((cu.R - cu.r) < l) && (cu.R <= l))
                {
                    // half-ellipse case
                    double xc1 = xd + Math.Abs(w * Math.Cos(theta));
                    double xc2 = xd - Math.Abs(w * Math.Cos(theta));
                    double zc1 = ((xc1 - start.x) / (end.x - start.x)) * (end.z - start.z) + start.z;
                    double zc2 = ((xc2 - start.x) / (end.x - start.x)) * (end.z - start.z) + start.z;
                    // select the higher point:
                    if (zc1 > zc2)
                    {
                        zc = zc1;
                        xc = xc1;
                    }
                    else
                    {
                        zc = zc2;
                        xc = xc2;
                    }

                }
                else if ((cu.R - cu.r) > l)
                { 
                    // quarter ellipse case
                    double xc1 = xd1 + Math.Abs(w * Math.Cos(theta));
                    double xc2 = xd2 - Math.Abs(w * Math.Cos(theta));
                    double zc1 = ((xc1 - start.x) / (end.x - start.x)) * (end.z - start.z) + start.z;
                    double zc2 = ((xc2 - start.x) / (end.x - start.x)) * (end.z - start.z) + start.z;
                    // select the higher point:
                    if (zc1 > zc2)
                    {
                        zc = zc1;
                        xc = xc1;
                    }
                    else
                    {
                        zc = zc2;
                        xc = xc2;
                    }
                }

                // now we have a valid xc value, so calculate the ze value:
                ze = zc + Math.Abs(h * Math.Sin(theta)) - cu.r;

                // finally, check that the CC point is in the edge
                if (isinrange(start.x,end.x,xc))
                    return ze;
                else
                    return null;

     


                // this line is unreachable (according to compiler)
                
            } // end of toroidal/spherical case
 
            
            // if we ever get here it is probably a serious error!
            System.Console.WriteLine("EdgeTest: ERROR: no case returned a valid ze!");
            return null;

        } // end of EdgeTest method
示例#13
0
        // testing that the isinside function works OK
        public static void isinside_test(GLWindow g)
        {
            Random r = new Random();

            // generate lots of random points
            int N = 100;
            List<GeoPoint> points = new List<GeoPoint>();
            for (int n = 0; n < N; n++)
            {
                GeoPoint p = new GeoPoint(0.001 * (float)r.Next(0, 1000), 0.001 * (float)r.Next(0, 1000), 0);
                points.Add(p);
            }

            // draw a triangle
            Geo.Point p1 = new Geo.Point(0.1, 0.1, 0);
            Geo.Point p2 = new Geo.Point(0.8, 0.1, 0);
            Geo.Point p3 = new Geo.Point(0.2, 0.6, 0);
            GeoLine l1 = new GeoLine(p1, p2);
            GeoLine l2 = new GeoLine(p1, p3);
            GeoLine l3 = new GeoLine(p3, p2);
            l1.color = System.Drawing.Color.RoyalBlue;
            l2.color = System.Drawing.Color.RoyalBlue;
            l3.color = System.Drawing.Color.RoyalBlue;
            g.addGeom(l1);
            g.addGeom(l2);
            g.addGeom(l3);
            // p.color = System.Drawing.Color.Aqua;

            // draw points
            Geo.Tri t = new Geo.Tri(p2, p3, p1);
            foreach (GeoPoint p in points)
            {
                if (DropCutter.isinside(t,p.p))
                    p.color = System.Drawing.Color.Aqua;
                else
                    p.color = System.Drawing.Color.Red;

                g.addGeom(p);
            }
        }
示例#14
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);
             * }
             */
        }
示例#15
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);
            }
            */
        }
示例#16
0
       } // end VertexTest

        public static double? FacetTest(Cutter cu, Geo.Point e, Geo.Tri t)
        { 
            // local copy of the surface normal

            t.recalc_normals(); // don't trust the pre-calculated normal! calculate it separately here.

            Vector n = new Vector(t.n.x, t.n.y, t.n.z);
            Geo.Point cc;

            if (n.z == 0)
            {
                // vertical plane, can't touch cutter against that!
                return null;
            }
            else if (n.z < 0)
            {
                // flip the normal so it points up (? is this always required?)
                n = -1*n;
            }

            // define plane containing facet
            double a = n.x;
            double b = n.y;
            double c = n.z;
            double d = - n.x * t.p[0].x - n.y * t.p[0].y - n.z * t.p[0].z;

            // the z-direction normal is a special case (?required?)
            // in debug phase, see if this is a useful case!
            if ((a == 0) && (b == 0))
            {
                // System.Console.WriteLine("facet-test:z-dir normal case!");
                e.z = t.p[0].z;
                cc = new Geo.Point(e.x,e.y,e.z);
                if (isinside(t, cc))
                {
                    // System.Console.WriteLine("facet-test:z-dir normal case!, returning {0}",e.z);
                    // System.Console.ReadKey();
                    return e.z;
                }
                else
                    return null;
            }

            // System.Console.WriteLine("facet-test:general case!");
            // facet test general case
            // uses trigonometry, so might be too slow?

            // flat endmill and ballnose should be simple to do without trig
            // toroidal case might require offset-ellipse idea?

            /*
            theta = asin(c);
            zf= -d/c - (a*xe+b*ye)/c+ (R-r)/tan(theta) + r/sin(theta) -r;
            e=[xe ye zf];
            u=[0  0  1];
            rc=e + ((R-r)*tan(theta)+r)*u - ((R-r)/cos(theta) + r)*n;
            t=isinside(p1,p2,p3,rc);
            */

            double theta = Math.Asin(c);
            double zf = -d/c - (a*e.x+b*e.y)/c + (cu.R-cu.r)/Math.Tan(theta) + cu.r/Math.Sin(theta) - cu.r;
            Vector ve = new Vector(e.x,e.y,zf);
            Vector u = new Vector(0,0,1);
            Vector rc = new Vector();
            rc = ve +((cu.R-cu.r)*Math.Tan(theta)+cu.r)*u - ((cu.R-cu.r)/Math.Cos(theta)+cu.r)*n;

            /*
            if (rc.z > 1000)
                System.Console.WriteLine("z>1000 !");
             */

            cc = new Geo.Point(rc.x, rc.y, rc.z);

            // check that CC lies in plane:
            // a*rc(1)+b*rc(2)+c*rc(3)+d
            double test = a * cc.x + b * cc.y + c * cc.z + d;
            if (test > 0.000001)
                System.Console.WriteLine("FacetTest ERROR! CC point not in plane");

            if (isinside(t, cc))
            {
                if (Math.Abs(zf) > 100)
                {
                    System.Console.WriteLine("serious problem... at" +e.x + "," + e.y);
                }
                return zf;
            }
            else
                return null;

        } // end FacetTest
示例#17
0
        public static double? FacetTest(Cutter cu, Geo.Point e, Geo.Tri t)
        {
            // local copy of the surface normal

            t.recalc_normals(); // don't trust the pre-calculated normal! calculate it separately here.

            Vector n = new Vector(t.n.x, t.n.y, t.n.z);
            Geo.Point cc;

            if (n.z == 0)
            {
                // vertical plane, can't touch cutter against that!
                return null;
            }
            else if (n.z < 0)
            {
                // flip the normal so it points up (? is this always required?)
                n = -1*n;
            }

            // define plane containing facet
            double a = n.x;
            double b = n.y;
            double c = n.z;
            double d = - n.x * t.p[0].x - n.y * t.p[0].y - n.z * t.p[0].z;

            // the z-direction normal is a special case (?required?)
            // in debug phase, see if this is a useful case!
            if ((a == 0) && (b == 0))
            {
                // System.Console.WriteLine("facet-test:z-dir normal case!");
                e.z = t.p[0].z;
                cc = new Geo.Point(e.x,e.y,e.z);
                if (isinside(t, cc))
                {
                    // System.Console.WriteLine("facet-test:z-dir normal case!, returning {0}",e.z);
                    // System.Console.ReadKey();
                    return e.z;
                }
                else
                    return null;
            }

            // System.Console.WriteLine("facet-test:general case!");
            // facet test general case
            // uses trigonometry, so might be too slow?

            // flat endmill and ballnose should be simple to do without trig
            // toroidal case might require offset-ellipse idea?

            /*
            theta = asin(c);
            zf= -d/c - (a*xe+b*ye)/c+ (R-r)/tan(theta) + r/sin(theta) -r;
            e=[xe ye zf];
            u=[0  0  1];
            rc=e + ((R-r)*tan(theta)+r)*u - ((R-r)/cos(theta) + r)*n;
            t=isinside(p1,p2,p3,rc);
            */

            double theta = Math.Asin(c);
            double zf = -d/c - (a*e.x+b*e.y)/c + (cu.R-cu.r)/Math.Tan(theta) + cu.r/Math.Sin(theta) - cu.r;
            Vector ve = new Vector(e.x,e.y,zf);
            Vector u = new Vector(0,0,1);
            Vector rc = new Vector();
            rc = ve +((cu.R-cu.r)*Math.Tan(theta)+cu.r)*u - ((cu.R-cu.r)/Math.Cos(theta)+cu.r)*n;

            /*
            if (rc.z > 1000)
                System.Console.WriteLine("z>1000 !");
             */

            cc = new Geo.Point(rc.x, rc.y, rc.z);

            // check that CC lies in plane:
            // a*rc(1)+b*rc(2)+c*rc(3)+d
            double test = a * cc.x + b * cc.y + c * cc.z + d;
            if (test > 0.000001)
                System.Console.WriteLine("FacetTest ERROR! CC point not in plane");

            if (isinside(t, cc))
            {
                if (Math.Abs(zf) > 100)
                {
                    System.Console.WriteLine("serious problem... at" +e.x + "," + e.y);
                }
                return zf;
            }
            else
                return null;
        }
示例#18
0
        public static bool isinside(Geo.Tri t, Geo.Point p)
        {
            // point in triangle test

            // a new Tri projected onto the xy plane:
            Geo.Point p1 = new Geo.Point(t.p[0].x, t.p[0].y, 0);
            Geo.Point p2 = new Geo.Point(t.p[1].x, t.p[1].y, 0);
            Geo.Point p3 = new Geo.Point(t.p[2].x, t.p[2].y, 0);
            Geo.Point pt = new Geo.Point(p.x, p.y, 0);

            bool b1 = isright(p1, p2, pt);
            bool b2 = isright(p3, p1, pt);
            bool b3 = isright(p2, p3, pt);

            if ((b1) && (b2) && (b3))
            {
                return true;
            }
            else if ((!b1) && (!b2) && (!b3))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
示例#19
0
 private void addRandomGeoLineToolStripMenuItem_Click(object sender, EventArgs e)
 {
     Geo.Point p1 = new Geo.Point((double)RandomNumber(-10, 10), (double)RandomNumber(-10, 10), (double)RandomNumber(-10, 10));
     Geo.Point p2 = new Geo.Point((double)RandomNumber(-10, 10), (double)RandomNumber(-10, 10), (double)RandomNumber(-10, 10));
     GeoLine l = new GeoLine(p1, p2);
     l.color = System.Drawing.Color.Red;
     addGeom(l);
     // l.gengldata();
     // Renderer.MakeRenderList(ref l.gldata[0]);
     // dlist.Add((int)l.gldata[0].dlistID);
     // geom.add(l);
     // GLPanel.Refresh();
 }
示例#20
0
        public static double? EdgeTest(Cutter cu, Geo.Point e, Geo.Point p1, Geo.Point p2)
        {
            // contact cutter against edge from p1 to p2

            // translate segment so that cutter is at (0,0)
            Geo.Point start = new Geo.Point(p1.x - e.x, p1.y - e.y, p1.z);
            Geo.Point end = new Geo.Point(p2.x - e.x, p2.y - e.y, p2.z);

            // find angle btw. segment and X-axis
            double dx = end.x - start.x;
            double dy = end.y - start.y;
            double alfa;
            if (dx != 0)
                alfa = Math.Atan(dy / dx);
            else
                alfa = Math.PI / 2;

            //alfa = -alfa;
            // rotation matrix for rotation around z-axis:
            // should probably implement a matrix class later

            // rotate by angle alfa
            // need copy of data that does not change as we go through each line:
            double sx = start.x, sy = start.y, ex = end.x, ey = end.y;
            start.x = sx * Math.Cos(alfa) + sy * Math.Sin(alfa);
            start.y = -sx * Math.Sin(alfa) + sy * Math.Cos(alfa);
            end.x = ex * Math.Cos(alfa) + ey * Math.Sin(alfa);
            end.y = -ex * Math.Sin(alfa) + ey * Math.Cos(alfa);

            // check if segment is below cutter

            if (start.y > 0)
            {
                alfa = alfa+Math.PI;
                start.x = sx * Math.Cos(alfa) + sy * Math.Sin(alfa);
                start.y = -sx * Math.Sin(alfa) + sy * Math.Cos(alfa);
                end.x = ex * Math.Cos(alfa) + ey * Math.Sin(alfa);
                end.y = -ex * Math.Sin(alfa) + ey * Math.Cos(alfa);
            }

            if (Math.Abs(start.y-end.y)>0.0000001)
            {
                System.Console.WriteLine("EdgeTest ERROR! (start.y - end.y) = " +(start.y-end.y));
                return null;
            }

            double l = -start.y; // distance from cutter to edge
            if (l < 0)
                System.Console.WriteLine("EdgeTest ERROR! l<0 !");

            // System.Console.WriteLine("l=" + l+" start.y="+start.y+" end.y="+end.y);

            // now we have two different algorithms depending on the cutter:
            if (cu.r == 0)
            {
                // this is the flat endmill case
                // it is easier and faster than the general case, so we handle it separately
                if (l > cu.R) // edge is outside of the cutter
                    return null;
                else // we are inside the cutter
                {   // so calculate CC point
                    double xc1 = Math.Sqrt(Math.Pow(cu.R, 2) - Math.Pow(l, 2));
                    double xc2 = -xc1;
                    double zc1 = ((xc1 - start.x) / (end.x - start.x)) * (end.z - start.z) + start.z;
                    double zc2 = ((xc2 - start.x) / (end.x - start.x)) * (end.z - start.z) + start.z;

                    // choose the higher point
                    double zc,xc;
                    if (zc1 > zc2)
                    {
                        zc = zc1;
                        xc = xc1;
                    }
                    else
                    {
                        zc = zc2;
                        xc = xc2;
                    }

                    // now that we have a CC point, check if it's in the edge
                    if ((start.x > xc) && (xc < end.x))
                        return null;
                    else if ((end.x < xc) && (xc > start.x))
                        return null;
                    else
                        return zc;

                }
                // unreachable place (according to compiler)
            } // end of flat endmill (r=0) case
            else if (cu.r > 0)
            {
                // System.Console.WriteLine("edgetest r>0 case!");

                // this is the general case (r>0)   ball-nose or bull-nose (spherical or toroidal)
                // later a separate case for the ball-cutter might be added (for performance)

                double xd=0, w=0, h=0, xd1=0, xd2=0, xc=0 , ze=0, zc=0;

                if (l > cu.R) // edge is outside of the cutter
                    return null;
                else if (((cu.R-cu.r)<l)&&(l<=cu.R))
                {    // toroidal case
                    xd=0; // center of ellipse
                    w=Math.Sqrt(Math.Pow(cu.R,2)-Math.Pow(l,2)); // width of ellipse
                    h=Math.Sqrt(Math.Pow(cu.r,2)-Math.Pow((l-(cu.R-cu.r)),2)); // height of ellipse
                }
                else if ((cu.R-cu.r)>=l)
                {
                    // quarter ellipse case
                    xd1=Math.Sqrt( Math.Pow((cu.R-cu.r),2)-Math.Pow(l,2));
                    xd2=-xd1;
                    h=cu.r; // ellipse height
                    w=Math.Sqrt( Math.Pow(cu.R,2)-Math.Pow(l,2) )- Math.Sqrt( Math.Pow((cu.R-cu.r),2)-Math.Pow(l,2) ); // ellipse height
                }

                // now there is a special case where the theta calculation will fail if
                // the segment is horziontal, i.e. start.z==end.z  so we need to catch that here
                if (start.z==end.z)
                {
                    if ((cu.R-cu.r)<l)
                    {
                        // half-ellipse case
                        xc=0;
                        h=Math.Sqrt(Math.Pow(cu.r,2)-Math.Pow((l-(cu.R-cu.r)),2));
                        ze = start.z + h - cu.r;
                    }
                    else if ((cu.R - cu.r) > l)
                    {
                        // quarter ellipse case
                        xc = 0;
                        ze = start.z;
                    }

                    // now we have a CC point
                    // so we need to check if the CC point is in the edge
                    if (isinrange(start.x, end.x, xc))
                        return ze;
                    else
                        return null;

                } // end horizontal edge special case

                // now the general case where the theta calculation works
                double theta = Math.Atan( h*(start.x-end.x)/(w*(start.z-end.z))  );

                // based on this calculate the CC point
                if (((cu.R - cu.r) < l) && (cu.R <= l))
                {
                    // half-ellipse case
                    double xc1 = xd + Math.Abs(w * Math.Cos(theta));
                    double xc2 = xd - Math.Abs(w * Math.Cos(theta));
                    double zc1 = ((xc1 - start.x) / (end.x - start.x)) * (end.z - start.z) + start.z;
                    double zc2 = ((xc2 - start.x) / (end.x - start.x)) * (end.z - start.z) + start.z;
                    // select the higher point:
                    if (zc1 > zc2)
                    {
                        zc = zc1;
                        xc = xc1;
                    }
                    else
                    {
                        zc = zc2;
                        xc = xc2;
                    }

                }
                else if ((cu.R - cu.r) > l)
                {
                    // quarter ellipse case
                    double xc1 = xd1 + Math.Abs(w * Math.Cos(theta));
                    double xc2 = xd2 - Math.Abs(w * Math.Cos(theta));
                    double zc1 = ((xc1 - start.x) / (end.x - start.x)) * (end.z - start.z) + start.z;
                    double zc2 = ((xc2 - start.x) / (end.x - start.x)) * (end.z - start.z) + start.z;
                    // select the higher point:
                    if (zc1 > zc2)
                    {
                        zc = zc1;
                        xc = xc1;
                    }
                    else
                    {
                        zc = zc2;
                        xc = xc2;
                    }
                }

                // now we have a valid xc value, so calculate the ze value:
                ze = zc + Math.Abs(h * Math.Sin(theta)) - cu.r;

                // finally, check that the CC point is in the edge
                if (isinrange(start.x,end.x,xc))
                    return ze;
                else
                    return null;

                // this line is unreachable (according to compiler)

            } // end of toroidal/spherical case

            // if we ever get here it is probably a serious error!
            System.Console.WriteLine("EdgeTest: ERROR: no case returned a valid ze!");
            return null;
        }