From 63ac0a1d0c8018dd4e8efb76d2e33efbf87f91f9 Mon Sep 17 00:00:00 2001 From: Michael Kirsch Date: Sun, 26 May 2019 19:21:17 +0200 Subject: [PATCH] apply to trafo functions to volume, add vec to vec rotation for object --- xs/src/libslic3r/Model.cpp | 19 +++++++++++++++---- xs/src/libslic3r/Model.hpp | 33 +++++++++++++++++++++++++++++++++ xs/xsp/Model.xsp | 16 +++++++++++++--- 3 files changed, 61 insertions(+), 7 deletions(-) diff --git a/xs/src/libslic3r/Model.cpp b/xs/src/libslic3r/Model.cpp index e9f0965a9..92c58ed67 100644 --- a/xs/src/libslic3r/Model.cpp +++ b/xs/src/libslic3r/Model.cpp @@ -731,7 +731,7 @@ void ModelObject::translate(coordf_t x, coordf_t y, coordf_t z) { for (ModelVolumePtrs::const_iterator v = this->volumes.begin(); v != this->volumes.end(); ++v) { - (*v)->trafo.translate(x, y, z); + (*v)->translate(x, y, z); } if (this->_bounding_box_valid) this->_bounding_box.translate(x, y, z); } @@ -747,7 +747,7 @@ ModelObject::scale(const Pointf3 &versor) { if (versor.x == 1 && versor.y == 1 && versor.z == 1) return; for (ModelVolumePtrs::const_iterator v = this->volumes.begin(); v != this->volumes.end(); ++v) { - (*v)->trafo.scale(versor.x, versor.y, versor.z); + (*v)->scale(versor); } // reset origin translation since it doesn't make sense anymore @@ -774,7 +774,18 @@ ModelObject::rotate(double angle, const Axis &axis) { if (angle == 0) return; for (ModelVolumePtrs::const_iterator v = this->volumes.begin(); v != this->volumes.end(); ++v) { - (*v)->trafo.rotate(angle, axis); + (*v)->rotate(angle, axis); + } + this->origin_translation = Pointf3(0,0,0); + this->invalidate_bounding_box(); +} + +void +ModelObject::rotate(const Vectorf3 &origin, const Vectorf3 &target) +{ + TransformationMatrix trafo = TransformationMatrix::mat_rotation(origin, target); + for (ModelVolumePtrs::const_iterator v = this->volumes.begin(); v != this->volumes.end(); ++v) { + (*v)->apply(trafo); } this->origin_translation = Pointf3(0,0,0); this->invalidate_bounding_box(); @@ -784,7 +795,7 @@ void ModelObject::mirror(const Axis &axis) { for (ModelVolumePtrs::const_iterator v = this->volumes.begin(); v != this->volumes.end(); ++v) { - (*v)->trafo.mirror(axis); + (*v)->mirror(axis); } 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 d0c4d6517..6360454ec 100644 --- a/xs/src/libslic3r/Model.hpp +++ b/xs/src/libslic3r/Model.hpp @@ -388,6 +388,11 @@ class ModelObject /// \param axis Axis the axis to be rotated around void rotate(double angle, const Axis &axis); + /// Rotate the current ModelObject by rotating ModelVolumes to align the given vectors + /// \param origin Vectorf3 + /// \param target Vectorf3 + void rotate(const Vectorf3 &origin, const Vectorf3 &target); + /// Mirror the current Model around a certain axis. /// \param axis Axis enum member void mirror(const Axis &axis); @@ -486,6 +491,34 @@ class ModelVolume BoundingBoxf3 get_transformed_bounding_box(TransformationMatrix const * additional_trafo = nullptr) const; + //Transformation matrix manipulators + + /// performs translation + void translate(double x, double y, double z) { this->trafo.translate(x,y,z); }; + void translate(Vectorf3 const &vector) { this->trafo.translate(vector); }; + void translateXY(Vectorf const &vector) { this->trafo.translateXY(vector); }; + + /// performs uniform scale + void scale(double factor) { this->trafo.scale(factor); }; + + /// performs per-axis scale + void scale(double x, double y, double z) { this->trafo.scale(x,y,z); }; + + /// performs per-axis scale via vector + void scale(Vectorf3 const &vector) { this->trafo.scale(vector); }; + + /// performs mirroring along given axis + void mirror(const Axis &axis) { this->trafo.mirror(axis); }; + + /// performs mirroring along given axis + void mirror(const Vectorf3 &normal) { this->trafo.mirror(normal); }; + + /// performs rotation around given axis + void rotate(double angle_rad, const Axis &axis) { this->trafo.rotate(angle_rad,axis); }; + + /// apply whichever matrix is supplied, multiplied from the left + void apply(TransformationMatrix const &trafo) { this->trafo.applyLeft(trafo); }; + /// Get the material id of this ModelVolume object /// \return t_model_material_id the material id string t_model_material_id material_id() const; diff --git a/xs/xsp/Model.xsp b/xs/xsp/Model.xsp index 75692ec9d..fab5bdcc3 100644 --- a/xs/xsp/Model.xsp +++ b/xs/xsp/Model.xsp @@ -228,8 +228,12 @@ ModelMaterial::attributes() void translate(double x, double y, double z); void scale_xyz(Pointf3* versor) %code{% THIS->scale(*versor); %}; - void rotate(float angle, Axis axis); + void rotate(double angle, Axis axis); + void rotate_vec_to_vec(Pointf3* origin, Pointf3* target) + %code{% THIS->rotate(*origin, *target); %}; void mirror(Axis axis); + + void transform_by_instance(ModelInstance* instance, bool dont_translate = false) %code{% THIS->transform_by_instance(*instance, dont_translate); %}; @@ -280,11 +284,17 @@ ModelMaterial::attributes() Clone bounding_box() %code%{ try { - RETVAL = THIS->mesh.bounding_box(); + RETVAL = THIS->get_transformed_bounding_box(NULL); } catch (std::exception& e) { croak("%s", e.what()); } %}; + + void translate(double x, double y, double z); + void scale_xyz(Pointf3* versor) + %code{% THIS->scale(*versor); %}; + void rotate(double angle, Axis axis); + Ref config() %code%{ RETVAL = &THIS->config; %}; @@ -297,7 +307,7 @@ ModelMaterial::attributes() THIS->transformed_mesh = THIS->get_transformed_mesh(&trafo); RETVAL = &THIS->transformed_mesh; %}; - + bool modifier() %code%{ RETVAL = THIS->modifier; %}; void set_modifier(bool modifier)