~ubuntu-branches/ubuntu/utopic/slic3r/utopic

« back to all changes in this revision

Viewing changes to lib/Slic3r/Print/Object.pm

  • Committer: Package Import Robot
  • Author(s): Chow Loong Jin
  • Date: 2014-06-17 01:27:26 UTC
  • Revision ID: package-import@ubuntu.com-20140617012726-2wrs4zdo251nr4vg
Tags: upstream-1.1.4+dfsg
ImportĀ upstreamĀ versionĀ 1.1.4+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package Slic3r::Print::Object;
 
2
use Moo;
 
3
 
 
4
use List::Util qw(min max sum first);
 
5
use Slic3r::Flow ':roles';
 
6
use Slic3r::Geometry qw(X Y Z PI scale unscale deg2rad rad2deg scaled_epsilon chained_path);
 
7
use Slic3r::Geometry::Clipper qw(diff diff_ex intersection intersection_ex union union_ex 
 
8
    offset offset_ex offset2 offset2_ex CLIPPER_OFFSET_SCALE JT_MITER);
 
9
use Slic3r::Print::State ':steps';
 
10
use Slic3r::Surface ':types';
 
11
 
 
12
has 'print'             => (is => 'ro', weak_ref => 1, required => 1);
 
13
has 'model_object'      => (is => 'ro', required => 1);  # caller is responsible for holding the Model object
 
14
has 'region_volumes'    => (is => 'rw', default => sub { [] });  # by region_id
 
15
has 'copies'            => (is => 'ro');  # Slic3r::Point objects in scaled G-code coordinates
 
16
has 'config'            => (is => 'ro', default => sub { Slic3r::Config::PrintObject->new });
 
17
has 'layer_height_ranges' => (is => 'rw', default => sub { [] }); # [ z_min, z_max, layer_height ]
 
18
 
 
19
has 'size'              => (is => 'rw'); # XYZ in scaled coordinates
 
20
has '_copies_shift'     => (is => 'rw');  # scaled coordinates to add to copies (to compensate for the alignment operated when creating the object but still preserving a coherent API for external callers)
 
21
has '_shifted_copies'   => (is => 'rw');  # Slic3r::Point objects in scaled G-code coordinates in our coordinates
 
22
has 'layers'            => (is => 'rw', default => sub { [] });
 
23
has 'support_layers'    => (is => 'rw', default => sub { [] });
 
24
has 'fill_maker'        => (is => 'lazy');
 
25
has '_state'            => (is => 'ro', default => sub { Slic3r::Print::State->new });
 
26
 
 
27
sub BUILD {
 
28
    my ($self) = @_;
 
29
    
 
30
    # Compute the translation to be applied to our meshes so that we work with smaller coordinates
 
31
        {
 
32
            my $bb = $self->model_object->bounding_box;
 
33
            
 
34
            # Translate meshes so that our toolpath generation algorithms work with smaller
 
35
            # XY coordinates; this translation is an optimization and not strictly required.
 
36
            # A cloned mesh will be aligned to 0 before slicing in _slice_region() since we
 
37
            #Ā don't assume it's already aligned and we don't alter the original position in model.
 
38
            # We store the XY translation so that we can place copies correctly in the output G-code
 
39
            # (copies are expressed in G-code coordinates and this translation is not publicly exposed).
 
40
            $self->_copies_shift(Slic3r::Point->new_scale($bb->x_min, $bb->y_min));
 
41
        $self->_trigger_copies;
 
42
            
 
43
            # Scale the object size and store it
 
44
            my $scaled_bb = $bb->clone;
 
45
            $scaled_bb->scale(1 / &Slic3r::SCALING_FACTOR);
 
46
            $self->size($scaled_bb->size);
 
47
        }
 
48
 }
 
49
 
 
50
sub _build_fill_maker {
 
51
    my $self = shift;
 
52
    return Slic3r::Fill->new(bounding_box => $self->bounding_box);
 
53
}
 
54
 
 
55
sub _trigger_copies {
 
56
    my $self = shift;
 
57
    
 
58
    return if !defined $self->_copies_shift;
 
59
    
 
60
    # order copies with a nearest neighbor search and translate them by _copies_shift
 
61
    $self->_shifted_copies([
 
62
        map {
 
63
            my $c = $_->clone;
 
64
            $c->translate(@{ $self->_copies_shift });
 
65
            $c;
 
66
        } @{$self->copies}[@{chained_path($self->copies)}]
 
67
    ]);
 
68
    
 
69
    $self->print->_state->invalidate(STEP_SKIRT);
 
70
    $self->print->_state->invalidate(STEP_BRIM);
 
71
}
 
72
 
 
73
# in unscaled coordinates
 
74
sub add_copy {
 
75
    my ($self, $x, $y) = @_;
 
76
    push @{$self->copies}, Slic3r::Point->new_scale($x, $y);
 
77
    $self->_trigger_copies;
 
78
}
 
79
 
 
80
sub delete_last_copy {
 
81
    my ($self) = @_;
 
82
    pop @{$self->copies};
 
83
    $self->_trigger_copies;
 
84
}
 
85
 
 
86
sub delete_all_copies {
 
87
    my ($self) = @_;
 
88
    @{$self->copies} = ();
 
89
    $self->_trigger_copies;
 
90
}
 
91
 
 
92
# this is the *total* layer count
 
93
# this value is not supposed to be compared with $layer->id
 
94
# since they have different semantics
 
95
sub layer_count {
 
96
    my $self = shift;
 
97
    return scalar @{ $self->layers } + scalar @{ $self->support_layers };
 
98
}
 
99
 
 
100
sub bounding_box {
 
101
    my $self = shift;
 
102
    
 
103
    # since the object is aligned to origin, bounding box coincides with size
 
104
    return Slic3r::Geometry::BoundingBox->new_from_points([
 
105
        Slic3r::Point->new(0,0),
 
106
        map Slic3r::Point->new($_->x, $_->y), $self->size  #))
 
107
    ]);
 
108
}
 
109
 
 
110
# this should be idempotent
 
