From 5a96bad8c2e282a9fb3e67d7b4efca5b59c974dd Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Fri, 8 Aug 2014 21:48:59 +0200 Subject: [PATCH] Ported some more methods to C++ --- lib/Slic3r.pm | 1 - lib/Slic3r/Model.pm | 25 --------------------- lib/Slic3r/TriangleMesh.pm | 22 ------------------- xs/src/libslic3r/Model.cpp | 36 +++++++++++++++++++++++++++++++ xs/src/libslic3r/Model.hpp | 5 +++-- xs/src/libslic3r/TriangleMesh.cpp | 11 ++++++++++ xs/src/libslic3r/TriangleMesh.hpp | 1 + xs/xsp/Model.xsp | 4 ++++ xs/xsp/TriangleMesh.xsp | 6 ++++++ 9 files changed, 61 insertions(+), 50 deletions(-) delete mode 100644 lib/Slic3r/TriangleMesh.pm diff --git a/lib/Slic3r.pm b/lib/Slic3r.pm index c8422a1ba..25db38c30 100644 --- a/lib/Slic3r.pm +++ b/lib/Slic3r.pm @@ -71,7 +71,6 @@ use Slic3r::Print::Object; use Slic3r::Print::Simple; use Slic3r::Print::SupportMaterial; use Slic3r::Surface; -use Slic3r::TriangleMesh; our $build = eval "use Slic3r::Build; 1"; use Thread::Semaphore; diff --git a/lib/Slic3r/Model.pm b/lib/Slic3r/Model.pm index 5f9a808b6..0399c236f 100644 --- a/lib/Slic3r/Model.pm +++ b/lib/Slic3r/Model.pm @@ -158,21 +158,6 @@ sub _arrange { ); } -# makes sure all objects have at least one instance -sub add_default_instances { - my ($self) = @_; - - # apply a default position to all objects not having one - my $added = 0; - foreach my $object (@{$self->objects}) { - if ($object->instances_count == 0) { - $object->add_instance(offset => Slic3r::Pointf->new(0,0)); - $added = 1; - } - } - return $added; -} - # this returns the bounding box of the *transformed* instances sub bounding_box { my $self = shift; @@ -535,16 +520,6 @@ sub unique_materials { return sort keys %materials; } -sub facets_count { - my $self = shift; - return sum(map $_->mesh->facets_count, grep !$_->modifier, @{$self->volumes}); -} - -sub needed_repair { - my $self = shift; - return (first { !$_->mesh->needed_repair } grep !$_->modifier, @{$self->volumes}) ? 0 : 1; -} - sub mesh_stats { my $self = shift; diff --git a/lib/Slic3r/TriangleMesh.pm b/lib/Slic3r/TriangleMesh.pm deleted file mode 100644 index b06ba281e..000000000 --- a/lib/Slic3r/TriangleMesh.pm +++ /dev/null @@ -1,22 +0,0 @@ -package Slic3r::TriangleMesh; -use strict; -use warnings; - -use List::Util qw(first); -use Slic3r::Geometry qw(X Y); -use Slic3r::Geometry::Clipper qw(union_ex offset); - -sub needed_repair { - my $self = shift; - - my $stats = $self->stats; - return (first { $stats->{$_} > 0 } - qw(degenerate_facets edges_fixed facets_removed facets_added facets_reversed backwards_edges)) ? 1 : 0; -} - -sub center { - my $self = shift; - return $self->bounding_box->center; -} - -1; diff --git a/xs/src/libslic3r/Model.cpp b/xs/src/libslic3r/Model.cpp index cf7d9d1c5..a63cb3c46 100644 --- a/xs/src/libslic3r/Model.cpp +++ b/xs/src/libslic3r/Model.cpp @@ -158,6 +158,21 @@ Model::has_objects_with_no_instances() const return false; } +// makes sure all objects have at least one instance +bool +Model::add_default_instances() +{ + bool added = false; + // apply a default position to all objects not having one + for (ModelObjectPtrs::const_iterator o = this->objects.begin(); o != this->objects.end(); ++o) { + if ((*o)->instances.empty()) { + (*o)->add_instance(); + added = true; + } + } + return true; +} + #ifdef SLIC3RXS REGISTER_CLASS(Model, "Model"); #endif @@ -321,6 +336,27 @@ ModelObject::raw_mesh(TriangleMesh* mesh) const } } +size_t +ModelObject::facets_count() const +{ + size_t num = 0; + for (ModelVolumePtrs::const_iterator v = this->volumes.begin(); v != this->volumes.end(); ++v) { + if ((*v)->modifier) continue; + num += (*v)->mesh.stl.stats.number_of_facets; + } + return num; +} + +bool +ModelObject::needed_repair() const +{ + for (ModelVolumePtrs::const_iterator v = this->volumes.begin(); v != this->volumes.end(); ++v) { + if ((*v)->modifier) continue; + if ((*v)->mesh.needed_repair()) return true; + } + return false; +} + #ifdef SLIC3RXS REGISTER_CLASS(ModelObject, "Model::Object"); #endif diff --git a/xs/src/libslic3r/Model.hpp b/xs/src/libslic3r/Model.hpp index da21759d4..4c193d980 100644 --- a/xs/src/libslic3r/Model.hpp +++ b/xs/src/libslic3r/Model.hpp @@ -53,6 +53,7 @@ class Model // void arrange_objects(coordf_t distance, const BoundingBox &bb); // void duplicate(size_t copies_num, coordf_t distance, const BoundingBox &bb); bool has_objects_with_no_instances() const; + bool add_default_instances(); // void bounding_box(BoundingBox* bb) const; // void align_to_origin(); // void center_instances_around_point(const Pointf &point); @@ -121,8 +122,8 @@ class ModelObject //void translate(coordf_t x, coordf_t y, coordf_t z); //size_t materials_count() const; //void unique_materials(std::vector* materials) const; - //size_t facets_count() const; - //bool needed_repair() const; + size_t facets_count() const; + bool needed_repair() const; private: Model* model; diff --git a/xs/src/libslic3r/TriangleMesh.cpp b/xs/src/libslic3r/TriangleMesh.cpp index 04e892592..01e008efd 100644 --- a/xs/src/libslic3r/TriangleMesh.cpp +++ b/xs/src/libslic3r/TriangleMesh.cpp @@ -153,6 +153,17 @@ TriangleMesh::reset_repair_stats() { this->stl.stats.normals_fixed = 0; } +bool +TriangleMesh::needed_repair() const +{ + return this->stl.stats.degenerate_facets > 0 + || this->stl.stats.edges_fixed > 0 + || this->stl.stats.facets_removed > 0 + || this->stl.stats.facets_added > 0 + || this->stl.stats.facets_reversed > 0 + || this->stl.stats.backwards_edges > 0; +} + void TriangleMesh::WriteOBJFile(char* output_file) { stl_generate_shared_vertices(&stl); diff --git a/xs/src/libslic3r/TriangleMesh.hpp b/xs/src/libslic3r/TriangleMesh.hpp index c35300c39..2099f5e1e 100644 --- a/xs/src/libslic3r/TriangleMesh.hpp +++ b/xs/src/libslic3r/TriangleMesh.hpp @@ -45,6 +45,7 @@ class TriangleMesh void convex_hull(Polygon* hull); void bounding_box(BoundingBoxf3* bb) const; void reset_repair_stats(); + bool needed_repair() const; stl_file stl; bool repaired; diff --git a/xs/xsp/Model.xsp b/xs/xsp/Model.xsp index d83b92b5e..04b7c8207 100644 --- a/xs/xsp/Model.xsp +++ b/xs/xsp/Model.xsp @@ -56,6 +56,7 @@ // void arrange_objects(coordf_t distance, const BoundingBox &bb); // void duplicate(size_t copies_num, coordf_t distance, const BoundingBox &bb); bool has_objects_with_no_instances() const; + bool add_default_instances(); // void bounding_box(BoundingBox* bb) const; // void center_instances_around_point(const Pointf &point); // void align_instances_to_origin(); @@ -163,6 +164,9 @@ ModelMaterial::attributes() %code%{ RETVAL = &THIS->origin_translation; %}; void set_origin_translation(Pointf* point) %code%{ THIS->origin_translation = *point; %}; + + bool needed_repair() const; + int facets_count(); }; diff --git a/xs/xsp/TriangleMesh.xsp b/xs/xsp/TriangleMesh.xsp index ba5cdd0ce..d763fd98a 100644 --- a/xs/xsp/TriangleMesh.xsp +++ b/xs/xsp/TriangleMesh.xsp @@ -38,6 +38,12 @@ RETVAL = new BoundingBoxf3(); THIS->bounding_box(RETVAL); %}; + Pointf3* center() + %code{% + BoundingBoxf3 bb; + THIS->bounding_box(&bb); + RETVAL = new Pointf3(bb.center()); + %}; int facets_count() %code{% RETVAL = THIS->stl.stats.number_of_facets; %}; void reset_repair_stats();