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

This commit is contained in:
Alessandro Ranellucci 2013-06-16 12:21:25 +02:00
parent 07407e5dbd
commit ac4a0bcdd8
20 changed files with 105 additions and 127 deletions

View file

@ -5,7 +5,7 @@ use Slic3r::Geometry qw(PI);
has 'layer_id' => (is => 'rw');
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] }
@ -16,7 +16,7 @@ sub infill_direction {
# set infill angle
my (@rotate, @shift);
$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]};
if (defined $self->layer_id) {

View file

@ -3,7 +3,7 @@ use Moo;
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);
sub fill_surface {
@ -13,7 +13,7 @@ sub fill_surface {
# no rotation is supported for this infill pattern
my $expolygon = $surface->expolygon;
my $bounding_box = [ $expolygon->bounding_box ];
my $bounding_box = $expolygon->bounding_box;
my $min_spacing = scale $params{flow_spacing};
my $distance = $min_spacing / $params{density};
@ -21,7 +21,7 @@ sub fill_surface {
my $flow_spacing = $params{flow_spacing};
if ($params{density} == 1 && !$params{dont_adjust}) {
$distance = $self->adjust_solid_spacing(
width => $bounding_box->[X2] - $bounding_box->[X1],
width => $bounding_box->size->[X],
distance => $distance,
);
$flow_spacing = unscale $distance;

View file

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

View file

@ -5,7 +5,7 @@ extends 'Slic3r::Fill::Base';
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);
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
# 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->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 $x = $bounding_box->[X1];
while ($x <= $bounding_box->[X2]) {
my $x = $bounding_box->x_min;
while ($x <= $bounding_box->x_max) {
my $p = [];
my @x = ($x + $x_offset, $x + $distance - $x_offset);
for (1..2) {
@$p = reverse @$p; # turn first half upside down
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,
[ $x[1], $y + $y_offset ],
[ $x[0], $y + $y_short - $y_offset ],

View file

@ -27,17 +27,11 @@ sub fill_surface {
$self->rotate_points($expolygon, $rotate_vector);
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) =~ /::([^:]+)$/;
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([
map [ map {$_*$distance_between_lines} $path->n_to_xy($_) ], @n,
@ -47,7 +41,7 @@ sub fill_surface {
$self->process_polyline($polyline, $bounding_box);
my @paths = map $_->clip_with_expolygon($expolygon),
$polyline->clip_with_polygon($bounding_box_polygon);
$polyline->clip_with_polygon($bounding_box->polygon);
if (0) {
require "Slic3r/SVG.pm";

View file

@ -5,7 +5,7 @@ extends 'Slic3r::Fill::Base';
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 {
my $self = shift;
@ -32,26 +32,26 @@ sub fill_surface {
# compute 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);
$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
if ($params{density} == 1 && !$params{dont_adjust}) {
$distance_between_lines = $self->adjust_solid_spacing(
width => $bounding_box->[X2] - $bounding_box->[X1],
width => $bounding_box->size->[X],
distance => $distance_between_lines,
);
$flow_spacing = unscale $distance_between_lines;
}
# generate the basic pattern
my $x = $bounding_box->[X1];
my $x = $bounding_box->x_min;
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) {
$vertical_line->[A][X] += $line_oscillation;
$vertical_line->[B][X] -= $line_oscillation;