· 6 years ago · Mar 21, 2019, 10:18 AM
1# Instantiated by Slic3r::Print::Object->_support_material()
2# only generate() and contact_distance() are called from the outside of this module.
3package Slic3r::Print::SupportMaterial;
4use Moo;
5use 5.010;
6use Math::Trig ':radial';
7use Math::Trig;
8use List::Util qw(max min);
9use List::Util qw(sum min max);
10use Slic3r::ExtrusionPath ':roles';
11use Slic3r::Flow ':roles';
12use Slic3r::Geometry qw(epsilon scale scaled_epsilon PI rad2deg deg2rad convex_hull);
13use Slic3r::Geometry::Clipper qw(offset diff union union_ex intersection offset_ex offset2
14 intersection_pl offset2_ex diff_pl diff_ex);
15use Slic3r::Surface ':types';
16
17has 'print_config' => (is => 'rw', required => 1);
18has 'object_config' => (is => 'rw', required => 1);
19has 'flow' => (is => 'rw', required => 1);
20has 'first_layer_flow' => (is => 'rw', required => 1);
21has 'interface_flow' => (is => 'rw', required => 1);
22
23use constant DEBUG_CONTACT_ONLY => 0;
24
25# increment used to reach MARGIN in steps to avoid trespassing thin objects
26use constant MARGIN_STEP => MARGIN/3;
27
28#The coordinates of the points that is to be supported.
29my $min_X=0;
30my $max_X=20;
31my $min_Y=0;
32my $max_Y=20;
33my $distance=10;
34my $Z;
35my $i;
36my $j;
37my $I;
38my $J;
39my $X_ref;
40my $Y_ref;
41my @Z;
42my @Y;
43my @X;
44my $X_branch;
45my $Y_branch;
46my $Z_branch;
47my @X_list;
48my @Y_list;
49my @Z_list;
50my @X_values;
51my @Y_values;
52my $dist;
53my $rXY;
54my $theta;
55my $phi;
56my $output;
57my $outfile;
58my $rho;
59#The minimum angle from horizontl your printer can make, in degrees
60my $min_angle= 40;
61
62#Ignore the next line, it is not an input parameter.
63($X_ref,$Y_ref)=grid($min_X,$max_X,$min_Y,$max_Y,$distance);@X=@$X_ref;@Y=@{$Y_ref};
64for $i (0..$#X){
65 $Z[$i]=20;#The function that defined the height of each point. This setting wil give you a flat roof. For a more advanced tree, try:
66 #$Z[$i]=-0.01*$X[$i]**2+0.2*$Y[$i]-0.005*$Y[$i]**2+20;
67}
68
69my $min_radian = deg2rad($min_angle);
70my $b = tan($min_radian);
71@Z=map{$_/$b}@Z;
72open $output;
73
74print $output "width=2;\n";
75print $output "sphere_radius=0;\n";
76print $output "base_plate_size=10;\n\n";
77
78
79sub generate {
80 # $object is Slic3r::Print::Object
81 my ($self, $object) = @_;
82
83 # Determine the top surfaces of the support, defined as:
84 # contact = overhangs - clearance + margin
85 # This method is responsible for identifying what contact surfaces
86 # should the support material expose to the object in order to guarantee
87 # that it will be effective, regardless of how it's built below.
88 my ($contact, $overhang) = $self->contact_area($object);
89
90 # Determine the top surfaces of the object. We need these to determine
91 # the layer heights of support material and to clip support to the object
92 # silhouette.
93 my ($top) = $self->object_top($object, $contact);
94
95 # We now know the upper and lower boundaries for our support material object
96 # (@$contact_z and @$top_z), so we can generate intermediate layers.
97 my $support_z = $self->support_layers_z(
98 [ sort keys %$contact ],
99 [ sort keys %$top ],
100 max(map $_->height, @{$object->layers})
101 );
102
103 # If we wanted to apply some special logic to the first support layers lying on
104 # object's top surfaces this is the place to detect them
105
106 my $shape = [];
107 if ($self->object_config->support_material_pattern eq 'pillars') {
108 $self->grid($min_X,$max_X,$min_Y,$max_Y,$distance);
109 }
110
111 # Propagate contact layers downwards to generate interface layers
112 my ($interface) = $self->generate_interface_layers($support_z, $contact, $top);
113 $self->clip_with_object($interface, $support_z, $object);
114 $self->clip_with_shape($interface, $shape) if @$shape;
115
116 # Propagate contact layers and interface layers downwards to generate
117 # the main support layers.
118 my ($base) = $self->generate_base_layers($support_z, $contact, $interface, $top);
119 $self->clip_with_object($base, $support_z, $object);
120 $self->clip_with_shape($base, $shape) if @$shape;
121
122 # Detect what part of base support layers are "reverse interfaces" because they
123 # lie above object's top surfaces.
124 $self->generate_bottom_interface_layers($support_z, $base, $top, $interface);
125
126 # Install support layers into object.
127 for my $i (0 .. $#$support_z) {
128 $object->add_support_layer(
129 $i, # id
130 ($i == 0) ? $support_z->[$i] : ($support_z->[$i] - $support_z->[$i-1]), # height
131 $support_z->[$i], # print_z
132 );
133 if ($i >= 1) {
134 $object->support_layers->[-2]->set_upper_layer($object->support_layers->[-1]);
135 $object->support_layers->[-1]->set_lower_layer($object->support_layers->[-2]);
136 }
137 }
138
139 # Generate the actual toolpaths and save them into each layer.
140 $self->generate_toolpaths($object, $overhang, $contact, $interface, $base);
141}
142
143sub contact_area {
144 # $object is Slic3r::Print::Object
145 my ($self, $object) = @_;
146 my $conf = $self->object_config;
147
148 # if user specified a custom angle threshold, convert it to radians
149 my $threshold_rad;
150 if (!($conf->support_material_threshold =~ /%$/)) {
151 $threshold_rad = deg2rad($conf->support_material_threshold + 1); # +1 makes the threshold inclusive
152 Slic3r::debugf "Threshold angle = %d°\n", rad2deg($threshold_rad);
153 }
154
155 # Build support on a build plate only? If so, then collect top surfaces into $buildplate_only_top_surfaces
156 # and subtract $buildplate_only_top_surfaces from the contact surfaces, so
157 # there is no contact surface supported by a top surface.
158 my $buildplate_only =
159 ( $conf->support_material || $conf->support_material_enforce_layers)
160 && $conf->support_material_buildplate_only;
161 my $buildplate_only_top_surfaces = [];
162
163 # determine contact areas
164 my %contact = (); # contact_z => [ polygons ]
165 my %overhang = (); # contact_z => [ polygons ] - this stores the actual overhang supported by each contact layer
166 for my $layer_id (0 .. $#{$object->layers}) {
167 # note $layer_id might != $layer->id when raft_layers > 0
168 # so $layer_id == 0 means first object layer
169 # and $layer->id == 0 means first print layer (including raft)
170
171 # if no raft, and we're at layer 0, skip to layer 1
172 if ( $conf->raft_layers == 0 && $layer_id == 0 ) {
173 next;
174 }
175 # with or without raft, if we're above layer 1, we need to quit
176 # support generation if supports are disabled, or if we're at a high
177 # enough layer that enforce-supports no longer applies
178 if ( $layer_id > 0
179 && !$conf->support_material
180 && ($layer_id >= $conf->support_material_enforce_layers) ) {
181 # if we are only going to generate raft just check
182 # the 'overhangs' of the first object layer
183 last;
184 }
185 my $layer = $object->get_layer($layer_id);
186 last if $conf->support_material_max_layers
187 && $layer_id > $conf->support_material_max_layers;
188
189 if ($buildplate_only) {
190 # Collect the top surfaces up to this layer and merge them.
191 my $projection_new = [];
192 push @$projection_new, ( map $_->p, map @{$_->slices->filter_by_type(S_TYPE_TOP)}, @{$layer->regions} );
193 if (@$projection_new) {
194 # Merge the new top surfaces with the preceding top surfaces.
195 # Apply the safety offset to the newly added polygons, so they will connect
196 # with the polygons collected before,
197 # but don't apply the safety offset during the union operation as it would
198 # inflate the polygons over and over.
199 push @$buildplate_only_top_surfaces, @{ offset($projection_new, scale(0.01)) };
200 $buildplate_only_top_surfaces = union($buildplate_only_top_surfaces, 0);
201 }
202 }
203
204 # detect overhangs and contact areas needed to support them
205 my (@overhang, @contact) = ();
206 if ($layer_id == 0) {
207 # this is the first object layer, so we're here just to get the object
208 # footprint for the raft
209 # we only consider contours and discard holes to get a more continuous raft
210 push @overhang, map $_->clone, map $_->contour, @{$layer->slices};
211 push @contact, @{offset(\@overhang, scale +MARGIN)};
212 } else {
213 my $lower_layer = $object->get_layer($layer_id-1);
214 foreach my $layerm (@{$layer->regions}) {
215 my $fw = $layerm->flow(FLOW_ROLE_EXTERNAL_PERIMETER)->scaled_width;
216 my $diff;
217
218 # If a threshold angle was specified, use a different logic for detecting overhangs.
219 if (($conf->support_material && defined $threshold_rad)
220 || $layer_id <= $conf->support_material_enforce_layers
221 || ($conf->raft_layers > 0 && $layer_id == 0)) {
222 my $d = 0;
223 my $layer_threshold_rad = $threshold_rad;
224 if ($layer_id <= $conf->support_material_enforce_layers) {
225 # Use ~45 deg number for enforced supports if we are in auto
226 $layer_threshold_rad = deg2rad(89);
227 }
228 if (defined $layer_threshold_rad) {
229 $d = scale $lower_layer->height
230 * ((cos $layer_threshold_rad) / (sin $layer_threshold_rad));
231 }
232
233 $diff = diff(
234 [ map $_->p, @{$layerm->slices} ],
235 offset([ map @$_, @{$lower_layer->slices} ], +$d),
236 );
237
238 # only enforce spacing from the object ($fw/2) if the threshold angle
239 # is not too high: in that case, $d will be very small (as we need to catch
240 # very short overhangs), and such contact area would be eaten by the
241 # enforced spacing, resulting in high threshold angles to be almost ignored
242 $diff = diff(
243 offset($diff, $d - $fw/2),
244 [ map @$_, @{$lower_layer->slices} ],
245 ) if $d > $fw/2;
246 } else {
247 $diff = diff(
248 [ map $_->p, @{$layerm->slices} ],
249 offset([ map @$_, @{$lower_layer->slices} ], +$conf->get_abs_value_over('support_material_threshold', $fw)),
250 );
251
252 # collapse very tiny spots
253 $diff = offset2($diff, -$fw/10, +$fw/10);
254
255 # $diff now contains the ring or stripe comprised between the boundary of
256 # lower slices and the centerline of the last perimeter in this overhanging layer.
257 # Void $diff means that there's no upper perimeter whose centerline is
258 # outside the lower slice boundary, thus no overhang
259 }
260
261 if ($conf->dont_support_bridges) {
262 # compute the area of bridging perimeters
263 my $bridged_perimeters; # Polygons
264 {
265 my $bridge_flow = $layerm->flow(FLOW_ROLE_PERIMETER, 1);
266
267 # Get the lower layer's slices and grow them by half the nozzle diameter
268 # because we will consider the upper perimeters supported even if half nozzle
269 # falls outside the lower slices.
270 my $lower_grown_slices;
271 {
272 my $nozzle_diameter = $self->print_config->get_at('nozzle_diameter', $layerm->region->config->perimeter_extruder-1);
273 $lower_grown_slices = offset(
274 [ map @$_, @{$lower_layer->slices} ],
275 +scale($nozzle_diameter/2),
276 );
277 }
278
279 # Get all perimeters as polylines.
280 # TODO: split_at_first_point() (called by as_polyline() for ExtrusionLoops)
281 # could split a bridge mid-way
282 my @overhang_perimeters = map $_->as_polyline, @{$layerm->perimeters->flatten};
283
284 # Only consider the overhang parts of such perimeters,
285 # overhangs being those parts not supported by
286 # workaround for Clipper bug, see Slic3r::Polygon::clip_as_polyline()
287 $_->[0]->translate(1,0) for @overhang_perimeters;
288 @overhang_perimeters = @{diff_pl(
289 \@overhang_perimeters,
290 $lower_grown_slices,
291 )};
292
293 # only consider straight overhangs
294 @overhang_perimeters = grep $_->is_straight, @overhang_perimeters;
295
296 # only consider overhangs having endpoints inside layer's slices
297 foreach my $polyline (@overhang_perimeters) {
298 $polyline->extend_start($fw);
299 $polyline->extend_end($fw);
300 }
301 @overhang_perimeters = grep {
302 $layer->slices->contains_point($_->first_point) && $layer->slices->contains_point($_->last_point)
303 } @overhang_perimeters;
304
305 # convert bridging polylines into polygons by inflating them with their thickness
306 {
307 # For bridges we can't assume width is larger than spacing because they
308 # are positioned according to non-bridging perimeters spacing.
309 my $w = max(
310 $bridge_flow->scaled_width,
311 $bridge_flow->scaled_spacing,
312 $fw, # width of external perimeters
313 $layerm->flow(FLOW_ROLE_PERIMETER)->scaled_width,
314 );
315 $bridged_perimeters = union([
316 # Also apply safety offset to ensure no gaps are left in between.
317 map @{$_->grow($w/2 + 10)}, @overhang_perimeters
318 ]);
319 }
320 }
321
322 } # if ($conf->dont_support_bridges)
323
324 if ($buildplate_only) {
325 # Don't support overhangs above the top surfaces.
326 # This step is done before the contact surface is calcuated by growing the overhang region.
327 $diff = diff($diff, $buildplate_only_top_surfaces);
328 }
329
330 next if !@$diff;
331 push @overhang, @$diff; # NOTE: this is not the full overhang as it misses the outermost half of the perimeter width!
332
333 # Let's define the required contact area by using a max gap of half the upper
334 # extrusion width and extending the area according to the configured margin.
335 # We increment the area in steps because we don't want our support to overflow
336 # on the other side of the object (if it's very thin).
337 {
338 my $slices_margin = offset([ map @$_, @{$lower_layer->slices} ], +$fw/2);
339 if ($buildplate_only) {
340 # Trim the inflated contact surfaces by the top surfaces as well.
341 push @$slices_margin, map $_->clone, @{$buildplate_only_top_surfaces};
342 $slices_margin = union($slices_margin);
343 }
344 for ($fw/2, map {scale MARGIN_STEP} 1..(MARGIN / MARGIN_STEP)) {
345 $diff = diff(
346 offset($diff, $_),
347 $slices_margin,
348 );
349 }
350 }
351 push @contact, @$diff;
352 }
353 }
354 next if !@contact;
355
356 # now apply the contact areas to the layer were they need to be made
357 {
358 # get the average nozzle diameter used on this layer
359 my @nozzle_diameters = map $self->print_config->get_at('nozzle_diameter', $_),
360 map { $_->config->perimeter_extruder-1, $_->config->infill_extruder-1, $_->config->solid_infill_extruder-1 }
361 map $_->region, @{$layer->regions};
362 my $nozzle_diameter = sum(@nozzle_diameters)/@nozzle_diameters;
363
364 my $contact_z = $layer->print_z - $self->contact_distance($layer->height, $nozzle_diameter);
365
366 # ignore this contact area if it's too low
367 next if $contact_z < $conf->get_value('first_layer_height') - epsilon;
368
369 $contact{$contact_z} = [ @contact ];
370 $overhang{$contact_z} = [ @overhang ];
371
372 if (0) {
373 require "Slic3r/SVG.pm";
374 Slic3r::SVG::output("out\\contact_" . $contact_z . ".svg",
375 green_expolygons => union_ex($buildplate_only_top_surfaces),
376 blue_expolygons => union_ex(\@contact),
377 red_expolygons => union_ex(\@overhang),
378 );
379 }
380 }
381 }
382
383 return (\%contact, \%overhang);
384}
385
386sub object_top {
387 my ($self, $object, $contact) = @_;
388
389 # find object top surfaces
390 # we'll use them to clip our support and detect where does it stick
391 my %top = (); # print_z => [ expolygons ]
392 return \%top if ($self->object_config->support_material_buildplate_only);
393
394 my $projection = [];
395 foreach my $layer (reverse @{$object->layers}) {
396 if (my @top = map @{$_->slices->filter_by_type(S_TYPE_TOP)}, @{$layer->regions}) {
397 # compute projection of the contact areas above this top layer
398 # first add all the 'new' contact areas to the current projection
399 # ('new' means all the areas that are lower than the last top layer
400 # we considered)
401 my $min_top = min(keys %top) // max(keys %$contact);
402 # use <= instead of just < because otherwise we'd ignore any contact regions
403 # having the same Z of top layers
404 push @$projection, map @{$contact->{$_}}, grep { $_ > $layer->print_z && $_ <= $min_top } keys %$contact;
405
406 # now find whether any projection falls onto this top surface
407 my $touching = intersection($projection, [ map $_->p, @top ]);
408 if (@$touching) {
409 # grow top surfaces so that interface and support generation are generated
410 # with some spacing from object - it looks we don't need the actual
411 # top shapes so this can be done here
412 $top{ $layer->print_z } = offset($touching, $self->flow->scaled_width);
413 }
414
415 # remove the areas that touched from the projection that will continue on
416 # next, lower, top surfaces
417 $projection = diff($projection, $touching);
418 }
419 }
420
421 return \%top;
422}
423
424sub support_layers_z {
425 my ($self, $contact_z, $top_z, $max_object_layer_height) = @_;
426
427 # quick table to check whether a given Z is a top surface
428 my %top = map { $_ => 1 } @$top_z;
429
430 # determine layer height for any non-contact layer
431 # we use max() to prevent many ultra-thin layers to be inserted in case
432 # layer_height > nozzle_diameter * 0.75
433 my $nozzle_diameter = $self->print_config->get_at('nozzle_diameter', $self->object_config->support_material_extruder-1);
434 my $support_material_height = max($max_object_layer_height, $nozzle_diameter * 0.75);
435 my $contact_distance = $self->contact_distance($support_material_height, $nozzle_diameter);
436
437 # initialize known, fixed, support layers
438 my @z = sort { $a <=> $b }
439 @$contact_z,
440 @$top_z, # TODO: why we have this?
441 (map $_ + $contact_distance, @$top_z);
442
443 # enforce first layer height
444 my $first_layer_height = $self->object_config->get_value('first_layer_height');
445 shift @z while @z && $z[0] <= $first_layer_height;
446 unshift @z, $first_layer_height;
447
448 # add raft layers by dividing the space between first layer and
449 # first contact layer evenly
450 if ($self->object_config->raft_layers > 1 && @z >= 2) {
451 # $z[1] is last raft layer (contact layer for the first layer object)
452 my $height = ($z[1] - $z[0]) / ($self->object_config->raft_layers - 1);
453 # since we already have two raft layers ($z[0] and $z[1]) we need to insert
454 # raft_layers-2 more
455 splice @z, 1, 0,
456 map { sprintf "%.2f", $_ }
457 map { $z[0] + $height * $_ }
458 1..($self->object_config->raft_layers - 2);
459 }
460
461 # create other layers (skip raft layers as they're already done and use thicker layers)
462 for (my $i = $#z; $i >= $self->object_config->raft_layers; $i--) {
463 my $target_height = $support_material_height;
464 if ($i > 0 && $top{ $z[$i-1] }) {
465 $target_height = $nozzle_diameter;
466 }
467
468 # enforce first layer height
469 if (($i == 0 && $z[$i] > $target_height + $first_layer_height)
470 || ($z[$i] - $z[$i-1] > $target_height + Slic3r::Geometry::epsilon)) {
471 splice @z, $i, 0, ($z[$i] - $target_height);
472 $i++;
473 }
474 }
475
476 # remove duplicates and make sure all 0.x values have the leading 0
477 {
478 my %sl = map { 1 * $_ => 1 } @z;
479 @z = sort { $a <=> $b } keys %sl;
480 }
481
482 return \@z;
483}
484
485sub generate_interface_layers {
486 my ($self, $support_z, $contact, $top) = @_;
487
488 # let's now generate interface layers below contact areas
489 my %interface = (); # layer_id => [ polygons ]
490 my $interface_layers_num = $self->object_config->support_material_interface_layers;
491 for my $layer_id (0 .. $#$support_z) {
492 my $z = $support_z->[$layer_id];
493 my $this = $contact->{$z} // next;
494
495 # count contact layer as interface layer
496 for (my $i = $layer_id-1; $i >= 0 && $i > $layer_id-$interface_layers_num; $i--) {
497 $z = $support_z->[$i];
498 my @overlapping_layers = $self->overlapping_layers($i, $support_z);
499 my @overlapping_z = map $support_z->[$_], @overlapping_layers;
500
501 # Compute interface area on this layer as diff of upper contact area
502 # (or upper interface area) and layer slices.
503 # This diff is responsible of the contact between support material and
504 # the top surfaces of the object. We should probably offset the top
505 # surfaces vertically before performing the diff, but this needs
506 # investigation.
507 $this = $interface{$i} = diff(
508 [
509 @$this, # clipped projection of the current contact regions
510 @{ $interface{$i} || [] }, # interface regions already applied to this layer
511 ],
512 [
513 (map @$_, map $top->{$_}, grep exists $top->{$_}, @overlapping_z), # top slices on this layer
514 (map @$_, map $contact->{$_}, grep exists $contact->{$_}, @overlapping_z), # contact regions on this layer
515 ],
516 1,
517 );
518 }
519 }
520
521 return \%interface;
522}
523
524sub generate_bottom_interface_layers {
525 my ($self, $support_z, $base, $top, $interface) = @_;
526
527 # If no interface layers are allowed, don't generate bottom interface layers.
528 return if $self->object_config->support_material_interface_layers == 0;
529
530 my $area_threshold = $self->interface_flow->scaled_spacing ** 2;
531
532 # loop through object's top surfaces
533 foreach my $top_z (sort keys %$top) {
534 my $this = $top->{$top_z};
535
536 # keep a count of the interface layers we generated for this top surface
537 my $interface_layers = 0;
538
539 # loop through support layers until we find the one(s) right above the top
540 # surface
541 foreach my $layer_id (0 .. $#$support_z) {
542 my $z = $support_z->[$layer_id];
543 next unless $z > $top_z;
544
545 if ($base->{$layer_id}) {
546 # get the support material area that should be considered interface
547 my $interface_area = intersection(
548 $base->{$layer_id},
549 $this,
550 );
551
552 # discard too small areas
553 $interface_area = [ grep abs($_->area) >= $area_threshold, @$interface_area ];
554
555 # subtract new interface area from base
556 $base->{$layer_id} = diff(
557 $base->{$layer_id},
558 $interface_area,
559 );
560
561 # add new interface area to interface
562 push @{$interface->{$layer_id}}, @$interface_area;
563 }
564
565 $interface_layers++;
566 last if $interface_layers == $self->object_config->support_material_interface_layers;
567 }
568 }
569}
570
571sub generate_base_layers {
572 my ($self, $support_z, $contact, $interface, $top) = @_;
573
574 # let's now generate support layers under interface layers
575 my $base = {}; # layer_id => [ polygons ]
576 {
577 for my $i (reverse 0 .. $#$support_z-1) {
578 my $z = $support_z->[$i];
579 my @overlapping_layers = $self->overlapping_layers($i, $support_z);
580 my @overlapping_z = map $support_z->[$_], @overlapping_layers;
581
582 # in case we have no interface layers, look at upper contact
583 # (1 interface layer means we only have contact layer, so $interface->{$i+1} is empty)
584 my @upper_contact = ();
585 if ($self->object_config->support_material_interface_layers <= 1) {
586 @upper_contact = @{ $contact->{$support_z->[$i+1]} || [] };
587 }
588
589 $base->{$i} = diff(
590 [
591 @{ $base->{$i+1} || [] }, # support regions on upper layer
592 @{ $interface->{$i+1} || [] }, # interface regions on upper layer
593 @upper_contact, # contact regions on upper layer
594 ],
595 [
596 (map @$_, map $top->{$_}, grep exists $top->{$_}, @overlapping_z), # top slices on this layer
597 (map @$_, map $interface->{$_}, grep exists $interface->{$_}, @overlapping_layers), # interface regions on this layer
598 (map @$_, map $contact->{$_}, grep exists $contact->{$_}, @overlapping_z), # contact regions on this layer
599 ],
600 1,
601 );
602 }
603 }
604
605 return $base;
606}
607
608# This method removes object silhouette from support material
609# (it's used with interface and base only). It removes a bit more,
610# leaving a thin gap between object and support in the XY plane.
611sub clip_with_object {
612 my ($self, $support, $support_z, $object) = @_;
613
614 foreach my $i (keys %$support) {
615 next if !@{$support->{$i}};
616
617 my $zmax = $support_z->[$i];
618 my $zmin = ($i == 0) ? 0 : $support_z->[$i-1];
619 my @layers = grep { $_->print_z > $zmin && ($_->print_z - $_->height) < $zmax }
620 @{$object->layers};
621
622 # $layer->slices contains the full shape of layer, thus including
623 # perimeter's width. $support contains the full shape of support
624 # material, thus including the width of its foremost extrusion.
625 # We leave a gap equal to a full extrusion width.
626 $support->{$i} = diff(
627 $support->{$i},
628 offset([ map @$_, map @{$_->slices}, @layers ], +$self->flow->scaled_width),
629 );
630 }
631}
632
633sub generate_toolpaths {
634 my ($self, $object, $overhang, $contact, $interface, $base) = @_;
635
636 my $flow = $self->flow;
637 my $interface_flow = $self->interface_flow;
638
639 # shape of contact area
640 my $contact_loops = 1;
641 my $circle_radius = 1.5 * $interface_flow->scaled_width;
642 my $circle_distance = 3 * $circle_radius;
643 my $circle = Slic3r::Polygon->new(map [ $circle_radius * cos $_, $circle_radius * sin $_ ],
644 (5*PI/3, 4*PI/3, PI, 2*PI/3, PI/3, 0));
645
646 Slic3r::debugf "Generating patterns\n";
647
648 # prepare fillers
649 my $pattern = $self->object_config->support_material_pattern;
650 my @angles = ($self->object_config->support_material_angle);
651 if ($pattern eq 'rectilinear-grid') {
652 $pattern = 'rectilinear';
653 push @angles, $angles[0] + 90;
654 } elsif ($pattern eq 'pillars') {
655 $pattern = 'honeycomb';
656 }
657
658 my $interface_angle = $self->object_config->support_material_angle + 90;
659 my $interface_spacing = $self->object_config->support_material_interface_spacing + $interface_flow->spacing;
660 my $interface_density = $interface_spacing == 0 ? 1 : $interface_flow->spacing / $interface_spacing;
661 my $support_spacing = $self->object_config->support_material_spacing + $flow->spacing;
662 my $support_density = $support_spacing == 0 ? 1 : $flow->spacing / $support_spacing;
663
664 my $process_layer = sub {
665 my ($layer_id) = @_;
666 my $layer = $object->support_layers->[$layer_id];
667 my $z = $layer->print_z;
668
669 # we redefine flows locally by applying this layer's height
670 my $_flow = $flow->clone;
671 my $_interface_flow = $interface_flow->clone;
672 $_flow->set_height($layer->height);
673 $_interface_flow->set_height($layer->height);
674
675 my $overhang = $overhang->{$z} || [];
676 my $contact = $contact->{$z} || [];
677 my $interface = $interface->{$layer_id} || [];
678 my $base = $base->{$layer_id} || [];
679
680 if (DEBUG_CONTACT_ONLY) {
681 $interface = [];
682 $base = [];
683 }
684
685 if (0) {
686 require "Slic3r/SVG.pm";
687 Slic3r::SVG::output("layer_" . $z . ".svg",
688 red_expolygons => union_ex($contact),
689 green_expolygons => union_ex($interface),
690 );
691 }
692
693 # islands
694 $layer->support_islands->append(@{union_ex([ @$interface, @$base, @$contact ])});
695
696 # contact
697 my $contact_infill = [];
698 if ($self->object_config->support_material_interface_layers == 0) {
699 # if no interface layers were requested we treat the contact layer
700 # exactly as a generic base layer
701 push @$base, @$contact;
702 } elsif (@$contact && $contact_loops > 0) {
703 # generate the outermost loop
704
705 # find centerline of the external loop (or any other kind of extrusions should the loop be skipped)
706 $contact = offset($contact, -$_interface_flow->scaled_width/2);
707
708 my @loops0 = ();
709 {
710 # find centerline of the external loop of the contours
711 my @external_loops = @$contact;
712
713 # only consider the loops facing the overhang
714 {
715 my $overhang_with_margin = offset($overhang, +$_interface_flow->scaled_width/2);
716 @external_loops = grep {
717 @{intersection_pl(
718 [ $_->split_at_first_point ],
719 $overhang_with_margin,
720 )}
721 } @external_loops;
722 }
723
724 # apply a pattern to the loop
725 my @positions = map @{Slic3r::Polygon->new(@$_)->equally_spaced_points($circle_distance)}, @external_loops;
726 @loops0 = @{diff(
727 [ @external_loops ],
728 [ map { my $c = $circle->clone; $c->translate(@$_); $c } @positions ],
729 )};
730 }
731
732 # make more loops
733 my @loops = @loops0;
734 for my $i (2..$contact_loops) {
735 my $d = ($i-1) * $_interface_flow->scaled_spacing;
736 push @loops, @{offset2(\@loops0, -$d -0.5*$_interface_flow->scaled_spacing, +0.5*$_interface_flow->scaled_spacing)};
737 }
738
739 # clip such loops to the side oriented towards the object
740 @loops = @{intersection_pl(
741 [ map $_->split_at_first_point, @loops ],
742 offset($overhang, +scale MARGIN),
743 )};
744
745 # add the contact infill area to the interface area
746 # note that growing loops by $circle_radius ensures no tiny
747 # extrusions are left inside the circles; however it creates
748 # a very large gap between loops and contact_infill, so maybe another
749 # solution should be found to achieve both goals
750 $contact_infill = diff(
751 $contact,
752 [ map @{$_->grow($circle_radius*1.1)}, @loops ],
753 );
754
755 # transform loops into ExtrusionPath objects
756 my $mm3_per_mm = $_interface_flow->mm3_per_mm;
757 @loops = map Slic3r::ExtrusionPath->new(
758 polyline => $_,
759 role => EXTR_ROLE_SUPPORTMATERIAL_INTERFACE,
760 mm3_per_mm => $mm3_per_mm,
761 width => $_interface_flow->width,
762 height => $layer->height,
763 ), @loops;
764
765 $layer->support_interface_fills->append(@loops);
766 }
767
768 # Allocate the fillers exclusively in the worker threads! Don't allocate them at the main thread,
769 # as Perl copies the C++ pointers by default, so then the C++ objects are shared between threads!
770 my %fillers = (
771 interface => Slic3r::Filler->new_from_type('rectilinear'),
772 support => Slic3r::Filler->new_from_type($pattern),
773 );
774 my $bounding_box = $object->bounding_box;
775 $fillers{interface}->set_bounding_box($object->bounding_box);
776 $fillers{support}->set_bounding_box($object->bounding_box);
777
778 # interface and contact infill
779 if (@$interface || @$contact_infill) {
780 # make interface layers alternate angles by 90 degrees
781 my $alternate_angle = $interface_angle + (90 * (($layer_id + 1) % 2));
782 $fillers{interface}->set_angle(deg2rad($alternate_angle));
783 $fillers{interface}->set_min_spacing($_interface_flow->spacing);
784
785 # find centerline of the external loop
786 $interface = offset2($interface, +scaled_epsilon, -(scaled_epsilon + $_interface_flow->scaled_width/2));
787
788 # join regions by offsetting them to ensure they're merged
789 $interface = offset([ @$interface, @$contact_infill ], scaled_epsilon);
790
791 # turn base support into interface when it's contained in our holes
792 # (this way we get wider interface anchoring)
793 {
794 my @p = @$interface;
795 @$interface = ();
796 foreach my $p (@p) {
797 if ($p->is_clockwise) {
798 my $p2 = $p->clone;
799 $p2->make_counter_clockwise;
800 next if !@{diff([$p2], $base, 1)};
801 }
802 push @$interface, $p;
803 }
804 }
805 $base = diff($base, $interface);
806
807 my @paths = ();
808 foreach my $expolygon (@{union_ex($interface)}) {
809 my $p = $fillers{interface}->fill_surface(
810 Slic3r::Surface->new(expolygon => $expolygon, surface_type => S_TYPE_INTERNAL),
811 density => $interface_density,
812 layer_height => $layer->height,
813 complete => 1,
814 );
815 my $mm3_per_mm = $_interface_flow->mm3_per_mm;
816
817 push @paths, map Slic3r::ExtrusionPath->new(
818 polyline => Slic3r::Polyline->new(@$_),
819 role => EXTR_ROLE_SUPPORTMATERIAL_INTERFACE,
820 mm3_per_mm => $mm3_per_mm,
821 width => $_interface_flow->width,
822 height => $layer->height,
823 ), @$p;
824 }
825
826 $layer->support_interface_fills->append(@paths);
827 }
828
829 # support or flange
830 if (@$base) {
831 my $filler = $fillers{support};
832 $filler->set_angle(deg2rad($angles[ ($layer_id) % @angles ]));
833
834 # We don't use $base_flow->spacing because we need a constant spacing
835 # value that guarantees that all layers are correctly aligned.
836 $filler->set_min_spacing($flow->spacing);
837
838 my $density = $support_density;
839 my $base_flow = $_flow;
840
841 # find centerline of the external loop/extrusions
842 my $to_infill = offset2($base, +scaled_epsilon, -(scaled_epsilon + $_flow->scaled_width/2));
843
844 my @paths = ();
845
846 # base flange
847 if ($layer_id == 0) {
848 $filler = $fillers{interface};
849 $filler->set_angle(deg2rad($self->object_config->support_material_angle + 90));
850 $density = 0.5;
851 $base_flow = $self->first_layer_flow;
852
853 # use the proper spacing for first layer as we don't need to align
854 # its pattern to the other layers
855 $filler->set_min_spacing($base_flow->spacing);
856
857 # subtract brim so that it goes around the object fully (and support gets its own brim)
858 if ($self->print_config->brim_width > 0) {
859 my $d = +scale $self->print_config->brim_width*2;
860 $to_infill = diff_ex(
861 $to_infill,
862 offset($object->get_layer(0)->slices->polygons, $d),
863 );
864 } else {
865 $to_infill = union_ex($to_infill);
866 }
867 } else {
868 # draw a perimeter all around support infill
869 # TODO: use brim ordering algorithm
870 my $mm3_per_mm = $_flow->mm3_per_mm;
871 push @paths, map Slic3r::ExtrusionPath->new(
872 polyline => $_->split_at_first_point,
873 role => EXTR_ROLE_SUPPORTMATERIAL,
874 mm3_per_mm => $mm3_per_mm,
875 width => $_flow->width,
876 height => $layer->height,
877 ), @$to_infill;
878
879 # TODO: use offset2_ex()
880 $to_infill = offset_ex($to_infill, -$_flow->scaled_spacing);
881 }
882
883 my $mm3_per_mm = $base_flow->mm3_per_mm;
884 foreach my $expolygon (@$to_infill) {
885 my $p = $filler->fill_surface(
886 Slic3r::Surface->new(expolygon => $expolygon, surface_type => S_TYPE_INTERNAL),
887 density => $density,
888 layer_height => $layer->height,
889 complete => 1,
890 );
891
892 push @paths, map Slic3r::ExtrusionPath->new(
893 polyline => Slic3r::Polyline->new(@$_),
894 role => EXTR_ROLE_SUPPORTMATERIAL,
895 mm3_per_mm => $mm3_per_mm,
896 width => $base_flow->width,
897 height => $layer->height,
898 ), @$p;
899 }
900
901 $layer->support_fills->append(@paths);
902 }
903
904 if (0) {
905 require "Slic3r/SVG.pm";
906 Slic3r::SVG::output("islands_" . $z . ".svg",
907 red_expolygons => union_ex($contact),
908 green_expolygons => union_ex($interface),
909 green_polylines => [ map $_->unpack->polyline, @{$layer->support_contact_fills} ],
910 polylines => [ map $_->unpack->polyline, @{$layer->support_fills} ],
911 );
912 }
913 };
914
915 Slic3r::parallelize(
916 threads => $self->print_config->threads,
917 items => [ 0 .. $#{$object->support_layers} ],
918 thread_cb => sub {
919 my $q = shift;
920 while (defined (my $layer_id = $q->dequeue)) {
921 $process_layer->($layer_id);
922 }
923 },
924 no_threads_cb => sub {
925 $process_layer->($_) for 0 .. $#{$object->support_layers};
926 },
927 );
928}
929
930while ($#X>0){
931 ($I,$J)=find_min_dist(\@X,\@Y,\@Z);
932 ($X_branch,$Y_branch,$Z_branch)=find_branch($X[$I],$Y[$I],$Z[$I],$X[$J],$Y[$J],$Z[$J]);
933 @X_list=($X_branch,$X[$I],$X[$J]);
934 @Y_list=($Y_branch,$Y[$I],$Y[$J]);
935 @Z_list=($Z_branch,$Z[$I],$Z[$J]);
936 for $j (0..$#Y_list){
937 if (abs($X_list[$j]) < 0.001){
938 $X_list[$j]=0;
939 }
940 if (abs($Y_list[$j]) < 0.001){
941 $Y_list[$j]=0;
942 }
943 if (abs($Z_list[$j]) < 0.001){
944 $Z_list[$J]=0;
945 }
946 }
947 branch(\@X_list,\@Y_list,\@Z_list);
948 splice(@X,$I,1,$X_branch);
949 splice(@X,$J,1);
950 splice(@Y,$I,1,$Y_branch);
951 splice(@Y,$J,1);
952 splice(@Z,$I,1,$Z_branch);
953 splice(@Z,$J,1);
954}
955
956print $output 'if(base_plate_size>0){';
957print $output "\n translate([$X[0],$Y[0],$Z[0]*$b])\n";
958print $output "cube([base_plate_size,base_plate_size,1],center=true);}";
959
960sub grid{
961 my $d=$_[4];
962 @X_values=$_[0]/$d..$_[1]/$d;
963 @X_values=map{$_*$d} @X_values;
964 @Y_values=$_[2]/$d..$_[3]/$d;
965 @Y_values=map{$_*$d} @Y_values;
966 for $i (0..$#X_values){
967 @Y=(@Y,@Y_values);
968 for $j (0..$#Y_values){
969 $X[$i*($#Y_values+1)+$j]= $X_values[$i];
970 }
971 }
972 return (\@X,\@Y);
973}
974
975sub branch{
976 my @X=@{ $_[0] };
977 my @Y=@{ $_[1] };
978 my @Z=@{ $_[2] };
979 @Z=map{$_*$b}@Z;
980 for $i (1..$#X){
981 ($rho, $theta, $phi) = cartesian_to_spherical($X[$i]-$X[0],$Y[$i]-$Y[0],$Z[$i]-$Z[0]);
982 $phi = rad2deg($phi);
983 if (abs($phi)<0.001){$phi=0;}
984 $theta = rad2deg($theta)+90;
985 if (abs($theta)<0.001){$theta=0;}
986 if (abs($rho)>0.001){
987 print $output "translate([$X[0],$Y[0],$Z[0]])\n";
988 print $output "rotate([0,0,$theta])\n";
989 print $output "rotate([$phi,0,0])\n";
990 print $output "translate([-width/2,-width/2,0])";
991 print $output "cube([width,width,$rho]);\n";
992 print $output 'if (sphere_radius>0){';
993 print $output "\n translate([$X[$i],$Y[$i],$Z[$i]])\n";
994 print $output "sphere(sphere_radius,center=1);}\n";}
995 }
996}
997
998sub find_min_dist{
999 my @X=@{ $_[0] };
1000 my @Y=@{ $_[1] };
1001 my @Z=@{ $_[2] };
1002 my $min_dist=($X[0]-$X[1])**2+($Y[0]-$Y[1])**2+($Z[0]-$Z[1])**2;
1003 my $max_Z=$Z[0];
1004 my $I=0;
1005 my $J=1;
1006 for $i (1..$#Z){
1007 if ($Z[$i]>=$max_Z){
1008 $max_Z=$Z[$i];
1009 $I=$i;}
1010 }
1011 for $j (0..$#X){
1012 if ($j!=$I){
1013 $dist=(($X[$I]-$X[$j])**2+($Y[$I]-$Y[$j])**2+($Z[$I]-$Z[$j])**2);
1014 if ($min_dist>$dist){
1015 $min_dist=$dist;
1016 $J=$j;
1017 }}}
1018 return ($I,$J);
1019}
1020
1021sub find_branch{
1022 my $X1=$_[0];
1023 my $Y1=$_[1];
1024 my $Z1=$_[2];
1025 my $X2=$_[3];
1026 my $Y2=$_[4];
1027 my $Z2=$_[5];
1028 $rXY=sqrt(($X1-$X2)**2+($Y1-$Y2)**2);
1029 if (abs($Z1-$Z2) < $rXY) {
1030 $Z_branch=($Z1+$Z2-$rXY)/2;
1031 $a=($Z1-$Z_branch)/$rXY;
1032 $X_branch=(1-$a)*$X1+$a*$X2;
1033 $Y_branch=(1-$a)*$Y1+$a*$Y2;
1034 }
1035 elsif ($Z1 < $Z2) {
1036 $X_branch=$X1;
1037 $Y_branch=$Y1;
1038 $Z_branch=$Z1;
1039 }
1040 else {
1041 $X_branch=$X2;
1042 $Y_branch=$Y2;
1043 $Z_branch=$Z2;
1044 }
1045 return ($X_branch,$Y_branch,$Z_branch);
1046}
1047
1048sub clip_with_shape {
1049 my ($self, $support, $shape) = @_;
1050
1051 foreach my $i (keys %$support) {
1052 # don't clip bottom layer with shape so that we
1053 # can generate a continuous base flange
1054 # also don't clip raft layers
1055 next if $i == 0;
1056 next if $i < $self->object_config->raft_layers;
1057 $support->{$i} = intersection(
1058 $support->{$i},
1059 $shape->[$i],
1060 );
1061 }
1062}
1063
1064# this method returns the indices of the layers overlapping with the given one
1065sub overlapping_layers {
1066 my ($self, $i, $support_z) = @_;
1067
1068 my $zmax = $support_z->[$i];
1069 my $zmin = ($i == 0) ? 0 : $support_z->[$i-1];
1070
1071 return grep {
1072 my $zmax2 = $support_z->[$_];
1073 my $zmin2 = ($_ == 0) ? 0 : $support_z->[$_-1];
1074 $zmax > $zmin2 && $zmin < $zmax2;
1075 } 0..$#$support_z;
1076}
1077
1078sub contact_distance {
1079 my ($self, $layer_height, $nozzle_diameter) = @_;
1080
1081 my $extra = $self->object_config->support_material_contact_distance;
1082 if ($extra == 0) {
1083 return $layer_height;
1084 } else {
1085 return $nozzle_diameter + $extra;
1086 }
1087}
1088
10891;