From ad7a1696f03df5ff53bb554ad861aed921de94a0 Mon Sep 17 00:00:00 2001 From: Michael Kirsch Date: Tue, 16 Jul 2019 21:12:42 +0200 Subject: [PATCH] reinstate direct mesh manipulation --- xs/src/libslic3r/TriangleMesh.cpp | 129 ++++++++++++++++++++++++++++-- xs/src/libslic3r/TriangleMesh.hpp | 34 +++++++- xs/xsp/TriangleMesh.xsp | 15 ++++ 3 files changed, 170 insertions(+), 8 deletions(-) diff --git a/xs/src/libslic3r/TriangleMesh.cpp b/xs/src/libslic3r/TriangleMesh.cpp index 01e07054b..45f9cdbab 100644 --- a/xs/src/libslic3r/TriangleMesh.cpp +++ b/xs/src/libslic3r/TriangleMesh.cpp @@ -280,6 +280,129 @@ TriangleMesh::WriteOBJFile(const std::string &output_file) const { #endif } +void TriangleMesh::scale(float factor) +{ + stl_scale(&(this->stl), factor); + stl_invalidate_shared_vertices(&this->stl); +} + +void TriangleMesh::scale(const Pointf3 &versor) +{ + float fversor[3]; + fversor[0] = versor.x; + fversor[1] = versor.y; + fversor[2] = versor.z; + stl_scale_versor(&this->stl, fversor); + stl_invalidate_shared_vertices(&this->stl); +} + +void TriangleMesh::translate(float x, float y, float z) +{ + stl_translate_relative(&(this->stl), x, y, z); + stl_invalidate_shared_vertices(&this->stl); +} + +void TriangleMesh::translate(Pointf3 vec) { + this->translate( + static_cast(vec.x), + static_cast(vec.y), + static_cast(vec.z) + ); +} + +void TriangleMesh::rotate(float angle, const Axis &axis) +{ + // admesh uses degrees + angle = Slic3r::Geometry::rad2deg(angle); + + 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) +{ + this->rotate(angle, X); +} + +void TriangleMesh::rotate_y(float angle) +{ + this->rotate(angle, Y); +} + +void TriangleMesh::rotate_z(float angle) +{ + this->rotate(angle, Z); +} + +void TriangleMesh::mirror(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::mirror_x() +{ + this->mirror(X); +} + +void TriangleMesh::mirror_y() +{ + this->mirror(Y); +} + +void TriangleMesh::mirror_z() +{ + this->mirror(Z); +} + +void TriangleMesh::align_to_origin() +{ + this->translate( + -(this->stl.stats.min.x), + -(this->stl.stats.min.y), + -(this->stl.stats.min.z) + ); +} + +void TriangleMesh::center_around_origin() +{ + this->align_to_origin(); + this->translate( + -(this->stl.stats.size.x/2), + -(this->stl.stats.size.y/2), + -(this->stl.stats.size.z/2) + ); +} + +void TriangleMesh::rotate(double angle, Point* center) +{ + this->rotate(angle, *center); +} +void TriangleMesh::rotate(double angle, const Point& center) +{ + this->translate(-center.x, -center.y, 0); + stl_rotate_z(&(this->stl), (float)angle); + this->translate(+center.x, +center.y, 0); +} + +void TriangleMesh::align_to_bed() +{ + stl_translate_relative(&(this->stl), 0.0f, 0.0f, -this->stl.stats.min.z); + stl_invalidate_shared_vertices(&this->stl); +} + TriangleMesh TriangleMesh::get_transformed_mesh(TransformationMatrix const & trafo) const { TriangleMesh mesh; @@ -296,12 +419,6 @@ void TriangleMesh::transform(TransformationMatrix const & trafo) stl_invalidate_shared_vertices(&(this->stl)); } -void TriangleMesh::align_to_bed() -{ - stl_translate_relative(&(this->stl), 0.0f, 0.0f, -this->stl.stats.min.z); - stl_invalidate_shared_vertices(&this->stl); -} - Pointf3s TriangleMesh::vertices() { Pointf3s tmp {}; diff --git a/xs/src/libslic3r/TriangleMesh.hpp b/xs/src/libslic3r/TriangleMesh.hpp index 7eb46013f..3b1702f93 100644 --- a/xs/src/libslic3r/TriangleMesh.hpp +++ b/xs/src/libslic3r/TriangleMesh.hpp @@ -63,12 +63,42 @@ class TriangleMesh bool is_manifold() const; void WriteOBJFile(const std::string &output_file) const; - TriangleMesh get_transformed_mesh(TransformationMatrix const & trafo) const; + /// Direct manipulators + + void scale(float factor); + void scale(const Pointf3 &versor); + + /// Translate the mesh to a new location. + void translate(float x, float y, float z); + + /// Translate the mesh to a new location. + void translate(Pointf3 vec); + + + void rotate(float angle, const Axis &axis); + void rotate_x(float angle); + void rotate_y(float angle); + void rotate_z(float angle); + void mirror(const Axis &axis); + void mirror_x(); + void mirror_y(); + void mirror_z(); + void align_to_origin(); + void center_around_origin(); + + /// Rotate angle around a specified point. + void rotate(double angle, const Point& center); + void rotate(double angle, Point* center); - void transform(TransformationMatrix const & trafo); void align_to_bed(); + + /// Matrix manipulators + TriangleMesh get_transformed_mesh(TransformationMatrix const & trafo) const; + void transform(TransformationMatrix const & trafo); + + TriangleMeshPtrs split() const; TriangleMeshPtrs cut_by_grid(const Pointf &grid) const; void merge(const TriangleMesh &mesh); diff --git a/xs/xsp/TriangleMesh.xsp b/xs/xsp/TriangleMesh.xsp index 6c58367ba..1af74c7f1 100644 --- a/xs/xsp/TriangleMesh.xsp +++ b/xs/xsp/TriangleMesh.xsp @@ -17,11 +17,26 @@ void repair(); float volume(); void WriteOBJFile(std::string output_file); + + void scale(float factor); + void scale_xyz(Pointf3* versor) + %code{% THIS->scale(*versor); %}; + void translate(float x, float y, float z); + void rotate_x(float angle); + void rotate_y(float angle); + void rotate_z(float angle); + void mirror_x(); + void mirror_y(); + void mirror_z(); + void align_to_origin(); + void rotate(double angle, Point* center); void align_to_bed(); + void transform(TransformationMatrix* trafo) %code{% THIS->transform(*trafo); %}; Clone get_transformed_mesh(TransformationMatrix* trafo) %code{% RETVAL=THIS->get_transformed_mesh(*trafo); %}; + TriangleMeshPtrs split(); TriangleMeshPtrs cut_by_grid(Pointf* grid) %code{% RETVAL = THIS->cut_by_grid(*grid); %};