private Task <DMesh3Builder> loadObj(string filename) { TaskCompletionSource <DMesh3Builder> tcs1 = new TaskCompletionSource <DMesh3Builder>(); Task <DMesh3Builder> t1 = tcs1.Task; t1.ConfigureAwait(false); // Start a background task that will complete tcs1.Task Task.Factory.StartNew(() => { DMesh3Builder meshBuilder = new DMesh3Builder(); try { IOReadResult result = StandardMeshReader.ReadFile(filename, new ReadOptions(), meshBuilder); } catch (Exception e) when( e is UnauthorizedAccessException || e is DirectoryNotFoundException || e is FileNotFoundException || e is NotSupportedException ) { Debug.LogError("Failed to Load" + filename + " : " + e.ToString()); meshBuilder = new DMesh3Builder(); } tcs1.SetResult(meshBuilder); }); return(t1); }
protected override void Recompute(DGArguments args) { System.Console.WriteLine("Reading file..."); ReadMesh = new DMesh3(); string path = CachedValue <string>(0, args); if (!File.Exists(path)) { return; } DMesh3Builder builder = new DMesh3Builder(); StandardMeshReader reader = new StandardMeshReader(); reader.MeshBuilder = builder; IOReadResult result = reader.Read(path, ReadOptions.Defaults); if (result.code != IOCode.Ok) { return; } ReadMesh = builder.Meshes[0]; }
private async Task <SimpleMeshBuilder> loadObj(string filename) { StandardMeshReader reader = new StandardMeshReader(); reader.MeshBuilder = new SimpleMeshBuilder(); try { IOReadResult result = reader.Read(filename, new ReadOptions()); } catch (Exception e) when( e is UnauthorizedAccessException || e is DirectoryNotFoundException || e is FileNotFoundException || e is NotSupportedException ) { Debug.LogError("Failed to Load" + filename + " : " + e.ToString()); } return(reader.MeshBuilder as SimpleMeshBuilder); }
private async Task <SimpleMeshBuilder> loadObj(string filename) { using (StreamReader reader = File.OpenText(filename)) { OBJReader objReader = new OBJReader(); SimpleMeshBuilder meshBuilder = new SimpleMeshBuilder(); try { IOReadResult result = objReader.Read(reader, new ReadOptions(), meshBuilder); } catch (Exception e) when( e is UnauthorizedAccessException || e is DirectoryNotFoundException || e is FileNotFoundException || e is NotSupportedException ) { Debug.LogError("Failed to Load" + filename + " : " + e.ToString()); meshBuilder = new SimpleMeshBuilder(); } return(meshBuilder); } }
public static void Main(string[] args) { CommandArgumentSet arguments = new CommandArgumentSet(); //arguments.Register("-tcount", int.MaxValue); //arguments.Register("-percent", 50.0f); //arguments.Register("-v", false); arguments.Register("-output", ""); if (arguments.Parse(args) == false) { return; } if (arguments.Filenames.Count != 1) { print_usage(); return; } string inputFilename = arguments.Filenames[0]; if (!File.Exists(inputFilename)) { System.Console.WriteLine("File {0} does not exist", inputFilename); return; } string outputFilename = Path.GetFileNameWithoutExtension(inputFilename); string format = Path.GetExtension(inputFilename); outputFilename = outputFilename + ".repaired" + format; if (arguments.Saw("-output")) { outputFilename = arguments.Strings["-output"]; } //int triCount = int.MaxValue; //if (arguments.Saw("-tcount")) // triCount = arguments.Integers["-tcount"]; //float percent = 50.0f; //if (arguments.Saw("-percent")) // percent = arguments.Floats["-percent"]; bool verbose = true; //if (arguments.Saw("-v")) // verbose = arguments.Flags["-v"]; List <DMesh3> meshes; try { DMesh3Builder builder = new DMesh3Builder(); IOReadResult result = StandardMeshReader.ReadFile(inputFilename, ReadOptions.Defaults, builder); if (result.code != IOCode.Ok) { System.Console.WriteLine("Error reading {0} : {1}", inputFilename, result.message); return; } meshes = builder.Meshes; } catch (Exception e) { System.Console.WriteLine("Exception reading {0} : {1}", inputFilename, e.Message); return; } if (meshes.Count == 0) { System.Console.WriteLine("file did not contain any valid meshes"); return; } DMesh3 mesh = meshes[0]; for (int k = 1; k < meshes.Count; ++k) { MeshEditor.Append(mesh, meshes[k]); } if (mesh.TriangleCount == 0) { System.Console.WriteLine("mesh does not contain any triangles"); return; } if (verbose) { System.Console.WriteLine("initial mesh contains {0} triangles", mesh.TriangleCount); } if (verbose) { System.Console.WriteLine("Repairing...", mesh.TriangleCount); } MeshAutoRepair repair = new MeshAutoRepair(mesh); repair.RemoveMode = MeshAutoRepair.RemoveModes.None; bool bOK = repair.Apply(); if (verbose) { if (bOK == false) { System.Console.WriteLine("repair failed!"); } else { System.Console.WriteLine("done! repaired mesh contains {0} triangles", mesh.TriangleCount); } } try { IOWriteResult wresult = StandardMeshWriter.WriteMesh(outputFilename, mesh, WriteOptions.Defaults); if (wresult.code != IOCode.Ok) { System.Console.WriteLine("Error writing {0} : {1}", inputFilename, wresult.message); return; } } catch (Exception e) { System.Console.WriteLine("Exception reading {0} : {1}", inputFilename, e.Message); return; } return; }
// // [TODO] // - binary output option // - option to strip data from inputs (eg remove normals/colors/uv/material from obj) // - option to remove material props from OBJ // - option to combine input meshes // - option to set float precision // - option to estimate normals for writing (eg for obj) // - option to set constant color for vertices // static void Main(string[] args) { if (args.Length != 2) { System.Console.WriteLine("gsMeshConvert v1.0 - Copyright gradientspace / Ryan Schmidt 2017"); System.Console.WriteLine("Questions? Comments? www.gradientspace.com or @gradientspace"); System.Console.WriteLine("usage: gsMeshConvert <input_mesh.format> (output_mesh.format)"); return; } string sInputFile = args[0]; if (!File.Exists(sInputFile)) { System.Console.WriteLine("cannot find file " + sInputFile); return; } string sOutputFile = args[1]; // check that can write output file DMesh3Builder builder = new DMesh3Builder(); StandardMeshReader reader = new StandardMeshReader() { MeshBuilder = builder }; ReadOptions read_options = ReadOptions.Defaults; IOReadResult readOK = reader.Read(sInputFile, read_options); if (readOK.code != IOCode.Ok) { System.Console.WriteLine("Error reading " + sInputFile); System.Console.WriteLine(readOK.message); return; } if (builder.Meshes.Count == 0) { System.Console.WriteLine("did not find any valid meshes in " + sInputFile); return; } List <WriteMesh> write_meshes = new List <WriteMesh>(); foreach (DMesh3 mesh in builder.Meshes) { write_meshes.Add(new WriteMesh(mesh)); } StandardMeshWriter writer = new StandardMeshWriter(); WriteOptions write_options = WriteOptions.Defaults; IOWriteResult writeOK = writer.Write(sOutputFile, write_meshes, write_options); if (writeOK.code != IOCode.Ok) { System.Console.WriteLine("Error writing " + sOutputFile); System.Console.WriteLine(writeOK.message); return; } // ok done! //System.Console.ReadKey(); }
public static void test_read_thingi10k() { //const string THINGIROOT = "D:\\meshes\\Thingi10K\\raw_meshes\\"; const string THINGIROOT = "F:\\Thingi10K\\raw_meshes\\"; string[] files = Directory.GetFiles(THINGIROOT); SafeListBuilder <string> failures = new SafeListBuilder <string>(); SafeListBuilder <string> empty = new SafeListBuilder <string>(); SafeListBuilder <string> closed = new SafeListBuilder <string>(); SafeListBuilder <string> open = new SafeListBuilder <string>(); SafeListBuilder <string> boundaries_failed = new SafeListBuilder <string>(); int k = 0; gParallel.ForEach(files, (filename) => { int i = k; Interlocked.Increment(ref k); System.Console.WriteLine("{0} : {1}", i, filename); DMesh3Builder builder = new DMesh3Builder(); StandardMeshReader reader = new StandardMeshReader() { MeshBuilder = builder }; IOReadResult result = reader.Read(filename, ReadOptions.Defaults); if (result.code != IOCode.Ok) { System.Console.WriteLine("{0} FAILED!", filename); failures.SafeAdd(filename); return; } bool is_open = false; bool loops_failed = false; bool is_empty = true; foreach (DMesh3 mesh in builder.Meshes) { if (mesh.TriangleCount > 0) { is_empty = false; } if (mesh.IsClosed() == false) { is_open = true; try { MeshBoundaryLoops loops = new MeshBoundaryLoops(mesh, false) { SpanBehavior = MeshBoundaryLoops.SpanBehaviors.ThrowException, FailureBehavior = MeshBoundaryLoops.FailureBehaviors.ThrowException }; loops.Compute(); } catch { loops_failed = true; } } } if (is_empty) { empty.SafeAdd(filename); } else if (is_open) { open.SafeAdd(filename); if (loops_failed) { boundaries_failed.SafeAdd(filename); } } else { closed.SafeAdd(filename); } }); foreach (string failure in failures.Result) { System.Console.WriteLine("FAIL: {0}", failure); } TestUtil.WriteTestOutputStrings(empty.List.ToArray(), "thingi10k_empty.txt"); TestUtil.WriteTestOutputStrings(closed.List.ToArray(), "thingi10k_closed.txt"); TestUtil.WriteTestOutputStrings(open.List.ToArray(), "thingi10k_open.txt"); TestUtil.WriteTestOutputStrings(boundaries_failed.List.ToArray(), "thingi10k_boundaries_failed.txt"); }
public static void test_autorepair_thingi10k() { //const string THINGIROOT = "E:\\Thingi10K\\"; string WRITEPATH = "E:\\Thingi10K\\repair_fails\\"; //string[] files = File.ReadAllLines("E:\\Thingi10K\\current\\thingi10k_open.txt"); string[] files = File.ReadAllLines("C:\\git\\gsGeometryTests\\test_output\\thingi10k_autorepair_failures.txt"); //string[] files = new string[] { // "E:\\Thingi10K\\raw_meshes\\37011.stl" //}; SafeListBuilder <string> failures = new SafeListBuilder <string>(); int count = 0; int MAX_NUM_FILES = 10000; gParallel.ForEach(files, (filename) => { if (count > MAX_NUM_FILES) { return; } int i = count; Interlocked.Increment(ref count); if (i % 10 == 0) { System.Console.WriteLine("{0} / {1}", i, files.Length); } long start_ticks = DateTime.Now.Ticks; DMesh3Builder builder = new DMesh3Builder(); StandardMeshReader reader = new StandardMeshReader() { MeshBuilder = builder }; IOReadResult result = reader.Read(filename, ReadOptions.Defaults); if (result.code != IOCode.Ok) { System.Console.WriteLine("{0} FAILED TO READ!", filename); failures.SafeAdd(filename); return; } DMesh3 mesh = builder.Meshes[0]; for (int k = 1; k < builder.Meshes.Count; ++k) { MeshEditor.Append(mesh, builder.Meshes[k]); } DMesh3 before = new DMesh3(mesh); try { MeshAutoRepair repair = new MeshAutoRepair(mesh); repair.Apply(); } catch (Exception e) { System.Console.WriteLine("EXCEPTION {0} : {1}", filename, e.Message); failures.SafeAdd(filename); return; } if (mesh.IsClosed() == false) { failures.SafeAdd(filename); Util.WriteDebugMesh(before, WRITEPATH + Path.GetFileNameWithoutExtension(filename) + ".obj"); Util.WriteDebugMesh(mesh, WRITEPATH + Path.GetFileNameWithoutExtension(filename) + ".failed.obj"); return; } else { if (mesh.CheckValidity(false, FailMode.ReturnOnly) == false) { System.Console.WriteLine("INVALID {0}", filename); failures.SafeAdd(filename); Util.WriteDebugMesh(before, WRITEPATH + Path.GetFileNameWithoutExtension(filename) + ".obj"); Util.WriteDebugMesh(mesh, WRITEPATH + Path.GetFileNameWithoutExtension(filename) + ".invalid.obj"); return; } } }); //foreach (string failure in failures.Result) { // System.Console.WriteLine("FAIL: {0}", failure); //} System.Console.WriteLine("repaired {0} of {1}", files.Length - failures.Result.Count, files.Length); TestUtil.WriteTestOutputStrings(make_strings(failures), "thingi10k_autorepair_failures_new.txt"); }
// // [TODO] // static void Main(string[] args) { CommandArgumentSet arguments = new CommandArgumentSet(); arguments.Register("-output", ""); if (arguments.Parse(args) == false) { return; } if (arguments.Filenames.Count != 1) { print_usage(); return; } string sInputFile = arguments.Filenames[0]; string sFilenameRoot = Path.GetFileNameWithoutExtension(sInputFile); if (!File.Exists(sInputFile)) { System.Console.WriteLine("cannot find file " + sInputFile); return; } DMesh3Builder builder = new DMesh3Builder(); StandardMeshReader reader = new StandardMeshReader() { MeshBuilder = builder }; ReadOptions read_options = ReadOptions.Defaults; read_options.ReadMaterials = true; IOReadResult readOK = reader.Read(sInputFile, read_options); if (readOK.code != IOCode.Ok) { System.Console.WriteLine("Error reading " + sInputFile); System.Console.WriteLine(readOK.message); return; } if (builder.Meshes.Count == 0) { System.Console.WriteLine("did not find any valid meshes in " + sInputFile); return; } // [TODO] out if count == 0 string sOutRoot = arguments.Strings["-output"]; if (sOutRoot.Length > 0) { bool bOutIsFolder = Directory.Exists(sOutRoot); if (!bOutIsFolder) { System.Console.WriteLine("-output folder {0} does not exist", sOutRoot); return; } } Dictionary <int, List <int> > MeshesByMaterial = new Dictionary <int, List <int> >(); MeshesByMaterial[-1] = new List <int>(); for (int i = 0; i < builder.Materials.Count; ++i) { MeshesByMaterial[i] = new List <int>(); } int N = builder.Meshes.Count; for (int i = 0; i < N; ++i) { int mati = builder.MaterialAssignment[i]; if (mati >= builder.Materials.Count) { mati = -1; } MeshesByMaterial[mati].Add(i); } int file_i = 0; foreach (int mat_i in MeshesByMaterial.Keys) { List <int> mesh_idxs = MeshesByMaterial[mat_i]; if (mesh_idxs.Count == 0) { continue; } WriteMesh[] write_meshes = new WriteMesh[mesh_idxs.Count]; for (int i = 0; i < mesh_idxs.Count; ++i) { write_meshes[i] = new WriteMesh(builder.Meshes[mesh_idxs[i]]); } string suffix = string.Format("_material{0}", file_i++); string sOutPath = Path.Combine(sOutRoot, sFilenameRoot + suffix + ".obj"); StandardMeshWriter writer = new StandardMeshWriter(); WriteOptions write_options = WriteOptions.Defaults; if (mat_i != -1) { write_options.bWriteMaterials = true; write_options.bPerVertexUVs = true; write_options.MaterialFilePath = Path.Combine(sOutRoot, sFilenameRoot + suffix + ".mtl"); GenericMaterial mat = builder.Materials[mat_i]; List <GenericMaterial> matList = new List <GenericMaterial>() { mat }; ConstantIndexMap idxmap = new ConstantIndexMap(0); for (int i = 0; i < write_meshes.Length; ++i) { write_meshes[i].Materials = matList; write_meshes[i].TriToMaterialMap = idxmap; } } IOWriteResult writeOK = writer.Write(sOutPath, new List <WriteMesh>(write_meshes), write_options); if (writeOK.code != IOCode.Ok) { System.Console.WriteLine("Error writing " + sOutPath); System.Console.WriteLine(writeOK.message); } } // ok done! //System.Console.ReadKey(); }
public bool ImportAutoUpdate(PrintMeshSO so) { SourceFilePath = so.SourceFilePath; if (!File.Exists(SourceFilePath)) { ErrorMessage = "MeshImporter.ImportAutoUpdate: file does not exist"; return(false); } DMesh3Builder builder = new DMesh3Builder(); StandardMeshReader reader = new StandardMeshReader() { MeshBuilder = builder }; long timestamp = File.GetLastWriteTime(SourceFilePath).Ticks; IOReadResult result = reader.Read(SourceFilePath, ReadOptions.Defaults); if (result.code != IOCode.Ok) { ErrorMessage = "MeshImporter.ImportAutoUpdate: failed with message " + result.message; return(false); } if (builder.Meshes.Count == 0) { ErrorMessage = "MeshImporter.ImportAutoUpdate: no meshes in file!"; return(false); } if (builder.Meshes.Count != 1) { ErrorMessage = "MeshImporter.ImportAutoUpdate: can only auto-update from file with single mesh!"; return(false); } DMesh3 mesh = builder.Meshes[0]; // unity xforms MeshTransforms.ConvertZUpToYUp(mesh); MeshTransforms.FlipLeftRightCoordSystems(mesh); // wait for any active tools to finish // [TODO] do we need to do this? while (CC.ActiveContext.ToolManager.HasActiveTool()) { Thread.Sleep(1000); } if (CC.ActiveScene.SceneObjects.Contains(so) == false) { ErrorMessage = "MeshImporter.ImportAutoUpdate: SO no longer exists"; return(false); } // change event?? so.LastReadFileTimestamp = timestamp; ThreadMailbox.PostToMainThread(() => { so.ReplaceMesh(mesh, true); }); return(true); }
public async Task ImportInteractive(string sFilename, Action <string> onCompletedF) { SourceFilePath = sFilename; DMesh3Builder builder = new DMesh3Builder(); StandardMeshReader reader = new StandardMeshReader() { MeshBuilder = builder }; await Task.Run(() => { IOReadResult result = reader.Read(SourceFilePath, ReadOptions.Defaults); if (result.code != IOCode.Ok) { ErrorMessage = "MeshImporter.Import: failed with message " + result.message; return; } }); if (builder.Meshes.Count == 0) { ErrorMessage = "MeshImporter.Import: no meshes in file!"; return; } await Task.Run(() => { // apply unity xforms foreach (DMesh3 mesh in builder.Meshes) { MeshTransforms.ConvertZUpToYUp(mesh); MeshTransforms.FlipLeftRightCoordSystems(mesh); } TriCount = 0; Bounds = AxisAlignedBox3d.Empty; foreach (var m in builder.Meshes) { TriCount += m.TriangleCount; Bounds.Contain(m.CachedBounds); } }); bool bSmall = (Bounds.MaxDim < HeightMinThreshold); bool bTall = (Bounds.Height > CC.Settings.BedSizeYMM); double maxXZ = Math.Max(Bounds.Width, Bounds.Depth); double bedMin = Math.Min(CC.Settings.BedSizeXMM, CC.Settings.BedSizeZMM); bool bLarge = (Bounds.Width > 2 * CC.Settings.BedSizeXMM) || (maxXZ > 2 * bedMin); bool bTriCount = (TriCount > TriCountThreshold); switch (CCPreferences.ImportAssistantMode) { case CCPreferences.ImportAssistantModes.PhysicalSizeOnly: bTriCount = false; break; case CCPreferences.ImportAssistantModes.MeshSizeOnly: bLarge = bSmall = bTall = false; break; case CCPreferences.ImportAssistantModes.Disabled: bLarge = bSmall = bTall = bTriCount = false; break; } if (bTriCount || bSmall || bLarge) { ImportMeshDialog.Show(CotangentUI.MainUICanvas, bSmall, bTall, bLarge, Bounds.Height, bTriCount, TriCount, async(scale, tricount) => { await process_and_complete_import(SourceFilePath, builder, scale, tricount, onCompletedF); }, async() => { await complete_import(SourceFilePath, builder, onCompletedF); } ); } else { await complete_import(SourceFilePath, builder, onCompletedF); } }
static void Main(string[] args) { CommandArgumentSet arguments = new CommandArgumentSet(); arguments.Register("-tcount", int.MaxValue); arguments.Register("-percent", 50.0f); arguments.Register("-v", false); arguments.Register("-output", ""); if (arguments.Parse(args) == false) { return; } if (arguments.Filenames.Count != 1) { print_usage(); return; } string inputFilename = arguments.Filenames[0]; if (!File.Exists(inputFilename)) { System.Console.WriteLine("File {0} does not exist", inputFilename); return; } string outputFilename = Path.GetFileNameWithoutExtension(inputFilename); string format = Path.GetExtension(inputFilename); outputFilename = outputFilename + ".reduced" + format; if (arguments.Saw("-output")) { outputFilename = arguments.Strings["-output"]; } int triCount = int.MaxValue; if (arguments.Saw("-tcount")) { triCount = arguments.Integers["-tcount"]; } float percent = 50.0f; if (arguments.Saw("-percent")) { percent = arguments.Floats["-percent"]; } bool verbose = false; if (arguments.Saw("-v")) { verbose = arguments.Flags["-v"]; } List <DMesh3> meshes; try { DMesh3Builder builder = new DMesh3Builder(); IOReadResult result = StandardMeshReader.ReadFile(inputFilename, ReadOptions.Defaults, builder); if (result.code != IOCode.Ok) { System.Console.WriteLine("Error reading {0} : {1}", inputFilename, result.message); return; } meshes = builder.Meshes; } catch (Exception e) { System.Console.WriteLine("Exception reading {0} : {1}", inputFilename, e.Message); return; } if (meshes.Count == 0) { System.Console.WriteLine("file did not contain any valid meshes"); return; } DMesh3 mesh = meshes[0]; for (int k = 1; k < meshes.Count; ++k) { MeshEditor.Append(mesh, meshes[k]); } if (mesh.TriangleCount == 0) { System.Console.WriteLine("mesh does not contain any triangles"); return; } if (verbose) { System.Console.WriteLine("initial mesh contains {0} triangles", mesh.TriangleCount); } Reducer r = new Reducer(mesh); if (triCount < int.MaxValue) { if (verbose) { System.Console.Write("reducing to {0} triangles...", triCount); } r.ReduceToTriangleCount(triCount); } else { int nT = (int)((float)mesh.TriangleCount * percent / 100.0f); nT = MathUtil.Clamp(nT, 1, mesh.TriangleCount); if (verbose) { System.Console.Write("reducing to {0} triangles...", nT); } r.ReduceToTriangleCount(nT); } if (verbose) { System.Console.WriteLine("done!"); } try { IOWriteResult wresult = StandardMeshWriter.WriteMesh(outputFilename, mesh, WriteOptions.Defaults); if (wresult.code != IOCode.Ok) { System.Console.WriteLine("Error writing {0} : {1}", inputFilename, wresult.message); return; } } catch (Exception e) { System.Console.WriteLine("Exception reading {0} : {1}", inputFilename, e.Message); return; } return; }
public static void test_write_solids() { //string FORMAT = ".obj"; string FORMAT = ".g3mesh"; string WRITEPATH = "E:\\Thingi10K\\closed\\"; string[] files = File.ReadAllLines("E:\\Thingi10K\\current\\thingi10k_closed.txt"); SafeListBuilder <string> failures = new SafeListBuilder <string>(); if (!Directory.Exists(WRITEPATH)) { Directory.CreateDirectory(WRITEPATH); } int k = 0; gParallel.ForEach(files, (filename) => { int i = k; Interlocked.Increment(ref k); if (i % 500 == 0) { System.Console.WriteLine("{0} : {1}", i, files.Length); } long start_ticks = DateTime.Now.Ticks; DMesh3Builder builder = new DMesh3Builder(); StandardMeshReader reader = new StandardMeshReader() { MeshBuilder = builder }; IOReadResult result = reader.Read(filename, ReadOptions.Defaults); if (result.code != IOCode.Ok) { System.Console.WriteLine("{0} FAILED!", filename); failures.SafeAdd(filename); return; } DMesh3 combineMesh = new DMesh3(); if (builder.Meshes.Count == 1) { combineMesh = builder.Meshes[0]; } else { foreach (DMesh3 mesh in builder.Meshes) { MeshEditor.Append(combineMesh, mesh); } } if (combineMesh.IsClosed() == false) { MergeCoincidentEdges closeCracks = new MergeCoincidentEdges(combineMesh); closeCracks.Apply(); } if (combineMesh.IsClosed() == false) { System.Console.WriteLine("NOT CLOSED: {0}", filename); return; } string outPath = Path.Combine(WRITEPATH, Path.GetFileNameWithoutExtension(filename) + FORMAT); StandardMeshWriter.WriteMesh(outPath, combineMesh, WriteOptions.Defaults); }); }
public static void test_specific_file() { //string filename = "F:\\Thingi10K\\raw_meshes\\1423009.stl"; //string filename = "E:\\Thingi10K\\raw_meshes\\99944.stl"; string filename = "E:\\Thingi10K\\raw_meshes\\57356.stl"; System.Console.WriteLine("reading {0}", filename); DMesh3Builder builder = new DMesh3Builder(); StandardMeshReader reader = new StandardMeshReader() { MeshBuilder = builder }; IOReadResult result = reader.Read(filename, ReadOptions.Defaults); if (result.code != IOCode.Ok) { System.Console.WriteLine("{0} FAILED!", filename); return; } System.Console.WriteLine("got {0} meshes", builder.Meshes.Count); bool is_open = false; bool loops_failed = false; bool is_empty = true; foreach (DMesh3 mesh in builder.Meshes) { if (mesh.TriangleCount > 0) { is_empty = false; } TestUtil.WriteTestOutputMesh(mesh, "thingi10k_specific_file_in.obj"); if (mesh.IsClosed() == false) { MergeCoincidentEdges closeCracks = new MergeCoincidentEdges(mesh); closeCracks.Apply(); } if (mesh.IsClosed() == false) { is_open = true; try { MeshBoundaryLoops loops = new MeshBoundaryLoops(mesh, false) { SpanBehavior = MeshBoundaryLoops.SpanBehaviors.Compute, FailureBehavior = MeshBoundaryLoops.FailureBehaviors.ConvertToOpenSpan }; loops.Compute(); } catch (Exception e) { System.Console.WriteLine("EXCEPTION: " + e.Message); loops_failed = true; } } System.Console.WriteLine("open: {0} loopsFailed: {1} empty: {2}", is_open, loops_failed, is_empty); TestUtil.WriteTestOutputMesh(mesh, "thingi10k_specific_file_out.obj"); } }
public static void test_repair_all() { //const string THINGIROOT = "D:\\meshes\\Thingi10K\\raw_meshes\\"; const string THINGIROOT = "E:\\Thingi10K\\raw_meshes\\"; string[] files = Directory.GetFiles(THINGIROOT); //files = File.ReadAllLines("C:\\git\\geometry3SharpDemos\\geometry3Test\\test_output\\thingi10k_open.txt"); SafeListBuilder <string> failures = new SafeListBuilder <string>(); SafeListBuilder <string> empty = new SafeListBuilder <string>(); SafeListBuilder <string> closed = new SafeListBuilder <string>(); SafeListBuilder <string> open = new SafeListBuilder <string>(); SafeListBuilder <string> boundaries_failed = new SafeListBuilder <string>(); SafeListBuilder <string> boundaries_spans_failed = new SafeListBuilder <string>(); SafeListBuilder <string> slow = new SafeListBuilder <string>(); SafeListBuilder <string> veryslow = new SafeListBuilder <string>(); int k = 0; int MAX_NUM_FILES = 10000; gParallel.ForEach(files, (filename) => { if (k > MAX_NUM_FILES) { return; } int i = k; Interlocked.Increment(ref k); System.Console.WriteLine("{0} : {1}", i, filename); long start_ticks = DateTime.Now.Ticks; DMesh3Builder builder = new DMesh3Builder(); StandardMeshReader reader = new StandardMeshReader() { MeshBuilder = builder }; IOReadResult result = reader.Read(filename, ReadOptions.Defaults); if (result.code != IOCode.Ok) { System.Console.WriteLine("{0} FAILED!", filename); failures.SafeAdd(filename); return; } bool is_open = false; bool loops_failed = false; bool loops_spans_failed = false; bool is_empty = true; foreach (DMesh3 mesh in builder.Meshes) { if (mesh.TriangleCount > 0) { is_empty = false; } if (mesh.IsClosed() == false) { MergeCoincidentEdges closeCracks = new MergeCoincidentEdges(mesh); closeCracks.Apply(); } if (mesh.IsClosed() == false) { is_open = true; try { MeshBoundaryLoops loops = new MeshBoundaryLoops(mesh, false) { SpanBehavior = MeshBoundaryLoops.SpanBehaviors.ThrowException, FailureBehavior = MeshBoundaryLoops.FailureBehaviors.ThrowException }; loops.Compute(); } catch { loops_failed = true; } if (loops_failed) { try { MeshBoundaryLoops loops = new MeshBoundaryLoops(mesh, false) { SpanBehavior = MeshBoundaryLoops.SpanBehaviors.Compute, FailureBehavior = MeshBoundaryLoops.FailureBehaviors.ConvertToOpenSpan }; loops.Compute(); } catch { loops_spans_failed = true; } } } } TimeSpan elapsed = new TimeSpan(DateTime.Now.Ticks - start_ticks); if (elapsed.TotalSeconds > 60) { veryslow.SafeAdd(filename); } else if (elapsed.TotalSeconds > 10) { slow.SafeAdd(filename); } if (is_empty) { empty.SafeAdd(filename); } else if (is_open) { open.SafeAdd(filename); if (loops_failed) { boundaries_failed.SafeAdd(filename); } if (loops_spans_failed) { boundaries_spans_failed.SafeAdd(filename); } } else { closed.SafeAdd(filename); } }); foreach (string failure in failures.Result) { System.Console.WriteLine("FAIL: {0}", failure); } TestUtil.WriteTestOutputStrings(make_strings(failures), "thingi10k_failures.txt"); TestUtil.WriteTestOutputStrings(make_strings(empty), "thingi10k_empty.txt"); TestUtil.WriteTestOutputStrings(make_strings(closed), "thingi10k_closed.txt"); TestUtil.WriteTestOutputStrings(make_strings(open), "thingi10k_open.txt"); TestUtil.WriteTestOutputStrings(make_strings(boundaries_failed), "thingi10k_boundaries_failed.txt"); TestUtil.WriteTestOutputStrings(make_strings(boundaries_spans_failed), "thingi10k_boundaries_spans_failed.txt"); TestUtil.WriteTestOutputStrings(make_strings(slow), "thingi10k_slow.txt"); TestUtil.WriteTestOutputStrings(make_strings(veryslow), "thingi10k_veryslow.txt"); }
static void Main(string[] args) { bool INTERACTIVE = true; //string inFile = "c:\\scratch\\test_bunny.obj"; //string inFile = "c:\\scratch\\bunny_100k.obj"; //string inFile = "c:\\scratch\\test_bunny_ascii.stl"; string inFile = "c:\\scratch\\test_bunny_binary.stl"; string outFile = "c:\\scratch\\test_bunny_out.obj"; ReadOptions read_options = new ReadOptions(); read_options.ReadMaterials = false; StandardMeshReader reader = new StandardMeshReader(); DMesh3Builder builder = new DMesh3Builder(); IOReadResult read_result = StandardMeshReader.ReadFile(inFile, read_options, builder); if (read_result.code != IOCode.Ok) { System.Console.WriteLine("Error reading " + inFile + " : " + read_result.message); if (INTERACTIVE) { System.Console.WriteLine("press enter key to exit"); System.Console.ReadLine(); } return; } WriteOptions write_options = new WriteOptions(); write_options.bCombineMeshes = true; write_options.bWriteBinary = true; List <WriteMesh> outMeshes = new List <WriteMesh>(); foreach (DMesh3 m in builder.Meshes) { outMeshes.Add(new WriteMesh(m)); } IOWriteResult write_result = StandardMeshWriter.WriteFile(outFile, outMeshes, write_options); if (write_result.code != IOCode.Ok) { System.Console.WriteLine("Error writing " + outFile + " : " + write_result.message); if (INTERACTIVE) { System.Console.WriteLine("press enter key to exit"); System.Console.ReadLine(); } return; } if (INTERACTIVE) { System.Console.WriteLine("Done conversion, press enter key to exit"); System.Console.ReadLine(); } }