From b1ad466189a93f5003528dcec7628c6fba794b8f Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Sun, 14 Jul 2013 00:38:01 +0200 Subject: [PATCH] Ported Slic3r::ExPolygon::Collection to XS --- lib/Slic3r.pm | 1 + lib/Slic3r/ExPolygon.pm | 34 ++-------------------- lib/Slic3r/GUI/Plater.pm | 42 +++++++++++++-------------- xs/lib/Slic3r/XS.pm | 6 ++++ xs/src/ExPolygon.hpp | 27 +++++++++++++++++ xs/src/ExPolygonCollection.hpp | 53 ++++++++++++++++++++++++++++++++++ xs/t/04_expolygon.t | 13 ++++++++- xs/xsp/ExPolygon.xsp | 16 ++-------- xs/xsp/ExPolygonCollection.xsp | 48 ++++++++++++++++++++++++++++++ xs/xsp/my.map | 1 + 10 files changed, 173 insertions(+), 68 deletions(-) create mode 100644 xs/src/ExPolygonCollection.hpp create mode 100644 xs/xsp/ExPolygonCollection.xsp diff --git a/lib/Slic3r.pm b/lib/Slic3r.pm index a8b0a5409..2c2883574 100644 --- a/lib/Slic3r.pm +++ b/lib/Slic3r.pm @@ -110,6 +110,7 @@ sub parallelize { sub thread_cleanup { # prevent destruction of shared objects no warnings 'redefine'; + *Slic3r::ExPolygon::Collection::DESTROY = sub {}; *Slic3r::ExPolygon::XS::DESTROY = sub {}; *Slic3r::Point::XS::DESTROY = sub {}; } diff --git a/lib/Slic3r/ExPolygon.pm b/lib/Slic3r/ExPolygon.pm index 19b3a90b6..8330bb7f2 100644 --- a/lib/Slic3r/ExPolygon.pm +++ b/lib/Slic3r/ExPolygon.pm @@ -301,47 +301,19 @@ package Slic3r::ExPolygon::XS; use base 'Slic3r::ExPolygon'; package Slic3r::ExPolygon::Collection; -use Moo; use Slic3r::Geometry qw(X1 Y1); -has 'expolygons' => (is => 'ro', default => sub { [] }); - -sub clone { - my $self = shift; - return (ref $self)->new( - expolygons => [ map $_->clone, @{$self->expolygons} ], - ); -} - sub align_to_origin { my $self = shift; - my @bb = Slic3r::Geometry::bounding_box([ map @$_, map @$_, @{$self->expolygons} ]); - $_->translate(-$bb[X1], -$bb[Y1]) for @{$self->expolygons}; - $self; -} - -sub scale { - my $self = shift; - $_->scale(@_) for @{$self->expolygons}; - $self; -} - -sub rotate { - my $self = shift; - $_->rotate(@_) for @{$self->expolygons}; - $self; -} - -sub translate { - my $self = shift; - $_->translate(@_) for @{$self->expolygons}; + my @bb = Slic3r::Geometry::bounding_box([ map @$_, map @$_, @$self ]); + $self->translate(-$bb[X1], -$bb[Y1]); $self; } sub size { my $self = shift; - return [ Slic3r::Geometry::size_2D([ map @$_, map @$_, @{$self->expolygons} ]) ]; + return [ Slic3r::Geometry::size_2D([ map @$_, map @$_, @$self ]) ]; } 1; diff --git a/lib/Slic3r/GUI/Plater.pm b/lib/Slic3r/GUI/Plater.pm index 8f62008bc..8df97c65b 100644 --- a/lib/Slic3r/GUI/Plater.pm +++ b/lib/Slic3r/GUI/Plater.pm @@ -161,6 +161,7 @@ sub new { my ($obj_idx, $thumbnail) = @{$event->GetData}; return if !$self->{objects}[$obj_idx]; # object was deleted before thumbnail generation completed $self->{objects}[$obj_idx]->thumbnail($thumbnail->clone); + $self->on_thumbnail_made($obj_idx); }); @@ -869,13 +870,12 @@ sub repaint { @{$parent->{object_previews}} = (); for my $obj_idx (0 .. $#{$parent->{objects}}) { my $object = $parent->{objects}[$obj_idx]; - next unless $object->thumbnail && @{$object->thumbnail->expolygons}; + next unless defined $object->thumbnail && @{$object->thumbnail}; for my $instance_idx (0 .. $#{$object->instances}) { my $instance = $object->instances->[$instance_idx]; - my $thumbnail = $object->transformed_thumbnail - ->clone - ->translate(map $parent->to_pixel($instance->[$_]) + $parent->{shift}[$_], (X,Y)); + my $thumbnail = $object->transformed_thumbnail->clone; + $thumbnail->translate(map $parent->to_pixel($instance->[$_]) + $parent->{shift}[$_], (X,Y)); push @{$parent->{object_previews}}, [ $obj_idx, $instance_idx, $thumbnail ]; @@ -887,11 +887,11 @@ sub repaint { } else { $dc->SetBrush($parent->{objects_brush}); } - $dc->DrawPolygon($parent->_y($_), 0, 0) for map $_->contour, @{ $parent->{object_previews}->[-1][2]->expolygons }; + $dc->DrawPolygon($parent->_y($_), 0, 0) for map $_->contour, @{ $parent->{object_previews}->[-1][2] }; # if sequential printing is enabled and we have more than one object if ($parent->{config}->complete_objects && (map @{$_->instances}, @{$parent->{objects}}) > 1) { - my $convex_hull = Slic3r::Polygon->new(@{convex_hull([ map @{$_->contour}, @{$parent->{object_previews}->[-1][2]->expolygons} ])}); + my $convex_hull = Slic3r::Polygon->new(@{convex_hull([ map @{$_->contour}, @{$parent->{object_previews}->[-1][2]} ])}); my ($clearance) = @{offset([$convex_hull], $parent->{config}->extruder_clearance_radius / 2 * $parent->{scaling_factor}, 100, JT_ROUND)}; $dc->SetPen($parent->{clearance_pen}); $dc->SetBrush($parent->{transparent_brush}); @@ -902,7 +902,7 @@ sub repaint { # draw skirt if (@{$parent->{object_previews}} && $parent->{config}->skirts) { - my $convex_hull = Slic3r::Polygon->new(@{convex_hull([ map @{$_->contour}, map @{$_->[2]->expolygons}, @{$parent->{object_previews}} ])}); + my $convex_hull = Slic3r::Polygon->new(@{convex_hull([ map @{$_->contour}, map @{$_->[2]}, @{$parent->{object_previews}} ])}); ($convex_hull) = @{offset([$convex_hull], $parent->{config}->skirt_distance * $parent->{scaling_factor}, 100, JT_ROUND)}; $dc->SetPen($parent->{skirt_pen}); $dc->SetBrush($parent->{transparent_brush}); @@ -924,7 +924,7 @@ sub mouse_event { $parent->selection_changed(0); for my $preview (@{$parent->{object_previews}}) { my ($obj_idx, $instance_idx, $thumbnail) = @$preview; - if (defined first { $_->contour->encloses_point($pos) } @{$thumbnail->expolygons}) { + if (defined first { $_->contour->encloses_point($pos) } @$thumbnail) { $parent->{selected_objects} = [ [$obj_idx, $instance_idx] ]; $parent->{list}->Select($obj_idx, 1); $parent->selection_changed(1); @@ -954,7 +954,7 @@ sub mouse_event { } elsif ($event->Moving) { my $cursor = wxSTANDARD_CURSOR; for my $preview (@{$parent->{object_previews}}) { - if (defined first { $_->contour->encloses_point($pos) } @{ $preview->[2]->expolygons }) { + if (defined first { $_->contour->encloses_point($pos) } @{ $preview->[2] }) { $cursor = Wx::Cursor->new(wxCURSOR_HAND); last; } @@ -1158,16 +1158,14 @@ sub make_thumbnail { my $mesh = $self->model_object->mesh; # $self->model_object is already aligned to origin my $thumbnail = Slic3r::ExPolygon::Collection->new( - expolygons => (@{$mesh->facets} <= 5000) - ? $mesh->horizontal_projection - : [ Slic3r::ExPolygon->new($self->convex_hull) ], + # only simplify expolygons larger than the threshold + grep @$_, + map { ($_->area >= 1) ? $_->simplify(0.5) : $_ } + (@{$mesh->facets} <= 5000) + ? @{$mesh->horizontal_projection} + : Slic3r::ExPolygon->new($self->convex_hull) ); - # only simplify expolygons larger than the threshold - @{$thumbnail->expolygons} = grep @$_, - map { ($_->area >= 1) ? $_->simplify(0.5) : $_ } - @{$thumbnail->expolygons}; - $self->thumbnail($thumbnail); # ignored in multi-threaded environments $self->free_model_object; @@ -1177,7 +1175,7 @@ sub make_thumbnail { sub _transform_thumbnail { my $self = shift; - return unless $self->thumbnail; + return unless defined $self->thumbnail; my $t = $self->_apply_transform($self->thumbnail); $t->scale($self->thumbnail_scaling_factor); @@ -1199,10 +1197,10 @@ sub _apply_transform { # the order of these transformations MUST be the same everywhere, including # in Slic3r::Print->add_model() - return $entity - ->clone - ->rotate(deg2rad($self->rotate), $self->bounding_box->center_2D) - ->scale($self->scale); + my $result = $entity->clone; + $result->rotate(deg2rad($self->rotate), Slic3r::Point::XS->new(@{$self->bounding_box->center_2D})); + $result->scale($self->scale); + return $result; } sub transformed_size { diff --git a/xs/lib/Slic3r/XS.pm b/xs/lib/Slic3r/XS.pm index b5c809f5a..c81690e72 100644 --- a/xs/lib/Slic3r/XS.pm +++ b/xs/lib/Slic3r/XS.pm @@ -26,4 +26,10 @@ sub rotate { $self->_rotate($angle, $center); } +package Slic3r::ExPolygon::Collection; +use overload + '@{}' => sub { $_[0]->arrayref }; + +sub clone { (ref $_[0])->_clone($_[0]) } + 1; diff --git a/xs/src/ExPolygon.hpp b/xs/src/ExPolygon.hpp index 7363a6a73..154969784 100644 --- a/xs/src/ExPolygon.hpp +++ b/xs/src/ExPolygon.hpp @@ -100,6 +100,33 @@ polygon2perl(Polygon& poly) { return sv_bless(newRV_noinc((SV*)av), gv_stashpv("Slic3r::Polygon", GV_ADD)); } +void +perl2expolygon(SV* expoly_sv, ExPolygon& expoly) +{ + AV* expoly_av = (AV*)SvRV(expoly_sv); + const unsigned int num_polygons = av_len(expoly_av)+1; + expoly.holes.resize(num_polygons-1); + + SV** polygon_sv = av_fetch(expoly_av, 0, 0); + perl2polygon(*polygon_sv, expoly.contour); + for (unsigned int i = 0; i < num_polygons-1; i++) { + polygon_sv = av_fetch(expoly_av, i+1, 0); + perl2polygon(*polygon_sv, expoly.holes[i]); + } +} + +SV* +expolygon2perl(ExPolygon& expoly) { + const unsigned int num_holes = expoly.holes.size(); + AV* av = newAV(); + av_extend(av, num_holes); // -1 +1 + av_store(av, 0, polygon2perl(expoly.contour)); + for (unsigned int i = 0; i < num_holes; i++) { + av_store(av, i+1, polygon2perl(expoly.holes[i])); + } + return sv_bless(newRV_noinc((SV*)av), gv_stashpv("Slic3r::ExPolygon", GV_ADD)); +} + } #endif diff --git a/xs/src/ExPolygonCollection.hpp b/xs/src/ExPolygonCollection.hpp new file mode 100644 index 000000000..127d5178f --- /dev/null +++ b/xs/src/ExPolygonCollection.hpp @@ -0,0 +1,53 @@ +#ifndef slic3r_ExPolygonCollection_hpp_ +#define slic3r_ExPolygonCollection_hpp_ + +extern "C" { +#include "EXTERN.h" +#include "perl.h" +#include "XSUB.h" +#include "ppport.h" +} + +#include "ExPolygon.hpp" + +namespace Slic3r { + +typedef std::vector ExPolygons; + +class ExPolygonCollection +{ + public: + ExPolygons expolygons; + SV* arrayref(); + void scale(double factor); + void translate(double x, double y); + void rotate(double angle, Point* center); +}; + +void +ExPolygonCollection::scale(double factor) +{ + for (ExPolygons::iterator it = expolygons.begin(); it != expolygons.end(); ++it) { + (*it).scale(factor); + } +} + +void +ExPolygonCollection::translate(double x, double y) +{ + for (ExPolygons::iterator it = expolygons.begin(); it != expolygons.end(); ++it) { + (*it).translate(x, y); + } +} + +void +ExPolygonCollection::rotate(double angle, Point* center) +{ + for (ExPolygons::iterator it = expolygons.begin(); it != expolygons.end(); ++it) { + (*it)._rotate(angle, center); + } +} + +} + +#endif diff --git a/xs/t/04_expolygon.t b/xs/t/04_expolygon.t index 0f2116c1f..a09df27b8 100644 --- a/xs/t/04_expolygon.t +++ b/xs/t/04_expolygon.t @@ -4,7 +4,7 @@ use strict; use warnings; use Slic3r::XS; -use Test::More tests => 9; +use Test::More tests => 11; use constant PI => 4 * atan2(1, 1); @@ -71,4 +71,15 @@ isa_ok $expolygon->[0][0], 'Slic3r::Point', 'Perl polygon points are blessed'; ], 'rotate around pure-Perl Point'; } +{ + my $expolygon2 = $expolygon->clone; + $expolygon2->scale(2); + my $collection = Slic3r::ExPolygon::Collection->new($expolygon->arrayref, $expolygon2->arrayref); + is_deeply [ @$collection ], [ $expolygon->arrayref, $expolygon2->arrayref ], + 'expolygon collection'; + my $collection2 = Slic3r::ExPolygon::Collection->new($expolygon, $expolygon2); + is_deeply [ @$collection ], [ @$collection2 ], + 'expolygon collection with XS expolygons'; +} + __END__ diff --git a/xs/xsp/ExPolygon.xsp b/xs/xsp/ExPolygon.xsp index 081b7ed25..fe7237008 100644 --- a/xs/xsp/ExPolygon.xsp +++ b/xs/xsp/ExPolygon.xsp @@ -8,6 +8,8 @@ %name{Slic3r::ExPolygon::XS} class ExPolygon { %name{_clone} ExPolygon(ExPolygon& self); ~ExPolygon(); + SV* arrayref() + %code{% RETVAL = expolygon2perl(*THIS); %}; void scale(double factor); void translate(double x, double y); void _rotate(double angle, Point* center); @@ -26,20 +28,6 @@ ExPolygon::new(...) OUTPUT: RETVAL -SV* -ExPolygon::arrayref() - CODE: - AV* av = newAV(); - av_fill(av, THIS->holes.size()); // -1 +1 - av_store(av, 0, polygon2perl(THIS->contour)); - int i = 0; - for (Polygons::iterator it = THIS->holes.begin(); it != THIS->holes.end(); ++it) { - av_store(av, ++i, polygon2perl(*it)); - } - RETVAL = sv_bless(newRV_noinc((SV*)av), gv_stashpv("Slic3r::ExPolygon", GV_ADD)); - OUTPUT: - RETVAL - %} }; diff --git a/xs/xsp/ExPolygonCollection.xsp b/xs/xsp/ExPolygonCollection.xsp new file mode 100644 index 000000000..8696f301e --- /dev/null +++ b/xs/xsp/ExPolygonCollection.xsp @@ -0,0 +1,48 @@ +%module{Slic3r::XS}; + +%{ +#include +#include "ExPolygonCollection.hpp" +%} + +%name{Slic3r::ExPolygon::Collection} class ExPolygonCollection { + %name{_clone} ExPolygonCollection(ExPolygonCollection& self); + ~ExPolygonCollection(); + void scale(double factor); + void translate(double x, double y); + void rotate(double angle, Point* center); +%{ + +ExPolygonCollection* +ExPolygonCollection::new(...) + CODE: + RETVAL = new ExPolygonCollection (); + // ST(0) is class name, others are expolygons + RETVAL->expolygons.resize(items-1); + for (unsigned int i = 1; i < items; i++) { + if (sv_isobject(ST(i)) && (SvTYPE(SvRV(ST(i))) == SVt_PVMG)) { + // a XS ExPolygon was supplied + RETVAL->expolygons[i-1] = *(ExPolygon *)SvIV((SV*)SvRV( ST(i) )); + } else { + // a Perl arrayref was supplied + perl2expolygon(ST(i), RETVAL->expolygons[i-1]); + } + } + OUTPUT: + RETVAL + +SV* +ExPolygonCollection::arrayref() + CODE: + AV* av = newAV(); + av_fill(av, THIS->expolygons.size()-1); + int i = 0; + for (ExPolygons::iterator it = THIS->expolygons.begin(); it != THIS->expolygons.end(); ++it) { + av_store(av, i++, expolygon2perl(*it)); + } + RETVAL = newRV_noinc((SV*)av); + OUTPUT: + RETVAL + +%} +}; diff --git a/xs/xsp/my.map b/xs/xsp/my.map index 7c189907b..9737c8c05 100644 --- a/xs/xsp/my.map +++ b/xs/xsp/my.map @@ -2,3 +2,4 @@ ZTable* O_OBJECT TriangleMesh* O_OBJECT Point* O_OBJECT ExPolygon* O_OBJECT +ExPolygonCollection* O_OBJECT