Example #1
0
        /**
         * @brief  *.stl file binary read function
         * @param  filePath
         * @retval meshList
         */
        private TriangleMesh[] ReadBinaryFile(string filePath)
        {
            List <TriangleMesh> meshList = new List <TriangleMesh>();
            int numOfMesh = 0;
            int i         = 0;
            int byteIndex = 0;

            byte[] fileBytes = File.ReadAllBytes(filePath);

            byte[] temp = new byte[4];

            /* 80 bytes title + 4 byte num of triangles + 50 bytes (1 of triangular mesh)  */
            if (fileBytes.Length > 120)
            {
                temp[0] = fileBytes[80];
                temp[1] = fileBytes[81];
                temp[2] = fileBytes[82];
                temp[3] = fileBytes[83];

                numOfMesh = System.BitConverter.ToInt32(temp, 0);

                byteIndex = 84;

                for (i = 0; i < numOfMesh; i++)
                {
                    TriangleMesh newMesh = new TriangleMesh();

                    /* this try-catch block will be reviewed */
                    try
                    {
                        /* face normal */
                        newMesh.normal1.x = System.BitConverter.ToSingle(new byte[] { fileBytes[byteIndex], fileBytes[byteIndex + 1], fileBytes[byteIndex + 2], fileBytes[byteIndex + 3] }, 0);
                        byteIndex        += 4;
                        newMesh.normal1.y = System.BitConverter.ToSingle(new byte[] { fileBytes[byteIndex], fileBytes[byteIndex + 1], fileBytes[byteIndex + 2], fileBytes[byteIndex + 3] }, 0);
                        byteIndex        += 4;
                        newMesh.normal1.z = System.BitConverter.ToSingle(new byte[] { fileBytes[byteIndex], fileBytes[byteIndex + 1], fileBytes[byteIndex + 2], fileBytes[byteIndex + 3] }, 0);
                        byteIndex        += 4;

                        /* normals of vertex 2 and 3 equals to vertex 1's normals */
                        newMesh.normal2 = newMesh.normal1;
                        newMesh.normal3 = newMesh.normal1;

                        /* vertex 1 */
                        newMesh.vert1.x = System.BitConverter.ToSingle(new byte[] { fileBytes[byteIndex], fileBytes[byteIndex + 1], fileBytes[byteIndex + 2], fileBytes[byteIndex + 3] }, 0);
                        byteIndex      += 4;
                        newMesh.vert1.y = System.BitConverter.ToSingle(new byte[] { fileBytes[byteIndex], fileBytes[byteIndex + 1], fileBytes[byteIndex + 2], fileBytes[byteIndex + 3] }, 0);
                        byteIndex      += 4;
                        newMesh.vert1.z = System.BitConverter.ToSingle(new byte[] { fileBytes[byteIndex], fileBytes[byteIndex + 1], fileBytes[byteIndex + 2], fileBytes[byteIndex + 3] }, 0);
                        byteIndex      += 4;

                        /* vertex 2 */
                        newMesh.vert2.x = System.BitConverter.ToSingle(new byte[] { fileBytes[byteIndex], fileBytes[byteIndex + 1], fileBytes[byteIndex + 2], fileBytes[byteIndex + 3] }, 0);
                        byteIndex      += 4;
                        newMesh.vert2.y = System.BitConverter.ToSingle(new byte[] { fileBytes[byteIndex], fileBytes[byteIndex + 1], fileBytes[byteIndex + 2], fileBytes[byteIndex + 3] }, 0);
                        byteIndex      += 4;
                        newMesh.vert2.z = System.BitConverter.ToSingle(new byte[] { fileBytes[byteIndex], fileBytes[byteIndex + 1], fileBytes[byteIndex + 2], fileBytes[byteIndex + 3] }, 0);
                        byteIndex      += 4;

                        /* vertex 3 */
                        newMesh.vert3.x = System.BitConverter.ToSingle(new byte[] { fileBytes[byteIndex], fileBytes[byteIndex + 1], fileBytes[byteIndex + 2], fileBytes[byteIndex + 3] }, 0);
                        byteIndex      += 4;
                        newMesh.vert3.y = System.BitConverter.ToSingle(new byte[] { fileBytes[byteIndex], fileBytes[byteIndex + 1], fileBytes[byteIndex + 2], fileBytes[byteIndex + 3] }, 0);
                        byteIndex      += 4;
                        newMesh.vert3.z = System.BitConverter.ToSingle(new byte[] { fileBytes[byteIndex], fileBytes[byteIndex + 1], fileBytes[byteIndex + 2], fileBytes[byteIndex + 3] }, 0);
                        byteIndex      += 4;

                        byteIndex += 2; // Attribute byte count
                    }
                    catch
                    {
                        processError = true;
                        break;
                    }

                    meshList.Add(newMesh);
                }
            }
            else
            {
                // nitentionally left blank
            }

            return(meshList.ToArray());
        }
