2012-08-29 14:49:38 +00:00
|
|
|
|
package Slic3r::Model;
|
|
|
|
|
use Moo;
|
|
|
|
|
|
2013-05-18 14:48:26 +00:00
|
|
|
|
use List::Util qw(first max);
|
|
|
|
|
use Slic3r::Geometry qw(X Y Z MIN move_points);
|
2012-08-29 14:49:38 +00:00
|
|
|
|
|
2012-08-29 16:23:34 +00:00
|
|
|
|
has 'materials' => (is => 'ro', default => sub { {} });
|
2012-08-29 14:49:38 +00:00
|
|
|
|
has 'objects' => (is => 'ro', default => sub { [] });
|
|
|
|
|
|
2012-08-29 17:37:27 +00:00
|
|
|
|
sub read_from_file {
|
|
|
|
|
my $class = shift;
|
|
|
|
|
my ($input_file) = @_;
|
|
|
|
|
|
|
|
|
|
my $model = $input_file =~ /\.stl$/i ? Slic3r::Format::STL->read_file($input_file)
|
|
|
|
|
: $input_file =~ /\.obj$/i ? Slic3r::Format::OBJ->read_file($input_file)
|
|
|
|
|
: $input_file =~ /\.amf(\.xml)?$/i ? Slic3r::Format::AMF->read_file($input_file)
|
|
|
|
|
: die "Input file must have .stl, .obj or .amf(.xml) extension\n";
|
|
|
|
|
|
2012-09-22 17:04:36 +00:00
|
|
|
|
$_->input_file($input_file) for @{$model->objects};
|
2012-08-29 17:37:27 +00:00
|
|
|
|
return $model;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-18 14:48:26 +00:00
|
|
|
|
sub merge {
|
|
|
|
|
my $class = shift;
|
|
|
|
|
my @models = @_;
|
|
|
|
|
|
2013-09-11 12:46:38 +00:00
|
|
|
|
my $new_model = ref($class)
|
|
|
|
|
? $class
|
|
|
|
|
: $class->new;
|
2013-12-12 19:19:33 +00:00
|
|
|
|
|
|
|
|
|
$new_model->add_object($_) for map @{$_->objects}, @models;
|
|
|
|
|
return $new_model;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub add_object {
|
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
|
|
my $new_object;
|
|
|
|
|
if (@_ == 1) {
|
|
|
|
|
# we have a Model::Object
|
|
|
|
|
my ($object) = @_;
|
2013-05-18 14:48:26 +00:00
|
|
|
|
|
2013-12-12 19:19:33 +00:00
|
|
|
|
$new_object = $self->add_object(
|
|
|
|
|
input_file => $object->input_file,
|
|
|
|
|
config => $object->config,
|
|
|
|
|
layer_height_ranges => $object->layer_height_ranges, # TODO: clone!
|
|
|
|
|
material_mapping => $object->material_mapping, # TODO: clone!
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
foreach my $volume (@{$object->volumes}) {
|
2013-05-18 14:48:26 +00:00
|
|
|
|
$new_object->add_volume(
|
2013-12-12 19:19:33 +00:00
|
|
|
|
material_id => $volume->material_id,
|
|
|
|
|
mesh => $volume->mesh->clone,
|
|
|
|
|
);
|
2013-05-18 14:48:26 +00:00
|
|
|
|
|
2013-12-12 19:19:33 +00:00
|
|
|
|
if (defined $volume->material_id) {
|
|
|
|
|
# merge material attributes (should we rename materials in case of duplicates?)
|
|
|
|
|
$self->set_material($volume->material_id, {
|
|
|
|
|
%{ $object->model->materials->{$volume->material_id} },
|
|
|
|
|
%{ $self->materials->{$volume->material_id} || {} },
|
|
|
|
|
});
|
|
|
|
|
}
|
2013-05-18 14:48:26 +00:00
|
|
|
|
}
|
2013-12-12 19:19:33 +00:00
|
|
|
|
|
|
|
|
|
$new_object->add_instance(
|
|
|
|
|
offset => $_->offset,
|
|
|
|
|
rotation => $_->rotation,
|
|
|
|
|
scaling_factor => $_->scaling_factor,
|
|
|
|
|
) for @{ $object->instances // [] };
|
|
|
|
|
} else {
|
|
|
|
|
push @{$self->objects}, $new_object = Slic3r::Model::Object->new(model => $self, @_);
|
2013-05-18 14:48:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-12 19:19:33 +00:00
|
|
|
|
return $new_object;
|
2013-05-18 14:48:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-12 19:19:33 +00:00
|
|
|
|
sub delete_object {
|
|
|
|
|
my ($self, $obj_idx) = @_;
|
|
|
|
|
splice @{$self->objects}, $obj_idx, 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub delete_all_objects {
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
@{$self->objects} = ();
|
2012-08-29 14:49:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-22 17:04:36 +00:00
|
|
|
|
sub set_material {
|
|
|
|
|
my $self = shift;
|
|
|
|
|
my ($material_id, $attributes) = @_;
|
|
|
|
|
|
2012-09-23 00:52:31 +00:00
|
|
|
|
return $self->materials->{$material_id} = Slic3r::Model::Region->new(
|
2012-09-22 17:04:36 +00:00
|
|
|
|
model => $self,
|
|
|
|
|
attributes => $attributes || {},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-12 19:19:33 +00:00
|
|
|
|
sub duplicate_objects_grid {
|
|
|
|
|
my ($self, $grid, $distance) = @_;
|
2013-05-18 14:48:26 +00:00
|
|
|
|
|
2013-12-12 19:19:33 +00:00
|
|
|
|
die "Grid duplication is not supported with multiple objects\n"
|
|
|
|
|
if @{$self->objects} > 1;
|
|
|
|
|
|
|
|
|
|
my $object = $self->objects->[0];
|
|
|
|
|
@{$object->instances} = ();
|
|
|
|
|
|
|
|
|
|
my $size = $object->bounding_box->size;
|
|
|
|
|
for my $x_copy (1..$grid->[X]) {
|
|
|
|
|
for my $y_copy (1..$grid->[Y]) {
|
2013-05-18 14:48:26 +00:00
|
|
|
|
$object->add_instance(
|
2013-12-12 19:19:33 +00:00
|
|
|
|
offset => [
|
|
|
|
|
($size->[X] + $distance) * ($x_copy-1),
|
|
|
|
|
($size->[Y] + $distance) * ($y_copy-1),
|
|
|
|
|
],
|
|
|
|
|
);
|
2013-05-18 14:48:26 +00:00
|
|
|
|
}
|
2013-12-12 19:19:33 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# this will append more instances to each object
|
|
|
|
|
# and then automatically rearrange everything
|
|
|
|
|
sub duplicate_objects {
|
|
|
|
|
my ($self, $config, $copies_num) = @_;
|
|
|
|
|
|
|
|
|
|
foreach my $object (@{$self->objects}) {
|
|
|
|
|
my @instances = @{$object->instances};
|
|
|
|
|
foreach my $instance (@instances) {
|
|
|
|
|
### $object->add_instance($instance->clone); if we had clone()
|
|
|
|
|
$object->add_instance(
|
|
|
|
|
offset => [ @{$instance->offset} ],
|
|
|
|
|
rotation => $instance->rotation,
|
|
|
|
|
scaling_factor => $instance->scaling_factor,
|
|
|
|
|
) for 2..$copies_num;
|
2013-05-18 14:48:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-12-12 19:19:33 +00:00
|
|
|
|
|
|
|
|
|
$self->arrange_objects($config);
|
2013-05-18 14:48:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-12 19:19:33 +00:00
|
|
|
|
# arrange objects preserving their instance count
|
|
|
|
|
# but altering their instance positions
|
|
|
|
|
sub arrange_objects {
|
|
|
|
|
my ($self, $config) = @_;
|
2013-05-18 14:48:26 +00:00
|
|
|
|
|
2013-12-12 19:19:33 +00:00
|
|
|
|
# get the (transformed) size of each instance so that we take
|
|
|
|
|
# into account their different transformations when packing
|
|
|
|
|
my @instance_sizes = ();
|
|
|
|
|
foreach my $object (@{$self->objects}) {
|
|
|
|
|
push @instance_sizes, map $object->instance_bounding_box($_)->size, 0..$#{$object->instances};
|
|
|
|
|
}
|
2013-05-18 14:48:26 +00:00
|
|
|
|
|
2013-12-12 19:19:33 +00:00
|
|
|
|
my @positions = $self->_arrange($config, \@instance_sizes);
|
|
|
|
|
|
|
|
|
|
foreach my $object (@{$self->objects}) {
|
|
|
|
|
$_->offset(shift @positions) for @{$object->instances};
|
|
|
|
|
$object->update_bounding_box;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# duplicate the entire model preserving instance relative positions
|
|
|
|
|
sub duplicate {
|
|
|
|
|
my ($self, $config, $copies_num) = @_;
|
|
|
|
|
|
|
|
|
|
my $model_size = $self->bounding_box->size;
|
|
|
|
|
my @positions = $self->_arrange($config, [ map $model_size, 2..$copies_num ]);
|
|
|
|
|
|
|
|
|
|
# note that this will leave the object count unaltered
|
|
|
|
|
|
|
|
|
|
foreach my $object (@{$self->objects}) {
|
|
|
|
|
foreach my $instance (@{$object->instances}) {
|
|
|
|
|
foreach my $pos (@positions) {
|
|
|
|
|
### $object->add_instance($instance->clone); if we had clone()
|
|
|
|
|
$object->add_instance(
|
|
|
|
|
offset => [ $instance->offset->[X] + $pos->[X], $instance->offset->[Y] + $pos->[Y] ],
|
|
|
|
|
rotation => $instance->rotation,
|
|
|
|
|
scaling_factor => $instance->scaling_factor,
|
|
|
|
|
);
|
2013-05-18 14:48:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-12-12 19:19:33 +00:00
|
|
|
|
$object->update_bounding_box;
|
2013-05-18 14:48:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-12 19:19:33 +00:00
|
|
|
|
sub _arrange {
|
|
|
|
|
my ($self, $config, $sizes) = @_;
|
|
|
|
|
|
|
|
|
|
my $partx = max(map $_->[X], @$sizes);
|
|
|
|
|
my $party = max(map $_->[Y], @$sizes);
|
|
|
|
|
return Slic3r::Geometry::arrange
|
|
|
|
|
(scalar(@$sizes), $partx, $party, (map $_, @{$config->bed_size}),
|
|
|
|
|
$config->min_object_distance, $config);
|
2013-05-18 14:48:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-12 19:19:33 +00:00
|
|
|
|
sub has_objects_with_no_instances {
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
return (first { !defined $_->instances } @{$self->objects}) ? 1 : 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# this returns the bounding box of the *transformed* instances
|
2013-06-16 10:21:25 +00:00
|
|
|
|
sub bounding_box {
|
2013-05-18 14:48:26 +00:00
|
|
|
|
my $self = shift;
|
2013-12-12 19:19:33 +00:00
|
|
|
|
return Slic3r::Geometry::BoundingBox->merge(map $_->bounding_box, @{ $self->objects });
|
2013-05-18 14:48:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub align_to_origin {
|
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
|
|
# calculate the displacements needed to
|
|
|
|
|
# have lowest value for each axis at coordinate 0
|
2013-06-02 14:56:08 +00:00
|
|
|
|
{
|
2013-06-16 10:21:25 +00:00
|
|
|
|
my $bb = $self->bounding_box;
|
2013-12-12 19:19:33 +00:00
|
|
|
|
$self->translate(map -$bb->extents->[$_][MIN], X,Y,Z);
|
2013-06-02 14:56:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# align all instances to 0,0 as well
|
|
|
|
|
{
|
|
|
|
|
my @instances = map @{$_->instances}, @{$self->objects};
|
|
|
|
|
my @extents = Slic3r::Geometry::bounding_box_3D([ map $_->offset, @instances ]);
|
2013-12-12 19:19:33 +00:00
|
|
|
|
foreach my $instance (@instances) {
|
|
|
|
|
$instance->offset->[X] -= $extents[X][MIN];
|
|
|
|
|
$instance->offset->[Y] -= $extents[Y][MIN];
|
|
|
|
|
}
|
2013-06-02 14:56:08 +00:00
|
|
|
|
}
|
2013-05-18 14:48:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-15 15:17:12 +00:00
|
|
|
|
# input point is expressed in unscaled coordinates
|
|
|
|
|
sub center_instances_around_point {
|
|
|
|
|
my ($self, $point) = @_;
|
|
|
|
|
|
|
|
|
|
my $bb = $self->bounding_box;
|
|
|
|
|
my $size = $bb->size;
|
|
|
|
|
my @shift = (
|
|
|
|
|
-$bb->x_min + $point->[X] - $size->[X]/2,
|
|
|
|
|
-$bb->y_min + $point->[Y] - $size->[Y]/2,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
foreach my $instance (map @{$_->instances}, @{$self->objects}) {
|
|
|
|
|
$instance->offset->[X] += $shift[X];
|
|
|
|
|
$instance->offset->[Y] += $shift[Y];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-12 19:19:33 +00:00
|
|
|
|
sub translate {
|
2013-05-18 14:48:26 +00:00
|
|
|
|
my $self = shift;
|
2013-06-22 17:36:50 +00:00
|
|
|
|
my @shift = @_;
|
|
|
|
|
|
2013-12-12 19:19:33 +00:00
|
|
|
|
$_->translate(@shift) for @{$self->objects};
|
2013-05-18 14:48:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-29 14:49:38 +00:00
|
|
|
|
# flattens everything to a single mesh
|
|
|
|
|
sub mesh {
|
|
|
|
|
my $self = shift;
|
|
|
|
|
|
2013-09-17 17:25:10 +00:00
|
|
|
|
my $mesh = Slic3r::TriangleMesh->new;
|
2013-12-12 19:19:33 +00:00
|
|
|
|
$mesh->merge($_->mesh) for @{$self->objects};
|
2013-09-17 17:25:10 +00:00
|
|
|
|
return $mesh;
|
2012-08-29 14:49:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-18 14:48:26 +00:00
|
|
|
|
# this method splits objects into multiple distinct objects by walking their meshes
|
|
|
|
|
sub split_meshes {
|
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
|
|
my @objects = @{$self->objects};
|
|
|
|
|
@{$self->objects} = ();
|
|
|
|
|
|
|
|
|
|
foreach my $object (@objects) {
|
|
|
|
|
if (@{$object->volumes} > 1) {
|
|
|
|
|
# We can't split meshes if there's more than one material, because
|
|
|
|
|
# we can't group the resulting meshes by object afterwards
|
|
|
|
|
push @{$self->objects}, $object;
|
|
|
|
|
next;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
my $volume = $object->volumes->[0];
|
2013-09-19 10:25:00 +00:00
|
|
|
|
foreach my $mesh (@{$volume->mesh->split}) {
|
2013-05-18 14:48:26 +00:00
|
|
|
|
my $new_object = $self->add_object(
|
|
|
|
|
input_file => $object->input_file,
|
2013-12-12 19:19:33 +00:00
|
|
|
|
config => $object->config->clone,
|
|
|
|
|
layer_height_ranges => $object->layer_height_ranges, # TODO: this needs to be cloned
|
2013-05-18 14:48:26 +00:00
|
|
|
|
);
|
|
|
|
|
$new_object->add_volume(
|
2013-09-19 10:25:00 +00:00
|
|
|
|
mesh => $mesh,
|
2013-05-18 14:48:26 +00:00
|
|
|
|
material_id => $volume->material_id,
|
|
|
|
|
);
|
|
|
|
|
|
2013-12-12 19:19:33 +00:00
|
|
|
|
# add one instance per original instance
|
2013-05-18 14:48:26 +00:00
|
|
|
|
$new_object->add_instance(
|
2013-12-12 19:19:33 +00:00
|
|
|
|
offset => [ @{$_->offset} ],
|
|
|
|
|
rotation => $_->rotation,
|
|
|
|
|
scaling_factor => $_->scaling_factor,
|
2013-05-18 14:48:26 +00:00
|
|
|
|
) for @{ $object->instances // [] };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-31 13:10:11 +00:00
|
|
|
|
sub print_info {
|
|
|
|
|
my $self = shift;
|
|
|
|
|
$_->print_info for @{$self->objects};
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-25 17:52:32 +00:00
|
|
|
|
sub get_material_name {
|
|
|
|
|
my $self = shift;
|
|
|
|
|
my ($material_id) = @_;
|
|
|
|
|
|
|
|
|
|
my $name;
|
|
|
|
|
if (exists $self->materials->{$material_id}) {
|
|
|
|
|
$name //= $self->materials->{$material_id}->attributes->{$_} for qw(Name name);
|
|
|
|
|
} elsif ($material_id eq '_') {
|
|
|
|
|
$name = 'Default material';
|
|
|
|
|
}
|
|
|
|
|
$name //= $material_id;
|
|
|
|
|
return $name;
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-23 00:52:31 +00:00
|
|
|
|
package Slic3r::Model::Region;
|
2012-08-29 14:49:38 +00:00
|
|
|
|
use Moo;
|
|
|
|
|
|
|
|
|
|
has 'model' => (is => 'ro', weak_ref => 1, required => 1);
|
|
|
|
|
has 'attributes' => (is => 'rw', default => sub { {} });
|
|
|
|
|
|
|
|
|
|
package Slic3r::Model::Object;
|
|
|
|
|
use Moo;
|
|
|
|
|
|
2013-07-31 13:10:11 +00:00
|
|
|
|
use File::Basename qw(basename);
|
2013-08-25 16:01:59 +00:00
|
|
|
|
use List::Util qw(first sum);
|
2013-12-12 19:19:33 +00:00
|
|
|
|
use Slic3r::Geometry qw(X Y Z MIN MAX);
|
|
|
|
|
|
|
|
|
|
has 'input_file' => (is => 'rw');
|
|
|
|
|
has 'model' => (is => 'ro', weak_ref => 1, required => 1);
|
|
|
|
|
has 'volumes' => (is => 'ro', default => sub { [] });
|
|
|
|
|
has 'instances' => (is => 'rw');
|
|
|
|
|
has 'config' => (is => 'rw', default => sub { Slic3r::Config->new });
|
|
|
|
|
has 'layer_height_ranges' => (is => 'rw', default => sub { [] }); # [ z_min, z_max, layer_height ]
|
2013-12-09 00:12:37 +00:00
|
|
|
|
has 'material_mapping' => (is => 'rw', default => sub { {} }); # { material_id => region_idx }
|
2013-12-12 19:19:33 +00:00
|
|
|
|
has '_bounding_box' => (is => 'rw');
|
2012-08-29 14:49:38 +00:00
|
|
|
|
|
|
|
|
|
sub add_volume {
|
|
|
|
|
my $self = shift;
|
2012-09-23 12:48:58 +00:00
|
|
|
|
my %args = @_;
|
2012-08-29 14:49:38 +00:00
|
|
|
|
|
2013-09-11 07:49:28 +00:00
|
|
|
|
push @{$self->volumes}, my $volume = Slic3r::Model::Volume->new(
|
|
|
|
|
object => $self,
|
|
|
|
|
%args,
|
|
|
|
|
);
|
2013-06-22 17:36:50 +00:00
|
|
|
|
$self->_bounding_box(undef);
|
2012-08-29 14:49:38 +00:00
|
|
|
|
return $volume;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub add_instance {
|
|
|
|
|
my $self = shift;
|
2013-12-12 19:19:33 +00:00
|
|
|
|
my %params = @_;
|
2012-08-29 14:49:38 +00:00
|
|
|
|
|
|
|
|
|
$self->instances([]) if !defined $self->instances;
|
2013-12-12 19:19:33 +00:00
|
|
|
|
push @{$self->instances}, my $i = Slic3r::Model::Instance->new(object => $self, %params);
|
|
|
|
|
$self->_bounding_box(undef);
|
|
|
|
|
return $i;
|
2012-08-29 14:49:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-12 19:19:33 +00:00
|
|
|
|
sub delete_last_instance {
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
pop @{$self->instances};
|
|
|
|
|
$self->_bounding_box(undef);
|
2012-08-29 17:37:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-12 19:19:33 +00:00
|
|
|
|
sub instances_count {
|
2013-05-18 14:48:26 +00:00
|
|
|
|
my $self = shift;
|
2013-12-12 19:19:33 +00:00
|
|
|
|
return scalar(@{ $self->instances // [] });
|
2013-05-18 14:48:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-12 19:19:33 +00:00
|
|
|
|
sub raw_mesh {
|
2013-06-13 12:33:10 +00:00
|
|
|
|
my $self = shift;
|
2013-12-12 19:19:33 +00:00
|
|
|
|
|
|
|
|
|
my $mesh = Slic3r::TriangleMesh->new;
|
|
|
|
|
$mesh->merge($_->mesh) for @{ $self->volumes };
|
|
|
|
|
return $mesh;
|
2013-06-13 12:33:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-12 19:19:33 +00:00
|
|
|
|
# flattens all volumes and instances into a single mesh
|
|
|
|
|
sub mesh {
|
2013-09-09 22:40:46 +00:00
|
|
|
|
my $self = shift;
|
2013-12-12 19:19:33 +00:00
|
|
|
|
|
|
|
|
|
my $mesh = $self->raw_mesh;
|
|
|
|
|
|
|
|
|
|
my @instance_meshes = ();
|
|
|
|
|
foreach my $instance (@{ $self->instances }) {
|
|
|
|
|
my $m = $mesh->clone;
|
|
|
|
|
$instance->transform_mesh($m);
|
|
|
|
|
push @instance_meshes, $m;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
my $full_mesh = Slic3r::TriangleMesh->new;
|
|
|
|
|
$full_mesh->merge($_) for @instance_meshes;
|
|
|
|
|
return $full_mesh;
|
2013-09-09 22:40:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-12 19:19:33 +00:00
|
|
|
|
sub update_bounding_box {
|
|
|
|
|
my ($self) = @_;
|
|
|
|
|
$self->_bounding_box($self->mesh->bounding_box);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# this returns the bounding box of the *transformed* instances
|
2013-06-13 12:33:10 +00:00
|
|
|
|
sub bounding_box {
|
|
|
|
|
my $self = shift;
|
2013-06-22 17:36:50 +00:00
|
|
|
|
|
2013-12-12 19:19:33 +00:00
|
|
|
|
$self->update_bounding_box if !defined $self->_bounding_box;
|
2013-12-06 18:34:50 +00:00
|
|
|
|
return $self->_bounding_box->clone;
|
2013-06-13 12:33:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-12 19:19:33 +00:00
|
|
|
|
# this returns the bounding box of the *transformed* given instance
|
|
|
|
|
sub instance_bounding_box {
|
|
|
|
|
my ($self, $instance_idx) = @_;
|
|
|
|
|
|
|
|
|
|
my $mesh = $self->raw_mesh;
|
|
|
|
|
$self->instances->[$instance_idx]->transform_mesh($mesh);
|
|
|
|
|
return $mesh->bounding_box;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-18 14:48:26 +00:00
|
|
|
|
sub align_to_origin {
|
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
|
|
# calculate the displacements needed to
|
|
|
|
|
# have lowest value for each axis at coordinate 0
|
2013-06-16 10:21:25 +00:00
|
|
|
|
my $bb = $self->bounding_box;
|
|
|
|
|
my @shift = map -$bb->extents->[$_][MIN], X,Y,Z;
|
2013-12-12 19:19:33 +00:00
|
|
|
|
$self->translate(@shift);
|
2013-06-13 12:33:10 +00:00
|
|
|
|
return @shift;
|
2013-05-18 14:48:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-12 19:19:33 +00:00
|
|
|
|
sub center_around_origin {
|
2013-05-18 14:48:26 +00:00
|
|
|
|
my $self = shift;
|
2013-06-22 17:36:50 +00:00
|
|
|
|
|
2013-12-12 19:19:33 +00:00
|
|
|
|
# calculate the displacements needed to
|
|
|
|
|
# center this object around the origin
|
|
|
|
|
my $bb = $self->raw_mesh->bounding_box;
|
|
|
|
|
|
|
|
|
|
# first align to origin on XYZ
|
|
|
|
|
my @shift = @{ $bb->vector_to_origin };
|
|
|
|
|
|
|
|
|
|
# then center it on XY
|
|
|
|
|
my $size = $bb->size;
|
|
|
|
|
$shift[$_] -= $size->[$_]/2 for X,Y;
|
|
|
|
|
|
|
|
|
|
$self->translate(@shift);
|
2012-10-21 18:56:19 +00:00
|
|
|
|
|
2013-12-12 19:19:33 +00:00
|
|
|
|
if (defined $self->instances) {
|
|
|
|
|
foreach my $instance (@{ $self->instances }) {
|
|
|
|
|
$instance->offset->[X] -= $shift[X];
|
|
|
|
|
$instance->offset->[Y] -= $shift[Y];
|
|
|
|
|
}
|
|
|
|
|
$self->update_bounding_box;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return @shift;
|
2012-10-21 18:56:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-12 19:19:33 +00:00
|
|
|
|
sub translate {
|
2013-05-18 14:48:26 +00:00
|
|
|
|
my $self = shift;
|
2013-12-12 19:19:33 +00:00
|
|
|
|
my @shift = @_;
|
2013-05-18 14:48:26 +00:00
|
|
|
|
|
2013-12-12 19:19:33 +00:00
|
|
|
|
$_->mesh->translate(@shift) for @{$self->volumes};
|
|
|
|
|
$self->_bounding_box->translate(@shift) if defined $self->_bounding_box;
|
2013-05-18 14:48:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-10-24 20:44:08 +00:00
|
|
|
|
sub materials_count {
|
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
|
|
my %materials = map { $_->material_id // '_default' => 1 } @{$self->volumes};
|
|
|
|
|
return scalar keys %materials;
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-25 17:52:32 +00:00
|
|
|
|
sub unique_materials {
|
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
|
|
my %materials = ();
|
|
|
|
|
$materials{ $_->material_id // '_' } = 1 for @{$self->volumes};
|
|
|
|
|
return sort keys %materials;
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-25 16:01:59 +00:00
|
|
|
|
sub facets_count {
|
|
|
|
|
my $self = shift;
|
2013-12-12 19:19:33 +00:00
|
|
|
|
return sum(map $_->mesh->facets_count, @{$self->volumes});
|
2013-08-25 16:01:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-31 13:10:11 +00:00
|
|
|
|
sub needed_repair {
|
|
|
|
|
my $self = shift;
|
2013-09-09 22:40:46 +00:00
|
|
|
|
return (first { !$_->mesh->needed_repair } @{$self->volumes}) ? 0 : 1;
|
2013-07-31 13:10:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-09-11 12:46:38 +00:00
|
|
|
|
sub mesh_stats {
|
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
|
|
# TODO: sum values from all volumes
|
|
|
|
|
return $self->volumes->[0]->mesh->stats;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-31 13:10:11 +00:00
|
|
|
|
sub print_info {
|
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
|
|
printf "Info about %s:\n", basename($self->input_file);
|
|
|
|
|
printf " size: x=%.3f y=%.3f z=%.3f\n", @{$self->size};
|
|
|
|
|
if (my $stats = $self->mesh_stats) {
|
|
|
|
|
printf " number of facets: %d\n", $stats->{number_of_facets};
|
|
|
|
|
printf " number of shells: %d\n", $stats->{number_of_parts};
|
|
|
|
|
printf " volume: %.3f\n", $stats->{volume};
|
|
|
|
|
if ($self->needed_repair) {
|
|
|
|
|
printf " needed repair: yes\n";
|
|
|
|
|
printf " degenerate facets: %d\n", $stats->{degenerate_facets};
|
|
|
|
|
printf " edges fixed: %d\n", $stats->{edges_fixed};
|
|
|
|
|
printf " facets removed: %d\n", $stats->{facets_removed};
|
|
|
|
|
printf " facets added: %d\n", $stats->{facets_added};
|
|
|
|
|
printf " facets reversed: %d\n", $stats->{facets_reversed};
|
|
|
|
|
printf " backwards edges: %d\n", $stats->{backwards_edges};
|
|
|
|
|
} else {
|
|
|
|
|
printf " needed repair: no\n";
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
printf " number of facets: %d\n", scalar(map @{$_->facets}, @{$self->volumes});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-29 14:49:38 +00:00
|
|
|
|
package Slic3r::Model::Volume;
|
|
|
|
|
use Moo;
|
|
|
|
|
|
2013-12-12 19:19:33 +00:00
|
|
|
|
has 'object' => (is => 'ro', weak_ref => 1, required => 1);
|
|
|
|
|
has 'material_id' => (is => 'rw');
|
|
|
|
|
has 'mesh' => (is => 'rw', required => 1);
|
2013-08-25 16:01:59 +00:00
|
|
|
|
|
2012-08-29 14:49:38 +00:00
|
|
|
|
package Slic3r::Model::Instance;
|
|
|
|
|
use Moo;
|
|
|
|
|
|
2013-12-12 19:19:33 +00:00
|
|
|
|
has 'object' => (is => 'ro', weak_ref => 1, required => 1);
|
|
|
|
|
has 'rotation' => (is => 'rw', default => sub { 0 }); # around mesh center point
|
|
|
|
|
has 'scaling_factor' => (is => 'rw', default => sub { 1 });
|
|
|
|
|
has 'offset' => (is => 'rw'); # must be arrayref in *unscaled* coordinates
|
|
|
|
|
|
|
|
|
|
sub transform_mesh {
|
2013-12-15 15:17:12 +00:00
|
|
|
|
my ($self, $mesh, $dont_translate) = @_;
|
2013-12-12 19:19:33 +00:00
|
|
|
|
|
|
|
|
|
$mesh->rotate($self->rotation, Slic3r::Point->new(0,0)); # rotate around mesh origin
|
|
|
|
|
$mesh->scale($self->scaling_factor); # scale around mesh origin
|
2013-12-15 15:17:12 +00:00
|
|
|
|
$mesh->translate(@{$self->offset}, 0) unless $dont_translate;
|
2013-12-12 19:19:33 +00:00
|
|
|
|
}
|
2012-08-29 14:49:38 +00:00
|
|
|
|
|
|
|
|
|
1;
|