Browse Source

Refactoring: use Slic3r::Geometry::BoundingBox objects everywhere

Alessandro Ranellucci 11 years ago
parent
commit
ac4a0bcdd8

+ 1 - 12
lib/Slic3r/ExPolygon.pm

@@ -142,18 +142,7 @@ sub point_on_segment {
 
 
 sub bounding_box {
 sub bounding_box {
     my $self = shift;
     my $self = shift;
-    return Slic3r::Geometry::bounding_box($self->contour);
-}
-
-sub bounding_box_polygon {
-    my $self = shift;
-    my @bb = $self->bounding_box;
-    return Slic3r::Polygon->new([
-        [ $bb[0], $bb[1] ],
-        [ $bb[2], $bb[1] ],
-        [ $bb[2], $bb[3] ],
-        [ $bb[0], $bb[3] ],
-    ]);
+    return $self->contour->bounding_box;
 }
 }
 
 
 sub clip_line {
 sub clip_line {

+ 1 - 1
lib/Slic3r/Fill.pm

@@ -40,7 +40,7 @@ sub filler {
     }
     }
     
     
     $self->fillers->{$filler} ||= $FillTypes{$filler}->new(
     $self->fillers->{$filler} ||= $FillTypes{$filler}->new(
-        bounding_box => [ $self->object->bounding_box ],
+        bounding_box => $self->object->bounding_box,
     );
     );
     return $self->fillers->{$filler};
     return $self->fillers->{$filler};
 }
 }

+ 2 - 2
lib/Slic3r/Fill/Base.pm

@@ -5,7 +5,7 @@ use Slic3r::Geometry qw(PI);
 
 
 has 'layer_id'            => (is => 'rw');
 has 'layer_id'            => (is => 'rw');
 has 'angle'               => (is => 'rw', default => sub { $Slic3r::Config->fill_angle });
 has 'angle'               => (is => 'rw', default => sub { $Slic3r::Config->fill_angle });
-has 'bounding_box'        => (is => 'ro', required => 1);
+has 'bounding_box'        => (is => 'ro', required => 1);  # Slic3r::Geometry::BoundingBox object
 
 
 sub angles () { [0, PI/2] }
 sub angles () { [0, PI/2] }
 
 