Example #2
0
        /**
         * @brief  *.stl file ascii read function
         * @param  filePath
         * @retval meshList
         */
        private TriangleMesh[] ReadASCIIFile(string filePath)
        {
            List <TriangleMesh> meshList = new List <TriangleMesh>();

            StreamReader txtReader = new StreamReader(filePath);

            string lineString;

            while (!txtReader.EndOfStream)
            {
                lineString = txtReader.ReadLine().Trim(); /* delete whitespace in front and tail of the string */
                string[] lineData = lineString.Split(' ');

                if (lineData[0] == "solid")
                {
                    while (lineData[0] != "endsolid")
                    {
                        lineString = txtReader.ReadLine().Trim(); // facetnormal
                        lineData   = lineString.Split(' ');

                        if (lineData[0] == "endsolid") // check if we reach at the end of file
                        {
                            break;
                        }

                        TriangleMesh newMesh = new TriangleMesh(); // define new mesh object

                        /* this try-catch block will be reviewed */
                        try
                        {
                            // FaceNormal
                            newMesh.normal1.x = float.Parse(lineData[2]);
                            newMesh.normal1.y = float.Parse(lineData[3]);
                            newMesh.normal1.z = float.Parse(lineData[4]);

                            /* normals of vertex 2 and 3 equals to vertex 1's normals */
                            newMesh.normal2 = newMesh.normal1;
                            newMesh.normal3 = newMesh.normal1;

                            //----------------------------------------------------------------------
                            lineString = txtReader.ReadLine(); // Just skip the OuterLoop line
                            //----------------------------------------------------------------------

                            // Vertex1
                            lineString = txtReader.ReadLine().Trim();
                            /* reduce spaces until string has proper format for split */
                            while (lineString.IndexOf("  ") != -1)
                            {
                                lineString = lineString.Replace("  ", " ");
                            }
                            lineData = lineString.Split(' ');

                            newMesh.vert1.x = float.Parse(lineData[1]); // x1
                            newMesh.vert1.y = float.Parse(lineData[2]); // y1
                            newMesh.vert1.z = float.Parse(lineData[3]); // z1

                            // Vertex2
                            lineString = txtReader.ReadLine().Trim();
                            /* reduce spaces until string has proper format for split */
                            while (lineString.IndexOf("  ") != -1)
                            {
                                lineString = lineString.Replace("  ", " ");
                            }
                            lineData = lineString.Split(' ');

                            newMesh.vert2.x = float.Parse(lineData[1]); // x2
                            newMesh.vert2.y = float.Parse(lineData[2]); // y2
                            newMesh.vert2.z = float.Parse(lineData[3]); // z2

                            // Vertex3
                            lineString = txtReader.ReadLine().Trim();
                            /* reduce spaces until string has proper format for split */
                            while (lineString.IndexOf("  ") != -1)
                            {
                                lineString = lineString.Replace("  ", " ");
                            }
                            lineData = lineString.Split(' ');

                            newMesh.vert3.x = float.Parse(lineData[1]); // x3
                            newMesh.vert3.y = float.Parse(lineData[2]); // y3
                            newMesh.vert3.z = float.Parse(lineData[3]); // z3
                        }
                        catch
                        {
                            processError = true;
                            break;
                        }

                        //----------------------------------------------------------------------
                        lineString = txtReader.ReadLine(); // Just skip the endloop
                        //----------------------------------------------------------------------
                        lineString = txtReader.ReadLine(); // Just skip the endfacet

                        meshList.Add(newMesh);             // add mesh to meshList
                    } // while linedata[0]
                } // if solid
            } // while !endofstream

            return(meshList.ToArray());
        }
Example #3
0
        /**
         * @brief  *.stl file ascii read function
         * @param  filePath
         * @retval meshList
         */
        private TriangleMesh[] ReadASCIIFile(string filePath)
        {
            List <TriangleMesh> meshList = new List <TriangleMesh>();

            StreamReader txtReader = new StreamReader(filePath);

            string line;

            while (!txtReader.EndOfStream)
            {
                line = txtReader.ReadLine().Trim().Replace(" ", "");

                switch (line)
                {
                case "solid":

                    while (line != "endsolid")
                    {
                        line = txtReader.ReadLine().Trim().Replace(" ", ""); //facetnormal

                        if (line == "endsolid")                              // Son satır endsolid denetlemesi
                        {
                            break;
                        }

                        TriangleMesh newMesh = new TriangleMesh();     // define new mesh object

                        /* this try-catch block will be reviewed */
                        try
                        {
                            // FaceNormal
                            newMesh.normal1.x = float.Parse(line.Substring(11, 14));
                            newMesh.normal1.y = float.Parse(line.Substring(25, 14));
                            newMesh.normal1.z = float.Parse(line.Substring(39, 14));

                            /* normals of vertex 2 and 3 equals to vertex 1's normals */
                            newMesh.normal2 = newMesh.normal1;
                            newMesh.normal3 = newMesh.normal1;

                            //----------------------------------------------------------------------
                            line = txtReader.ReadLine().Trim().Replace(" ", "");     // OuterLoop
                            //----------------------------------------------------------------------

                            // Vertex1
                            line = txtReader.ReadLine().Trim().Replace(" ", "");

                            newMesh.vert1.x = float.Parse(line.Substring(6, 14));     //x1
                            newMesh.vert1.y = float.Parse(line.Substring(20, 14));    // y1
                            newMesh.vert1.z = float.Parse(line.Substring(34, 14));    // z1

                            // Vertex2
                            line            = txtReader.ReadLine().Trim().Replace(" ", "");
                            newMesh.vert2.x = float.Parse(line.Substring(6, 14));     //x1
                            newMesh.vert2.y = float.Parse(line.Substring(20, 14));    // y1
                            newMesh.vert2.z = float.Parse(line.Substring(34, 14));    // z1
                            // Vertex3
                            line            = txtReader.ReadLine().Trim().Replace(" ", "");
                            newMesh.vert3.x = float.Parse(line.Substring(6, 14));     //x1
                            newMesh.vert3.y = float.Parse(line.Substring(20, 14));    // y1
                            newMesh.vert3.z = float.Parse(line.Substring(34, 14));    // z1
                        }
                        catch
                        {
                            processError = true;
                            break;
                        }

                        //----------------------------------------------------------------------
                        line = txtReader.ReadLine().Trim().Replace(" ", ""); // endfacet or EndLoop

                        meshList.Add(newMesh);                               // add mesh to meshList
                    }


                    break;

                default:
                    // intentionally left blank
                    break;
                }
            }

            return(meshList.ToArray());
        }