From f29faf0548ea58f34e99befb7e58d923c28e3d75 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Mon, 24 Jun 2013 19:35:49 +0200 Subject: [PATCH] New Slic3r::TriangleMesh::XS class --- lib/Slic3r/GUI/SkeinPanel.pm | 5 ++- slic3r.pl | 5 ++- xs/src/TriangleMesh.cpp | 72 +++++++++++++++++++++++++++++++ xs/src/TriangleMesh.hpp | 15 +++++++ xs/src/myinit.h | 6 +-- xs/t/01_trianglemesh.t | 4 ++ xs/xsp/TriangleMesh.xsp | 84 ++++++------------------------------ xs/xsp/my.map | 1 + 8 files changed, 116 insertions(+), 76 deletions(-) create mode 100644 xs/src/TriangleMesh.cpp create mode 100644 xs/src/TriangleMesh.hpp diff --git a/lib/Slic3r/GUI/SkeinPanel.pm b/lib/Slic3r/GUI/SkeinPanel.pm index 85d2d5dae..5f0054374 100644 --- a/lib/Slic3r/GUI/SkeinPanel.pm +++ b/lib/Slic3r/GUI/SkeinPanel.pm @@ -220,7 +220,10 @@ sub repair_stl { $dlg->Destroy; } - Slic3r::TriangleMesh::XS::stl_repair($input_file, $output_file); + my $tmesh = Slic3r::TriangleMesh::XS->new(); + $tmesh->ReadSTLFile($input_file); + $tmesh->Repair; + $tmesh->WriteOBJFile($output_file); Slic3r::GUI::show_info($self, "Your file was repaired.", "Repair"); } diff --git a/slic3r.pl b/slic3r.pl index 771dc80ce..c46a88786 100755 --- a/slic3r.pl +++ b/slic3r.pl @@ -100,7 +100,10 @@ if (@ARGV) { # slicing from command line my $output_file = $file; $output_file =~ s/\.(stl)$/_fixed.obj/i; - Slic3r::TriangleMesh::XS::stl_repair($file, $output_file); + my $tmesh = Slic3r::TriangleMesh::XS->new(); + $tmesh->ReadSTLFile($file); + $tmesh->Repair; + $tmesh->WriteOBJFile($output_file); } exit; } diff --git a/xs/src/TriangleMesh.cpp b/xs/src/TriangleMesh.cpp new file mode 100644 index 000000000..382227bef --- /dev/null +++ b/xs/src/TriangleMesh.cpp @@ -0,0 +1,72 @@ +#include "TriangleMesh.hpp" + +TriangleMesh::TriangleMesh() {} +TriangleMesh::~TriangleMesh() { + stl_close(&stl); +} + +void +TriangleMesh::ReadSTLFile(char* input_file) { + stl_open(&stl, input_file); +} + +void +TriangleMesh::Repair() { + int i; + + // checking exact + stl_check_facets_exact(&stl); + stl.stats.facets_w_1_bad_edge = (stl.stats.connected_facets_2_edge - stl.stats.connected_facets_3_edge); + stl.stats.facets_w_2_bad_edge = (stl.stats.connected_facets_1_edge - stl.stats.connected_facets_2_edge); + stl.stats.facets_w_3_bad_edge = (stl.stats.number_of_facets - stl.stats.connected_facets_1_edge); + + // checking nearby + int last_edges_fixed = 0; + float tolerance = stl.stats.shortest_edge; + float increment = stl.stats.bounding_diameter / 10000.0; + int iterations = 2; + if (stl.stats.connected_facets_3_edge < stl.stats.number_of_facets) { + for (i = 0; i < iterations; i++) { + if (stl.stats.connected_facets_3_edge < stl.stats.number_of_facets) { + printf("Checking nearby. Tolerance= %f Iteration=%d of %d...", tolerance, i + 1, iterations); + stl_check_facets_nearby(&stl, tolerance); + printf(" Fixed %d edges.\n", stl.stats.edges_fixed - last_edges_fixed); + last_edges_fixed = stl.stats.edges_fixed; + tolerance += increment; + } else { + printf("All facets connected. No further nearby check necessary.\n"); + break; + } + } + } else { + printf("All facets connected. No nearby check necessary.\n"); + } + + // remove_unconnected + if (stl.stats.connected_facets_3_edge < stl.stats.number_of_facets) { + printf("Removing unconnected facets...\n"); + stl_remove_unconnected_facets(&stl); + } else + printf("No unconnected need to be removed.\n"); + + // fill_holes + if (stl.stats.connected_facets_3_edge < stl.stats.number_of_facets) { + printf("Filling holes...\n"); + stl_fill_holes(&stl); + } else + printf("No holes need to be filled.\n"); + + // normal_directions + printf("Checking normal directions...\n"); + stl_fix_normal_directions(&stl); + + // normal_values + printf("Checking normal values...\n"); + stl_fix_normal_values(&stl); +} + +void +TriangleMesh::WriteOBJFile(char* output_file) { + stl_generate_shared_vertices(&stl); + stl_write_obj(&stl, output_file); +} diff --git a/xs/src/TriangleMesh.hpp b/xs/src/TriangleMesh.hpp new file mode 100644 index 000000000..4b1d8949b --- /dev/null +++ b/xs/src/TriangleMesh.hpp @@ -0,0 +1,15 @@ +#include + +class TriangleMesh +{ + public: + TriangleMesh(); + ~TriangleMesh(); + void ReadSTLFile(char* input_file); + void Repair(); + void WriteOBJFile(char* output_file); + private: + stl_file stl; +}; + + diff --git a/xs/src/myinit.h b/xs/src/myinit.h index 570908808..46e564d2e 100644 --- a/xs/src/myinit.h +++ b/xs/src/myinit.h @@ -3,8 +3,7 @@ #include -typedef std::vector Ztable2; - +//////////////// class ZTable { public: @@ -17,7 +16,8 @@ ZTable::ZTable(std::vector* ztable) : z(*ztable) { } +//////////////// -#include +#include "TriangleMesh.hpp" #endif diff --git a/xs/t/01_trianglemesh.t b/xs/t/01_trianglemesh.t index 97e98fcce..92f8bc7ba 100644 --- a/xs/t/01_trianglemesh.t +++ b/xs/t/01_trianglemesh.t @@ -9,4 +9,8 @@ use Test::More tests => 1; is Slic3r::TriangleMesh::XS::hello_world(), 'Hello world!', 'hello world'; +my $t = Slic3r::TriangleMesh::XS->new(); +$t->ReadSTLFile("../../stl/testcube20mm.stl"); +use XXX; XXX($t); + __END__ diff --git a/xs/xsp/TriangleMesh.xsp b/xs/xsp/TriangleMesh.xsp index 9e0da64a6..6e6702b2b 100644 --- a/xs/xsp/TriangleMesh.xsp +++ b/xs/xsp/TriangleMesh.xsp @@ -1,84 +1,26 @@ %module{Slic3r::XS}; + +%{ +#include +%} + +%name{Slic3r::TriangleMesh::XS} class TriangleMesh { + TriangleMesh(); + ~TriangleMesh(); + void ReadSTLFile(char* input_file); + void Repair(); + void WriteOBJFile(char* output_file); +}; + %package{Slic3r::TriangleMesh::XS}; %{ PROTOTYPES: DISABLE -#include - std::string hello_world() CODE: RETVAL = "Hello world!"; OUTPUT: RETVAL - -void -stl_repair(input_file, output_file) - char* input_file; - char* output_file; - CODE: - int i; - - stl_file stl_in; - stl_open(&stl_in, input_file); - - // checking exact - stl_check_facets_exact(&stl_in); - stl_in.stats.facets_w_1_bad_edge = (stl_in.stats.connected_facets_2_edge - stl_in.stats.connected_facets_3_edge); - stl_in.stats.facets_w_2_bad_edge = (stl_in.stats.connected_facets_1_edge - stl_in.stats.connected_facets_2_edge); - stl_in.stats.facets_w_3_bad_edge = (stl_in.stats.number_of_facets - stl_in.stats.connected_facets_1_edge); - - // checking nearby - int last_edges_fixed = 0; - float tolerance = stl_in.stats.shortest_edge; - float increment = stl_in.stats.bounding_diameter / 10000.0; - int iterations = 2; - if (stl_in.stats.connected_facets_3_edge < stl_in.stats.number_of_facets) { - for (i = 0; i < iterations; i++) { - if (stl_in.stats.connected_facets_3_edge < stl_in.stats.number_of_facets) { - printf("Checking nearby. Tolerance= %f Iteration=%d of %d...", tolerance, i + 1, iterations); - stl_check_facets_nearby(&stl_in, tolerance); - printf(" Fixed %d edges.\n", stl_in.stats.edges_fixed - last_edges_fixed); - last_edges_fixed = stl_in.stats.edges_fixed; - tolerance += increment; - } else { - printf("All facets connected. No further nearby check necessary.\n"); - break; - } - } - } else { - printf("All facets connected. No nearby check necessary.\n"); - } - - // remove_unconnected - if (stl_in.stats.connected_facets_3_edge < stl_in.stats.number_of_facets) { - printf("Removing unconnected facets...\n"); - stl_remove_unconnected_facets(&stl_in); - } else - printf("No unconnected need to be removed.\n"); - - // fill_holes - if (stl_in.stats.connected_facets_3_edge < stl_in.stats.number_of_facets) { - printf("Filling holes...\n"); - stl_fill_holes(&stl_in); - } else - printf("No holes need to be filled.\n"); - - // normal_directions - printf("Checking normal directions...\n"); - stl_fix_normal_directions(&stl_in); - - // normal_values - printf("Checking normal values...\n"); - stl_fix_normal_values(&stl_in); - - // write STL - //stl_write_ascii(&stl_in, output_file, (char*)"Repaired by Slic3r + ADMesh"); - - stl_generate_shared_vertices(&stl_in); - stl_write_obj(&stl_in, output_file); - - stl_close(&stl_in); - %} diff --git a/xs/xsp/my.map b/xs/xsp/my.map index 6ce884228..656389f00 100644 --- a/xs/xsp/my.map +++ b/xs/xsp/my.map @@ -1,2 +1,3 @@ ZTable* O_OBJECT +TriangleMesh* O_OBJECT std::vector< unsigned int >* T_STD_VECTOR_UINT_PTR