From 4745d139559a084d0850d6be6918aac16596c033 Mon Sep 17 00:00:00 2001 From: Michael Kirsch Date: Sun, 21 Jul 2019 21:26:39 +0200 Subject: [PATCH] rework the former property origin_translation --- lib/Slic3r/GUI/Plater.pm | 11 ++++------- lib/Slic3r/Model.pm | 4 ++-- xs/src/libslic3r/IO/AMF.cpp | 12 +++++++----- xs/src/libslic3r/IO/TMF.cpp | 8 +++++--- xs/src/libslic3r/Model.cpp | 25 ++++++++++++------------- xs/src/libslic3r/Model.hpp | 20 ++++++++++++++------ xs/t/19_model.t | 5 +---- xs/xsp/Model.xsp | 12 ++++++++---- 8 files changed, 53 insertions(+), 44 deletions(-) diff --git a/lib/Slic3r/GUI/Plater.pm b/lib/Slic3r/GUI/Plater.pm index f444fc071..e8bfc1b1f 100644 --- a/lib/Slic3r/GUI/Plater.pm +++ b/lib/Slic3r/GUI/Plater.pm @@ -2593,6 +2593,7 @@ sub reload_from_disk { $new_obj->clear_instances; $new_obj->add_instance($_) for @{$org_obj->instances}; $new_obj->config->apply($org_obj->config); + $new_obj->set_trafo_obj($org_obj->get_trafo_obj()) if $reload_preserve_trafo; my $new_vol_idx = 0; my $org_vol_idx = 0; @@ -2605,8 +2606,8 @@ sub reload_from_disk { $new_obj->get_volume($new_vol_idx)->apply_transformation($org_obj->get_volume($org_vol_idx)->get_transformation) if $reload_preserve_trafo; $new_obj->get_volume($new_vol_idx++)->config->apply($org_obj->get_volume($org_vol_idx++)->config); } else { - # reload has more volumes than original (first file), apply config and trafo from the first volume - $new_obj->get_volume($new_vol_idx)->apply_transformation($org_obj->get_volume(0)->get_transformation) if $reload_preserve_trafo; + # reload has more volumes than original (first file), apply config and trafo from the parent object + $new_obj->get_volume($new_vol_idx)->apply_transformation($org_obj->get_trafo_obj()) if $reload_preserve_trafo; $new_obj->get_volume($new_vol_idx++)->config->apply($org_obj->get_volume(0)->config); $volume_unmatched=1; } @@ -2623,8 +2624,6 @@ sub reload_from_disk { if ($reload_behavior==1) { # Reload behavior: copy my $new_volume = $new_obj->add_volume($org_volume); - #$new_volume->mesh->translate(@{$org_obj->origin_translation->negative}); - #$new_volume->mesh->translate(@{$new_obj->origin_translation}); if ($new_volume->name =~ m/link to path\z/) { my $new_name = $new_volume->name; $new_name =~ s/ - no link to path$/ - copied/; @@ -2658,14 +2657,11 @@ sub reload_from_disk { $new_volume->set_input_file_vol_idx($org_volume->input_file_vol_idx); $new_volume->config->apply($org_volume->config); $new_volume->set_modifier($org_volume->modifier); - #$new_volume->mesh->translate(@{$new_obj->origin_translation}); } } } if (!$org_volume->input_file) { my $new_volume = $new_obj->add_volume($org_volume); # error -> copy old mesh - #$new_volume->mesh->translate(@{$org_obj->origin_translation->negative}); - #$new_volume->mesh->translate(@{$new_obj->origin_translation}); if ($new_volume->name =~ m/copied\z/) { my $new_name = $new_volume->name; $new_name =~ s/ - copied$/ - no link to path/; @@ -2678,6 +2674,7 @@ sub reload_from_disk { } $org_vol_idx++; } + $new_obj->center_around_origin(); } $self->remove($obj_idx); diff --git a/lib/Slic3r/Model.pm b/lib/Slic3r/Model.pm index cba64323a..b703bc4b5 100644 --- a/lib/Slic3r/Model.pm +++ b/lib/Slic3r/Model.pm @@ -36,8 +36,8 @@ sub add_object { if defined $args{config}; $new_object->set_layer_height_ranges($args{layer_height_ranges}) if defined $args{layer_height_ranges}; - $new_object->set_origin_translation($args{origin_translation}) - if defined $args{origin_translation}; + $new_object->apply_transformation($args{trafo_obj}) + if defined $args{trafo_obj}; return $new_object; } diff --git a/xs/src/libslic3r/IO/AMF.cpp b/xs/src/libslic3r/IO/AMF.cpp index 3ba7a7fff..93c0420ad 100644 --- a/xs/src/libslic3r/IO/AMF.cpp +++ b/xs/src/libslic3r/IO/AMF.cpp @@ -538,6 +538,8 @@ AMF::write(const Model& model, std::string output_file) std::vector vertices_offsets; size_t num_vertices = 0; + + Pointf3 origin_translation = object->origin_translation(); for (ModelVolume *volume : object->volumes) { volume->mesh.require_shared_vertices(); @@ -552,9 +554,9 @@ AMF::write(const Model& model, std::string output_file) // below. file << " " << endl << " " << endl - << " " << (stl.v_shared[i].x - object->origin_translation.x) << "" << endl - << " " << (stl.v_shared[i].y - object->origin_translation.y) << "" << endl - << " " << (stl.v_shared[i].z - object->origin_translation.z) << "" << endl + << " " << (stl.v_shared[i].x - origin_translation.x) << "" << endl + << " " << (stl.v_shared[i].y - origin_translation.y) << "" << endl + << " " << (stl.v_shared[i].z - origin_translation.z) << "" << endl << " " << endl << " " << endl; @@ -597,8 +599,8 @@ AMF::write(const Model& model, std::string output_file) for (const ModelInstance* instance : object->instances) instances << " " << endl - << " " << instance->offset.x + object->origin_translation.x << "" << endl - << " " << instance->offset.y + object->origin_translation.y << "" << endl + << " " << instance->offset.x + origin_translation.x << "" << endl + << " " << instance->offset.y + origin_translation.y << "" << endl << " " << instance->rotation << "" << endl << " " << instance->scaling_factor << "" << endl << " " << endl; diff --git a/xs/src/libslic3r/IO/TMF.cpp b/xs/src/libslic3r/IO/TMF.cpp index ab493307b..7e131723f 100644 --- a/xs/src/libslic3r/IO/TMF.cpp +++ b/xs/src/libslic3r/IO/TMF.cpp @@ -144,6 +144,8 @@ TMFEditor::write_object(boost::nowide::ofstream& fout, const ModelObject* object std::vector vertices_offsets; int num_vertices = 0; + Pointf3 origin_translation = object->origin_translation(); + for (const auto volume : object->volumes){ // Require mesh vertices. volume->mesh.require_shared_vertices(); @@ -160,9 +162,9 @@ TMFEditor::write_object(boost::nowide::ofstream& fout, const ModelObject* object // In order to do this we compensate for this translation in the instance placement // below. fout << " origin_translation.x) << "\""; - fout << " y=\"" << (stl.v_shared[i].y - object->origin_translation.y) << "\""; - fout << " z=\"" << (stl.v_shared[i].z - object->origin_translation.z) << "\"/>\n"; + fout << " x=\"" << (stl.v_shared[i].x - origin_translation.x) << "\""; + fout << " y=\"" << (stl.v_shared[i].y - origin_translation.y) << "\""; + fout << " z=\"" << (stl.v_shared[i].z - origin_translation.z) << "\"/>\n"; } num_vertices += stl.stats.shared_vertices; } diff --git a/xs/src/libslic3r/Model.cpp b/xs/src/libslic3r/Model.cpp index 987c0cd3d..a20864529 100644 --- a/xs/src/libslic3r/Model.cpp +++ b/xs/src/libslic3r/Model.cpp @@ -452,7 +452,7 @@ ModelObject::ModelObject(Model *model, const ModelObject &other, bool copy_volum layer_height_ranges(other.layer_height_ranges), part_number(other.part_number), layer_height_spline(other.layer_height_spline), - origin_translation(other.origin_translation), + trafo_obj(other.trafo_obj), _bounding_box(other._bounding_box), _bounding_box_valid(other._bounding_box_valid), model(model) @@ -482,8 +482,8 @@ ModelObject::swap(ModelObject &other) std::swap(this->volumes, other.volumes); std::swap(this->config, other.config); std::swap(this->layer_height_ranges, other.layer_height_ranges); - std::swap(this->layer_height_spline, other.layer_height_spline); - std::swap(this->origin_translation, other.origin_translation); + std::swap(this->layer_height_spline, other.layer_height_spline); + std::swap(this->trafo_obj, other.trafo_obj); std::swap(this->_bounding_box, other._bounding_box); std::swap(this->_bounding_box_valid, other._bounding_box_valid); std::swap(this->part_number, other.part_number); @@ -604,6 +604,13 @@ ModelObject::repair() (*v)->mesh.repair(); } +Pointf3 +ModelObject::origin_translation() const +{ + return Pointf3(trafo_obj.m03, trafo_obj.m13, trafo_obj.m23); +} + + // flattens all volumes and instances into a single mesh TriangleMesh ModelObject::mesh() const @@ -684,7 +691,6 @@ ModelObject::align_to_ground() bb.merge(v->bounding_box()); this->translate(0, 0, -bb.min.z); - this->origin_translation.translate(0, 0, -bb.min.z); } void @@ -706,7 +712,6 @@ ModelObject::center_around_origin() vector.y -= size.y/2; this->translate(vector); - this->origin_translation.translate(vector); if (!this->instances.empty()) { for (ModelInstancePtrs::const_iterator i = this->instances.begin(); i != this->instances.end(); ++i) { @@ -759,9 +764,7 @@ ModelObject::scale(const Pointf3 &versor) trafo.applyLeft(center_trafo.inverse()); this->apply_transformation(trafo); - - // reset origin translation since it doesn't make sense anymore - this->origin_translation = Pointf3(0,0,0); + this->invalidate_bounding_box(); } @@ -790,7 +793,6 @@ ModelObject::rotate(double angle, const Axis &axis) this->apply_transformation(trafo); - this->origin_translation = Pointf3(0,0,0); this->invalidate_bounding_box(); } @@ -805,7 +807,6 @@ ModelObject::rotate(double angle, const Vectorf3 &axis) this->apply_transformation(trafo); - this->origin_translation = Pointf3(0,0,0); this->invalidate_bounding_box(); } @@ -819,7 +820,6 @@ ModelObject::rotate(const Vectorf3 &origin, const Vectorf3 &target) this->apply_transformation(trafo); - this->origin_translation = Pointf3(0,0,0); this->invalidate_bounding_box(); } @@ -832,7 +832,6 @@ ModelObject::mirror(const Axis &axis) this->apply_transformation(trafo); - this->origin_translation = Pointf3(0,0,0); this->invalidate_bounding_box(); } @@ -849,6 +848,7 @@ TransformationMatrix ModelObject::get_undo_trafo() const void ModelObject::apply_transformation(const TransformationMatrix & trafo) { + this->trafo_obj.applyLeft(trafo); this->trafo_undo_stack.applyLeft(trafo); for (ModelVolumePtrs::const_iterator v = this->volumes.begin(); v != this->volumes.end(); ++v) { (*v)->apply_transformation(trafo); @@ -886,7 +886,6 @@ ModelObject::transform_by_instance(ModelInstance instance, bool dont_translate) for (ModelInstance* i : this->instances) { i->set_complete_trafo(i->get_trafo_matrix().multiplyRight(temp_trafo)); } - this->origin_translation = Pointf3(0,0,0); this->invalidate_bounding_box(); } diff --git a/xs/src/libslic3r/Model.hpp b/xs/src/libslic3r/Model.hpp index cdad0fc8a..0f60976e9 100644 --- a/xs/src/libslic3r/Model.hpp +++ b/xs/src/libslic3r/Model.hpp @@ -277,12 +277,6 @@ class ModelObject int part_number; ///< It's used for the 3MF items part numbers in the build element. LayerHeightSpline layer_height_spline; ///< Spline based variations of layer thickness for interactive user manipulation - Pointf3 origin_translation; - ///< This vector accumulates the total translation applied to the object by the - ///< center_around_origin() method. Callers might want to apply the same translation - ///< to new volumes before adding them to this object in order to preserve alignment - ///< when user expects that. - // these should be private but we need to expose them via XS until all methods are ported BoundingBoxf3 _bounding_box; bool _bounding_box_valid; @@ -336,6 +330,17 @@ class ModelObject /// Repair all TriangleMesh objects found in each ModelVolume. void repair(); + Pointf3 origin_translation() const; + ///< This vector returns the total translation applied to the object by the + ///< transformation methods. Callers might want to apply the same translation + ///< to new volumes before adding them to this object in order to preserve alignment + ///< when user expects that. + + TransformationMatrix get_trafo_obj() const {return this->trafo_obj;}; + /// return a copy of the private trafo_obj, that accumulates all top-level transformations + + void set_trafo_obj(TransformationMatrix const & trafo) {this->trafo_obj = trafo;}; + /// Flatten all volumes and instances into a single mesh and applying all the ModelInstances transformations. TriangleMesh mesh() const; @@ -465,6 +470,9 @@ class ModelObject /// Trafo to collect the transformation applied to all volumes over a series of manipulations TransformationMatrix trafo_undo_stack; + /// Trafo that accumulates all transformations applied to the whole object + TransformationMatrix trafo_obj; + /// = Operator overloading /// \param other ModelObject the other ModelObject to be copied /// \return ModelObject& the current ModelObject to enable operator cascading diff --git a/xs/t/19_model.t b/xs/t/19_model.t index d6f6d97a1..24a3a9552 100644 --- a/xs/t/19_model.t +++ b/xs/t/19_model.t @@ -4,15 +4,12 @@ use strict; use warnings; use Slic3r::XS; -use Test::More tests => 4; +use Test::More tests => 2; { my $model = Slic3r::Model->new; my $object = $model->_add_object; isa_ok $object, 'Slic3r::Model::Object::Ref'; - isa_ok $object->origin_translation, 'Slic3r::Pointf3::Ref'; - $object->origin_translation->translate(10,0,0); - is_deeply \@{$object->origin_translation}, [10,0,0], 'origin_translation is modified by ref'; my $lhr = [ [ 5, 10, 0.1 ] ]; $object->set_layer_height_ranges($lhr); diff --git a/xs/xsp/Model.xsp b/xs/xsp/Model.xsp index 5e9740613..157a8c9f6 100644 --- a/xs/xsp/Model.xsp +++ b/xs/xsp/Model.xsp @@ -215,10 +215,14 @@ ModelMaterial::attributes() void set_layer_height_spline(LayerHeightSpline* spline) %code%{ THIS->layer_height_spline = *spline; %}; - Ref origin_translation() - %code%{ RETVAL = &THIS->origin_translation; %}; - void set_origin_translation(Pointf3* point) - %code%{ THIS->origin_translation = *point; %}; + Clone origin_translation() + %code%{ RETVAL = THIS->origin_translation(); %}; + + Clone get_trafo_obj() + %code%{ RETVAL = THIS->get_trafo_obj(); %}; + + void set_trafo_obj(TransformationMatrix* trafo) + %code%{ THIS->set_trafo_obj(*trafo); %}; bool needed_repair() const; int materials_count() const;