111
sub slice {
 
112
    my $self = shift;
 
113
    my %params = @_;
 
114
    
 
115
    # init layers
 
116
    {
 
117
        @{$self->layers} = ();
 
118
    
 
119
        # make layers taking custom heights into account
 
120
        my $print_z = my $slice_z = my $height = my $id = 0;
 
121
        my $first_object_layer_height = -1;
 
122
    
 
123
        # add raft layers
 
124
        if ($self->config->raft_layers > 0) {
 
125
            $id += $self->config->raft_layers;
 
126
        
 
127
            # raise first object layer Z by the thickness of the raft itself
 
128
            # plus the extra distance required by the support material logic
 
129
            $print_z += $self->config->get_value('first_layer_height');
 
130
            $print_z += $self->config->layer_height * ($self->config->raft_layers - 1);
 
131
        
 
132
            # at this stage we don't know which nozzles are actually used for the first layer
 
133
            # so we compute the average of all of them
 
134
            my $nozzle_diameter = sum(@{$self->print->config->nozzle_diameter})/@{$self->print->config->nozzle_diameter};
 
135
            my $distance = Slic3r::Print::SupportMaterial::contact_distance($nozzle_diameter);
 
136
        
 
137
            # force first layer print_z according to the contact distance
 
138
            # (the loop below will raise print_z by such height)
 
139
            $first_object_layer_height = $distance;
 
140
        }
 
141
    
 
142
        # loop until we have at least one layer and the max slice_z reaches the object height
 
143
        my $max_z = unscale($self->size->z);
 
144
        while (($slice_z - $height) <= $max_z) {
 
145
            # assign the default height to the layer according to the general settings
 
146
            $height = ($id == 0)
 
147
                ? $self->config->get_value('first_layer_height')
 
148
                : $self->config->layer_height;
 
149
        
 
150
            # look for an applicable custom range
 
151
            if (my $range = first { $_->[0] <= $slice_z && $_->[1] > $slice_z } @{$self->layer_height_ranges}) {
 
152
                $height = $range->[2];
 
153
        
 
154
                # if user set custom height to zero we should just skip the range and resume slicing over it
 
155
                if ($height == 0) {
 
156
                    $slice_z += $range->[1] - $range->[0];
 
157
                    next;
 
158
                }
 
159
            }
 
160
            
 
161
            if ($first_object_layer_height != -1 && !@{$self->layers}) {
 
162
                $height = $first_object_layer_height;
 
163
            }
 
164
            
 
165
            $print_z += $height;
 
166
            $slice_z += $height/2;
 
167
        
 
168
            ### Slic3r::debugf "Layer %d: height = %s; slice_z = %s; print_z = %s\n", $id, $height, $slice_z, $print_z;
 
169
        
 
170
            push @{$self->layers}, Slic3r::Layer->new(
 
171
                object  => $self,
 
172
                id      => $id,
 
173
                height  => $height,
 
174
                print_z => $print_z,
 
175
                slice_z => $slice_z,
 
176
            );
 
177
            if (@{$self->layers} >= 2) {
 
178
                $self->layers->[-2]->upper_layer($self->layers->[-1]);
 
179
                $self->layers->[-1]->lower_layer($self->layers->[-2]);
 
180
            }
 
181
            $id++;
 
182
        
 
183
            $slice_z += $height/2;   # add the other half layer
 
184
        }
 
185
    }
 
186
    
 
187
    # make sure all layers contain layer region objects for all regions
 
188
    my $regions_count = $self->print->regions_count;
 
189
    foreach my $layer (@{ $self->layers }) {
 
190
        $layer->region($_) for 0 .. ($regions_count-1);
 
191
    }
 
192
    
 
193
    # get array of Z coordinates for slicing
 
194
    my @z = map $_->slice_z, @{$self->layers};
 
195
    
 
196
    # slice all non-modifier volumes
 
197
    for my $region_id (0..$#{$self->region_volumes}) {
 
198
        my $expolygons_by_layer = $self->_slice_region($region_id, \@z, 0);
 
199
        for my $layer_id (0..$#$expolygons_by_layer) {
 
200
            my $layerm = $self->layers->[$layer_id]->regions->[$region_id];
 
201
            $layerm->slices->clear;
 
202
            foreach my $expolygon (@{ $expolygons_by_layer->[$layer_id] }) {
 
203
                $layerm->slices->append(Slic3r::Surface->new(
 
204
                    expolygon    => $expolygon,
 
205
                    surface_type => S_TYPE_INTERNAL,
 
206
                ));
 
207
            }
 
208
        }
 
209
    }
 
210
    
 
211
    # then slice all modifier volumes
 
212
    if (@{$self->region_volumes} > 1) {
 
213
        for my $region_id (0..$#{$self->region_volumes}) {
 
214
            my $expolygons_by_layer = $self->_slice_region($region_id, \@z, 1);
 
215
            
 
216
            # loop through the other regions and 'steal' the slices belonging to this one
 
217
            for my $other_region_id (0..$#{$self->region_volumes}) {
 
218
                next if $other_region_id == $region_id;
 
219
                
 
220
                for my $layer_id (0..$#$expolygons_by_layer) {
 
221
                    my $layerm = $self->layers->[$layer_id]->regions->[$region_id];
 
222
                    my $other_layerm = $self->layers->[$layer_id]->regions->[$other_region_id];
 
223
                    
 
224
                    my $other_slices = [ map $_->p, @{$other_layerm->slices} ];  # Polygons
 
225
                    my $my_parts = intersection_ex(
 
226
                        $other_slices,
 
227
                        [ map @$_, @{ $expolygons_by_layer->[$layer_id] } ],
 
228
                    );
 
229
                    next if !@$my_parts;
 
230
                    
 
231
                    # append new parts to our region
 
232
                    foreach my $expolygon (@$my_parts) {
 
233
                        $layerm->slices->append(Slic3r::Surface->new(
 
234
                            expolygon    => $expolygon,
 
235
                            surface_type => S_TYPE_INTERNAL,
 
236
                        ));
 
237
                    }
 
238
                    
 
239
                    # remove such parts from original region
 
240
                    $other_layerm->slices->clear;
 
241
                    $other_layerm->slices->append(Slic3r::Surface->new(
 
242
                        expolygon    => $_,
 
243
                        surface_type => S_TYPE_INTERNAL,
 
244
                    )) for @{ diff_ex($other_slices, [ map @$_, @$my_parts ]) };
 
245
                }
 
246
            }
 
247
        }
 
248
    }
 
249
    
 
250
    # remove last layer(s) if empty
 
251
    pop @{$self->layers} while @{$self->layers} && (!map @{$_->slices}, @{$self->layers->[-1]->regions});
 
252
    
 
253
    foreach my $layer (@{ $self->layers }) {
 
254
        # merge all regions' slices to get islands
 
255
        $layer->make_slices;
 
256
    }
 
257
    
 
258
    # detect slicing errors
 
259
    my $warning_thrown = 0;
 
260
    for my $i (0 .. $#{$self->layers}) {
 
261
        my $layer = $self->layers->[$i];
 
262
        next unless $layer->slicing_errors;
 
263
        if (!$warning_thrown) {
 
264
            warn "The model has overlapping or self-intersecting facets. I tried to repair it, "
 
265
                . "however you might want to check the results or repair the input file and retry.\n";
 
266
            $warning_thrown = 1;
 
267
        }
 
268
        
 
269
        # try to repair the layer surfaces by merging all contours and all holes from
 
270
        # neighbor layers
 
271
        Slic3r::debugf "Attempting to repair layer %d\n", $i;
 
272
        
 
273
        foreach my $region_id (0 .. $#{$layer->regions}) {
 
274
            my $layerm = $layer->region($region_id);
 
275
            
 
276
            my (@upper_surfaces, @lower_surfaces);
 
277
            for (my $j = $i+1; $j <= $#{$self->layers}; $j++) {
 
278
                if (!$self->layers->[$j]->slicing_errors) {
 
279
                    @upper_surfaces = @{$self->layers->[$j]->region($region_id)->slices};
 
280
                    last;
 
281
                }
 
282
            }
 
283
            for (my $j = $i-1; $j >= 0; $j--) {
 
284
                if (!$self->layers->[$j]->slicing_errors) {
 
285
                    @lower_surfaces = @{$self->layers->[$j]->region($region_id)->slices};
 
286
                    last;
 
287
                }
 
288
            }
 
289
            
 
290
            my $union = union_ex([
 
291
                map $_->expolygon->contour, @upper_surfaces, @lower_surfaces,
 
292
            ]);
 
293
            my $diff = diff_ex(
 
294
                [ map @$_, @$union ],
 
295
                [ map @{$_->expolygon->holes}, @upper_surfaces, @lower_surfaces, ],
 
296
            );
 
297
            
 
298
            $layerm->slices->clear;
 
299
            $layerm->slices->append(
 
300
                map Slic3r::Surface->new
 
301
                    (expolygon => $_, surface_type => S_TYPE_INTERNAL),
 
302
                    @$diff
 
303
            );
 
304
        }
 
305
            
 
306
        # update layer slices after repairing the single regions
 
307
        $layer->make_slices;
 
308
    }
 
309
    
 
310
    # remove empty layers from bottom
 
311
    while (@{$self->layers} && !@{$self->layers->[0]->slices}) {
 
312
        shift @{$self->layers};
 
313
        for (my $i = 0; $i <= $#{$self->layers}; $i++) {
 
314
            $self->layers->[$i]->id( $self->layers->[$i]->id-1 );
 
315
        }
 
316
    }
 
317
    
 
318
    # simplify slices if required
 
319
    if ($self->print->config->resolution) {
 
320
        $self->_simplify_slices(scale($self->print->config->resolution));
 
321
    }
 
322
}
 
