Ported more things to XS

This commit is contained in:
Alessandro Ranellucci 2014-09-21 10:51:36 +02:00
parent 5a96bad8c2
commit 73b3c06361
18 changed files with 383 additions and 297 deletions

View file

@ -158,76 +158,6 @@ sub _arrange {
);
}
# this returns the bounding box of the *transformed* instances
sub bounding_box {
my $self = shift;
return undef if !@{$self->objects};
my $bb = $self->objects->[0]->bounding_box;
$bb->merge($_->bounding_box) for @{$self->objects}[1..$#{$self->objects}];
return $bb;
}
# input point is expressed in unscaled coordinates
sub center_instances_around_point {
my ($self, $point) = @_;
my $bb = $self->bounding_box;
return if !defined $bb;
my $size = $bb->size;
my @shift = (
-$bb->x_min + $point->[X] - $size->x/2,
-$bb->y_min + $point->[Y] - $size->y/2, #//
);
foreach my $object (@{$self->objects}) {
foreach my $instance (@{$object->instances}) {
$instance->set_offset(Slic3r::Pointf->new(
$instance->offset->x + $shift[X],
$instance->offset->y + $shift[Y], #++
));
}
$object->update_bounding_box;
}
}
sub align_instances_to_origin {
my ($self) = @_;
my $bb = $self->bounding_box;
return if !defined $bb;
my $new_center = $bb->size;
$new_center->translate(-$new_center->x/2, -$new_center->y/2); #//
$self->center_instances_around_point($new_center);
}
sub translate {
my $self = shift;
my @shift = @_;
$_->translate(@shift) for @{$self->objects};
}
# flattens everything to a single mesh
sub mesh {
my $self = shift;
my $mesh = Slic3r::TriangleMesh->new;
$mesh->merge($_->mesh) for @{$self->objects};
return $mesh;
}
# flattens everything to a single mesh
sub raw_mesh {
my $self = shift;
my $mesh = Slic3r::TriangleMesh->new;
$mesh->merge($_->raw_mesh) for @{$self->objects};
return $mesh;
}
# this method splits objects into multiple distinct objects by walking their meshes
sub split_meshes {
my $self = shift;
@ -369,105 +299,6 @@ sub add_instance {
}
}
sub raw_bounding_box {
my $self = shift;
my @meshes = map $_->mesh->clone, grep !$_->modifier, @{ $self->volumes };
die "No meshes found" if !@meshes;
my $instance = $self->instances->[0];
$instance->transform_mesh($_, 1) for @meshes;
my $bb = (shift @meshes)->bounding_box;
$bb->merge($_->bounding_box) for @meshes;
return $bb;
}
# flattens all volumes and instances into a single mesh
sub mesh {
my $self = shift;
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;
}
sub update_bounding_box {
my ($self) = @_;
$self->_bounding_box($self->mesh->bounding_box);
}
# this returns the bounding box of the *transformed* instances
sub bounding_box {
my $self = shift;
$self->update_bounding_box if !defined $self->_bounding_box;
return $self->_bounding_box->clone;
}
# this returns the bounding box of the *transformed* given instance
sub instance_bounding_box {
my ($self, $instance_idx) = @_;
$instance_idx //= 0;
my $mesh = $self->raw_mesh;
$self->instances->[$instance_idx]->transform_mesh($mesh);
return $mesh->bounding_box;
}
sub center_around_origin {
my $self = shift;
# calculate the displacements needed to
# center this object around the origin
my $bb = $self->raw_mesh->bounding_box;
# first align to origin on XY
my @shift = (
-$bb->x_min,
-$bb->y_min,
0,
);
# then center it on XY
my $size = $bb->size;
$shift[X] -= $size->x/2;
$shift[Y] -= $size->y/2; #//
$self->translate(@shift);
$self->origin_translation->translate(@shift[X,Y]);
if ($self->instances_count > 0) {
foreach my $instance (@{ $self->instances }) {
$instance->set_offset(Slic3r::Pointf->new(
$instance->offset->x - $shift[X],
$instance->offset->y - $shift[Y], #--
));
}
$self->update_bounding_box;
}
return @shift;
}
sub translate {
my $self = shift;
my @shift = @_;
$_->mesh->translate(@shift) for @{$self->volumes};
$self->_bounding_box->translate(@shift) if defined $self->_bounding_box;
}
sub rotate {
my ($self, $angle, $axis) = @_;
@ -497,29 +328,6 @@ sub flip {
$self->invalidate_bounding_box;
}
sub scale_xyz {
my ($self, $versor) = @_;
$_->mesh->scale_xyz($versor) for @{$self->volumes};
$self->invalidate_bounding_box;
}
sub materials_count {
my $self = shift;
my %materials = map { $_->material_id // '_default' => 1 } @{$self->volumes};
return scalar keys %materials;
}
sub unique_materials {
my $self = shift;
my %materials = ();
$materials{ $_->material_id } = 1
for grep { defined $_->material_id } @{$self->volumes};
return sort keys %materials;
}
sub mesh_stats {
my $self = shift;
@ -552,54 +360,4 @@ sub print_info {
}
}
sub cut {
my ($self, $z) = @_;
# clone this one to duplicate instances, materials etc.
my $model = Slic3r::Model->new;
my $upper = $model->add_object($self);
my $lower = $model->add_object($self);
$upper->clear_volumes;
$lower->clear_volumes;
foreach my $volume (@{$self->volumes}) {
if ($volume->modifier) {
# don't cut modifiers
$upper->add_volume($volume);
$lower->add_volume($volume);
} else {
my $upper_mesh = Slic3r::TriangleMesh->new;
my $lower_mesh = Slic3r::TriangleMesh->new;
$volume->mesh->cut($z + $volume->mesh->bounding_box->z_min, $upper_mesh, $lower_mesh);
$upper_mesh->repair;
$lower_mesh->repair;
$upper_mesh->reset_repair_stats;
$lower_mesh->reset_repair_stats;
if ($upper_mesh->facets_count > 0) {
$upper->add_volume(
name => $volume->name,
material_id => $volume->material_id,
mesh => $upper_mesh,
modifier => $volume->modifier,
config => $volume->config,
);
}
if ($lower_mesh->facets_count > 0) {
$lower->add_volume(
name => $volume->name,
material_id => $volume->material_id,
mesh => $lower_mesh,
modifier => $volume->modifier,
config => $volume->config,
);
}
}
}
$upper = undef if !@{$upper->volumes};
$lower = undef if !@{$lower->volumes};
return ($model, $upper, $lower);
}
1;