diff --git a/lib/Slic3r/Geometry.pm b/lib/Slic3r/Geometry.pm index 42fab2d89..fec771561 100644 --- a/lib/Slic3r/Geometry.pm +++ b/lib/Slic3r/Geometry.pm @@ -27,9 +27,6 @@ our @EXPORT_OK = qw( use constant PI => 4 * atan2(1, 1); use constant A => 0; use constant B => 1; -use constant X => 0; -use constant Y => 1; -use constant Z => 2; use constant X1 => 0; use constant Y1 => 1; use constant X2 => 2; diff --git a/lib/Slic3r/Model.pm b/lib/Slic3r/Model.pm index 1a2f62afd..099f6461c 100644 --- a/lib/Slic3r/Model.pm +++ b/lib/Slic3r/Model.pm @@ -262,37 +262,6 @@ sub add_instance { } } -sub rotate { - my ($self, $angle, $axis) = @_; - - # we accept angle in radians but mesh currently uses degrees - $angle = rad2deg($angle); - - if ($axis == X) { - $_->mesh->rotate_x($angle) for @{$self->volumes}; - } elsif ($axis == Y) { - $_->mesh->rotate_y($angle) for @{$self->volumes}; - } elsif ($axis == Z) { - $_->mesh->rotate_z($angle) for @{$self->volumes}; - } - $self->set_origin_translation(Slic3r::Pointf3->new(0,0,0)); - $self->invalidate_bounding_box; -} - -sub flip { - my ($self, $axis) = @_; - - if ($axis == X) { - $_->mesh->flip_x for @{$self->volumes}; - } elsif ($axis == Y) { - $_->mesh->flip_y for @{$self->volumes}; - } elsif ($axis == Z) { - $_->mesh->flip_z for @{$self->volumes}; - } - $self->set_origin_translation(Slic3r::Pointf3->new(0,0,0)); - $self->invalidate_bounding_box; -} - sub mesh_stats { my $self = shift; diff --git a/xs/src/libslic3r/Model.cpp b/xs/src/libslic3r/Model.cpp index d03a2916c..6fafe9bf0 100644 --- a/xs/src/libslic3r/Model.cpp +++ b/xs/src/libslic3r/Model.cpp @@ -1,4 +1,5 @@ #include "Model.hpp" +#include "Geometry.hpp" namespace Slic3r { @@ -514,6 +515,29 @@ ModelObject::scale(const Pointf3 &versor) this->invalidate_bounding_box(); } +void +ModelObject::rotate(float angle, const Axis &axis) +{ + // we accept angle in radians but mesh currently uses degrees + angle = Slic3r::Geometry::rad2deg(angle); + + for (ModelVolumePtrs::const_iterator v = this->volumes.begin(); v != this->volumes.end(); ++v) { + (*v)->mesh.rotate(angle, axis); + } + this->origin_translation = Pointf3(0,0,0); + this->invalidate_bounding_box(); +} + +void +ModelObject::flip(const Axis &axis) +{ + for (ModelVolumePtrs::const_iterator v = this->volumes.begin(); v != this->volumes.end(); ++v) { + (*v)->mesh.flip(axis); + } + this->origin_translation = Pointf3(0,0,0); + this->invalidate_bounding_box(); +} + size_t ModelObject::materials_count() const { diff --git a/xs/src/libslic3r/Model.hpp b/xs/src/libslic3r/Model.hpp index 2ec7c5b08..edce23d9c 100644 --- a/xs/src/libslic3r/Model.hpp +++ b/xs/src/libslic3r/Model.hpp @@ -129,6 +129,8 @@ class ModelObject void translate(const Vectorf3 &vector); void translate(coordf_t x, coordf_t y, coordf_t z); void scale(const Pointf3 &versor); + void rotate(float angle, const Axis &axis); + void flip(const Axis &axis); size_t materials_count() const; size_t facets_count() const; bool needed_repair() const; diff --git a/xs/src/libslic3r/TriangleMesh.cpp b/xs/src/libslic3r/TriangleMesh.cpp index b25df7e0e..e8c8ef781 100644 --- a/xs/src/libslic3r/TriangleMesh.cpp +++ b/xs/src/libslic3r/TriangleMesh.cpp @@ -200,40 +200,58 @@ void TriangleMesh::translate(float x, float y, float z) stl_invalidate_shared_vertices(&this->stl); } +void TriangleMesh::rotate(float angle, const Axis &axis) +{ + if (axis == X) { + stl_rotate_x(&(this->stl), angle); + } else if (axis == Y) { + stl_rotate_y(&(this->stl), angle); + } else if (axis == Z) { + stl_rotate_z(&(this->stl), angle); + } + stl_invalidate_shared_vertices(&this->stl); +} + void TriangleMesh::rotate_x(float angle) { - stl_rotate_x(&(this->stl), angle); - stl_invalidate_shared_vertices(&this->stl); + this->rotate(angle, X); } void TriangleMesh::rotate_y(float angle) { - stl_rotate_y(&(this->stl), angle); - stl_invalidate_shared_vertices(&this->stl); + this->rotate(angle, Y); } void TriangleMesh::rotate_z(float angle) { - stl_rotate_z(&(this->stl), angle); + this->rotate(angle, Z); +} + +void TriangleMesh::flip(const Axis &axis) +{ + if (axis == X) { + stl_mirror_yz(&this->stl); + } else if (axis == Y) { + stl_mirror_xz(&this->stl); + } else if (axis == Z) { + stl_mirror_xy(&this->stl); + } stl_invalidate_shared_vertices(&this->stl); } void TriangleMesh::flip_x() { - stl_mirror_yz(&this->stl); - stl_invalidate_shared_vertices(&this->stl); + this->flip(X); } void TriangleMesh::flip_y() { - stl_mirror_xz(&this->stl); - stl_invalidate_shared_vertices(&this->stl); + this->flip(Y); } void TriangleMesh::flip_z() { - stl_mirror_xy(&this->stl); - stl_invalidate_shared_vertices(&this->stl); + this->flip(Z); } void TriangleMesh::align_to_origin() diff --git a/xs/src/libslic3r/TriangleMesh.hpp b/xs/src/libslic3r/TriangleMesh.hpp index 43ac68419..ff12e8427 100644 --- a/xs/src/libslic3r/TriangleMesh.hpp +++ b/xs/src/libslic3r/TriangleMesh.hpp @@ -32,9 +32,11 @@ class TriangleMesh void scale(float factor); void scale(const Pointf3 &versor); void translate(float x, float y, float z); + void rotate(float angle, const Axis &axis); void rotate_x(float angle); void rotate_y(float angle); void rotate_z(float angle); + void flip(const Axis &axis); void flip_x(); void flip_y(); void flip_z(); diff --git a/xs/src/libslic3r/libslic3r.h b/xs/src/libslic3r/libslic3r.h index 25d3c7f48..45c42c7f9 100644 --- a/xs/src/libslic3r/libslic3r.h +++ b/xs/src/libslic3r/libslic3r.h @@ -17,7 +17,12 @@ typedef long coord_t; typedef double coordf_t; -namespace Slic3r {} +namespace Slic3r { + +// TODO: make sure X = 0 +enum Axis { X, Y, Z }; + +} using namespace Slic3r; /* Implementation of CONFESS("foo"): */ diff --git a/xs/xsp/Geometry.xsp b/xs/xsp/Geometry.xsp index 0df74a4bd..98d5bcdd5 100644 --- a/xs/xsp/Geometry.xsp +++ b/xs/xsp/Geometry.xsp @@ -87,4 +87,16 @@ simplify_polygons(polygons, tolerance) OUTPUT: RETVAL +IV +_constant() + ALIAS: + X = X + Y = Y + Z = Z + PROTOTYPE: + CODE: + RETVAL = ix; + OUTPUT: RETVAL + %} + diff --git a/xs/xsp/Model.xsp b/xs/xsp/Model.xsp index 9870d7dd5..cf9b974f4 100644 --- a/xs/xsp/Model.xsp +++ b/xs/xsp/Model.xsp @@ -182,6 +182,8 @@ 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 flip(Axis axis); Model* cut(double z) %code%{ diff --git a/xs/xsp/my.map b/xs/xsp/my.map index 37a4594d2..da9d3adc7 100644 --- a/xs/xsp/my.map +++ b/xs/xsp/my.map @@ -184,6 +184,7 @@ Clone O_OBJECT_SLIC3R_T GLVertexArray* O_OBJECT_SLIC3R +Axis T_UV ExtrusionLoopRole T_UV ExtrusionRole T_UV FlowRole T_UV diff --git a/xs/xsp/typemap.xspt b/xs/xsp/typemap.xspt index 6d3f92998..afc256d13 100644 --- a/xs/xsp/typemap.xspt +++ b/xs/xsp/typemap.xspt @@ -173,6 +173,12 @@ %typemap{SupportLayerPtrs*}; +%typemap{Axis}{parsed}{ + %cpp_type{Axis}; + %precall_code{% + $CVar = (Axis)SvUV($PerlVar); + %}; +}; %typemap{SurfaceType}{parsed}{ %cpp_type{SurfaceType}; %precall_code{%