323
 
 
324
sub _slice_region {
 
325
    my ($self, $region_id, $z, $modifier) = @_;
 
326
 
 
327
    return [] if !defined $self->region_volumes->[$region_id];
 
328
    
 
329
    # compose mesh
 
330
    my $mesh;
 
331
    foreach my $volume_id (@{$self->region_volumes->[$region_id]}) {
 
332
        my $volume = $self->model_object->volumes->[$volume_id];
 
333
        next if $volume->modifier && !$modifier;
 
334
        next if !$volume->modifier && $modifier;
 
335
        
 
336
        if (defined $mesh) {
 
337
            $mesh->merge($volume->mesh);
 
338
        } else {
 
339
            $mesh = $volume->mesh->clone;
 
340
        }
 
341
    }
 
342
    return if !defined $mesh;
 
343
 
 
344
    # transform mesh
 
345
    # we ignore the per-instance transformations currently and only 
 
346
    # consider the first one
 
347
    $self->model_object->instances->[0]->transform_mesh($mesh, 1);
 
348
 
 
349
    # align mesh to Z = 0 and apply XY shift
 
350
    $mesh->translate((map unscale(-$_), @{$self->_copies_shift}), -$self->model_object->bounding_box->z_min);
 
351
    
 
352
    # perform actual slicing
 
353
    return $mesh->slice($z);
 
354
}
 
