private string[] ConvertToObj(List <Polygon> polygons) { ObjFile file = new ObjFile() { ObjectName = "ConvertedObject" }; foreach (var polygon in polygons) { ObjFile.Face face = new ObjFile.Face(); foreach (var vertex in polygon.Vertexes) { var foundVertex = file.Vertexes.Find(v => v.Equals(vertex)); int number; if (foundVertex == null) { file.Vertexes.Add(vertex); number = file.Vertexes.Count; } else { number = file.Vertexes.IndexOf(foundVertex); } face.AddComponent(new ObjFile.Face.Component(number, 0)); } file.AddFace(face); } return(file.Write()); }
protected virtual void SaveObj(string fileName) { var image = OriginalImagePictureBox.Image; var triangulator = new Triangulator(); try { var objs = triangulator.Triangulate(image, new Request() { IgnoreTransparent = IgnoreTransparentCheckBox.Checked, OffsetX = (float)OffsetXNumericUpDown.Value, OffsetZ = (float)OffsetZNumericUpDown.Value, MinimumHeight = (float)HeightMinNumericUpDown.Value, MaximumHeight = (float)HeightMaxNumericUpDown.Value, ScaleX = (float)ScaleXNumericUpDown.Value, ScaleZ = (float)ScaleZNumericUpDown.Value, SplitType = (SplitComboBox.SelectedItem as SplitComboBoxItem).Type, SplitX = (int)SplitXNumericUpDown.Value, SplitY = (int)SplitYNumericUpDown.Value, Angle = (float)AngleNumericUpDown.Value, Color = FillColorPictureBox.BackColor, FlipType = (FlipComboBox.SelectedItem as FlipComboBoxItem).Type }); // Single if (objs.Length == 1 && objs[0].Length == 1) { ObjFile.Write(objs[0][0], fileName); return; } // Multiple var fileNameDirectory = Path.GetDirectoryName(fileName); var fileNameTemplate = Path.GetFileNameWithoutExtension(fileName); for (var x = 0; x < objs.Length; x++) { var line = objs[x]; for (var y = 0; y < line.Length; y++) { ObjFile.Write(line[y], Path.Combine(fileNameDirectory, string.Format("{0}.{1:D4}.{2:D4}.obj", fileNameTemplate, y, x))); } } } catch (Exception ex) { MessageBox.Show(this, ex.ToString(), "Triangulator", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public void Write_NormalsTextures() { Mesh expected = GenerateNormalsTextureMesh(); WriteResult writeResult = ObjFile.Write(expected, this.outputPath); ReadResult readResult = ObjFile.Read(this.outputPath); Assert.AreEqual(expected.vertices, readResult.Mesh.vertices); Assert.AreEqual(expected.normals, readResult.Mesh.normals); Assert.AreEqual(expected.triangles, readResult.Mesh.triangles); Assert.AreEqual(expected.uv2, readResult.Mesh.uv2); Assert.AreEqual(expected.uv3, readResult.Mesh.uv3); }
public void Write_NormalsNoTextures() { Mesh mesh = GenerateNormalsNoTextureMesh(); ObjFile.Write(mesh, this.outputPath); ReadResult readResult = ObjFile.Read(this.outputPath); Assert.AreEqual(mesh.vertices, readResult.Mesh.vertices); Assert.AreEqual(mesh.normals, readResult.Mesh.normals); Assert.AreEqual(mesh.triangles, readResult.Mesh.triangles); Assert.AreEqual(mesh.uv2, readResult.Mesh.uv2); Assert.AreEqual(mesh.uv3, readResult.Mesh.uv3); }
public void Test_ReadWrite(string path) { string inputPath = InputPath(path); ReadResult expected = ObjFile.Read(inputPath); Assert.IsTrue(expected.Succeeded); Mesh mesh = expected.Mesh; WriteResult writeResult = ObjFile.Write(mesh, this.outputPath); Assert.IsTrue(writeResult.Succeeded); ReadResult actual = ObjFile.Read(this.outputPath); Assert.IsTrue(actual.Succeeded); Assert.AreEqual(expected.Mesh.vertices, actual.Mesh.vertices); Assert.AreEqual(expected.Mesh.normals, actual.Mesh.normals); Assert.AreEqual(expected.Mesh.triangles, actual.Mesh.triangles); Assert.AreEqual(expected.Mesh.uv2, actual.Mesh.uv2); Assert.AreEqual(expected.Mesh.uv3, actual.Mesh.uv3); }