@@ -16,7 +16,7 @@ sub infill_direction {
     # set infill angle
     # set infill angle
     my (@rotate, @shift);
     my (@rotate, @shift);
     $rotate[0] = Slic3r::Geometry::deg2rad($self->angle);
     $rotate[0] = Slic3r::Geometry::deg2rad($self->angle);
-    $rotate[1] = Slic3r::Geometry::bounding_box_center($self->bounding_box);
+    $rotate[1] = $self->bounding_box->center_2D;
     @shift = @{$rotate[1]};
     @shift = @{$rotate[1]};
     
     
     if (defined $self->layer_id) {
     if (defined $self->layer_id) {

+ 3 - 3
lib/Slic3r/Fill/Concentric.pm

@@ -3,7 +3,7 @@ use Moo;
 
 
 extends 'Slic3r::Fill::Base';
 extends 'Slic3r::Fill::Base';
 
 
-use Slic3r::Geometry qw(scale unscale X1 X2);
+use Slic3r::Geometry qw(scale unscale X);
 use Slic3r::Geometry::Clipper qw(offset2 union_pt traverse_pt PFT_EVENODD);
 use Slic3r::Geometry::Clipper qw(offset2 union_pt traverse_pt PFT_EVENODD);
 
 
 sub fill_surface {
 sub fill_surface {
@@ -13,7 +13,7 @@ sub fill_surface {
     # no rotation is supported for this infill pattern
     # no rotation is supported for this infill pattern
     
     
     my $expolygon = $surface->expolygon;
     my $expolygon = $surface->expolygon;
-    my $bounding_box = [ $expolygon->bounding_box ];
+    my $bounding_box = $expolygon->bounding_box;
     
     
     my $min_spacing = scale $params{flow_spacing};
     my $min_spacing = scale $params{flow_spacing};
     my $distance = $min_spacing / $params{density};
     my $distance = $min_spacing / $params{density};
@@ -21,7 +21,7 @@ sub fill_surface {
     my $flow_spacing = $params{flow_spacing};
     my $flow_spacing = $params{flow_spacing};
     if ($params{density} == 1 && !$params{dont_adjust}) {
     if ($params{density} == 1 && !$params{dont_adjust}) {
         $distance = $self->adjust_solid_spacing(
         $distance = $self->adjust_solid_spacing(
-            width       => $bounding_box->[X2] - $bounding_box->[X1],
+            width       => $bounding_box->size->[X],
             distance    => $distance,
             distance    => $distance,
         );
         );
         $flow_spacing = unscale $distance;
         $flow_spacing = unscale $distance;

+ 2 - 2
lib/Slic3r/Fill/Flowsnake.pm

@@ -4,7 +4,7 @@ use Moo;
 extends 'Slic3r::Fill::PlanePath';
 extends 'Slic3r::Fill::PlanePath';
 
 
 use Math::PlanePath::Flowsnake;
 use Math::PlanePath::Flowsnake;
-use Slic3r::Geometry qw(X X1 X2);
+use Slic3r::Geometry qw(X);
 
 
 # Sorry, this fill is currently broken.
 # Sorry, this fill is currently broken.
 
 
@@ -12,7 +12,7 @@ sub process_polyline {
     my $self = shift;
     my $self = shift;
     my ($polyline, $bounding_box) = @_;
     my ($polyline, $bounding_box) = @_;
     
     
-    $_->[X] += ($bounding_box->[X1] + $bounding_box->[X2]/2) for @$polyline;
+    $_->[X] += $bounding_box->center_2D->[X] for @$polyline;
 }
 }
 
 
 1;
 1;

+ 11 - 11
lib/Slic3r/Fill/Honeycomb.pm

@@ -5,7 +5,7 @@ extends 'Slic3r::Fill::Base';
 
 
 has 'cache'         => (is => 'rw', default => sub {{}});
 has 'cache'         => (is => 'rw', default => sub {{}});
 
 
-use Slic3r::Geometry qw(PI X1 Y1 X2 Y2 X Y scale);
+use Slic3r::Geometry qw(PI X Y MIN MAX scale);
 use Slic3r::Geometry::Clipper qw(intersection_ex);
 use Slic3r::Geometry::Clipper qw(intersection_ex);
 
 
 sub angles () { [0, PI/3, PI/3*2] }
 sub angles () { [0, PI/3, PI/3*2] }
@@ -39,28 +39,28 @@ sub fill_surface {
         # adjust actual bounding box to the nearest multiple of our hex pattern
         # adjust actual bounding box to the nearest multiple of our hex pattern
         # and align it so that it matches across layers
         # and align it so that it matches across layers
         
         
-        my $bounding_box = [ @{$self->bounding_box} ];  # clone
-        $bounding_box->[$_] = 0 for X1, Y1;
+        my $bounding_box = $self->bounding_box->clone;
+        $bounding_box->extents->[$_][MIN] = 0 for X, Y;
         {
         {
-            my $bb_polygon = Slic3r::Polygon->new_from_bounding_box($bounding_box);
+            my $bb_polygon = $bounding_box->polygon;
             $bb_polygon->scale(sqrt 2);
             $bb_polygon->scale(sqrt 2);
             $bb_polygon->rotate($rotate_vector->[0][0], $hex_center);
             $bb_polygon->rotate($rotate_vector->[0][0], $hex_center);
-            $bounding_box = [ Slic3r::Geometry::bounding_box($bb_polygon) ];
-            # $bounding_box->[X1] and [Y1] represent the displacement between new bounding box offset and old one
-            $bounding_box->[X1] -= $bounding_box->[X1] % $hex_width;
-            $bounding_box->[Y1] -= $bounding_box->[Y1] % $pattern_height;
+            $bounding_box = $bb_polygon->bounding_box;
+            # $bounding_box->y_min and $bounding_box->y_max represent the displacement between new bounding box offset and old one
+            $bounding_box->extents->[X][MIN] -= $bounding_box->x_min % $hex_width;
+            $bounding_box->extents->[Y][MAX] -= $bounding_box->y_min % $pattern_height;
         }
         }
         
         
         my @polygons = ();
         my @polygons = ();
-        my $x = $bounding_box->[X1];
-        while ($x <= $bounding_box->[X2]) {
+        my $x = $bounding_box->x_min;
+        while ($x <= $bounding_box->x_max) {
             my $p = [];
             my $p = [];
             
             
             my @x = ($x + $x_offset, $x + $distance - $x_offset);
             my @x = ($x + $x_offset, $x + $distance - $x_offset);
             for (1..2) {
             for (1..2) {
                 @$p = reverse @$p; # turn first half upside down
                 @$p = reverse @$p; # turn first half upside down
                 my @p = ();
                 my @p = ();
-                for (my $y = $bounding_box->[Y1]; $y <= $bounding_box->[Y2]; $y += $y_short + $hex_side + $y_short + $hex_side) {
+                for (my $y = $bounding_box->x_min; $y <= $bounding_box->y_max; $y += $y_short + $hex_side + $y_short + $hex_side) {
                     push @$p,
                     push @$p,
                         [ $x[1], $y + $y_offset ],
                         [ $x[1], $y + $y_offset ],
                         [ $x[0], $y + $y_short - $y_offset ],
                         [ $x[0], $y + $y_short - $y_offset ],

+ 3 - 9
lib/Slic3r/Fill/PlanePath.pm

@@ -27,17 +27,11 @@ sub fill_surface {
     $self->rotate_points($expolygon, $rotate_vector);
     $self->rotate_points($expolygon, $rotate_vector);
     
     
     my $distance_between_lines = scale $params{flow_spacing} / $params{density} * $self->multiplier;
     my $distance_between_lines = scale $params{flow_spacing} / $params{density} * $self->multiplier;
-    my $bounding_box = [ Slic3r::Geometry::bounding_box([map @$_, @$expolygon]) ];
-    my $bounding_box_polygon = Slic3r::Polygon->new([
-        [ $bounding_box->[X1], $bounding_box->[Y1] ],
-        [ $bounding_box->[X2], $bounding_box->[Y1] ],
-        [ $bounding_box->[X2], $bounding_box->[Y2] ],
-        [ $bounding_box->[X1], $bounding_box->[Y2] ],
-    ]);
+    my $bounding_box = $expolygon->bounding_box;
     
     
     (ref $self) =~ /::([^:]+)$/;
     (ref $self) =~ /::([^:]+)$/;
     my $path = "Math::PlanePath::$1"->new;
     my $path = "Math::PlanePath::$1"->new;
-    my @n = $self->get_n($path, [map +($_ / $distance_between_lines), @$bounding_box]);
+    my @n = $self->get_n($path, [ map +($_ / $distance_between_lines), @{$bounding_box->bb} ]);
     
     
     my $polyline = Slic3r::Polyline->new([
     my $polyline = Slic3r::Polyline->new([
         map [ map {$_*$distance_between_lines} $path->n_to_xy($_) ], @n,
         map [ map {$_*$distance_between_lines} $path->n_to_xy($_) ], @n,
@@ -47,7 +41,7 @@ sub fill_surface {
     $self->process_polyline($polyline, $bounding_box);
     $self->process_polyline($polyline, $bounding_box);
     
     
     my @paths = map $_->clip_with_expolygon($expolygon),
     my @paths = map $_->clip_with_expolygon($expolygon),
-        $polyline->clip_with_polygon($bounding_box_polygon);
+        $polyline->clip_with_polygon($bounding_box->polygon);
     
     
     if (0) {
     if (0) {
         require "Slic3r/SVG.pm";
         require "Slic3r/SVG.pm";

+ 7 - 7
lib/Slic3r/Fill/Rectilinear.pm

@@ -5,7 +5,7 @@ extends 'Slic3r::Fill::Base';
 
 
 has 'cache'         => (is => 'rw', default => sub {{}});
 has 'cache'         => (is => 'rw', default => sub {{}});
 
 
-use Slic3r::Geometry qw(X1 Y1 X2 Y2 A B X Y scale unscale scaled_epsilon);
+use Slic3r::Geometry qw(A B X Y scale unscale scaled_epsilon);
 
 
 sub fill_surface {
 sub fill_surface {
     my $self = shift;
     my $self = shift;
@@ -32,26 +32,26 @@ sub fill_surface {
         # compute bounding box
         # compute bounding box
         my $bounding_box;
         my $bounding_box;
         {
         {
-            my $bb_polygon = Slic3r::Polygon->new_from_bounding_box($self->bounding_box);
+            my $bb_polygon = $self->bounding_box->polygon;
             $bb_polygon->scale(sqrt 2);
             $bb_polygon->scale(sqrt 2);
             $self->rotate_points($bb_polygon, $rotate_vector);
             $self->rotate_points($bb_polygon, $rotate_vector);
-            $bounding_box = [ $bb_polygon->bounding_box ];
+            $bounding_box = $bb_polygon->bounding_box;
         }
         }
         
         
         # define flow spacing according to requested density
         # define flow spacing according to requested density
         if ($params{density} == 1 && !$params{dont_adjust}) {
         if ($params{density} == 1 && !$params{dont_adjust}) {
             $distance_between_lines = $self->adjust_solid_spacing(
             $distance_between_lines = $self->adjust_solid_spacing(
-                width       => $bounding_box->[X2] - $bounding_box->[X1],
+                width       => $bounding_box->size->[X],
                 distance    => $distance_between_lines,
                 distance    => $distance_between_lines,
             );
             );
             $flow_spacing = unscale $distance_between_lines;
             $flow_spacing = unscale $distance_between_lines;
         }
         }
         
         
         # generate the basic pattern
         # generate the basic pattern
-        my $x = $bounding_box->[X1];
+        my $x = $bounding_box->x_min;
         my @vertical_lines = ();
         my @vertical_lines = ();
-        for (my $i = 0; $x <= $bounding_box->[X2] + scaled_epsilon; $i++) {
-            my $vertical_line = Slic3r::Line->new([$x, $bounding_box->[Y2]], [$x, $bounding_box->[Y1]]);
+        for (my $i = 0; $x <= $bounding_box->x_max + scaled_epsilon; $i++) {
+            my $vertical_line = Slic3r::Line->new([$x, $bounding_box->y_max], [$x, $bounding_box->y_min]);
             if ($is_line_pattern && $i % 2) {
             if ($is_line_pattern && $i % 2) {
                 $vertical_line->[A][X] += $line_oscillation;
                 $vertical_line->[A][X] += $line_oscillation;
                 $vertical_line->[B][X] -= $line_oscillation;
                 $vertical_line->[B][X] -= $line_oscillation;

+ 5 - 4
lib/Slic3r/GUI/Plater.pm

@@ -7,7 +7,7 @@ use File::Basename qw(basename dirname);
 use List::Util qw(max sum first);
 use List::Util qw(max sum first);
 use Math::Clipper qw(offset JT_ROUND);
 use Math::Clipper qw(offset JT_ROUND);
 use Math::ConvexHull::MonotoneChain qw(convex_hull);
 use Math::ConvexHull::MonotoneChain qw(convex_hull);
-use Slic3r::Geometry qw(X Y Z X1 Y1 X2 Y2 MIN MAX);
+use Slic3r::Geometry qw(X Y Z MIN MAX);
 use threads::shared qw(shared_clone);
 use threads::shared qw(shared_clone);
 use Wx qw(:bitmap :brush :button :cursor :dialog :filedialog :font :keycode :icon :id :listctrl :misc :panel :pen :sizer :toolbar :window);
 use Wx qw(:bitmap :brush :button :cursor :dialog :filedialog :font :keycode :icon :id :listctrl :misc :panel :pen :sizer :toolbar :window);
 use Wx::Event qw(EVT_BUTTON EVT_COMMAND EVT_KEY_DOWN EVT_LIST_ITEM_ACTIVATED EVT_LIST_ITEM_DESELECTED EVT_LIST_ITEM_SELECTED EVT_MOUSE_EVENTS EVT_PAINT EVT_TOOL EVT_CHOICE);
 use Wx::Event qw(EVT_BUTTON EVT_COMMAND EVT_KEY_DOWN EVT_LIST_ITEM_ACTIVATED EVT_LIST_ITEM_DESELECTED EVT_LIST_ITEM_SELECTED EVT_MOUSE_EVENTS EVT_PAINT EVT_TOOL EVT_CHOICE);
@@ -765,7 +765,7 @@ sub recenter {
     return unless @{$self->{objects}};
     return unless @{$self->{objects}};
     
     
     # calculate displacement needed to center the print
     # calculate displacement needed to center the print
-    my @print_bb = Slic3r::Geometry::bounding_box([
+    my $print_bb = Slic3r::Geometry::BoundingBox->new_from_points([
         map {
         map {
             my $obj = $_;
             my $obj = $_;
             my $bb = $obj->transformed_bounding_box;
             my $bb = $obj->transformed_bounding_box;
@@ -776,9 +776,10 @@ sub recenter {
     
     
     # $self->{shift} contains the offset in pixels to add to object instances in order to center them
     # $self->{shift} contains the offset in pixels to add to object instances in order to center them
     # it is expressed in upwards Y
     # it is expressed in upwards Y
+    my $print_size = $print_bb->size;
     $self->{shift} = [
     $self->{shift} = [
-        $self->to_pixel(-$print_bb[X1]) + ($self->{canvas}->GetSize->GetWidth  - $self->to_pixel($print_bb[X2] - $print_bb[X1])) / 2,
-        $self->to_pixel(-$print_bb[Y1]) + ($self->{canvas}->GetSize->GetHeight - $self->to_pixel($print_bb[Y2] - $print_bb[Y1])) / 2,
+        $self->to_pixel(-$print_bb->x_min) + ($self->{canvas}->GetSize->GetWidth  - $self->to_pixel($print_size->[X])) / 2,
+        $self->to_pixel(-$print_bb->y_min) + ($self->{canvas}->GetSize->GetHeight - $self->to_pixel($print_size->[Y])) / 2,
     ];
     ];
 }
 }
 
 

+ 28 - 1
lib/Slic3r/Geometry/BoundingBox.pm

@@ -36,7 +36,14 @@ sub bb {
 
 
 sub polygon {
 sub polygon {
     my $self = shift;
     my $self = shift;
-    return Slic3r::Polygon->new_from_bounding_box($self->bb);
+    
+    my $e = $self->extents;
+    return Slic3r::Polygon->new([
+        [ $e->[X][MIN], $e->[Y][MIN] ],
+        [ $e->[X][MAX], $e->[Y][MIN] ],
+        [ $e->[X][MAX], $e->[Y][MAX] ],
+        [ $e->[X][MIN], $e->[Y][MAX] ],
+    ]);
 }
 }
 
 
 # note to $self
 # note to $self
@@ -84,4 +91,24 @@ sub max_point {
     return Slic3r::Point->new($self->extents->[X][MAX], $self->extents->[Y][MAX]);
     return Slic3r::Point->new($self->extents->[X][MAX], $self->extents->[Y][MAX]);
 }
 }
 
 
+sub x_min {
+    my $self = shift;
+    return $self->extents->[X][MIN];
+}
+
+sub x_max {
+    my $self = shift;
+    return $self->extents->[X][MAX];
+}
+
+sub y_min {
+    my $self = shift;
+    return $self->extents->[Y][MIN];
+}
+
+sub y_max {
+    my $self = shift;
+    return $self->extents->[Y][MAX];
+}
+
 1;
 1;

Some files were not shown because too many files changed in this diff