From 8e0e03247d72da806c919db6f1b1505c528f793e Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Mon, 10 Jun 2013 15:34:45 +0200 Subject: [PATCH] Bugfix: ineffective scale() method in recently created BoundingBox objects caused wrong positioning for scaled objects in plater. Includes regression test. #1171 --- lib/Slic3r/Geometry/BoundingBox.pm | 26 +++++++++++++++++++++++--- t/geometry.t | 12 ++++++++++-- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/lib/Slic3r/Geometry/BoundingBox.pm b/lib/Slic3r/Geometry/BoundingBox.pm index cbcec824c..3cd019f0c 100644 --- a/lib/Slic3r/Geometry/BoundingBox.pm +++ b/lib/Slic3r/Geometry/BoundingBox.pm @@ -7,6 +7,26 @@ has 'extents' => (is => 'ro', required => 1); sub clone { Storable::dclone($_[0]) } +# 2D +sub new_from_points { + my $class = shift; + my ($points) = @_; + + my $bb = [ Slic3r::Geometry::bounding_box($points) ]; + return $class->new(extents => [ + [ $bb->[X1], $bb->[X2] ], + [ $bb->[Y1], $bb->[Y2] ], + ]); +} + +# 3D +sub new_from_points_3D { + my $class = shift; + my ($points) = @_; + + return $class->new(extents => [ Slic3r::Geometry::bounding_box_3D($points) ]); +} + # four-arguments 2D bb sub bb { my $self = shift; @@ -39,9 +59,9 @@ sub scale { my $self = shift; my ($factor) = @_; - $_ *= $factor - for map @$_[MIN,MAX], - grep $_, @{$self->extents}[X,Y,Z]; + for (@{$self->extents}) { + $_ *= $factor for @$_[MIN,MAX]; + } $self; } diff --git a/t/geometry.t b/t/geometry.t index c0e1f753b..edbe7262a 100644 --- a/t/geometry.t +++ b/t/geometry.t @@ -2,7 +2,7 @@ use Test::More; use strict; use warnings; -plan tests => 23; +plan tests => 24; BEGIN { use FindBin; @@ -173,4 +173,12 @@ is Slic3r::Geometry::can_connect_points(@$points, $polygons), 0, 'can_connect_po is_deeply $result, [ [10, 0], [5, 5], [0, 0], [10, 0] ], 'split_at_index'; } -#========================================================== \ No newline at end of file +#========================================================== + +{ + my $bb = Slic3r::Geometry::BoundingBox->new_from_points([ [0, 1], [10, 2], [20, 2] ]); + $bb->scale(2); + is_deeply $bb->extents, [ [0,40], [2,4] ], 'bounding box is scaled correctly'; +} + +#==========================================================