public bool LoadDXF(string filename)
 {
     try
     {
         StreamReader sr = new StreamReader(filename);
         m_fullname = filename;
         m_name     = Path.GetFileName(filename);
         while (!sr.EndOfStream)
         {
             string line = sr.ReadLine();
             line = line.Trim();
             if (line == "3DFACE")
             {
                 Polygon poly = new Polygon(); //create a new polygon
                 m_lstpolys.Add(poly);         // add it to the polygon list
                 Point3d [] pnts;
                 LoadDXFPolyPoints(out pnts, sr);
                 poly.m_points = new Point3d[pnts.Length]; // create the storage
                 int idx = 0;
                 foreach (Point3d p in pnts)
                 {
                     poly.m_points[idx++] = AddUniqueVert(p);
                 }
                 poly.CalcNormal();
                 poly.CalcCenter();
             }
         }
         sr.Close();
         return(true);
     }catch (Exception)
     {
         return(false);
     }
 }
        /// <summary>
        /// This function loads an ascii STL file
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public bool LoadSTL_ASCII(string filename)
        {
            try
            {
                StreamReader sr = new StreamReader(filename);
                m_fullname = filename;
                m_name     = Path.GetFileName(filename);
                //first line should be "solid <name> "
                string    line = sr.ReadLine();
                string [] toks = line.Split(' ');
                if (!toks[0].ToLower().StartsWith("solid"))
                {
                    return(false); // does not start with "solid"
                }
                while (!sr.EndOfStream)
                {
                    line = sr.ReadLine().Trim();
                    if (line.ToLower().StartsWith("facet"))
                    {
                        line = sr.ReadLine().Trim();            //outerloop
                        Polygon poly = new Polygon();           //create a new polygon
                        m_lstpolys.Add(poly);                   // add it to the polygon list
                        poly.m_points = new Point3d[3];         // create the storage

                        for (int idx = 0; idx < 3; idx++)       //read the point
                        {
                            Point3d tmp        = new Point3d(); // create a temp point
                            char[]  delimiters = new char[] { ' ' };
                            line = sr.ReadLine().Trim();        //outerloop
                            toks = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                            // tmp.x = float.Parse(toks[1].Trim());
                            // tmp.y = float.Parse(toks[2].Trim());
                            // tmp.z = float.Parse(toks[3].Trim());
                            float tf = 0.0f;
                            Single.TryParse(toks[1], out tf);
                            tmp.x = tf;
                            Single.TryParse(toks[2], out tf);
                            tmp.y = tf;
                            Single.TryParse(toks[3], out tf);
                            tmp.z = tf;
                            poly.m_points[idx] = AddUniqueVert(tmp);
                        }

                        poly.CalcNormal();
                        poly.CalcCenter();
                        line = sr.ReadLine().Trim();//endloop
                    }
                }
                sr.Close();
            }
            catch (Exception)
            {
                return(false);
            }
            FindMinMax();
            return(true);
        }
        /*
            Given a radius length r and an angle t in radians and a circle's center (h,k),
         *  you can calculate the coordinates of a point on the circumference as follows
         *  (this is pseudo-code, you'll have to adapt it to your language):
            float x = r*cos(t) + h;
            float y = r*sin(t) + k;
         */
        public void Create(double bottomradius, double topradius, double height, int numdivscirc, int numdivsheight)
        {
            //generate sets of points that describe a circle vertically
               // int idx = 0; // this points to the first point in the circle
            double zlev = 0.0; // start at the bottom of the cylinder
            //Name = "Cylinder";
               // for (int cnt = 0; cnt < numdivsheight; cnt++)
               // {
            GenerateCirclePoints(bottomradius, numdivscirc, zlev); // bottom
            zlev += height;
            GenerateCirclePoints(topradius, numdivscirc, zlev); // top
            // now generate side polygons
            for (int cnt = 0; cnt < numdivscirc; cnt++)
            {
                /* left
                 3
                 |\
                 | \
                 |__\
                 2   1

                 right

                 2   3
                 _____
                 \   |
                  \  |
                   \ |
                    \|
                     1
                 *
                 *
                 */

                // the left looks correct
                int topidx = numdivscirc + 1; // index to the first point in the top circle
                Polygon plyl = new Polygon();
                m_lstpolys.Add(plyl);
                plyl.m_points = new Point3d[3]; // create some point storage
                plyl.m_points[0] = (Point3d)m_lstpoints[cnt];
                plyl.m_points[1] = (Point3d)m_lstpoints[(cnt + 1) % numdivscirc];
                plyl.m_points[2] = (Point3d)m_lstpoints[cnt + topidx];
                plyl.CalcCenter();
                plyl.CalcNormal();

                // bottom faces
                int centeridx = numdivscirc;
                Polygon plb = new Polygon();
                m_lstpolys.Add(plb);
                plb.m_points = new Point3d[3]; // create some point storage
                plb.m_points[0] = (Point3d)m_lstpoints[centeridx]; // the first point is always the center point
                plb.m_points[1] = (Point3d)m_lstpoints[(cnt + 1) % numdivscirc];
                plb.m_points[2] = (Point3d)m_lstpoints[cnt];
                plb.CalcCenter();
                plb.CalcNormal();

                Polygon plyr = new Polygon();
                m_lstpolys.Add(plyr);
                plyr.m_points = new Point3d[3]; // create some point storage
                plyr.m_points[0] = (Point3d)m_lstpoints[(cnt + 1) % numdivscirc];
                plyr.m_points[1] = (Point3d)m_lstpoints[((cnt + 1) % numdivscirc) + topidx]; //
                plyr.m_points[2] = (Point3d)m_lstpoints[cnt + topidx]; // the point directly above it

                plyr.CalcCenter();
                plyr.CalcNormal();

                //int topidx = numdivscirc + 1; // index to the first point in the top circle
                // top faces
                centeridx = topidx + numdivscirc;
                Polygon plt = new Polygon();
                m_lstpolys.Add(plt);
                plt.m_points = new Point3d[3]; // create some point storage
                plt.m_points[0] = (Point3d)m_lstpoints[centeridx]; // the first point is always the center pointt
                plt.m_points[2] = (Point3d)m_lstpoints[(cnt + 1) % numdivscirc + topidx];
                plt.m_points[1] = (Point3d)m_lstpoints[topidx + cnt];
                plt.CalcCenter();
                plt.CalcNormal();

            }
            //idx += numdivscirc;
               // }
            CalcCenter();
            CalcMinMaxes();
        }
        public bool GenerateFromBitmap(string file, Vector3d f) 
        {
            try
            {
                m_name = Path.GetFileName(file);
                Bitmap bm = new Bitmap(file);
                // add 3d points
                for (int y = 0; y < bm.Height; y++) 
                {
                    for (int x = 0; x < bm.Width; x++) 
                    {
                        Color clr = bm.GetPixel(x, y);
                        Point3d pnt = new Point3d();
                        pnt.x = f.x * ((float)x);
                        pnt.y = f.y * ((float)y);
                        pnt.z = f.z * ((float)clr.R);
                        m_lstpoints.Add(pnt);
                    }
                }
                // now generate polys
                for (int y = 0; y < bm.Height  ; y++)
                {
                    for (int x = 0; x < bm.Width ; x++)
                    {
                        if (y == (bm.Height - 1)) continue;
                        if (x == (bm.Width - 1)) continue;
                        Polygon ply = new Polygon();
                        ply.m_points = new Point3d[3];
                        int idx1 = (y * bm.Width) + x;
                        int idx2 = (y * bm.Width) + x + 1;
                        int idx3 = (y * bm.Width) + x + bm.Width ;
                        ply.m_points[0] = (Point3d)m_lstpoints[idx1];
                        ply.m_points[1] = (Point3d)m_lstpoints[idx2];
                        ply.m_points[2] = (Point3d)m_lstpoints[idx3];
                        ply.CalcCenter();
                        ply.CalcNormal();
                        m_lstpolys.Add(ply);
                        
                       
                        Polygon ply2 = new Polygon();
                        ply2.m_points = new Point3d[3];
                        idx1 = (y * bm.Width) + x + 1;
                        idx2 = (y * bm.Width) + x + bm.Width + 1;
                        idx3 = (y * bm.Width) + x + bm.Width;
                        ply2.m_points[0] = (Point3d)m_lstpoints[idx1];
                        ply2.m_points[1] = (Point3d)m_lstpoints[idx2];
                        ply2.m_points[2] = (Point3d)m_lstpoints[idx3];

                        ply2.CalcCenter();
                        ply2.CalcNormal();
                        m_lstpolys.Add(ply2);
                         
                    }
                }
                Update();
                return true;
            }
            catch (Exception) 
            {
                return false;
            }
        }
