public IOWriteResult Write(BinaryWriter writer, List <WriteMesh> vMeshes, WriteOptions options) { string header = "g3sharp_stl "; byte[] header_bytes = ASCIIEncoding.ASCII.GetBytes(header); byte[] stl_header = new byte[80]; Array.Clear(stl_header, 0, stl_header.Length); Array.Copy(header_bytes, stl_header, header_bytes.Length); writer.Write(stl_header); int total_tris = 0; foreach (WriteMesh mesh in vMeshes) { total_tris += mesh.Mesh.TriangleCount; } writer.Write(total_tris); for (int mi = 0; mi < vMeshes.Count; ++mi) { IMesh mesh = vMeshes[mi].Mesh; if (options.ProgressFunc != null) { options.ProgressFunc(mi, vMeshes.Count - 1); } Func <int, stl_triangle> producerF = (ti) => { var tri = new stl_triangle(); Index3i t = mesh.GetTriangle(ti); Vector3d a = mesh.GetVertex(t.a), b = mesh.GetVertex(t.b), c = mesh.GetVertex(t.c); Vector3d n = MathUtil.Normal(a, b, c); tri.nx = (float)n.x; tri.ny = (float)n.y; tri.nz = (float)n.z; tri.ax = (float)a.x; tri.ay = (float)a.y; tri.az = (float)a.z; tri.bx = (float)b.x; tri.by = (float)b.y; tri.bz = (float)b.z; tri.cx = (float)c.x; tri.cy = (float)c.y; tri.cz = (float)c.z; tri.attrib = 0; return(tri); }; Action <stl_triangle> consumerF = (tri) => { byte[] tri_bytes = Util.StructureToByteArray(tri); writer.Write(tri_bytes); }; var stream = new ParallelStream <int, stl_triangle>(); stream.ProducerF = producerF; stream.ConsumerF = consumerF; // parallel version is slower =\ //stream.Run_Thread(mesh.TriangleIndices()); stream.Run(mesh.TriangleIndices()); } return(new IOWriteResult(IOCode.Ok, "")); }
public IOWriteResult Write(TextWriter writer, List <WriteMesh> vMeshes, WriteOptions options) { if (options.bCombineMeshes == true) { writer.WriteLine("solid \"mesh\""); } string three_floats = Util.MakeVec3FormatString(0, 1, 2, options.RealPrecisionDigits); for (int mi = 0; mi < vMeshes.Count; ++mi) { IMesh mesh = vMeshes[mi].Mesh; if (options.ProgressFunc != null) { options.ProgressFunc(mi, vMeshes.Count - 1); } string solid_name = string.Format("mesh_{0}", mi); if (options.bCombineMeshes == false) { if (vMeshes[mi].Name != null && vMeshes[mi].Name.Length > 0) { solid_name = vMeshes[mi].Name; } writer.WriteLine("solid \"{0}\"", solid_name); } foreach (int ti in mesh.TriangleIndices()) { Index3i t = mesh.GetTriangle(ti); Vector3d a = mesh.GetVertex(t.a), b = mesh.GetVertex(t.b), c = mesh.GetVertex(t.c); Vector3d n = MathUtil.Normal(a, b, c); writer.WriteLine("facet normal " + three_floats, n.x, n.y, n.z); writer.WriteLine("outer loop" + writer.NewLine + "vertex " + three_floats, a.x, a.y, a.z); writer.WriteLine("vertex " + three_floats, b.x, b.y, b.z); writer.WriteLine("vertex " + three_floats, c.x, c.y, c.z); writer.WriteLine("endloop" + writer.NewLine + "endfacet"); } if (options.bCombineMeshes == false) { writer.WriteLine("endsolid \"{0}\"", solid_name); } } if (options.bCombineMeshes == true) { writer.WriteLine("endsolid \"mesh\""); } return(new IOWriteResult(IOCode.Ok, "")); }
public IOWriteResult Write(TextWriter writer, List <WriteMesh> vMeshes, WriteOptions options) { if (options.groupNamePrefix != null) { GroupNamePrefix = options.groupNamePrefix; } if (options.GroupNameF != null) { GroupNameF = options.GroupNameF; } int nAccumCountV = 1; // OBJ indices always start at 1 int nAccumCountUV = 1; // collect materials string sMaterialLib = ""; int nHaveMaterials = 0; if (options.bWriteMaterials && options.MaterialFilePath.Length > 0) { List <GenericMaterial> vMaterials = MeshIOUtil.FindUniqueMaterialList(vMeshes); IOWriteResult ok = write_materials(vMaterials, options); if (ok.code == IOCode.Ok) { sMaterialLib = Path.GetFileName(options.MaterialFilePath); nHaveMaterials = vMeshes.Count; } } if (options.AsciiHeaderFunc != null) { writer.WriteLine(options.AsciiHeaderFunc()); } if (sMaterialLib != "") { writer.WriteLine("mtllib {0}", sMaterialLib); } for (int mi = 0; mi < vMeshes.Count; ++mi) { IMesh mesh = vMeshes[mi].Mesh; if (options.ProgressFunc != null) { options.ProgressFunc(mi, vMeshes.Count - 1); } bool bVtxColors = options.bPerVertexColors && mesh.HasVertexColors; bool bNormals = options.bPerVertexNormals && mesh.HasVertexNormals; // use separate UV set if we have it, otherwise write per-vertex UVs if we have those bool bVtxUVs = options.bPerVertexUVs && mesh.HasVertexUVs; if (vMeshes[mi].UVs != null) { bVtxUVs = false; } int[] mapV = new int[mesh.MaxVertexID]; // write vertices for this mesh foreach (int vi in mesh.VertexIndices()) { mapV[vi] = nAccumCountV++; Vector3d v = mesh.GetVertex(vi); if (bVtxColors) { Vector3d c = mesh.GetVertexColor(vi); writer.WriteLine("v {0} {1} {2} {3:F8} {4:F8} {5:F8}", v[0], v[1], v[2], c[0], c[1], c[2]); } else { writer.WriteLine("v {0} {1} {2}", v[0], v[1], v[2]); } if (bNormals) { Vector3d n = mesh.GetVertexNormal(vi); writer.WriteLine("vn {0:F10} {1:F10} {2:F10}", n[0], n[1], n[2]); } if (bVtxUVs) { Vector2f uv = mesh.GetVertexUV(vi); writer.WriteLine("vt {0:F10} {1:F10}", uv.x, uv.y); } } // write independent UVs for this mesh, if we have them IIndexMap mapUV = (bVtxUVs) ? new IdentityIndexMap() : null; DenseUVMesh uvSet = null; if (vMeshes[mi].UVs != null) { uvSet = vMeshes[mi].UVs; int nUV = uvSet.UVs.Length; IndexMap fullMap = new IndexMap(false, nUV); // [TODO] do we really need a map here? is just integer shift, no? for (int ui = 0; ui < nUV; ++ui) { writer.WriteLine("vt {0:F8} {1:F8}", uvSet.UVs[ui].x, uvSet.UVs[ui].y); fullMap[ui] = nAccumCountUV++; } mapUV = fullMap; } // check if we need to write usemtl lines for this mesh bool bWriteMaterials = nHaveMaterials > 0 && vMeshes[mi].TriToMaterialMap != null && vMeshes[mi].Materials != null; // various ways we can write triangles to minimize state changes... // [TODO] support writing materials when mesh has groups!! if (options.bWriteGroups && mesh.HasTriangleGroups) { write_triangles_bygroup(writer, mesh, mapV, uvSet, mapUV, bNormals); } else { write_triangles_flat(writer, vMeshes[mi], mapV, uvSet, mapUV, bNormals, bWriteMaterials); } } return(new IOWriteResult(IOCode.Ok, "")); }
public IOWriteResult Write(TextWriter writer, List <WriteMesh> vMeshes, WriteOptions options) { int N = vMeshes.Count; writer.WriteLine("OFF"); string three_floats = Util.MakeVec3FormatString(0, 1, 2, options.RealPrecisionDigits); int nTotalV = 0, nTotalT = 0, nTotalE = 0; // OFF only supports one mesh, so have to collapse all input meshes // into a single list, with mapping for triangles // [TODO] can skip this if input is a single mesh! int[][] mapV = new int[N][]; for (int mi = 0; mi < N; ++mi) { nTotalV += vMeshes[mi].Mesh.VertexCount; nTotalT += vMeshes[mi].Mesh.TriangleCount; nTotalE += 0; mapV[mi] = new int[vMeshes[mi].Mesh.MaxVertexID]; } writer.WriteLine(string.Format("{0} {1} {2}", nTotalV, nTotalT, nTotalE)); // write all vertices, and construct vertex re-map int vi = 0; for (int mi = 0; mi < N; ++mi) { IMesh mesh = vMeshes[mi].Mesh; if (options.ProgressFunc != null) { options.ProgressFunc(mi, 2 * (N - 1)); } foreach (int vid in mesh.VertexIndices()) { Vector3d v = mesh.GetVertex(vid); writer.WriteLine(three_floats, v.x, v.y, v.z); mapV[mi][vid] = vi; vi++; } } // write all triangles for (int mi = 0; mi < N; ++mi) { IMesh mesh = vMeshes[mi].Mesh; if (options.ProgressFunc != null) { options.ProgressFunc(N + mi, 2 * (N - 1)); } foreach (int ti in mesh.TriangleIndices()) { Index3i t = mesh.GetTriangle(ti); t[0] = mapV[mi][t[0]]; t[1] = mapV[mi][t[1]]; t[2] = mapV[mi][t[2]]; writer.WriteLine(string.Format("3 {0} {1} {2}", t[0], t[1], t[2])); } } return(new IOWriteResult(IOCode.Ok, "")); }