From 9b12d6037ade34504a058b76be3bfdc0e887d9e1 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Mon, 9 Sep 2013 23:38:49 +0200 Subject: [PATCH] Ported TriangleMesh::scale_xyz() --- xs/src/TriangleMesh.cpp | 9 +++++++++ xs/src/TriangleMesh.hpp | 1 + xs/src/admesh/stl.h | 1 + xs/src/admesh/util.c | 38 ++++++++++++++++++++++++-------------- xs/t/01_trianglemesh.t | 10 +++++++--- xs/xsp/TriangleMesh.xsp | 2 ++ xs/xsp/typemap.xspt | 1 + 7 files changed, 45 insertions(+), 17 deletions(-) diff --git a/xs/src/TriangleMesh.cpp b/xs/src/TriangleMesh.cpp index db2083e59..894e0d77f 100644 --- a/xs/src/TriangleMesh.cpp +++ b/xs/src/TriangleMesh.cpp @@ -126,6 +126,15 @@ void TriangleMesh::scale(float factor) stl_scale(&(this->stl), factor); } +void TriangleMesh::scale(std::vector versor) +{ + float fversor[3]; + fversor[0] = versor[0]; + fversor[1] = versor[1]; + fversor[2] = versor[2]; + stl_scale(&this->stl, fversor); +} + void TriangleMesh::translate(float x, float y, float z) { stl_translate(&(this->stl), x, y, z); diff --git a/xs/src/TriangleMesh.hpp b/xs/src/TriangleMesh.hpp index 973371d7c..e3cda215d 100644 --- a/xs/src/TriangleMesh.hpp +++ b/xs/src/TriangleMesh.hpp @@ -23,6 +23,7 @@ class TriangleMesh void repair(); void WriteOBJFile(char* output_file); void scale(float factor); + void scale(std::vector versor); void translate(float x, float y, float z); void align_to_origin(); void rotate(double angle, Point* center); diff --git a/xs/src/admesh/stl.h b/xs/src/admesh/stl.h index d5a1286aa..04aa8220d 100644 --- a/xs/src/admesh/stl.h +++ b/xs/src/admesh/stl.h @@ -155,6 +155,7 @@ extern void stl_fix_normal_directions(stl_file *stl); extern void stl_fix_normal_values(stl_file *stl); extern void stl_reverse_all_facets(stl_file *stl); extern void stl_translate(stl_file *stl, float x, float y, float z); +extern void stl_scale(stl_file *stl, float versor[3]); extern void stl_scale(stl_file *stl, float factor); extern void stl_rotate_x(stl_file *stl, float angle); extern void stl_rotate_y(stl_file *stl, float angle); diff --git a/xs/src/admesh/util.c b/xs/src/admesh/util.c index 332b21ce6..65a226aba 100644 --- a/xs/src/admesh/util.c +++ b/xs/src/admesh/util.c @@ -102,42 +102,52 @@ stl_translate(stl_file *stl, float x, float y, float z) } void -stl_scale(stl_file *stl, float factor) +stl_scale(stl_file *stl, float versor[3]) { int i; int j; // scale extents - stl->stats.min.x *= factor; - stl->stats.min.y *= factor; - stl->stats.min.z *= factor; - stl->stats.max.x *= factor; - stl->stats.max.y *= factor; - stl->stats.max.z *= factor; + stl->stats.min.x *= versor[0]; + stl->stats.min.y *= versor[1]; + stl->stats.min.z *= versor[2]; + stl->stats.max.x *= versor[0]; + stl->stats.max.y *= versor[1]; + stl->stats.max.z *= versor[2]; // scale size - stl->stats.size.x *= factor; - stl->stats.size.y *= factor; - stl->stats.size.z *= factor; + stl->stats.size.x *= versor[0]; + stl->stats.size.y *= versor[1]; + stl->stats.size.z *= versor[2]; // scale volume if (stl->stats.volume > 0.0) { - stl->stats.volume *= (factor * factor * factor); + stl->stats.volume *= (versor[0] * versor[1] * versor[2]); } for(i = 0; i < stl->stats.number_of_facets; i++) { for(j = 0; j < 3; j++) { - stl->facet_start[i].vertex[j].x *= factor; - stl->facet_start[i].vertex[j].y *= factor; - stl->facet_start[i].vertex[j].z *= factor; + stl->facet_start[i].vertex[j].x *= versor[0]; + stl->facet_start[i].vertex[j].y *= versor[1]; + stl->facet_start[i].vertex[j].z *= versor[2]; } } stl_invalidate_shared_vertices(stl); } +void +stl_scale(stl_file *stl, float factor) +{ + float versor[3]; + versor[0] = factor; + versor[1] = factor; + versor[2] = factor; + stl_scale(stl, versor); +} + static void calculate_normals(stl_file *stl) { long i; diff --git a/xs/t/01_trianglemesh.t b/xs/t/01_trianglemesh.t index 3d9521a68..0b42b977f 100644 --- a/xs/t/01_trianglemesh.t +++ b/xs/t/01_trianglemesh.t @@ -4,7 +4,7 @@ use strict; use warnings; use Slic3r::XS; -use Test::More tests => 47; +use Test::More tests => 48; is Slic3r::TriangleMesh::XS::hello_world(), 'Hello world!', 'hello world'; @@ -32,14 +32,18 @@ my $cube = { $m->scale(2); ok abs($m->stats->{volume} - 40*40*40) < 1E-2, 'scale'; + $m->scale_xyz([2,1,1]); + ok abs($m->stats->{volume} - 2*40*40*40) < 1E-2, 'scale_xyz'; + $m->translate(5,10,0); - is_deeply $m->vertices->[0], [45,50,0], 'translate'; + is_deeply $m->vertices->[0], [85,50,0], 'translate'; $m->align_to_origin; is_deeply $m->vertices->[2], [0,0,0], 'align_to_origin'; - is_deeply $m->size, [40,40,40], 'size'; + is_deeply $m->size, [80,40,40], 'size'; + $m->scale_xyz([0.5,1,1]); $m->rotate(45, Slic3r::Point->new(20,20)); ok abs($m->size->[0] - sqrt(2)*40) < 1E-4, 'rotate'; diff --git a/xs/xsp/TriangleMesh.xsp b/xs/xsp/TriangleMesh.xsp index aebc8453e..b527216f7 100644 --- a/xs/xsp/TriangleMesh.xsp +++ b/xs/xsp/TriangleMesh.xsp @@ -13,6 +13,8 @@ void repair(); void WriteOBJFile(char* output_file); void scale(float factor); + void scale_xyz(std::vector versor) + %code{% THIS->scale(versor); %}; void translate(float x, float y, float z); void align_to_origin(); void rotate(double angle, Point* center); diff --git a/xs/xsp/typemap.xspt b/xs/xsp/typemap.xspt index 9a1e79314..2927404dd 100644 --- a/xs/xsp/typemap.xspt +++ b/xs/xsp/typemap.xspt @@ -1,6 +1,7 @@ %typemap{bool}{simple}; %typemap{std::string}; %typemap{std::vector*}; +%typemap{std::vector}; %typemap{std::vector*}; %typemap{SV*}; %typemap{AV*};