Exemple #5
0
        public bool GenerateFromBitmap(string file, ScaleFactor f)
        {
            try
            {
                m_name = Path.GetFileName(file);
                Bitmap bm = new Bitmap(file);
                // add 3d points
                for (int y = 0; y < bm.Height; y++)
                {
                    for (int x = 0; x < bm.Width; x++)
                    {
                        Color   clr = bm.GetPixel(x, y);
                        Point3d pnt = new Point3d();
                        pnt.x = f.x * ((double)x);
                        pnt.y = f.y * ((double)y);
                        pnt.z = f.z * ((double)clr.R);
                        m_lstpoints.Add(pnt);
                    }
                }
                // now generate polys
                for (int y = 0; y < bm.Height; y++)
                {
                    for (int x = 0; x < bm.Width; x++)
                    {
                        if (y == (bm.Height - 1))
                        {
                            continue;
                        }
                        if (x == (bm.Width - 1))
                        {
                            continue;
                        }
                        Polygon ply = new Polygon();
                        ply.m_points = new Point3d[3];
                        int idx1 = (y * bm.Width) + x;
                        int idx2 = (y * bm.Width) + x + 1;
                        int idx3 = (y * bm.Width) + x + bm.Width;
                        ply.m_points[0] = (Point3d)m_lstpoints[idx1];
                        ply.m_points[1] = (Point3d)m_lstpoints[idx2];
                        ply.m_points[2] = (Point3d)m_lstpoints[idx3];
                        ply.CalcCenter();
                        ply.CalcNormal();
                        m_lstpolys.Add(ply);


                        Polygon ply2 = new Polygon();
                        ply2.m_points    = new Point3d[3];
                        idx1             = (y * bm.Width) + x + 1;
                        idx2             = (y * bm.Width) + x + bm.Width + 1;
                        idx3             = (y * bm.Width) + x + bm.Width;
                        ply2.m_points[0] = (Point3d)m_lstpoints[idx1];
                        ply2.m_points[1] = (Point3d)m_lstpoints[idx2];
                        ply2.m_points[2] = (Point3d)m_lstpoints[idx3];

                        ply2.CalcCenter();
                        ply2.CalcNormal();
                        m_lstpolys.Add(ply2);
                    }
                }
                Update();
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
        /*
         *  Given a radius length r and an angle t in radians and a circle's center (h,k),
         *  you can calculate the coordinates of a point on the circumference as follows
         *  (this is pseudo-code, you'll have to adapt it to your language):
         *  float x = r*cos(t) + h;
         *  float y = r*sin(t) + k;
         */

        public void Create(double bottomradius, double topradius, double height, int numdivscirc, int numdivsheight)
        {
            //generate sets of points that describe a circle vertically
            // int idx = 0; // this points to the first point in the circle
            double zlev = 0.0; // start at the bottom of the cylinder

            //Name = "Cylinder";
            // for (int cnt = 0; cnt < numdivsheight; cnt++)
            // {
            GenerateCirclePoints(bottomradius, numdivscirc, zlev); // bottom
            zlev += height;
            GenerateCirclePoints(topradius, numdivscirc, zlev);    // top
            // now generate side polygons
            for (int cnt = 0; cnt < numdivscirc; cnt++)
            {
                /* left
                 * 3
                 |\
                 | \
                 |__\
                 | 2   1
                 |
                 | right
                 |
                 | 2   3
                 | _____
                 \   |
                 \  |
                 \ |
                 \|
                 \|  1
                 *
                 *
                 */


                // the left looks correct
                int     topidx = numdivscirc + 1; // index to the first point in the top circle
                Polygon plyl   = new Polygon();
                m_lstpolys.Add(plyl);
                plyl.m_points    = new Point3d[3]; // create some point storage
                plyl.m_points[0] = (Point3d)m_lstpoints[cnt];
                plyl.m_points[1] = (Point3d)m_lstpoints[(cnt + 1) % numdivscirc];
                plyl.m_points[2] = (Point3d)m_lstpoints[cnt + topidx];
                plyl.CalcCenter();
                plyl.CalcNormal();

                // bottom faces
                int     centeridx = numdivscirc;
                Polygon plb       = new Polygon();
                m_lstpolys.Add(plb);
                plb.m_points    = new Point3d[3];                  // create some point storage
                plb.m_points[0] = (Point3d)m_lstpoints[centeridx]; // the first point is always the center point
                plb.m_points[1] = (Point3d)m_lstpoints[(cnt + 1) % numdivscirc];
                plb.m_points[2] = (Point3d)m_lstpoints[cnt];
                plb.CalcCenter();
                plb.CalcNormal();



                Polygon plyr = new Polygon();
                m_lstpolys.Add(plyr);
                plyr.m_points    = new Point3d[3];                                           // create some point storage
                plyr.m_points[0] = (Point3d)m_lstpoints[(cnt + 1) % numdivscirc];
                plyr.m_points[1] = (Point3d)m_lstpoints[((cnt + 1) % numdivscirc) + topidx]; //
                plyr.m_points[2] = (Point3d)m_lstpoints[cnt + topidx];                       // the point directly above it

                plyr.CalcCenter();
                plyr.CalcNormal();

                //int topidx = numdivscirc + 1; // index to the first point in the top circle
                // top faces
                centeridx = topidx + numdivscirc;
                Polygon plt = new Polygon();
                m_lstpolys.Add(plt);
                plt.m_points    = new Point3d[3];                  // create some point storage
                plt.m_points[0] = (Point3d)m_lstpoints[centeridx]; // the first point is always the center pointt
                plt.m_points[2] = (Point3d)m_lstpoints[(cnt + 1) % numdivscirc + topidx];
                plt.m_points[1] = (Point3d)m_lstpoints[topidx + cnt];
                plt.CalcCenter();
                plt.CalcNormal();
            }
            //idx += numdivscirc;
            // }
            CalcCenter();
            CalcMinMaxes();
        }
        public bool LoadObjFile(string fileName)
         
        {
            try
            {
                    if (string.IsNullOrEmpty(fileName))
                {
                        {
                        return(false);

                          
                    }
                }
                  
                    if (!File.Exists(fileName))
                {
                        {
                        DebugLogger.Instance().LogError("3ds file could not be found " + fileName);

                        return(false);

                        //        throw new ArgumentException("3ds file could not be found", "fileName");
                          
                    }
                }
                this.m_fullname = fileName;
                this.m_name     = Path.GetFileNameWithoutExtension(fileName);
                    using (StreamReader sr = File.OpenText(fileName))
                        {
                           
                           int curLineNo = 0;      
                           
                           string line = null;
                           bool   done = false;
                        //ArrayList lclpoints = new ArrayList();
                        ArrayList lclply = new ArrayList();
                        ArrayList lclnrm = new ArrayList();

                             while ((line = sr.ReadLine()) != null)
                        {
                                 {
                                     curLineNo++;
                                    
                                      if (done || line.Trim() == string.Empty || line.StartsWith("#"))
                                {
                                          {
                                               continue;
                                              
                                    }
                                }
                                    
                                     string[] parts = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                                    
                                      switch (parts[0])
                                          {
                                              case "v":// vertex               
                                        Point3d pnt = new Point3d();

                                        double[] v = ParseVector(parts);
                                        pnt.x = v[0];
                                        pnt.y = v[1];
                                        pnt.z = v[2];
                                             m_lstpoints.Add(pnt);

                                        break;

                                              case "vn":// vertex normal
                                              lclnrm.Add(ParseVector(parts));

                                               break;

                                               //case "g":
                                                //    done = true;
                                                //    break;
                                              case "f":
                                              // a face
                                             
                                             
                                               if (parts.Length > 5)
                                        {
                                                   {
                                                        throw new NotSupportedException(string.Format("Face found with more than four indices (line {0})", curLineNo));
                                                       
                                            }
                                        }
                                             
                                               if (parts.Length < 3)
                                        {
                                                   {
                                                        throw new FormatException(string.Format("Face found with less three indices (line {0})", curLineNo));       
                                                       
                                            }
                                        }
                                                   
                                                   
                                              Polygon ply;
                                        int       fp1, fp2, fp3;
                                               if (parts.Length == 4)// a triangle
                                        {
                                                   {
                                                       // tris.Add(new Triangle(ParseFacePart(parts[1]), ParseFacePart(parts[2]), ParseFacePart(parts[3])));
                                                  ply = new Polygon();

                                                m_lstpolys.Add(ply);

                                                try
                                                {
                                                    ply.m_points    = new Point3d[3];
                                                    fp1             = ParseFacePart(parts[1]) - 1;
                                                    fp2             = ParseFacePart(parts[2]) - 1;
                                                    fp3             = ParseFacePart(parts[3]) - 1;
                                                    ply.m_points[0] = (Point3d)m_lstpoints[fp1];
                                                    ply.m_points[1] = (Point3d)m_lstpoints[fp2];
                                                    ply.m_points[2] = (Point3d)m_lstpoints[fp3];
                                                    ply.CalcCenter();
                                                    ply.CalcMinMax();
                                                    ply.CalcNormal();
                                                    ply.CalcRadius();
                                                }
                                                catch (Exception ex)
                                                {
                                                    DebugLogger.Instance().LogError(ex.Message);
                                                    return(false);
                                                }

                                                     
                                            }
                                        }
                                               else if (parts.Length == 5)// a quad
                                        {
                                                   {
                                                //              quads.Add(new Quad(ParseFacePart(parts[1]), ParseFacePart(parts[2]), ParseFacePart(parts[3]), ParseFacePart(parts[4])));

                                                ply = new Polygon();

                                                m_lstpolys.Add(ply);
                                                ply.m_points    = new Point3d[3];
                                                fp1             = ParseFacePart(parts[1]) - 1;
                                                fp2             = ParseFacePart(parts[2]) - 1;
                                                fp3             = ParseFacePart(parts[3]) - 1;
                                                ply.m_points[0] = (Point3d)m_lstpoints[fp1];
                                                ply.m_points[1] = (Point3d)m_lstpoints[fp2];
                                                ply.m_points[2] = (Point3d)m_lstpoints[fp3];   
                                                ply.CalcCenter();

                                                ply.CalcMinMax();
                                                ply.CalcNormal();
                                                ply.CalcRadius();

                                                ply = new Polygon();
                                                m_lstpolys.Add(ply);
                                                ply.m_points    = new Point3d[3];
                                                fp1             = ParseFacePart(parts[2]) - 1;
                                                fp2             = ParseFacePart(parts[3]) - 1;
                                                fp3             = ParseFacePart(parts[4]) - 1;
                                                ply.m_points[0] = (Point3d)m_lstpoints[fp1];
                                                ply.m_points[1] = (Point3d)m_lstpoints[fp2];
                                                ply.m_points[2] = (Point3d)m_lstpoints[fp3];
                                                ply.CalcCenter();
                                                ply.CalcMinMax();
                                                ply.CalcNormal();
                                                ply.CalcRadius();
                                                     
                                            }
                                        }
                                             
                                               break;
                                              
                                    }
                                    
                                   
                            }
                        }
                                 // Console.WriteLine("v: {0} n: {1} q:{2}", vectors.Count,normals.Count, quads.Count);
                           
                          
                    }
                    return(true);
            }
            catch (Exception ex)
            {
                DebugLogger.Instance().LogError(ex.Message);
                return(false);
            }
             
        }
        /// <summary>
        /// This function loads an ascii STL file
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public bool LoadSTL_ASCII(string filename)
        {
            try
            {
                StreamReader sr = new StreamReader(filename);
                m_fullname = filename;
                m_name = Path.GetFileName(filename);
                //first line should be "solid <name> "
                string line = sr.ReadLine();
                string []toks = line.Split(' ');
                if (!toks[0].ToLower().StartsWith("solid"))
                    return false; // does not start with "solid"
                while (!sr.EndOfStream)
                {
                    line = sr.ReadLine().Trim();
                    if (line.ToLower().StartsWith("facet"))
                    {
                        line = sr.ReadLine().Trim();//outerloop
                        Polygon poly = new Polygon();//create a new polygon
                        m_lstpolys.Add(poly); // add it to the polygon list
                        poly.m_points = new Point3d[3]; // create the storage

                        for (int idx = 0; idx < 3; idx++)//read the point
                        {
                            Point3d tmp = new Point3d(); // create a temp point
                            char[] delimiters = new char[] {' '};
                            line = sr.ReadLine().Trim();//outerloop
                            toks = line.Split(delimiters,StringSplitOptions.RemoveEmptyEntries);
                           // tmp.x = float.Parse(toks[1].Trim());
                           // tmp.y = float.Parse(toks[2].Trim());
                           // tmp.z = float.Parse(toks[3].Trim());
                            float tf = 0.0f;
                             Single.TryParse(toks[1],out tf);
                             tmp.x = tf;
                             Single.TryParse(toks[2], out tf);
                             tmp.y = tf;
                             Single.TryParse(toks[3], out tf);
                             tmp.z = tf;
                            poly.m_points[idx] = AddUniqueVert(tmp);
                        }

                        poly.CalcNormal();
                        poly.CalcCenter();
                        line = sr.ReadLine().Trim();//endloop
                    }
                }
                sr.Close();
            }
            catch (Exception )
            {
                return false;
            }
            FindMinMax();
            return true;
        }
 public bool LoadDXF(string filename)
 {
     try
     {
         StreamReader sr = new StreamReader(filename);
         m_fullname = filename;
         m_name = Path.GetFileName(filename);
         while (!sr.EndOfStream)
         {
             string line = sr.ReadLine();
             line = line.Trim();
             if (line == "3DFACE")
             {
                 Polygon poly = new Polygon();//create a new polygon
                 m_lstpolys.Add(poly); // add it to the polygon list
                 Point3d []pnts;
                 LoadDXFPolyPoints(out pnts, sr);
                 poly.m_points = new Point3d[pnts.Length]; // create the storage
                 int idx = 0;
                 foreach(Point3d p in pnts)
                 {
                     poly.m_points[idx++] = AddUniqueVert(p);
                 }
                 poly.CalcNormal();
                 poly.CalcCenter();
             }
         }
         sr.Close();
         return true;
     }catch( Exception)
     {
         return false;
     }
 }