2013-06-07 21:16:02 +00:00
|
|
|
package Slic3r::Geometry::BoundingBox;
|
|
|
|
use Moo;
|
2013-09-11 09:55:08 +00:00
|
|
|
|
|
|
|
use List::Util qw(min max);
|
|
|
|
use Slic3r::Geometry qw(X Y Z MIN MAX X1 Y1 X2 Y2 Z1 Z2);
|
2013-06-07 21:16:02 +00:00
|
|
|
use Storable qw();
|
|
|
|
|
|
|
|
has 'extents' => (is => 'ro', required => 1);
|
|
|
|
|
|
|
|
sub clone { Storable::dclone($_[0]) }
|
|
|
|
|
2013-06-10 13:34:45 +00:00
|
|
|
# 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] ],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2013-09-11 09:55:08 +00:00
|
|
|
# 2D/3D
|
|
|
|
sub new_from_bb {
|
|
|
|
my $class = shift;
|
|
|
|
my ($bb) = @_;
|
|
|
|
|
|
|
|
return $class->new(extents => [
|
|
|
|
[ $bb->[X1], $bb->[X2] ],
|
|
|
|
[ $bb->[Y1], $bb->[Y2] ],
|
|
|
|
(@$bb == 6) ? [ $bb->[Z1], $bb->[Z2] ] : (),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2013-06-10 13:34:45 +00:00
|
|
|
# 3D
|
|
|
|
sub new_from_points_3D {
|
|
|
|
my $class = shift;
|
|
|
|
my ($points) = @_;
|
|
|
|
|
|
|
|
return $class->new(extents => [ Slic3r::Geometry::bounding_box_3D($points) ]);
|
|
|
|
}
|
|
|
|
|
2013-09-11 09:55:08 +00:00
|
|
|
sub merge {
|
|
|
|
my $class = shift;
|
|
|
|
my (@bounding_boxes) = @_;
|
|
|
|
|
|
|
|
my $self = ref($class)
|
|
|
|
? $class
|
|
|
|
: shift @bounding_boxes;
|
|
|
|
|
|
|
|
foreach my $bounding_box (@bounding_boxes) {
|
|
|
|
for my $axis (X .. $#{$self->extents}) {
|
|
|
|
$self->extents->[$axis][MIN] = min($self->extents->[$axis][MIN], $bounding_box->extents->[$axis][MIN]);
|
|
|
|
$self->extents->[$axis][MAX] = max($self->extents->[$axis][MAX], $bounding_box->extents->[$axis][MAX]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
2013-06-07 21:16:02 +00:00
|
|
|
# four-arguments 2D bb
|
|
|
|
sub bb {
|
|
|
|
my $self = shift;
|
|
|
|
my $extents = $self->extents;
|
|
|
|
return [ $extents->[X][MIN], $extents->[Y][MIN], $extents->[X][MAX], $extents->[Y][MAX] ];
|
|
|
|
}
|
|
|
|
|
|
|
|
sub polygon {
|
|
|
|
my $self = shift;
|
2013-06-16 10:21:25 +00:00
|
|
|
|
|
|
|
my $e = $self->extents;
|
2013-07-05 12:29:57 +00:00
|
|
|
return Slic3r::Polygon->new(
|
2013-06-16 10:21:25 +00:00
|
|
|
[ $e->[X][MIN], $e->[Y][MIN] ],
|
|
|
|
[ $e->[X][MAX], $e->[Y][MIN] ],
|
|
|
|
[ $e->[X][MAX], $e->[Y][MAX] ],
|
|
|
|
[ $e->[X][MIN], $e->[Y][MAX] ],
|
2013-07-05 12:29:57 +00:00
|
|
|
);
|
2013-06-07 21:16:02 +00:00
|
|
|
}
|
|
|
|
|
2013-06-15 13:50:02 +00:00
|
|
|
# note to $self
|
2013-06-07 21:16:02 +00:00
|
|
|
sub rotate {
|
2013-06-15 13:50:02 +00:00
|
|
|
die "Rotating an axis-aligned bounding box doesn't make any sense";
|
2013-06-07 21:16:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub scale {
|
|
|
|
my $self = shift;
|
|
|
|
my ($factor) = @_;
|
|
|
|
|
2013-06-10 13:34:45 +00:00
|
|
|
for (@{$self->extents}) {
|
|
|
|
$_ *= $factor for @$_[MIN,MAX];
|
|
|
|
}
|
2013-06-07 21:16:02 +00:00
|
|
|
|
|
|
|
$self;
|
|
|
|
}
|
|
|
|
|
2013-06-22 17:36:50 +00:00
|
|
|
sub translate {
|
|
|
|
my $self = shift;
|
|
|
|
my @shift = @_;
|
|
|
|
|
|
|
|
for my $axis (X .. $#{$self->extents}) {
|
|
|
|
$self->extents->[$axis][MIN] += $shift[$axis];
|
|
|
|
$self->extents->[$axis][MAX] += $shift[$axis];
|
|
|
|
}
|
|
|
|
|
|
|
|
$self;
|
|
|
|
}
|
|
|
|
|
2013-06-07 21:16:02 +00:00
|
|
|
sub size {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
my $extents = $self->extents;
|
|
|
|
return [ map $extents->[$_][MAX] - $extents->[$_][MIN], grep $extents->[$_], (X,Y,Z) ];
|
|
|
|
}
|
|
|
|
|
|
|
|
sub center {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
my $extents = $self->extents;
|
|
|
|
return [ map +($extents->[$_][MAX] + $extents->[$_][MIN])/2, grep $extents->[$_], (X,Y,Z) ];
|
|
|
|
}
|
|
|
|
|
|
|
|
sub center_2D {
|
|
|
|
my $self = shift;
|
|
|
|
return Slic3r::Point->new(@{$self->center}[X,Y]);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub min_point {
|
|
|
|
my $self = shift;
|
|
|
|
return Slic3r::Point->new($self->extents->[X][MIN], $self->extents->[Y][MIN]);
|
|
|
|
}
|
|
|
|
|
2013-11-27 11:18:24 +00:00
|
|
|
sub min_point3 {
|
|
|
|
my $self = shift;
|
|
|
|
return [ map $self->extents->[$_][MIN], (X,Y,Z) ];
|
|
|
|
}
|
|
|
|
|
|
|
|
sub vector_to_origin {
|
|
|
|
my $self = shift;
|
|
|
|
return [ map -$_, @{$self->min_point3} ];
|
|
|
|
}
|
|
|
|
|
2013-06-07 21:16:02 +00:00
|
|
|
sub max_point {
|
|
|
|
my $self = shift;
|
|
|
|
return Slic3r::Point->new($self->extents->[X][MAX], $self->extents->[Y][MAX]);
|
|
|
|
}
|
|
|
|
|
2013-06-16 10:21:25 +00:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2013-12-17 23:13:41 +00:00
|
|
|
sub z_min {
|
|
|
|
my $self = shift;
|
|
|
|
return $self->extents->[Z][MIN];
|
|
|
|
}
|
|
|
|
|
|
|
|
sub z_max {
|
|
|
|
my $self = shift;
|
|
|
|
return $self->extents->[Z][MAX];
|
|
|
|
}
|
|
|
|
|
2013-06-07 21:16:02 +00:00
|
|
|
1;
|