Rename materials to regions

degen-loop-screen
Alessandro Ranellucci 2012-09-23 02:52:31 +02:00
parent e696764af8
commit ec0d3987db
7 changed files with 96 additions and 96 deletions

View File

@ -43,15 +43,15 @@ use Slic3r::Format::STL;
use Slic3r::GCode;
use Slic3r::Geometry qw(PI);
use Slic3r::Layer;
use Slic3r::Layer::Material;
use Slic3r::Layer::Region;
use Slic3r::Line;
use Slic3r::Model;
use Slic3r::Point;
use Slic3r::Polygon;
use Slic3r::Polyline;
use Slic3r::Print;
use Slic3r::Print::Material;
use Slic3r::Print::Object;
use Slic3r::Print::Region;
use Slic3r::Surface;
use Slic3r::TriangleMesh;
eval "use Slic3r::Build";

View File

@ -5,7 +5,7 @@ use Slic3r::Geometry::Clipper qw(union_ex);
has 'id' => (is => 'rw', required => 1); # sequential number of layer, 0-based
has 'object' => (is => 'ro', weak_ref => 1, required => 1);
has 'materials' => (is => 'ro', default => sub { [] });
has 'regions' => (is => 'ro', default => sub { [] });
has 'slicing_errors' => (is => 'rw');
has 'slice_z' => (is => 'lazy');
@ -14,7 +14,7 @@ has 'height' => (is => 'lazy');
has 'flow' => (is => 'ro', default => sub { $Slic3r::flow });
# collection of expolygons generated by slicing the original geometry;
# also known as 'islands' (all materials are merged here)
# also known as 'islands' (all regions are merged here)
has 'slices' => (is => 'rw');
# ordered collection of extrusion paths to fill surfaces for support material
@ -42,37 +42,37 @@ sub _build_height {
return $self->id == 0 ? $Slic3r::Config->get_value('first_layer_height') : $Slic3r::Config->layer_height;
}
sub material {
sub region {
my $self = shift;
my ($material_idx) = @_;
my ($region_id) = @_;
if (!defined $self->materials->[$material_idx]) {
$self->materials->[$material_idx] = Slic3r::Layer::Material->new(
layer => $self,
material => $self->object->print->materials->[$material_idx],
if (!defined $self->regions->[$region_id]) {
$self->regions->[$region_id] = Slic3r::Layer::Region->new(
layer => $self,
region => $self->object->print->regions->[$region_id],
);
}
return $self->materials->[$material_idx];
return $self->regions->[$region_id];
}
# merge all materials' slices to get islands
# merge all regions' slices to get islands
sub make_slices {
my $self = shift;
# optimization for single-material layers
my @materials_with_slices = grep { @{$_->slices} } @{$self->materials};
if (@materials_with_slices == 1) {
$self->slices([ map $_->expolygon, @{$materials_with_slices[0]->slices} ]);
# optimization for single-region layers
my @regions_with_slices = grep { @{$_->slices} } @{$self->regions};
if (@regions_with_slices == 1) {
$self->slices([ map $_->expolygon, @{$regions_with_slices[0]->slices} ]);
return;
}
$self->slices(union_ex([ map $_->p, map @{$_->slices}, @{$self->materials} ]));
$self->slices(union_ex([ map $_->p, map @{$_->slices}, @{$self->regions} ]));
}
sub make_perimeters {
my $self = shift;
Slic3r::debugf "Making perimeters for layer %d\n", $self->id;
$_->make_perimeters for @{$self->materials};
$_->make_perimeters for @{$self->regions};
}
1;

View File

@ -1,4 +1,4 @@
package Slic3r::Layer::Material;
package Slic3r::Layer::Region;
use Moo;
use Math::Clipper ':all';
@ -13,7 +13,7 @@ has 'layer' => (
required => 1,
handles => [qw(id slice_z print_z height flow)],
);
has 'material' => (is => 'ro', required => 1);
has 'region' => (is => 'ro', required => 1);
has 'perimeter_flow' => (is => 'lazy');
has 'infill_flow' => (is => 'lazy');
@ -50,15 +50,15 @@ has 'fills' => (is => 'rw', default => sub { [] });
sub _build_perimeter_flow {
my $self = shift;
return $self->id == 0
? $self->material->first_layer_flows->{perimeter}
: $self->material->flows->{perimeter}
? $self->region->first_layer_flows->{perimeter}
: $self->region->flows->{perimeter}
}
sub _build_infill_flow {
my $self = shift;
return $self->id == 0
? $self->material->first_layer_flows->{infill}
: $self->material->flows->{infill}
? $self->region->first_layer_flows->{infill}
: $self->region->flows->{infill}
}
# build polylines from lines

View File

@ -31,7 +31,7 @@ sub set_material {
my $self = shift;
my ($material_id, $attributes) = @_;
return $self->materials->{$material_id} = Slic3r::Model::Material->new(
return $self->materials->{$material_id} = Slic3r::Model::Region->new(
model => $self,
attributes => $attributes || {},
);
@ -58,7 +58,7 @@ sub mesh {
return Slic3r::TriangleMesh->merge(@meshes);
}
package Slic3r::Model::Material;
package Slic3r::Model::Region;
use Moo;
has 'model' => (is => 'ro', weak_ref => 1, required => 1);

View File

@ -16,7 +16,7 @@ has 'objects' => (is => 'rw', default => sub {[]});
has 'total_extrusion_length' => (is => 'rw');
has 'processing_time' => (is => 'rw');
has 'extruders' => (is => 'rw', default => sub {[]});
has 'materials' => (is => 'rw', default => sub {[]});
has 'regions' => (is => 'rw', default => sub {[]});
has 'support_material_flow' => (is => 'rw');
has 'first_layer_support_material_flow' => (is => 'rw');
@ -70,21 +70,21 @@ sub add_model {
{
my @material_ids = sort keys %{$model->materials};
@material_ids = (0) if !@material_ids;
for (my $i = $self->materials_count; $i < @material_ids; $i++) {
push @{$self->materials}, Slic3r::Print::Material->new;
$materials{$material_ids[$i]} = $#{$self->materials};
for (my $i = $self->regions_count; $i < @material_ids; $i++) {
push @{$self->regions}, Slic3r::Print::Region->new;
$materials{$material_ids[$i]} = $#{$self->regions};
}
}
foreach my $object (@{ $model->objects }) {
my @meshes = (); # by material_id
my @meshes = (); # by region_id
foreach my $volume (@{$object->volumes}) {
# should the object contain multiple volumes of the same material, merge them
my $material_id = defined $volume->material_id ? $materials{$volume->material_id} : 0;
my $region_id = defined $volume->material_id ? $materials{$volume->material_id} : 0;
my $mesh = $volume->mesh->clone;
$meshes[$material_id] = $meshes[$material_id]
? Slic3r::TriangleMesh->merge($meshes[$material_id], $mesh)
$meshes[$region_id] = $meshes[$region_id]
? Slic3r::TriangleMesh->merge($meshes[$region_id], $mesh)
: $mesh;
}
@ -168,8 +168,8 @@ sub validate {
sub init_extruders {
my $self = shift;
# map materials to extruders (ghetto mapping for now)
my %extruder_mapping = map { $_ => $_ } 0..$#{$self->materials};
# map regions to extruders (ghetto mapping for now)
my %extruder_mapping = map { $_ => $_ } 0..$#{$self->regions};
# initialize all extruder(s) we need
my @used_extruders = (
@ -185,20 +185,20 @@ sub init_extruders {
);
}
# calculate materials' flows
# calculate regions' flows
$Slic3r::flow = $self->extruders->[0]->make_flow(width => $self->config->extrusion_width);
for my $material_id (0 .. $#{$self->materials}) {
my $material = $self->materials->[$material_id];
for my $region_id (0 .. $#{$self->regions}) {
my $region = $self->regions->[$region_id];
# per-role extruders and flows
for (qw(perimeter infill)) {
$material->extruders->{$_} = ($self->materials_count > 1)
? $self->extruders->[$extruder_mapping{$material_id}]
$region->extruders->{$_} = ($self->regions_count > 1)
? $self->extruders->[$extruder_mapping{$region_id}]
: $self->extruders->[$self->config->get("${_}_extruder")-1];
$material->flows->{$_} = $material->extruders->{$_}->make_flow(
$region->flows->{$_} = $region->extruders->{$_}->make_flow(
width => $self->config->get("${_}_extrusion_width") || $self->config->extrusion_width,
);
$material->first_layer_flows->{$_} = $material->extruders->{$_}->make_flow(
$region->first_layer_flows->{$_} = $region->extruders->{$_}->make_flow(
layer_height => $self->config->get_value('first_layer_height'),
width => $self->config->first_layer_extrusion_width,
);
@ -239,9 +239,9 @@ sub layer_count {
return $count;
}
sub materials_count {
sub regions_count {
my $self = shift;
return scalar @{$self->materials};
return scalar @{$self->regions};
}
sub duplicate {
@ -328,11 +328,11 @@ sub export_gcode {
$status_cb->(20, "Generating perimeters");
$_->make_perimeters for @{$self->objects};
# simplify slices (both layer and material slices),
# simplify slices (both layer and region slices),
# we only need the max resolution for perimeters
foreach my $layer (map @{$_->layers}, @{$self->objects}) {
$_->simplify(scale &Slic3r::RESOLUTION)
for @{$layer->slices}, (map $_->expolygon, map @{$_->slices}, @{$layer->materials});
for @{$layer->slices}, (map $_->expolygon, map @{$_->slices}, @{$layer->regions});
}
# this will clip $layer->surfaces to the infill boundaries
@ -342,12 +342,12 @@ sub export_gcode {
# decide what surfaces are to be filled
$status_cb->(35, "Preparing infill surfaces");
$_->prepare_fill_surfaces for map @{$_->materials}, map @{$_->layers}, @{$self->objects};
$_->prepare_fill_surfaces for map @{$_->regions}, map @{$_->layers}, @{$self->objects};
# this will detect bridges and reverse bridges
# and rearrange top/bottom/internal surfaces
$status_cb->(45, "Detect bridges");
$_->process_bridges for map @{$_->materials}, map @{$_->layers}, @{$self->objects};
$_->process_bridges for map @{$_->regions}, map @{$_->layers}, @{$self->objects};
# detect which fill surfaces are near external layers
# they will be split in internal and internal-solid surfaces
@ -355,7 +355,7 @@ sub export_gcode {
$_->discover_horizontal_shells for @{$self->objects};
# free memory
$_->surfaces(undef) for map @{$_->materials}, map @{$_->layers}, @{$self->objects};
$_->surfaces(undef) for map @{$_->regions}, map @{$_->layers}, @{$self->objects};
# combine fill surfaces to honor the "infill every N layers" option
$status_cb->(70, "Combining infill");
@ -369,8 +369,8 @@ sub export_gcode {
items => sub {
my @items = (); # [obj_idx, layer_id]
for my $obj_idx (0 .. $#{$self->objects}) {
for my $material_id (0 .. ($self->materials_count-1)) {
push @items, map [$obj_idx, $_, $material_id], 0..($self->objects->[$obj_idx]->layer_count-1);
for my $region_id (0 .. ($self->regions_count-1)) {
push @items, map [$obj_idx, $_, $region_id], 0..($self->objects->[$obj_idx]->layer_count-1);
}
}
@items;
@ -380,11 +380,11 @@ sub export_gcode {
$Slic3r::Geometry::Clipper::clipper = Math::Clipper->new;
my $fills = {};
while (defined (my $obj_layer = $q->dequeue)) {
my ($obj_idx, $layer_id, $material_id) = @$obj_layer;
my ($obj_idx, $layer_id, $region_id) = @$obj_layer;
$fills->{$obj_idx} ||= {};
$fills->{$obj_idx}{$layer_id} ||= {};
$fills->{$obj_idx}{$layer_id}{$material_id} = [
$fill_maker->make_fill($self->objects->[$obj_idx]->layers->[$layer_id]->materials->[$material_id]),
$fills->{$obj_idx}{$layer_id}{$region_id} = [
$fill_maker->make_fill($self->objects->[$obj_idx]->layers->[$layer_id]->regions->[$region_id]),
];
}
return $fills;
@ -395,14 +395,14 @@ sub export_gcode {
my $object = $self->objects->[$obj_idx];
foreach my $layer_id (keys %{$fills->{$obj_idx}}) {
my $layer = $object->layers->[$layer_id];
foreach my $material_id (keys %{$fills->{$obj_idx}{$layer_id}}) {
$layer->materials->[$material_id]->fills($fills->{$obj_idx}{$layer_id}{$material_id});
foreach my $region_id (keys %{$fills->{$obj_idx}{$layer_id}}) {
$layer->regions->[$region_id]->fills($fills->{$obj_idx}{$layer_id}{$region_id});
}
}
}
},
no_threads_cb => sub {
foreach my $layerm (map @{$_->materials}, map @{$_->layers}, @{$self->objects}) {
foreach my $layerm (map @{$_->regions}, map @{$_->layers}, @{$self->objects}) {
$layerm->fills([ $fill_maker->make_fill($layerm) ]);
}
},
@ -416,7 +416,7 @@ sub export_gcode {
}
# free memory (note that support material needs fill_surfaces)
$_->fill_surfaces(undef) for map @{$_->materials}, map @{$_->layers}, @{$self->objects};
$_->fill_surfaces(undef) for map @{$_->regions}, map @{$_->layers}, @{$self->objects};
# make skirt
$status_cb->(88, "Generating skirt");
@ -544,7 +544,7 @@ sub make_skirt {
my @layers = map $self->objects->[$obj_idx]->layer($_), 0..($skirt_height-1);
my @layer_points = (
(map @$_, map @$_, map @{$_->slices}, @layers),
(map @$_, map @{$_->thin_walls}, map @{$_->materials}, @layers),
(map @$_, map @{$_->thin_walls}, map @{$_->regions}, @layers),
(map @{$_->unpack->polyline}, map @{$_->support_fills->paths}, grep $_->support_fills, @layers),
);
push @points, map move_points($_, @layer_points), @{$self->objects->[$obj_idx]->copies};
@ -579,7 +579,7 @@ sub make_brim {
my $layer0 = $self->objects->[$obj_idx]->layers->[0];
my @object_islands = (
(map $_->contour, @{$layer0->slices}),
(map { $_->isa('Slic3r::Polygon') ? $_ : $_->grow($grow_distance) } map @{$_->thin_walls}, @{$layer0->materials}),
(map { $_->isa('Slic3r::Polygon') ? $_ : $_->grow($grow_distance) } map @{$_->thin_walls}, @{$layer0->regions}),
(map $_->unpack->polyline->grow($grow_distance), map @{$_->support_fills->paths}, grep $_->support_fills, $layer0),
);
foreach my $copy (@{$self->objects->[$obj_idx]->copies}) {
@ -720,16 +720,16 @@ sub write_gcode {
$gcodegen->shift_x($shift[X] + unscale $copy->[X]);
$gcodegen->shift_y($shift[Y] + unscale $copy->[Y]);
foreach my $material_id (0 .. ($self->materials_count-1)) {
my $layerm = $layer->materials->[$material_id];
my $material = $self->materials->[$material_id];
foreach my $region_id (0 .. ($self->regions_count-1)) {
my $layerm = $layer->regions->[$region_id];
my $region = $self->regions->[$region_id];
# extrude perimeters
$gcode .= $gcodegen->set_extruder($material->extruders->{perimeter});
$gcode .= $gcodegen->set_extruder($region->extruders->{perimeter});
$gcode .= $gcodegen->extrude($_, 'perimeter') for @{ $layerm->perimeters };
# extrude fills
$gcode .= $gcodegen->set_extruder($material->extruders->{infill});
$gcode .= $gcodegen->set_extruder($region->extruders->{infill});
$gcode .= $gcodegen->set_acceleration($Slic3r::Config->infill_acceleration);
for my $fill (@{ $layerm->fills }) {
if ($fill->isa('Slic3r::ExtrusionPath::Collection')) {

View File

@ -8,7 +8,7 @@ use Slic3r::Surface ':types';
has 'print' => (is => 'ro', weak_ref => 1, required => 1);
has 'input_file' => (is => 'rw', required => 0);
has 'meshes' => (is => 'rw', default => sub { [] }); # by material_id
has 'meshes' => (is => 'rw', default => sub { [] }); # by region_id
has 'size' => (is => 'rw', required => 1);
has 'copies' => (is => 'rw', default => sub {[ [0,0] ]});
has 'layers' => (is => 'rw', default => sub { [] });
@ -35,13 +35,13 @@ sub slice {
my %params = @_;
# process facets
for my $material_id (0 .. $#{$self->meshes}) {
my $mesh = $self->meshes->[$material_id]; # ignore undef meshes
for my $region_id (0 .. $#{$self->meshes}) {
my $mesh = $self->meshes->[$region_id]; # ignore undef meshes
my $apply_lines = sub {
my $lines = shift;
foreach my $layer_id (keys %$lines) {
my $layerm = $self->layer($layer_id)->material($material_id);
my $layerm = $self->layer($layer_id)->region($region_id);
push @{$layerm->lines}, @{$lines->{$layer_id}};
}
};
@ -78,11 +78,11 @@ sub slice {
# remove last layer if empty
# (we might have created it because of the $max_layer = ... + 1 code in TriangleMesh)
pop @{$self->layers} if !map @{$_->lines}, @{$self->layers->[-1]->materials};
pop @{$self->layers} if !map @{$_->lines}, @{$self->layers->[-1]->regions};
foreach my $layer (@{ $self->layers }) {
# make sure all layers contain layer material objects for all materials
$layer->material($_) for 0 .. ($self->print->materials_count-1);
# make sure all layers contain layer region objects for all regions
$layer->region($_) for 0 .. ($self->print->regions_count-1);
Slic3r::debugf "Making surfaces for layer %d (slice z = %f):\n",
$layer->id, unscale $layer->slice_z if $Slic3r::debug;
@ -95,7 +95,7 @@ sub slice {
# inside a closed polyline)
# build surfaces from sparse lines
foreach my $layerm (@{$layer->materials}) {
foreach my $layerm (@{$layer->regions}) {
my ($slicing_errors, $loops) = Slic3r::TriangleMesh::make_loops($layerm->lines);
$layer->slicing_errors(1) if $slicing_errors;
$layerm->make_surfaces($loops);
@ -104,7 +104,7 @@ sub slice {
$layerm->lines(undef);
}
# merge all materials' slices to get islands
# merge all regions' slices to get islands
$layer->make_slices;
}
@ -123,19 +123,19 @@ sub slice {
# neighbor layers
Slic3r::debugf "Attempting to repair layer %d\n", $i;
foreach my $material_id (0 .. $#{$layer->materials}) {
my $layerm = $layer->material($material_id);
foreach my $region_id (0 .. $#{$layer->regions}) {
my $layerm = $layer->region($region_id);
my (@upper_surfaces, @lower_surfaces);
for (my $j = $i+1; $j <= $#{$self->layers}; $j++) {
if (!$self->layers->[$j]->slicing_errors) {
@upper_surfaces = @{$self->layers->[$j]->material($material_id)->slices};
@upper_surfaces = @{$self->layers->[$j]->region($region_id)->slices};
last;
}
}
for (my $j = $i-1; $j >= 0; $j--) {
if (!$self->layers->[$j]->slicing_errors) {
@lower_surfaces = @{$self->layers->[$j]->material($material_id)->slices};
@lower_surfaces = @{$self->layers->[$j]->region($region_id)->slices};
last;
}
}
@ -153,7 +153,7 @@ sub slice {
@$diff;
}
# update layer slices after repairing the single materials
# update layer slices after repairing the single regions
$layer->make_slices;
}
@ -177,10 +177,10 @@ sub make_perimeters {
# this algorithm makes sure that almost one perimeter is overlapping
if ($Slic3r::Config->extra_perimeters && $Slic3r::Config->perimeters > 0) {
for my $material_id (0 .. ($self->print->materials_count-1)) {
for my $region_id (0 .. ($self->print->regions_count-1)) {
for my $layer_id (0 .. $self->layer_count-2) {
my $layerm = $self->layers->[$layer_id]->materials->[$material_id];
my $upper_layerm = $self->layers->[$layer_id+1]->materials->[$material_id];
my $layerm = $self->layers->[$layer_id]->regions->[$region_id];
my $upper_layerm = $self->layers->[$layer_id+1]->regions->[$region_id];
my $perimeter_flow_spacing = $layerm->perimeter_flow->spacing;
my $scaled_perimeter_flow_spacing = scale $perimeter_flow_spacing;
@ -253,11 +253,11 @@ sub detect_surfaces_type {
@$expolygons;
};
for my $material_id (0 .. ($self->print->materials_count-1)) {
for my $region_id (0 .. ($self->print->regions_count-1)) {
for (my $i = 0; $i < $self->layer_count; $i++) {
my $layerm = $self->layers->[$i]->materials->[$material_id];
my $layerm = $self->layers->[$i]->regions->[$region_id];
# comparison happens against the *full* slices (considering all materials)
# comparison happens against the *full* slices (considering all regions)
my $upper_layer = $self->layers->[$i+1];
my $lower_layer = $i > 0 ? $self->layers->[$i-1] : undef;
@ -304,7 +304,7 @@ sub detect_surfaces_type {
# clip surfaces to the fill boundaries
foreach my $layer (@{$self->layers}) {
my $layerm = $layer->materials->[$material_id];
my $layerm = $layer->regions->[$region_id];
my $fill_boundaries = [ map @$_, @{$layerm->surfaces} ];
@{$layerm->surfaces} = ();
foreach my $surface (@{$layerm->slices}) {
@ -327,9 +327,9 @@ sub discover_horizontal_shells {
my $area_threshold = scale($Slic3r::flow->spacing) ** 2;
for my $material_id (0 .. ($self->print->materials_count-1)) {
for my $region_id (0 .. ($self->print->regions_count-1)) {
for (my $i = 0; $i < $self->layer_count; $i++) {
my $layerm = $self->layers->[$i]->materials->[$material_id];
my $layerm = $self->layers->[$i]->regions->[$region_id];
foreach my $type (S_TYPE_TOP, S_TYPE_BOTTOM) {
# find surfaces of current type for current layer
# and offset them to take perimeters into account
@ -346,8 +346,8 @@ sub discover_horizontal_shells {
next if $n < 0 || $n >= $self->layer_count;
Slic3r::debugf " looking for neighbors on layer %d...\n", $n;
my @neighbor_surfaces = @{$self->layers->[$n]->materials->[$material_id]->surfaces};
my @neighbor_fill_surfaces = @{$self->layers->[$n]->materials->[$material_id]->fill_surfaces};
my @neighbor_surfaces = @{$self->layers->[$n]->regions->[$region_id]->surfaces};
my @neighbor_fill_surfaces = @{$self->layers->[$n]->regions->[$region_id]->fill_surfaces};
# find intersection between neighbor and current layer's surfaces
# intersections have contours and holes
@ -378,7 +378,7 @@ sub discover_horizontal_shells {
# polygons as $internal; they will be removed by removed_small_features()
# assign resulting inner surfaces to layer
my $neighbor_fill_surfaces = $self->layers->[$n]->materials->[$material_id]->fill_surfaces;
my $neighbor_fill_surfaces = $self->layers->[$n]->regions->[$region_id]->fill_surfaces;
@$neighbor_fill_surfaces = ();
push @$neighbor_fill_surfaces, Slic3r::Surface->new
(expolygon => $_, surface_type => S_TYPE_INTERNAL)
@ -415,10 +415,10 @@ sub combine_infill {
my $area_threshold = scale($Slic3r::flow->spacing) ** 2;
for my $material_id (0 .. ($self->print->materials_count-1)) {
for my $region_id (0 .. ($self->print->regions_count-1)) {
# start from bottom, skip first layer
for (my $i = 1; $i < $self->layer_count; $i++) {
my $layerm = $self->layers->[$i]->materials->[$material_id];
my $layerm = $self->layers->[$i]->regions->[$region_id];
# skip layer if no internal fill surfaces
next if !grep $_->surface_type == S_TYPE_INTERNAL, @{$layerm->fill_surfaces};
@ -427,7 +427,7 @@ sub combine_infill {
# we do this from the greater depth to the smaller
for (my $d = $Slic3r::Config->infill_every_layers - 1; $d >= 1; $d--) {
next if ($i - $d) < 0;
my $lower_layerm = $self->layer($i - 1)->materials->[$material_id];
my $lower_layerm = $self->layer($i - 1)->regions->[$region_id];
# select surfaces of the lower layer having the depth we're looking for
my @lower_surfaces = grep $_->depth_layers == $d && $_->surface_type == S_TYPE_INTERNAL,

View File

@ -1,4 +1,4 @@
package Slic3r::Print::Material;
package Slic3r::Print::Region;
use Moo;
has 'extruders' => (is => 'rw', default => sub { {} }); # by role