Browse Source

Fix one more centering problem caused by wrong bounding box implementation

Alessandro Ranellucci 11 years ago
parent
commit
7bf308c08f
3 changed files with 14 additions and 19 deletions
  1. 8 3
      lib/Slic3r/GUI/Plater.pm
  2. 2 13
      lib/Slic3r/Geometry/BoundingBox.pm
  3. 4 3
      lib/Slic3r/Polyline.pm

+ 8 - 3
lib/Slic3r/GUI/Plater.pm

@@ -1067,7 +1067,7 @@ sub OnDropFiles {
 package Slic3r::GUI::Plater::Object;
 use Moo;
 
-use Math::ConvexHull::MonotoneChain qw(convex_hull);
+use Math::ConvexHull::MonotoneChain qw();
 use Slic3r::Geometry qw(X Y Z MIN MAX deg2rad);
 
 has 'name'                  => (is => 'rw', required => 1);
@@ -1075,6 +1075,7 @@ has 'input_file'            => (is => 'rw', required => 1);
 has 'input_file_object_id'  => (is => 'rw');  # undef means keep model object
 has 'model_object'          => (is => 'rw', required => 1, trigger => 1);
 has 'bounding_box'          => (is => 'rw');  # 3D bb of original object (aligned to origin) with no rotation or scaling
+has 'convex_hull'           => (is => 'rw');  # 2D convex hull of original object (aligned to origin) with no rotation or scaling
 has 'scale'                 => (is => 'rw', default => sub { 1 }, trigger => \&_transform_thumbnail);
 has 'rotate'                => (is => 'rw', default => sub { 0 }, trigger => \&_transform_thumbnail); # around object center point
 has 'instances'             => (is => 'rw', default => sub { [] }); # upward Y axis
@@ -1138,10 +1139,11 @@ sub make_thumbnail {
     my $self = shift;
     
     my $mesh = $self->model_object->mesh;  # $self->model_object is already aligned to origin
+    $self->convex_hull(Slic3r::Polygon->new(Math::ConvexHull::MonotoneChain::convex_hull($mesh->vertices)));
     my $thumbnail = Slic3r::ExPolygon::Collection->new(
     	expolygons => (@{$mesh->facets} <= 5000)
     		? $mesh->horizontal_projection
-    		: [ Slic3r::ExPolygon->new(convex_hull($mesh->vertices)) ],
+    		: [ Slic3r::ExPolygon->new($self->convex_hull) ],
     );
     
     # only simplify expolygons larger than the threshold
@@ -1168,7 +1170,10 @@ sub _transform_thumbnail {
 # bounding box with applied rotation and scaling
 sub transformed_bounding_box {
     my $self = shift;
-    return $self->_apply_transform($self->bounding_box);
+    
+    my $bb = Slic3r::Geometry::BoundingBox->new_from_points($self->_apply_transform($self->convex_hull));
+    $bb->extents->[Z] = $self->bounding_box->clone->extents->[Z];
+    return $bb;
 }
 
 sub _apply_transform {

+ 2 - 13
lib/Slic3r/Geometry/BoundingBox.pm

@@ -39,20 +39,9 @@ sub polygon {
     return Slic3r::Polygon->new_from_bounding_box($self->bb);
 }
 
+# note to $self
 sub rotate {
-    my $self = shift;
-    my ($angle, $center) = @_;
-    
-    # rotate the 2D bounding box polygon and leave Z unaltered
-    my $bb_p = $self->polygon;
-    $bb_p->rotate($angle, $center);
-    my @bb = $bb_p->bounding_box;
-    $self->extents->[X][MIN] = $bb[X1];
-    $self->extents->[Y][MIN] = $bb[Y1];
-    $self->extents->[X][MAX] = $bb[X2];
-    $self->extents->[Y][MAX] = $bb[Y2];
-    
-    $self;
+    die "Rotating an axis-aligned bounding box doesn't make any sense";
 }
 
 sub scale {

+ 4 - 3
lib/Slic3r/Polyline.pm

@@ -156,11 +156,12 @@ sub translate {
 sub scale {
     my $self = shift;
     my ($factor) = @_;
-    return if $factor == 1;
     
     # transform point coordinates
-    foreach my $point (@$self) {
-        $point->[$_] *= $factor for X,Y;
+    if ($factor != 1) {
+        foreach my $point (@$self) {
+            $point->[$_] *= $factor for X,Y;
+        }
     }
     return $self;
 }