/** * @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 var colordata = GetColor(float.Parse(lineData[4].Replace(',', '.')), filePath); newMesh.col1.R = Convert.ToSingle(colordata[0], CultureInfo.InvariantCulture); newMesh.col1.G = Convert.ToSingle(colordata[1], CultureInfo.InvariantCulture); newMesh.col1.B = Convert.ToSingle(colordata[2], CultureInfo.InvariantCulture); // 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 var colordata2 = GetColor(float.Parse(lineData[4].Replace(',', '.')), filePath); newMesh.col2.R = Convert.ToSingle(colordata2[0], CultureInfo.InvariantCulture); newMesh.col2.G = Convert.ToSingle(colordata2[1], CultureInfo.InvariantCulture); newMesh.col2.B = Convert.ToSingle(colordata2[2], CultureInfo.InvariantCulture); // 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 var colordata3 = GetColor(float.Parse(lineData[4].Replace(',', '.')), filePath); newMesh.col3.R = Convert.ToSingle(colordata3[0], CultureInfo.InvariantCulture); newMesh.col3.G = Convert.ToSingle(colordata3[1], CultureInfo.InvariantCulture); newMesh.col3.B = Convert.ToSingle(colordata3[2], CultureInfo.InvariantCulture); } 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()); }
/** * @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()); }