355
 
 
356
sub make_perimeters {
 
357
    my $self = shift;
 
358
    
 
359
    # compare each layer to the one below, and mark those slices needing
 
360
    # one additional inner perimeter, like the top of domed objects-
 
361
    
 
362
    # this algorithm makes sure that at least one perimeter is overlapping
 
363
    # but we don't generate any extra perimeter if fill density is zero, as they would be floating
 
364
    # inside the object - infill_only_where_needed should be the method of choice for printing
 
365
    #Ā hollow objects
 
366
    for my $region_id (0 .. ($self->print->regions_count-1)) {
 
367
        my $region = $self->print->regions->[$region_id];
 
368
        my $region_perimeters = $region->config->perimeters;
 
369
        
 
370
        if ($region->config->extra_perimeters && $region_perimeters > 0 && $region->config->fill_density > 0) {
 
371
            for my $i (0 .. $#{$self->layers}-1) {
 
372
                my $layerm          = $self->layers->[$i]->regions->[$region_id];
 
373
                my $upper_layerm    = $self->layers->[$i+1]->regions->[$region_id];
 
374
                my $perimeter_spacing       = $layerm->flow(FLOW_ROLE_PERIMETER)->scaled_spacing;
 
375
                
 
376
                my $overlap = $perimeter_spacing;  # one perimeter
 
377
                
 
378
                my $diff = diff(
 
379
                    offset([ map @{$_->expolygon}, @{$layerm->slices} ], -($region_perimeters * $perimeter_spacing)),
 
380
                    offset([ map @{$_->expolygon}, @{$upper_layerm->slices} ], -$overlap),
 
381
                );
 
382
                next if !@$diff;
 
383
                # if we need more perimeters, $diff should contain a narrow region that we can collapse
 
384
                
 
385
                # we use a higher miterLimit here to handle areas with acute angles
 
386
                # in those cases, the default miterLimit would cut the corner and we'd
 
387
                # get a triangle that would trigger a non-needed extra perimeter
 
388
                $diff = diff(
 
389
                    $diff,
 
390
                    offset2($diff, -$perimeter_spacing, +$perimeter_spacing, CLIPPER_OFFSET_SCALE, JT_MITER, 5),
 
391
                    1,
 
392
                );
 
393
                next if !@$diff;
 
394
                # diff contains the collapsed area
 
395
                
 
396
                foreach my $slice (@{$layerm->slices}) {
 
397
                    my $extra_perimeters = 0;
 
398
                    CYCLE: while (1) {
 
399
                        # compute polygons representing the thickness of the hypotetical new internal perimeter
 
400
                        # of our slice
 
401
                        $extra_perimeters++;
 
402
                        my $hypothetical_perimeter = diff(
 
403
                            offset($slice->expolygon->arrayref, -($perimeter_spacing * ($region_perimeters + $extra_perimeters-1))),
 
404
                            offset($slice->expolygon->arrayref, -($perimeter_spacing * ($region_perimeters + $extra_perimeters))),
 
405
                        );
 
406
                        last CYCLE if !@$hypothetical_perimeter;  # no extra perimeter is possible
 
407
                        
 
408
                        # only add the perimeter if there's an intersection with the collapsed area
 
409
                        last CYCLE if !@{ intersection($diff, $hypothetical_perimeter) };
 
410
                        Slic3r::debugf "  adding one more perimeter at layer %d\n", $layerm->id;
 
411
                        $slice->extra_perimeters($extra_perimeters);
 
412
                    }
 
413
                }
 
414
            }
 
415
        }
 
416
    }
 
417
    
 
418
    Slic3r::parallelize(
 
419
        threads => $self->print->config->threads,
 
420
        items => sub { 0 .. $#{$self->layers} },
 
421
        thread_cb => sub {
 
422
            my $q = shift;
 
423
            while (defined (my $i = $q->dequeue)) {
 
424
                $self->layers->[$i]->make_perimeters;
 
425
            }
 
426
        },
 
427
        collect_cb => sub {},
 
428
        no_threads_cb => sub {
 
429
            $_->make_perimeters for @{$self->layers};
 
430
        },
 
431
    );
 
432
    
 
433
    # simplify slices (both layer and region slices),
 
434
    # we only need the max resolution for perimeters
 
435
    ### This makes this method not-idempotent, so we keep it disabled for now.
 
436
    ###$self->_simplify_slices(&Slic3r::SCALED_RESOLUTION);
 
437
}
 
438
 
 
439
sub detect_surfaces_type {
 
440
    my $self = shift;
 
441
    Slic3r::debugf "Detecting solid surfaces...\n";
 
442
    
 
443
    for my $region_id (0 .. ($self->print->regions_count-1)) {
 
444
        for my $i (0 .. $#{$self->layers}) {
 
445
            my $layerm = $self->layers->[$i]->regions->[$region_id];
 
446
        
 
447
            # prepare a reusable subroutine to make surface differences
 
448
            my $difference = sub {
 
449
                my ($subject, $clip, $result_type) = @_;
 
450
                my $diff = diff(
 
451
                    [ map @$_, @$subject ],
 
452
                    [ map @$_, @$clip ],
 
453
                );
 
454
                
 
455
                # collapse very narrow parts (using the safety offset in the diff is not enough)
 
456
                my $offset = $layerm->flow(FLOW_ROLE_PERIMETER)->scaled_width / 10;
 
457
                return map Slic3r::Surface->new(expolygon => $_, surface_type => $result_type),
 
458
                    @{ offset2_ex($diff, -$offset, +$offset) };
 
459
            };
 
460
            
 
461
            # comparison happens against the *full* slices (considering all regions)
 
462
            # unless internal shells are requested
 
463
            my $upper_layer = $self->layers->[$i+1];
 
464
            my $lower_layer = $i > 0 ? $self->layers->[$i-1] : undef;
 
465
            
 
466
            # find top surfaces (difference between current surfaces
 
467
            # of current layer and upper one)
 
468
            my @top = ();
 
469
            if ($upper_layer) {
 
470
                my $upper_slices = $self->config->interface_shells
 
471
                    ? [ map $_->expolygon, @{$upper_layer->regions->[$region_id]->slices} ]
 
472
                    : $upper_layer->slices;
 
473
                
 
474
                @top = $difference->(
 
475
                    [ map $_->expolygon, @{$layerm->slices} ],
 
476
                    $upper_slices,
 
477
                    S_TYPE_TOP,
 
478
                );
 
479
            } else {
 
480
                # if no upper layer, all surfaces of this one are solid
 
481
                # we clone surfaces because we're going to clear the slices collection
 
482
                @top = map $_->clone, @{$layerm->slices};
 
483
                $_->surface_type(S_TYPE_TOP) for @top;
 
484
            }
 
485
            
 
486
            # find bottom surfaces (difference between current surfaces
 
487
            # of current layer and lower one)
 
488
            my @bottom = ();
 
489
            if ($lower_layer) {
 
490
                # any surface lying on the void is a true bottom bridge
 
491
                push @bottom, $difference->(
 
492
                    [ map $_->expolygon, @{$layerm->slices} ],
 
493
                    $lower_layer->slices,
 
494
                    S_TYPE_BOTTOMBRIDGE,
 
495
                );
 
496
                
 
497
                # if user requested internal shells, we need to identify surfaces
 
498
                # lying on other slices not belonging to this region
 
499
                if ($self->config->interface_shells) {
 
500
                    # non-bridging bottom surfaces: any part of this layer lying 
 
501
                    # on something else, excluding those lying on our own region
 
502
                    my $supported = intersection_ex(
 
503
                        [ map @{$_->expolygon}, @{$layerm->slices} ],
 
504
                        [ map @$_, @{$lower_layer->slices} ],
 
505
                    );
 
506
                    push @bottom, $difference->(
 
507
                        $supported,
 
508
                        [ map $_->expolygon, @{$lower_layer->regions->[$region_id]->slices} ],
 
509
                        S_TYPE_BOTTOM,
 
510
                    );
 
511
                }
 
512
            } else {
 
513
                # if no lower layer, all surfaces of this one are solid
 
514
                # we clone surfaces because we're going to clear the slices collection
 
515
                @bottom = map $_->clone, @{$layerm->slices};
 
516
                $_->surface_type(S_TYPE_BOTTOM) for @bottom;
 
517
            }
 
518
            
 
519
            # now, if the object contained a thin membrane, we could have overlapping bottom
 
520
            # and top surfaces; let's do an intersection to discover them and consider them
 
521
            # as bottom surfaces (to allow for bridge detection)
 
522
            if (@top && @bottom) {
 
523
                my $overlapping = intersection_ex([ map $_->p, @top ], [ map $_->p, @bottom ]);
 
524
                Slic3r::debugf "  layer %d contains %d membrane(s)\n", $layerm->id, scalar(@$overlapping)
 
525
                    if $Slic3r::debug;
 
526
                @top = $difference->([map $_->expolygon, @top], $overlapping, S_TYPE_TOP);
 
527
            }
 
528
            
 
529
            # find internal surfaces (difference between top/bottom surfaces and others)
 
530
            my @internal = $difference->(
 
531
                [ map $_->expolygon, @{$layerm->slices} ],
 
532
                [ map $_->expolygon, @top, @bottom ],
 
533
                S_TYPE_INTERNAL,
 
534
            );
 
535
            
 
536
            # save surfaces to layer
 
537
            $layerm->slices->clear;
 
538
            $layerm->slices->append(@bottom, @top, @internal);
 
539
            
 
540
            Slic3r::debugf "  layer %d has %d bottom, %d top and %d internal surfaces\n",
 
541
                $layerm->id, scalar(@bottom), scalar(@top), scalar(@internal) if $Slic3r::debug;
 
542
        }
 
543
        
 
544
        # clip surfaces to the fill boundaries
 
545
        foreach my $layer (@{$self->layers}) {
 
546
            my $layerm = $layer->regions->[$region_id];
 
547
            my $fill_boundaries = [ map $_->clone->p, @{$layerm->fill_surfaces} ];
 
548
            $layerm->fill_surfaces->clear;
 
549
            foreach my $surface (@{$layerm->slices}) {
 
550
                my $intersection = intersection_ex(
 
551
                    [ $surface->p ],
 
552
                    $fill_boundaries,
 
553
                );
 
554
                $layerm->fill_surfaces->append(map Slic3r::Surface->new
 
555
                    (expolygon => $_, surface_type => $surface->surface_type),
 
556
                    @$intersection);
 
557
            }
 
558
        }
 
559
    }
 
560
}
 
561
 
 
562
sub clip_fill_surfaces {
 
563
    my $self = shift;
 
564
    return unless $self->config->infill_only_where_needed;
 
565
    
 
566
    # We only want infill under ceilings; this is almost like an
 
567
    # internal support material.
 
568
    
 
569
    my $additional_margin = scale 3*0;
 
570
    
 
571
    my $overhangs = [];  # arrayref of polygons
 
572
    for my $layer_id (reverse 0..$#{$self->layers}) {
 
573
        my $layer = $self->layers->[$layer_id];
 
574
        my @layer_internal = ();  # arrayref of Surface objects
 
575
        my @new_internal = ();    # arrayref of Surface objects
 
576
        
 
577
        # clip this layer's internal surfaces to @overhangs
 
578
        foreach my $layerm (@{$layer->regions}) {
 
579
            # we assume that this step is run before bridge_over_infill() and combine_infill()
 
580
            # so these are the only internal types we might have
 
581
            my (@internal, @other) = ();
 
582
            foreach my $surface (map $_->clone, @{$layerm->fill_surfaces}) {
 
583
                $surface->surface_type == S_TYPE_INTERNAL
 
584
                    ? push @internal, $surface
 
585
                    : push @other, $surface;
 
586
            }
 
587
            
 
588
            # keep all the original internal surfaces to detect overhangs in this layer
 
589
            push @layer_internal, @internal;
 
590
            
 
591
            push @new_internal, my @new = map Slic3r::Surface->new(
 
592
                expolygon       => $_,
 
593
                surface_type    => S_TYPE_INTERNAL,
 
594
            ),
 
595
            @{intersection_ex(
 
596
                $overhangs,
 
597
                [ map $_->p, @internal ],
 
598
            )};
 
599
            
 
600
            $layerm->fill_surfaces->clear;
 
601
            $layerm->fill_surfaces->append(@new, @other);
 
602
        }
 
603
        
 
604
        # get this layer's overhangs defined as the full slice minus the internal infill
 
605
        # (thus we also consider perimeters)
 
606
        if ($layer_id > 0) {
 
607
            my $solid = diff(
 
608
                [ map $_->p, map @{$_->fill_surfaces}, @{$layer->regions} ],
 
609
                [ map $_->p, @layer_internal ],
 
610
            );
 
611
            $overhangs = offset($solid, +$additional_margin);
 
612
            
 
613
            push @$overhangs, map $_->p, @new_internal;  # propagate upper overhangs
 
614
        }
 
615
    }
 
616
}
 
617
 
 
618
sub bridge_over_infill {
 
619
    my $self = shift;
 
620
    
 
621
    for my $region_id (0..$#{$self->print->regions}) {
 
622
        my $fill_density = $self->print->regions->[$region_id]->config->fill_density;
 
623
        next if $fill_density == 100 || $fill_density == 0;
 
624
        
 
625
        for my $layer_id (1..$#{$self->layers}) {
 
626
            my $layer       = $self->layers->[$layer_id];
 
627
            my $layerm      = $layer->regions->[$region_id];
 
628
            my $lower_layer = $self->layers->[$layer_id-1];
 
629
            
 
630
            # compute the areas needing bridge math 
 
631
            my @internal_solid = @{$layerm->fill_surfaces->filter_by_type(S_TYPE_INTERNALSOLID)};
 
632
            my @lower_internal = map @{$_->fill_surfaces->filter_by_type(S_TYPE_INTERNAL)}, @{$lower_layer->regions};
 
633
            my $to_bridge = intersection_ex(
 
634
                [ map $_->p, @internal_solid ],
 
635
                [ map $_->p, @lower_internal ],
 
636
            );
 
637
            next unless @$to_bridge;
 
638
            Slic3r::debugf "Bridging %d internal areas at layer %d\n", scalar(@$to_bridge), $layer_id;
 
639
            
 
640
            # build the new collection of fill_surfaces
 
641
            {
 
642
                my @new_surfaces = map $_->clone, grep $_->surface_type != S_TYPE_INTERNALSOLID, @{$layerm->fill_surfaces};
 
643
                push @new_surfaces, map Slic3r::Surface->new(
 
644
                        expolygon       => $_,
 
645
                        surface_type    => S_TYPE_INTERNALBRIDGE,
 
646
                    ), @$to_bridge;
 
647
                push @new_surfaces, map Slic3r::Surface->new(
 
648
                        expolygon       => $_,
 
649
                        surface_type    => S_TYPE_INTERNALSOLID,
 
650
                    ), @{diff_ex(
 
651
                        [ map $_->p, @internal_solid ],
 
652
                        [ map @$_, @$to_bridge ],
 
653
                        1,
 
654
                    )};
 
655
                $layerm->fill_surfaces->clear;
 
656
                $layerm->fill_surfaces->append(@new_surfaces);
 
657
            }
 
658
            
 
659
            # exclude infill from the layers below if needed
 
660
            # see discussion at https://github.com/alexrj/Slic3r/issues/240
 
661
            # Update: do not exclude any infill. Sparse infill is able to absorb the excess material.
 
662
            if (0) {
 
663
                my $excess = $layerm->extruders->{infill}->bridge_flow->width - $layerm->height;
 
664
                for (my $i = $layer_id-1; $excess >= $self->layers->[$i]->height; $i--) {
 
665
                    Slic3r::debugf "  skipping infill below those areas at layer %d\n", $i;
 
666
                    foreach my $lower_layerm (@{$self->layers->[$i]->regions}) {
 
667
                        my @new_surfaces = ();
 
668
                        # subtract the area from all types of surfaces
 
669
                        foreach my $group (@{$lower_layerm->fill_surfaces->group}) {
 
670
                            push @new_surfaces, map $group->[0]->clone(expolygon => $_),
 
671
                                @{diff_ex(
 
672
                                    [ map $_->p, @$group ],
 
673
                                    [ map @$_, @$to_bridge ],
 
674
                                )};
 
675
                            push @new_surfaces, map Slic3r::Surface->new(
 
676
                                expolygon       => $_,
 
677
                                surface_type    => S_TYPE_INTERNALVOID,
 
678
                            ), @{intersection_ex(
 
679
                                [ map $_->p, @$group ],
 
680
                                [ map @$_, @$to_bridge ],
 
681
                            )};
 
682
                        }
 
683
                        $lower_layerm->fill_surfaces->clear;
 
684
                        $lower_layerm->fill_surfaces->append(@new_surfaces);
 
685
                    }
 
686
                    
 
687
                    $excess -= $self->layers->[$i]->height;
 
688
                }
 
689
            }
 
690
        }
 
691
    }
 
692
}
 
693
 
 
694
sub process_external_surfaces {
 
695
    my ($self) = @_;
 
696
    
 
697
    for my $region_id (0 .. ($self->print->regions_count-1)) {
 
698
        $self->layers->[0]->regions->[$region_id]->process_external_surfaces(undef);
 
699
        for my $i (1 .. $#{$self->layers}) {
 
700
            $self->layers->[$i]->regions->[$region_id]->process_external_surfaces($self->layers->[$i-1]);
 
701
        }
 
702
    }
 
703
}
 
704
 
 
705
sub discover_horizontal_shells {
 
706
    my $self = shift;
 
707
    
 
708
    Slic3r::debugf "==> DISCOVERING HORIZONTAL SHELLS\n";
 
709
    
 
710
    for my $region_id (0 .. ($self->print->regions_count-1)) {
 
711
        for (my $i = 0; $i <= $#{$self->layers}; $i++) {
 
712
            my $layerm = $self->layers->[$i]->regions->[$region_id];
 
713
            
 
714
            if ($layerm->config->solid_infill_every_layers && $layerm->config->fill_density > 0
 
715
                && ($i % $layerm->config->solid_infill_every_layers) == 0) {
 
716
                $_->surface_type(S_TYPE_INTERNALSOLID) for @{$layerm->fill_surfaces->filter_by_type(S_TYPE_INTERNAL)};
 
717
            }
 
718
            
 
719
            EXTERNAL: foreach my $type (S_TYPE_TOP, S_TYPE_BOTTOM, S_TYPE_BOTTOMBRIDGE) {
 
720
                # find slices of current type for current layer
 
721
                # use slices instead of fill_surfaces because they also include the perimeter area
 
722
                # which needs to be propagated in shells; we need to grow slices like we did for
 
723
                # fill_surfaces though.  Using both ungrown slices and grown fill_surfaces will
 
724
                # not work in some situations, as there won't be any grown region in the perimeter 
 
725
                # area (this was seen in a model where the top layer had one extra perimeter, thus
 
726
                # its fill_surfaces were thinner than the lower layer's infill), however it's the best
 
727
                # solution so far. Growing the external slices by EXTERNAL_INFILL_MARGIN will put
 
728
                # too much solid infill inside nearly-vertical slopes.
 
729
                my $solid = [
 
730
                    (map $_->p, @{$layerm->slices->filter_by_type($type)}),
 
731
                    (map $_->p, @{$layerm->fill_surfaces->filter_by_type($type)}),
 
732
                ];
 
733
                next if !@$solid;
 
734
                Slic3r::debugf "Layer %d has %s surfaces\n", $i, ($type == S_TYPE_TOP) ? 'top' : 'bottom';
 
735
                
 
736
                my $solid_layers = ($type == S_TYPE_TOP)
 
737
                    ? $layerm->config->top_solid_layers
 
738
                    : $layerm->config->bottom_solid_layers;
 
739
                NEIGHBOR: for (my $n = ($type == S_TYPE_TOP) ? $i-1 : $i+1; 
 
740
                        abs($n - $i) <= $solid_layers-1; 
 
741
                        ($type == S_TYPE_TOP) ? $n-- : $n++) {
 
742
                    
 
743
                    next if $n < 0 || $n > $#{$self->layers};
 
744
                    Slic3r::debugf "  looking for neighbors on layer %d...\n", $n;
 
745
                    
 
746
                    my $neighbor_layerm = $self->layers->[$n]->regions->[$region_id];
 
747
                    my $neighbor_fill_surfaces = $neighbor_layerm->fill_surfaces;
 
748
                    my @neighbor_fill_surfaces = map $_->clone, @$neighbor_fill_surfaces;  # clone because we will use these surfaces even after clearing the collection
 
749
                    
 
750
                    # find intersection between neighbor and current layer's surfaces
 
751
                    # intersections have contours and holes
 
752
                    # we update $solid so that we limit the next neighbor layer to the areas that were
 
753
                    # found on this one - in other words, solid shells on one layer (for a given external surface)
 
754
                    # are always a subset of the shells found on the previous shell layer
 
755
                    # this approach allows for DWIM in hollow sloping vases, where we want bottom
 
756
                    # shells to be generated in the base but not in the walls (where there are many
 
757
                    # narrow bottom surfaces): reassigning $solid will consider the 'shadow' of the 
 
758
                    # upper perimeter as an obstacle and shell will not be propagated to more upper layers
 
759
                    my $new_internal_solid = $solid = intersection(
 
760
                        $solid,
 
761
                        [ map $_->p, grep { ($_->surface_type == S_TYPE_INTERNAL) || ($_->surface_type == S_TYPE_INTERNALSOLID) } @neighbor_fill_surfaces ],
 
762
                        1,
 
763
                    );
 
764
                    next EXTERNAL if !@$new_internal_solid;
 
765
                    
 
766
                    if ($layerm->config->fill_density == 0) {
 
767
                        # if we're printing a hollow object we discard any solid shell thinner
 
768
                        # than a perimeter width, since it's probably just crossing a sloping wall
 
769
                        # and it's not wanted in a hollow print even if it would make sense when
 
770
                        # obeying the solid shell count option strictly (DWIM!)
 
771
                        my $margin = $neighbor_layerm->flow(FLOW_ROLE_PERIMETER)->scaled_width;
 
772
                        my $too_narrow = diff(
 
773
                            $new_internal_solid,
 
774
                            offset2($new_internal_solid, -$margin, +$margin, CLIPPER_OFFSET_SCALE, JT_MITER, 5),
 
775
                            1,
 
776
                        );
 
777
                        $new_internal_solid = $solid = diff(
 
778
                            $new_internal_solid,
 
779
                            $too_narrow,
 
780
                        ) if @$too_narrow;
 
781
                    }
 
782
                    
 
783
                    # make sure the new internal solid is wide enough, as it might get collapsed
 
784
                    # when spacing is added in Fill.pm
 
785
                    {
 
786
                        my $margin = 3 * $layerm->flow(FLOW_ROLE_SOLID_INFILL)->scaled_width; # require at least this size
 
787
                        # we use a higher miterLimit here to handle areas with acute angles
 
788
                        # in those cases, the default miterLimit would cut the corner and we'd
 
789
                        # get a triangle in $too_narrow; if we grow it below then the shell
 
790
                        # would have a different shape from the external surface and we'd still
 
791
                        # have the same angle, so the next shell would be grown even more and so on.
 
792
                        my $too_narrow = diff(
 
793
                            $new_internal_solid,
 
794
                            offset2($new_internal_solid, -$margin, +$margin, CLIPPER_OFFSET_SCALE, JT_MITER, 5),
 
795
                            1,
 
796
                        );
 
797
                        
 
798
                        if (@$too_narrow) {
 
799
                            # grow the collapsing parts and add the extra area to  the neighbor layer 
 
800
                            # as well as to our original surfaces so that we support this 
 
801
                            # additional area in the next shell too
 
802
                        
 
803
                            # make sure our grown surfaces don't exceed the fill area
 
804
                            my @grown = @{intersection(
 
805
                                offset($too_narrow, +$margin),
 
806
                                [ map $_->p, @neighbor_fill_surfaces ],
 
807
                            )};
 
808
                            $new_internal_solid = $solid = [ @grown, @$new_internal_solid ];
 
809
                        }
 
810
                    }
 
811
                    
 
812
                    # internal-solid are the union of the existing internal-solid surfaces
 
813
                    # and new ones
 
814
                    my $internal_solid = union_ex([
 
815
                        ( map $_->p, grep $_->surface_type == S_TYPE_INTERNALSOLID, @neighbor_fill_surfaces ),
 
816
                        @$new_internal_solid,
 
817
                    ]);
 
818
                    
 
819
                    # subtract intersections from layer surfaces to get resulting internal surfaces
 
820
                    my $internal = diff_ex(
 
821
                        [ map $_->p, grep $_->surface_type == S_TYPE_INTERNAL, @neighbor_fill_surfaces ],
 
822
                        [ map @$_, @$internal_solid ],
 
823
                        1,
 
824
                    );
 
825
                    Slic3r::debugf "    %d internal-solid and %d internal surfaces found\n",
 
826
                        scalar(@$internal_solid), scalar(@$internal);
 
827
                    
 
828
                    # assign resulting internal surfaces to layer
 
829
                    $neighbor_fill_surfaces->clear;
 
830
                    $neighbor_fill_surfaces->append(map Slic3r::Surface->new
 
831
                        (expolygon => $_, surface_type => S_TYPE_INTERNAL), @$internal);
 
832
                    
 
833
                    # assign new internal-solid surfaces to layer
 
834
                    $neighbor_fill_surfaces->append(map Slic3r::Surface->new
 
835
                        (expolygon => $_, surface_type => S_TYPE_INTERNALSOLID), @$internal_solid);
 
836
                    
 
837
                    # assign top and bottom surfaces to layer
 
838
                    foreach my $s (@{Slic3r::Surface::Collection->new(grep { ($_->surface_type == S_TYPE_TOP) || $_->is_bottom } @neighbor_fill_surfaces)->group}) {
 
839
                        my $solid_surfaces = diff_ex(
 
840
                            [ map $_->p, @$s ],
 
841
                            [ map @$_, @$internal_solid, @$internal ],
 
842
                            1,
 
843
                        );
 
844
                        $neighbor_fill_surfaces->append(map $s->[0]->clone(expolygon => $_), @$solid_surfaces);
 
845
                    }
 
846
                }
 
847
            }
 
848
        }
 
849
    }
 
850
}
 
851
 
 
852
# combine fill surfaces across layers
 
853
sub combine_infill {
 
854
    my $self = shift;
 
855
    
 
856
    return unless defined first { $_->config->infill_every_layers > 1 && $_->config->fill_density > 0 } @{$self->print->regions};
 
857
    
 
858
    my @layer_heights = map $_->height, @{$self->layers};
 
859
    
 
860
    for my $region_id (0 .. ($self->print->regions_count-1)) {
 
861
        my $region = $self->print->regions->[$region_id];
 
862
        my $every = $region->config->infill_every_layers;
 
863
        
 
864
        # limit the number of combined layers to the maximum height allowed by this regions' nozzle
 
865
        my $nozzle_diameter = $self->print->config->get_at('nozzle_diameter', $region->config->infill_extruder-1);
 
866
        
 
867
        # define the combinations
 
868
        my @combine = ();   # layer_id => thickness in layers
 
869
        {
 
870
            my $current_height = my $layers = 0;
 
871
            for my $layer_id (1 .. $#layer_heights) {
 
872
                my $height = $self->layers->[$layer_id]->height;
 
873
                
 
874
                if ($current_height + $height >= $nozzle_diameter || $layers >= $every) {
 
875
                    $combine[$layer_id-1] = $layers;
 
876
                    $current_height = $layers = 0;
 
877
                }
 
878
                
 
879
                $current_height += $height;
 
880
                $layers++;
 
881
            }
 
882
        }
 
883
        
 
884
        # skip bottom layer
 
885
        for my $layer_id (1 .. $#combine) {
 
886
            next unless ($combine[$layer_id] // 1) > 1;
 
887
            my @layerms = map $self->layers->[$_]->regions->[$region_id],
 
888
                ($layer_id - ($combine[$layer_id]-1) .. $layer_id);
 
889
            
 
890
            # only combine internal infill
 
891
            for my $type (S_TYPE_INTERNAL) {
 
892
                # we need to perform a multi-layer intersection, so let's split it in pairs
 
893
                
 
894
                # initialize the intersection with the candidates of the lowest layer
 
895
                my $intersection = [ map $_->expolygon, @{$layerms[0]->fill_surfaces->filter_by_type($type)} ];
 
896
                
 
897
                # start looping from the second layer and intersect the current intersection with it
 
898
                for my $layerm (@layerms[1 .. $#layerms]) {
 
899
                    $intersection = intersection_ex(
 
900
                        [ map @$_, @$intersection ],
 
901
                        [ map @{$_->expolygon}, @{$layerm->fill_surfaces->filter_by_type($type)} ],
 
902
                    );
 
903
                }
 
904
                
 
905
                my $area_threshold = $layerms[0]->infill_area_threshold;
 
906
                @$intersection = grep $_->area > $area_threshold, @$intersection;
 
907
                next if !@$intersection;
 
908
                Slic3r::debugf "  combining %d %s regions from layers %d-%d\n",
 
909
                    scalar(@$intersection),
 
910
                    ($type == S_TYPE_INTERNAL ? 'internal' : 'internal-solid'),
 
911
                    $layer_id-($every-1), $layer_id;
 
912
                
 
913
                # $intersection now contains the regions that can be combined across the full amount of layers
 
914
                # so let's remove those areas from all layers
 
915
                
 
916
                 my @intersection_with_clearance = map @{$_->offset(
 
917
                       $layerms[-1]->flow(FLOW_ROLE_SOLID_INFILL)->scaled_width    / 2
 
918
                     + $layerms[-1]->flow(FLOW_ROLE_PERIMETER)->scaled_width / 2
 
919
                     # Because fill areas for rectilinear and honeycomb are grown 
 
920
                     # later to overlap perimeters, we need to counteract that too.
 
921
                     + (($type == S_TYPE_INTERNALSOLID || $region->config->fill_pattern =~ /(rectilinear|honeycomb)/)
 
922
                       ? $layerms[-1]->flow(FLOW_ROLE_SOLID_INFILL)->scaled_width * &Slic3r::INFILL_OVERLAP_OVER_SPACING
 
923
                       : 0)
 
924
                     )}, @$intersection;
 
925
 
 
926
                
 
927
                foreach my $layerm (@layerms) {
 
928
                    my @this_type   = @{$layerm->fill_surfaces->filter_by_type($type)};
 
929
                    my @other_types = map $_->clone, grep $_->surface_type != $type, @{$layerm->fill_surfaces};
 
930
                    
 
931
                    my @new_this_type = map Slic3r::Surface->new(expolygon => $_, surface_type => $type),
 
932
                        @{diff_ex(
 
933
                            [ map $_->p, @this_type ],
 
934
                            [ @intersection_with_clearance ],
 
935
                        )};
 
936
                    
 
937
                    # apply surfaces back with adjusted depth to the uppermost layer
 
938
                    if ($layerm->id == $layer_id) {
 
939
                        push @new_this_type,
 
940
                            map Slic3r::Surface->new(
 
941
                                expolygon        => $_,
 
942
                                surface_type     => $type,
 
943
                                thickness        => sum(map $_->height, @layerms),
 
944
                                thickness_layers => scalar(@layerms),
 
945
                            ),
 
946
                            @$intersection;
 
947
                    } else {
 
948
                        # save void surfaces
 
949
                        push @this_type,
 
950
                            map Slic3r::Surface->new(expolygon => $_, surface_type => S_TYPE_INTERNALVOID),
 
951
                            @{intersection_ex(
 
952
                                [ map @{$_->expolygon}, @this_type ],
 
953
                                [ @intersection_with_clearance ],
 
954
                            )};
 
955
                    }
 
956
                    
 
957
                    $layerm->fill_surfaces->clear;
 
958
                    $layerm->fill_surfaces->append(@new_this_type, @other_types);
 
959
                }
 
960
            }
 
961
        }
 
962
    }
 
963
}
 
964
 
 
965
sub generate_support_material {
 
966
    my $self = shift;
 
967
    
 
968
    # TODO: make this method idempotent by removing all support layers
 
969
    # before checking whether we need to generate support or not
 
970
    return unless ($self->config->support_material || $self->config->raft_layers > 0)
 
971
        && scalar(@{$self->layers}) >= 2;
 
972
    
 
973
    my $first_layer_flow = Slic3r::Flow->new_from_width(
 
974
        width               => ($self->config->first_layer_extrusion_width || $self->config->support_material_extrusion_width),
 
975
        role                => FLOW_ROLE_SUPPORT_MATERIAL,
 
976
        nozzle_diameter     => $self->print->config->nozzle_diameter->[ $self->config->support_material_extruder-1 ]
 
977
                                // $self->print->config->nozzle_diameter->[0],
 
978
        layer_height        => $self->config->get_abs_value('first_layer_height'),
 
979
        bridge_flow_ratio   => 0,
 
980
    );
 
981
    
 
982
    my $s = Slic3r::Print::SupportMaterial->new(
 
983
        print_config        => $self->print->config,
 
984
        object_config       => $self->config,
 
985
        first_layer_flow    => $first_layer_flow,
 
986
        flow                => $self->support_material_flow,
 
987
        interface_flow      => $self->support_material_flow(FLOW_ROLE_SUPPORT_MATERIAL_INTERFACE),
 
988
    );
 
989
    $s->generate($self);
 
990
}
 
991
 
 
992
sub _simplify_slices {
 
993
    my ($self, $distance) = @_;
 
994
    
 
995
    foreach my $layer (@{$self->layers}) {
 
996
        $layer->slices->simplify($distance);
 
997
        $_->slices->simplify($distance) for @{$layer->regions};
 
998
    }
 
999
}
 
1000
 
 
1001
sub support_material_flow {
 
1002
    my ($self, $role) = @_;
 
1003
    
 
1004
    $role //= FLOW_ROLE_SUPPORT_MATERIAL;
 
1005
    my $extruder = ($role == FLOW_ROLE_SUPPORT_MATERIAL)
 
1006
        ? $self->config->support_material_extruder
 
1007
        : $self->config->support_material_interface_extruder;
 
1008
    
 
1009
    # we use a bogus layer_height because we use the same flow for all
 
1010
    # support material layers
 
1011
    return Slic3r::Flow->new_from_width(
 
1012
        width               => $self->config->support_material_extrusion_width || $self->config->extrusion_width,
 
1013
        role                => $role,
 
1014
        nozzle_diameter     => $self->print->config->nozzle_diameter->[$extruder-1] // $self->print->config->nozzle_diameter->[0],
 
1015
        layer_height        => $self->config->layer_height,
 
1016
        bridge_flow_ratio   => 0,
 
1017
    );
 
1018
}
 
1019
 
 
1020
1;