SupportMaterial.pm 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  1. # Instantiated by Slic3r::Print::Object->_support_material()
  2. # only generate() and contact_distance() are called from the outside of this module.
  3. package Slic3r::Print::SupportMaterial;
  4. use Moo;
  5. use List::Util qw(sum min max);
  6. use Slic3r::ExtrusionPath ':roles';
  7. use Slic3r::Flow ':roles';
  8. use Slic3r::Geometry qw(epsilon scale scaled_epsilon PI rad2deg deg2rad convex_hull);
  9. use Slic3r::Geometry::Clipper qw(offset diff union union_ex intersection offset_ex offset2
  10. intersection_pl offset2_ex diff_pl JT_MITER JT_ROUND);
  11. use Slic3r::Surface ':types';
  12. has 'print_config' => (is => 'rw', required => 1);
  13. has 'object_config' => (is => 'rw', required => 1);
  14. has 'flow' => (is => 'rw', required => 1);
  15. has 'first_layer_flow' => (is => 'rw', required => 1);
  16. has 'interface_flow' => (is => 'rw', required => 1);
  17. use constant DEBUG_CONTACT_ONLY => 0;
  18. # increment used to reach MARGIN in steps to avoid trespassing thin objects
  19. use constant MARGIN => 1.5;
  20. use constant MARGIN_STEP => MARGIN/3;
  21. # generate a tree-like structure to save material
  22. use constant PILLAR_SIZE => 2.5;
  23. use constant PILLAR_SPACING => 10;
  24. sub generate {
  25. # $object is Slic3r::Print::Object
  26. my ($self, $object) = @_;
  27. # Determine the top surfaces of the support, defined as:
  28. # contact = overhangs - clearance + margin
  29. # This method is responsible for identifying what contact surfaces
  30. # should the support material expose to the object in order to guarantee
  31. # that it will be effective, regardless of how it's built below.
  32. my ($contact, $overhang) = $self->contact_area($object);
  33. # Determine the top surfaces of the object. We need these to determine
  34. # the layer heights of support material and to clip support to the object
  35. # silhouette.
  36. my ($top) = $self->object_top($object, $contact);
  37. # We now know the upper and lower boundaries for our support material object
  38. # (@$contact_z and @$top_z), so we can generate intermediate layers.
  39. my $support_z = $self->support_layers_z(
  40. $object,
  41. [ sort keys %$contact ],
  42. [ sort keys %$top ],
  43. max(map $_->height, @{$object->layers})
  44. );
  45. # If we wanted to apply some special logic to the first support layers lying on
  46. # object's top surfaces this is the place to detect them
  47. my $shape = [];
  48. if ($self->object_config->support_material_pattern eq 'pillars') {
  49. $self->generate_pillars_shape($contact, $support_z, $shape);
  50. }
  51. # Propagate contact layers downwards to generate interface layers
  52. my ($interface) = $self->generate_top_interface_layers($support_z, $contact, $top);
  53. $self->clip_with_object($interface, $support_z, $object);
  54. $self->clip_with_shape($interface, $shape) if @$shape;
  55. # Propagate contact layers and interface layers downwards to generate
  56. # the main support layers.
  57. my ($base) = $self->generate_base_layers($support_z, $contact, $interface, $top);
  58. $self->clip_with_object($base, $support_z, $object);
  59. $self->clip_with_shape($base, $shape) if @$shape;
  60. # Detect what part of base support layers are "reverse interfaces" because they
  61. # lie above object's top surfaces.
  62. $self->generate_bottom_interface_layers($support_z, $base, $top, $interface);
  63. # Install support layers into object.
  64. for my $i (0 .. $#$support_z) {
  65. $object->add_support_layer(
  66. $i, # id
  67. ($i == 0) ? $support_z->[$i] : ($support_z->[$i] - $support_z->[$i-1]), # height
  68. $support_z->[$i], # print_z
  69. );
  70. if ($i >= 1) {
  71. $object->support_layers->[-2]->set_upper_layer($object->support_layers->[-1]);
  72. $object->support_layers->[-1]->set_lower_layer($object->support_layers->[-2]);
  73. }
  74. }
  75. # Generate the actual toolpaths and save them into each layer.
  76. $self->generate_toolpaths($object, $overhang, $contact, $interface, $base);
  77. }
  78. sub contact_area {
  79. # $object is Slic3r::Print::Object
  80. my ($self, $object) = @_;
  81. # if user specified a custom angle threshold, convert it to radians
  82. my $threshold_rad;
  83. if ($self->object_config->support_material_threshold) {
  84. $threshold_rad = deg2rad($self->object_config->support_material_threshold + 1); # +1 makes the threshold inclusive
  85. Slic3r::debugf "Threshold angle = %d°\n", rad2deg($threshold_rad);
  86. }
  87. # Build support on a build plate only? If so, then collect top surfaces into $buildplate_only_top_surfaces
  88. # and subtract $buildplate_only_top_surfaces from the contact surfaces, so
  89. # there is no contact surface supported by a top surface.
  90. my $buildplate_only = $self->object_config->support_material && $self->object_config->support_material_buildplate_only;
  91. my $buildplate_only_top_surfaces = [];
  92. # determine contact areas
  93. my %contact = (); # contact_z => [ polygons ]
  94. my %overhang = (); # contact_z => [ polygons ] - this stores the actual overhang supported by each contact layer
  95. for my $layer_id (0 .. $#{$object->layers}) {
  96. # note $layer_id might != $layer->id when raft_layers > 0
  97. # so $layer_id == 0 means first object layer
  98. # and $layer->id == 0 means first print layer (including raft)
  99. if ($self->object_config->raft_layers == 0) {
  100. next if $layer_id == 0;
  101. } elsif (!$self->object_config->support_material) {
  102. # if we are only going to generate raft just check
  103. # the 'overhangs' of the first object layer
  104. last if $layer_id > 0;
  105. }
  106. my $layer = $object->get_layer($layer_id);
  107. if ($buildplate_only) {
  108. # Collect the top surfaces up to this layer and merge them.
  109. my $projection_new = [];
  110. push @$projection_new, ( map $_->p, map @{$_->slices->filter_by_type(S_TYPE_TOP)}, @{$layer->regions} );
  111. if (@$projection_new) {
  112. # Merge the new top surfaces with the preceding top surfaces.
  113. # Apply the safety offset to the newly added polygons, so they will connect
  114. # with the polygons collected before,
  115. # but don't apply the safety offset during the union operation as it would
  116. # inflate the polygons over and over.
  117. push @$buildplate_only_top_surfaces, @{ offset($projection_new, scale(0.01)) };
  118. $buildplate_only_top_surfaces = union($buildplate_only_top_surfaces, 0);
  119. }
  120. }
  121. # detect overhangs and contact areas needed to support them
  122. my (@overhang, @contact) = ();
  123. if ($layer_id == 0) {
  124. # this is the first object layer, so we're here just to get the object
  125. # footprint for the raft
  126. # we only consider contours and discard holes to get a more continuous raft
  127. push @overhang, map $_->clone, map $_->contour, @{$layer->slices};
  128. # Extend by SUPPORT_MATERIAL_MARGIN, which is 1.5mm
  129. # MARGIN is the C++ Slic3r::SUPPORT_MATERIAL_MARGIN constant.
  130. push @contact, @{offset(\@overhang, scale +MARGIN)};
  131. } else {
  132. my $lower_layer = $object->get_layer($layer_id-1);
  133. foreach my $layerm (@{$layer->regions}) {
  134. # Extrusion width accounts for the roundings of the extrudates.
  135. # It is the maximum widh of the extrudate.
  136. my $fw = $layerm->flow(FLOW_ROLE_EXTERNAL_PERIMETER)->scaled_width;
  137. my $diff;
  138. # If a threshold angle was specified, use a different logic for detecting overhangs.
  139. if (defined $threshold_rad
  140. || $layer_id < $self->object_config->support_material_enforce_layers
  141. || ($self->object_config->raft_layers > 0 && $layer_id == 0)) {
  142. my $d = defined $threshold_rad
  143. ? scale $lower_layer->height * ((cos $threshold_rad) / (sin $threshold_rad))
  144. : 0;
  145. # Shrinking the supported layer by layer_height/atan(threshold_rad).
  146. $diff = diff(
  147. offset([ map $_->p, @{$layerm->slices} ], -$d),
  148. [ map @$_, @{$lower_layer->slices} ],
  149. );
  150. # only enforce spacing from the object ($fw/2) if the threshold angle
  151. # is not too high: in that case, $d will be very small (as we need to catch
  152. # very short overhangs), and such contact area would be eaten by the
  153. # enforced spacing, resulting in high threshold angles to be almost ignored
  154. $diff = diff(
  155. offset($diff, $d - $fw/2),
  156. [ map @$_, @{$lower_layer->slices} ],
  157. ) if $d > $fw/2;
  158. } else {
  159. # Automatic overhang detection.
  160. $diff = diff(
  161. [ map $_->p, @{$layerm->slices} ],
  162. offset([ map @$_, @{$lower_layer->slices} ],
  163. #FIXME Vojtech: Why 2x extrusion width? Isn't this too much? Should it not be /2?
  164. +$fw/2),
  165. );
  166. # collapse very tiny spots
  167. $diff = offset2($diff, -$fw/10, +$fw/10);
  168. # $diff now contains the ring or stripe comprised between the boundary of
  169. # lower slices and the centerline of the last perimeter in this overhanging layer.
  170. # Void $diff means that there's no upper perimeter whose centerline is
  171. # outside the lower slice boundary, thus no overhang
  172. }
  173. if ($self->object_config->dont_support_bridges) {
  174. # compute the area of bridging perimeters
  175. # Note: this is duplicate code from GCode.pm, we need to refactor
  176. my $bridged_perimeters; # Polygons
  177. {
  178. my $bridge_flow = $layerm->flow(FLOW_ROLE_PERIMETER, 1);
  179. my $nozzle_diameter = $self->print_config->get_at('nozzle_diameter', $layerm->region->config->perimeter_extruder-1);
  180. my $lower_grown_slices = offset([ map @$_, @{$lower_layer->slices} ], +scale($nozzle_diameter/2));
  181. # TODO: split_at_first_point() could split a bridge mid-way
  182. my @overhang_perimeters =
  183. map { $_->isa('Slic3r::ExtrusionLoop') ? $_->polygon->split_at_first_point : $_->polyline->clone }
  184. map @$_, @{$layerm->perimeters};
  185. # workaround for Clipper bug, see Slic3r::Polygon::clip_as_polyline()
  186. $_->[0]->translate(1,0) for @overhang_perimeters;
  187. @overhang_perimeters = @{diff_pl(
  188. \@overhang_perimeters,
  189. $lower_grown_slices,
  190. )};
  191. # only consider straight overhangs
  192. @overhang_perimeters = grep $_->is_straight, @overhang_perimeters;
  193. # only consider overhangs having endpoints inside layer's slices
  194. foreach my $polyline (@overhang_perimeters) {
  195. $polyline->extend_start($fw);
  196. $polyline->extend_end($fw);
  197. }
  198. @overhang_perimeters = grep {
  199. $layer->slices->contains_point($_->first_point) && $layer->slices->contains_point($_->last_point)
  200. } @overhang_perimeters;
  201. # convert bridging polylines into polygons by inflating them with their thickness
  202. {
  203. # since we're dealing with bridges, we can't assume width is larger than spacing,
  204. # so we take the largest value and also apply safety offset to be ensure no gaps
  205. # are left in between
  206. my $w = max($bridge_flow->scaled_width, $bridge_flow->scaled_spacing);
  207. $bridged_perimeters = union([
  208. map @{$_->grow($w/2 + 10)}, @overhang_perimeters
  209. ]);
  210. }
  211. }
  212. if (1) {
  213. # remove the entire bridges and only support the unsupported edges
  214. my @bridges = map $_->expolygon,
  215. grep $_->bridge_angle != -1,
  216. @{$layerm->fill_surfaces->filter_by_type(S_TYPE_BOTTOMBRIDGE)};
  217. $diff = diff(
  218. $diff,
  219. [
  220. (map @$_, @bridges),
  221. @$bridged_perimeters,
  222. ],
  223. 1,
  224. );
  225. push @$diff, @{intersection(
  226. [ map @{$_->grow(+scale MARGIN)}, @{$layerm->unsupported_bridge_edges} ],
  227. [ map @$_, @bridges ],
  228. )};
  229. } else {
  230. # just remove bridged areas
  231. $diff = diff(
  232. $diff,
  233. $layerm->bridged,
  234. 1,
  235. );
  236. }
  237. } # if ($self->object_config->dont_support_bridges)
  238. if ($buildplate_only) {
  239. # Don't support overhangs above the top surfaces.
  240. # This step is done before the contact surface is calcuated by growing the overhang region.
  241. $diff = diff($diff, $buildplate_only_top_surfaces);
  242. }
  243. next if !@$diff;
  244. push @overhang, @$diff; # NOTE: this is not the full overhang as it misses the outermost half of the perimeter width!
  245. # Let's define the required contact area by using a max gap of half the upper
  246. # extrusion width and extending the area according to the configured margin.
  247. # We increment the area in steps because we don't want our support to overflow
  248. # on the other side of the object (if it's very thin).
  249. {
  250. my $slices_margin = offset([ map @$_, @{$lower_layer->slices} ], +$fw/2);
  251. if ($buildplate_only) {
  252. # Trim the inflated contact surfaces by the top surfaces as well.
  253. push @$slices_margin, map $_->clone, @{$buildplate_only_top_surfaces};
  254. $slices_margin = union($slices_margin);
  255. }
  256. for ($fw/2, map {scale MARGIN_STEP} 1..(MARGIN / MARGIN_STEP)) {
  257. $diff = diff(
  258. offset(
  259. $diff,
  260. $_,
  261. JT_ROUND,
  262. scale(0.05)),
  263. $slices_margin
  264. );
  265. }
  266. }
  267. push @contact, @$diff;
  268. }
  269. }
  270. next if !@contact;
  271. # now apply the contact areas to the layer were they need to be made
  272. {
  273. # get the average nozzle diameter used on this layer
  274. my @nozzle_diameters = map $self->print_config->get_at('nozzle_diameter', $_),
  275. map { $_->config->perimeter_extruder-1, $_->config->infill_extruder-1, $_->config->solid_infill_extruder-1 }
  276. map $_->region, @{$layer->regions};
  277. my $nozzle_diameter = sum(@nozzle_diameters)/@nozzle_diameters;
  278. my $contact_z = $layer->print_z - $self->contact_distance($layer->height, $nozzle_diameter);
  279. # Ignore this contact area if it's too low.
  280. #FIXME Better to control the thickness of the interface layer printed, but that would
  281. # require having attributes (extrusion width / height, bridge flow etc) per island.
  282. next if $contact_z < $self->object_config->get_value('first_layer_height') - epsilon;
  283. $contact{$contact_z} = [ @contact ];
  284. $overhang{$contact_z} = [ @overhang ];
  285. if (0) {
  286. require "Slic3r/SVG.pm";
  287. Slic3r::SVG::output(Slic3r::DEBUG_OUT_PATH_PREFIX . "contact_" . $contact_z . ".svg",
  288. green_expolygons => union_ex($buildplate_only_top_surfaces),
  289. blue_expolygons => union_ex(\@contact),
  290. red_expolygons => union_ex(\@overhang),
  291. );
  292. }
  293. }
  294. }
  295. return (\%contact, \%overhang);
  296. }
  297. sub object_top {
  298. my ($self, $object, $contact) = @_;
  299. # find object top surfaces
  300. # we'll use them to clip our support and detect where does it stick
  301. my %top = (); # print_z => [ expolygons ]
  302. return \%top if ($self->object_config->support_material_buildplate_only);
  303. # Sum of unsupported contact areas above the current $layer->print_z.
  304. my $projection = [];
  305. foreach my $layer (reverse @{$object->layers}) {
  306. if (my @top = map @{$_->slices->filter_by_type(S_TYPE_TOP)}, @{$layer->regions}) {
  307. # compute projection of the contact areas above this top layer
  308. # first add all the 'new' contact areas to the current projection
  309. # ('new' means all the areas that are lower than the last top layer
  310. # we considered)
  311. my $min_top = min(keys %top) // max(keys %$contact);
  312. # use <= instead of just < because otherwise we'd ignore any contact regions
  313. # having the same Z of top layers
  314. push @$projection, map @{$contact->{$_}}, grep { $_ > $layer->print_z && $_ <= $min_top } keys %$contact;
  315. # Now find whether any projection of the contact surfaces above $layer->print_z not yet supported by any top surfaces above $layer->z falls onto this top surface.
  316. # $touching are the contact surfaces supported exclusively by this @top surfaaces.
  317. my $touching = intersection($projection, [ map $_->p, @top ]);
  318. if (@$touching) {
  319. # grow top surfaces so that interface and support generation are generated
  320. # with some spacing from object - it looks we don't need the actual
  321. # top shapes so this can be done here
  322. $top{ $layer->print_z } = offset($touching, $self->flow->scaled_width + $self->object_config->support_material_xy_spacing);
  323. }
  324. # remove the areas that touched from the projection that will continue on
  325. # next, lower, top surfaces
  326. $projection = diff($projection, $touching);
  327. }
  328. }
  329. return \%top;
  330. }
  331. sub support_layers_z {
  332. my ($self, $object, $contact_z, $top_z, $max_object_layer_height) = @_;
  333. # quick table to check whether a given Z is a top surface
  334. my %top = map { $_ => 1 } @$top_z;
  335. # determine layer height for any non-contact layer
  336. # we use max() to prevent many ultra-thin layers to be inserted in case
  337. # layer_height > nozzle_diameter * 0.75
  338. my $nozzle_diameter = $self->print_config->get_at('nozzle_diameter', $self->object_config->support_material_extruder-1);
  339. my $support_material_height = max($max_object_layer_height, $nozzle_diameter * 0.75);
  340. my $contact_distance = $self->contact_distance($support_material_height, $nozzle_diameter);
  341. # initialize known, fixed, support layers
  342. my @z = @$contact_z;
  343. my $synchronize = $self->object_config->support_material_synchronize_layers;
  344. if (! $synchronize) {
  345. push @z,
  346. # TODO: why we have this?
  347. # Vojtech: To detect the bottom interface layers by finding a Z value in the $top_z.
  348. @$top_z;
  349. push @z,
  350. # Top surfaces of the bottom interface layers.
  351. (map $_ + $contact_distance, @$top_z);
  352. }
  353. @z = sort { $a <=> $b } @z;
  354. # enforce first layer height
  355. my $first_layer_height = $self->object_config->get_value('first_layer_height');
  356. shift @z while @z && $z[0] <= $first_layer_height;
  357. unshift @z, $first_layer_height;
  358. # add raft layers by dividing the space between first layer and
  359. # first contact layer evenly
  360. if ($self->object_config->raft_layers > 1 && @z >= 2) {
  361. # $z[1] is last raft layer (contact layer for the first layer object)
  362. my $height = ($z[1] - $z[0]) / ($self->object_config->raft_layers - 1);
  363. # since we already have two raft layers ($z[0] and $z[1]) we need to insert
  364. # raft_layers-2 more
  365. splice @z, 1, 0,
  366. map { sprintf "%.2f", $_ }
  367. map { $z[0] + $height * $_ }
  368. 1..($self->object_config->raft_layers - 2);
  369. }
  370. if ($synchronize) {
  371. @z = splice @z, $self->object_config->raft_layers;
  372. # if ($self->object_config->raft_layers > scalar(@z));
  373. push @z, map $_->print_z, @{$object->layers};
  374. } else {
  375. # create other layers (skip raft layers as they're already done and use thicker layers)
  376. for (my $i = $#z; $i >= $self->object_config->raft_layers; $i--) {
  377. my $target_height = $support_material_height;
  378. if ($i > 0 && $top{ $z[$i-1] }) {
  379. # Bridge flow?
  380. #FIXME We want to enforce not only the bridge flow height, but also the interface gap!
  381. # This will introduce an additional layer if the gap is set to an extreme value!
  382. $target_height = $nozzle_diameter;
  383. }
  384. # enforce first layer height
  385. #FIXME better to split the layers regularly, than to bite a constant height one at a time,
  386. # and then be left with a very thin layer at the end.
  387. if (($i == 0 && $z[$i] > $target_height + $first_layer_height)
  388. || ($z[$i] - $z[$i-1] > $target_height + Slic3r::Geometry::epsilon)) {
  389. splice @z, $i, 0, ($z[$i] - $target_height);
  390. $i++;
  391. }
  392. }
  393. }
  394. # remove duplicates and make sure all 0.x values have the leading 0
  395. {
  396. my %sl = map { 1 * $_ => 1 } @z;
  397. @z = sort { $a <=> $b } keys %sl;
  398. }
  399. return \@z;
  400. }
  401. sub generate_top_interface_layers {
  402. my ($self, $support_z, $contact, $top) = @_;
  403. # If no interface layers are allowed, don't generate top interface layers.
  404. return if $self->object_config->support_material_interface_layers == 0;
  405. # let's now generate interface layers below contact areas
  406. my %interface = (); # layer_id => [ polygons ]
  407. my $interface_layers_num = $self->object_config->support_material_interface_layers;
  408. for my $layer_id (0 .. $#$support_z) {
  409. my $z = $support_z->[$layer_id];
  410. my $this = $contact->{$z} // next;
  411. # count contact layer as interface layer
  412. for (my $i = $layer_id-1; $i >= 0 && $i > $layer_id-$interface_layers_num; $i--) {
  413. $z = $support_z->[$i];
  414. my @overlapping_layers = $self->overlapping_layers($i, $support_z);
  415. my @overlapping_z = map $support_z->[$_], @overlapping_layers;
  416. # Compute interface area on this layer as diff of upper contact area
  417. # (or upper interface area) and layer slices.
  418. # This diff is responsible of the contact between support material and
  419. # the top surfaces of the object. We should probably offset the top
  420. # surfaces vertically before performing the diff, but this needs
  421. # investigation.
  422. $this = $interface{$i} = diff(
  423. [
  424. @$this, # clipped projection of the current contact regions
  425. @{ $interface{$i} || [] }, # interface regions already applied to this layer
  426. ],
  427. [
  428. (map @$_, map $top->{$_}, grep exists $top->{$_}, @overlapping_z), # top slices on this layer
  429. (map @$_, map $contact->{$_}, grep exists $contact->{$_}, @overlapping_z), # contact regions on this layer
  430. ],
  431. 1,
  432. );
  433. }
  434. }
  435. return \%interface;
  436. }
  437. sub generate_bottom_interface_layers {
  438. my ($self, $support_z, $base, $top, $interface) = @_;
  439. # If no interface layers are allowed, don't generate bottom interface layers.
  440. return if $self->object_config->support_material_interface_layers == 0;
  441. my $area_threshold = $self->interface_flow->scaled_spacing ** 2;
  442. # loop through object's top surfaces
  443. foreach my $top_z (sort keys %$top) {
  444. my $this = $top->{$top_z};
  445. # keep a count of the interface layers we generated for this top surface
  446. my $interface_layers = 0;
  447. # loop through support layers until we find the one(s) right above the top
  448. # surface
  449. foreach my $layer_id (0 .. $#$support_z) {
  450. my $z = $support_z->[$layer_id];
  451. next unless $z > $top_z;
  452. if ($base->{$layer_id}) {
  453. # get the support material area that should be considered interface
  454. my $interface_area = intersection(
  455. $base->{$layer_id},
  456. $this,
  457. );
  458. # discard too small areas
  459. $interface_area = [ grep abs($_->area) >= $area_threshold, @$interface_area ];
  460. # subtract new interface area from base
  461. $base->{$layer_id} = diff(
  462. $base->{$layer_id},
  463. $interface_area,
  464. );
  465. # add new interface area to interface
  466. push @{$interface->{$layer_id}}, @$interface_area;
  467. }
  468. $interface_layers++;
  469. last if $interface_layers == $self->object_config->support_material_interface_layers;
  470. }
  471. }
  472. }
  473. sub generate_base_layers {
  474. my ($self, $support_z, $contact, $interface, $top) = @_;
  475. # let's now generate support layers under interface layers
  476. my $base = {}; # layer_id => [ polygons ]
  477. {
  478. my $fillet_radius_scaled = scale($self->object_config->support_material_spacing);
  479. for my $i (reverse 0 .. $#$support_z-1) {
  480. my $z = $support_z->[$i];
  481. my @overlapping_layers = $self->overlapping_layers($i, $support_z);
  482. my @overlapping_z = map $support_z->[$_], @overlapping_layers;
  483. # in case we have no interface layers, look at upper contact
  484. # (1 interface layer means we only have contact layer, so $interface->{$i+1} is empty)
  485. my @upper_contact = ();
  486. if ($self->object_config->support_material_interface_layers <= 1) {
  487. @upper_contact = @{ $contact->{$support_z->[$i+1]} || [] };
  488. }
  489. my $trim_polygons = [
  490. (map @$_, map $top->{$_}, grep exists $top->{$_}, @overlapping_z), # top slices on this layer
  491. (map @$_, map $interface->{$_}, grep exists $interface->{$_}, @overlapping_layers), # interface regions on this layer
  492. (map @$_, map $contact->{$_}, grep exists $contact->{$_}, @overlapping_z), # contact regions on this layer
  493. ];
  494. $base->{$i} = diff(
  495. [
  496. @{ $base->{$i+1} || [] }, # support regions on upper layer
  497. @{ $interface->{$i+1} || [] }, # interface regions on upper layer
  498. @upper_contact, # contact regions on upper layer
  499. ],
  500. $trim_polygons,
  501. 1, # safety offset to merge the touching source polygons
  502. );
  503. if (0) {
  504. # Fillet the base polygons and trim them again with the top, interface and contact layers.
  505. $base->{$i} = diff(
  506. offset2(
  507. $base->{$i},
  508. $fillet_radius_scaled,
  509. -$fillet_radius_scaled,
  510. # Use a geometric offsetting for filleting.
  511. JT_ROUND,
  512. 0.2*$fillet_radius_scaled),
  513. $trim_polygons,
  514. 0); # don't apply the safety offset.
  515. }
  516. }
  517. }
  518. return $base;
  519. }
  520. # This method removes object silhouette from support material
  521. # (it's used with interface and base only). It removes a bit more,
  522. # leaving a thin gap between object and support in the XY plane.
  523. sub clip_with_object {
  524. my ($self, $support, $support_z, $object) = @_;
  525. foreach my $i (keys %$support) {
  526. next if !@{$support->{$i}};
  527. my $zmax = $support_z->[$i];
  528. my $zmin = ($i == 0) ? 0 : $support_z->[$i-1];
  529. my @layers = grep { $_->print_z > $zmin && ($_->print_z - $_->height) < $zmax }
  530. @{$object->layers};
  531. # $layer->slices contains the full shape of layer, thus including
  532. # perimeter's width. $support contains the full shape of support
  533. # material, thus including the width of its foremost extrusion.
  534. # We leave a gap equal to a full extrusion width + an offset
  535. # if the user wants to play around with this setting.
  536. $support->{$i} = diff(
  537. $support->{$i},
  538. offset([ map @$_, map @{$_->slices}, @layers ], +$self->object_config->support_material_xy_spacing),
  539. );
  540. }
  541. }
  542. sub generate_toolpaths {
  543. my ($self, $object, $overhang, $contact, $interface, $base) = @_;
  544. my $flow = $self->flow;
  545. my $interface_flow = $self->interface_flow;
  546. # shape of contact area
  547. my $contact_loops = $self->object_config->support_material_interface_contact_loops ? 1 : 0;
  548. my $circle_radius = 1.5 * $interface_flow->scaled_width - ($self->object_config->support_material_xy_spacing / 0.000001) ;
  549. my $circle_distance = 3 * $circle_radius;
  550. my $circle = Slic3r::Polygon->new(map [ $circle_radius * cos $_, $circle_radius * sin $_ ],
  551. (5*PI/3, 4*PI/3, PI, 2*PI/3, PI/3, 0));
  552. Slic3r::debugf "Generating patterns\n";
  553. # prepare fillers
  554. my $pattern = $self->object_config->support_material_pattern;
  555. my $with_sheath = $self->object_config->support_material_with_sheath;
  556. my @angles = ($self->object_config->support_material_angle);
  557. if ($pattern eq 'rectilinear-grid') {
  558. $pattern = 'rectilinear';
  559. push @angles, $angles[0] + 90;
  560. } elsif ($pattern eq 'pillars') {
  561. $pattern = 'honeycomb';
  562. }
  563. my $interface_angle = $self->object_config->support_material_angle + 90;
  564. my $interface_spacing = $self->object_config->support_material_interface_spacing + $interface_flow->spacing;
  565. my $interface_density = $interface_spacing == 0 ? 1 : $interface_flow->spacing / $interface_spacing;
  566. my $support_spacing = $self->object_config->support_material_spacing + $flow->spacing;
  567. my $support_density = $support_spacing == 0 ? 1 : $flow->spacing / $support_spacing;
  568. my $process_layer = sub {
  569. my ($layer_id) = @_;
  570. my $layer = $object->support_layers->[$layer_id];
  571. my $z = $layer->print_z;
  572. # we redefine flows locally by applying this layer's height
  573. my $_flow = $flow->clone;
  574. my $_interface_flow = $interface_flow->clone;
  575. $_flow->set_height($layer->height);
  576. $_interface_flow->set_height($layer->height);
  577. my $overhang = $overhang->{$z} || [];
  578. my $contact = $contact->{$z} || [];
  579. my $interface = $interface->{$layer_id} || [];
  580. my $base = $base->{$layer_id} || [];
  581. if (DEBUG_CONTACT_ONLY) {
  582. $interface = [];
  583. $base = [];
  584. }
  585. if (0) {
  586. require "Slic3r/SVG.pm";
  587. Slic3r::SVG::output(Slic3r::DEBUG_OUT_PATH_PREFIX . "layer_" . $z . ".svg",
  588. blue_expolygons => union_ex($base),
  589. red_expolygons => union_ex($contact),
  590. green_expolygons => union_ex($interface),
  591. );
  592. }
  593. # islands
  594. $layer->support_islands->append(@{union_ex([ @$interface, @$base, @$contact ])});
  595. # contact
  596. my $contact_infill = [];
  597. if ($self->object_config->support_material_interface_layers == 0) {
  598. # if no interface layers were requested we treat the contact layer
  599. # exactly as a generic base layer
  600. push @$base, @$contact;
  601. } elsif ($contact_loops == 0) {
  602. # No contact loops, but some interface layers. Print the contact layer as a normal interface layer.
  603. push @$interface, @$contact;
  604. } elsif (@$contact) {
  605. # generate the outermost loop
  606. # find centerline of the external loop (or any other kind of extrusions should the loop be skipped)
  607. $contact = offset($contact, -($_interface_flow->scaled_width + ($self->object_config->support_material_xy_spacing / 0.000001)) /2);
  608. my @loops0 = ();
  609. {
  610. # find centerline of the external loop of the contours
  611. my @external_loops = @$contact;
  612. # only consider the loops facing the overhang
  613. {
  614. my $overhang_with_margin = offset($overhang, +$_interface_flow->scaled_width/2);
  615. @external_loops = grep {
  616. @{intersection_pl(
  617. [ $_->split_at_first_point ],
  618. $overhang_with_margin,
  619. )}
  620. } @external_loops;
  621. }
  622. # apply a pattern to the loop
  623. my @positions = map @{Slic3r::Polygon->new(@$_)->equally_spaced_points($circle_distance)}, @external_loops;
  624. @loops0 = @{diff(
  625. [ @external_loops ],
  626. [ map { my $c = $circle->clone; $c->translate(@$_); $c } @positions ],
  627. )};
  628. }
  629. # make more loops
  630. my @loops = @loops0;
  631. for my $i (2..$contact_loops) {
  632. my $d = ($i-1) * $_interface_flow->scaled_spacing;
  633. push @loops, @{offset2(\@loops0, -$d -0.5*$_interface_flow->scaled_spacing, +0.5*$_interface_flow->scaled_spacing)};
  634. }
  635. # clip such loops to the side oriented towards the object
  636. @loops = @{intersection_pl(
  637. [ map $_->split_at_first_point, @loops ],
  638. offset($overhang, +scale MARGIN),
  639. )};
  640. # add the contact infill area to the interface area
  641. # note that growing loops by $circle_radius ensures no tiny
  642. # extrusions are left inside the circles; however it creates
  643. # a very large gap between loops and contact_infill, so maybe another
  644. # solution should be found to achieve both goals
  645. $contact_infill = diff(
  646. $contact,
  647. [ map @{$_->grow($circle_radius*1.1)}, @loops ],
  648. );
  649. # transform loops into ExtrusionPath objects
  650. my $mm3_per_mm = $_interface_flow->mm3_per_mm;
  651. @loops = map Slic3r::ExtrusionPath->new(
  652. polyline => $_,
  653. role => EXTR_ROLE_SUPPORTMATERIAL_INTERFACE,
  654. mm3_per_mm => $mm3_per_mm,
  655. width => $_interface_flow->width,
  656. height => $layer->height,
  657. ), @loops;
  658. $layer->support_interface_fills->append(@loops);
  659. }
  660. # Allocate the fillers exclusively in the worker threads! Don't allocate them at the main thread,
  661. # as Perl copies the C++ pointers by default, so then the C++ objects are shared between threads!
  662. my %fillers = (
  663. interface => Slic3r::Filler->new_from_type('rectilinear'),
  664. support => Slic3r::Filler->new_from_type($pattern),
  665. );
  666. my $bounding_box = $object->bounding_box;
  667. $fillers{interface}->set_bounding_box($object->bounding_box);
  668. $fillers{support}->set_bounding_box($object->bounding_box);
  669. # interface and contact infill
  670. if (@$interface || @$contact_infill) {
  671. $fillers{interface}->set_angle($interface_angle);
  672. $fillers{interface}->set_spacing($_interface_flow->spacing);
  673. # find centerline of the external loop
  674. $interface = offset2($interface, +scaled_epsilon, -(scaled_epsilon + $_interface_flow->scaled_width/2));
  675. # join regions by offsetting them to ensure they're merged
  676. $interface = offset([ @$interface, @$contact_infill ], scaled_epsilon);
  677. # turn base support into interface when it's contained in our holes
  678. # (this way we get wider interface anchoring)
  679. {
  680. my @p = @$interface;
  681. @$interface = ();
  682. foreach my $p (@p) {
  683. if ($p->is_clockwise) {
  684. my $p2 = $p->clone;
  685. $p2->make_counter_clockwise;
  686. next if !@{diff([$p2], $base, 1)};
  687. }
  688. push @$interface, $p;
  689. }
  690. }
  691. $base = diff($base, $interface);
  692. my @paths = ();
  693. foreach my $expolygon (@{union_ex($interface)}) {
  694. my $polylines = $fillers{interface}->fill_surface(
  695. Slic3r::Surface->new(expolygon => $expolygon, surface_type => S_TYPE_INTERNAL),
  696. density => $interface_density,
  697. layer_height => $layer->height,
  698. complete => 1,
  699. );
  700. my $mm3_per_mm = $_interface_flow->mm3_per_mm;
  701. push @paths, map Slic3r::ExtrusionPath->new(
  702. polyline => Slic3r::Polyline->new(@$_),
  703. role => EXTR_ROLE_SUPPORTMATERIAL_INTERFACE,
  704. mm3_per_mm => $mm3_per_mm,
  705. width => $_interface_flow->width,
  706. height => $layer->height,
  707. ), @$polylines,
  708. }
  709. $layer->support_interface_fills->append(@paths);
  710. }
  711. # support or flange
  712. if (@$base) {
  713. my $filler = $fillers{support};
  714. $filler->set_angle($angles[ ($layer_id) % @angles ]);
  715. # We don't use $base_flow->spacing because we need a constant spacing
  716. # value that guarantees that all layers are correctly aligned.
  717. $filler->set_spacing($flow->spacing);
  718. my $density = $support_density;
  719. my $base_flow = $_flow;
  720. # find centerline of the external loop/extrusions
  721. my $to_infill = offset2_ex($base, +scaled_epsilon, -(scaled_epsilon + $_flow->scaled_width/2));
  722. if (0) {
  723. require "Slic3r/SVG.pm";
  724. Slic3r::SVG::output(Slic3r::DEBUG_OUT_PATH_PREFIX . "to_infill_base" . $z . ".svg",
  725. red_expolygons => union_ex($contact),
  726. green_expolygons => union_ex($interface),
  727. blue_expolygons => $to_infill,
  728. );
  729. }
  730. my @paths = ();
  731. # base flange
  732. if ($layer_id == 0) {
  733. $filler = $fillers{interface};
  734. $filler->set_angle($self->object_config->support_material_angle + 90);
  735. $density = 0.5;
  736. $base_flow = $self->first_layer_flow;
  737. # use the proper spacing for first layer as we don't need to align
  738. # its pattern to the other layers
  739. $filler->set_spacing($base_flow->spacing);
  740. } elsif ($with_sheath) {
  741. # draw a perimeter all around support infill
  742. # TODO: use brim ordering algorithm
  743. my $mm3_per_mm = $_flow->mm3_per_mm;
  744. push @paths, map Slic3r::ExtrusionPath->new(
  745. polyline => $_->split_at_first_point,
  746. role => EXTR_ROLE_SUPPORTMATERIAL,
  747. mm3_per_mm => $mm3_per_mm,
  748. width => $_flow->width,
  749. height => $layer->height,
  750. ), map @$_, @$to_infill;
  751. # TODO: use offset2_ex()
  752. $to_infill = offset_ex([ map @$_, @$to_infill ], -$_flow->scaled_spacing);
  753. }
  754. foreach my $expolygon (@$to_infill) {
  755. my $polylines = $filler->fill_surface(
  756. Slic3r::Surface->new(expolygon => $expolygon, surface_type => S_TYPE_INTERNAL),
  757. density => $density,
  758. layer_height => $layer->height,
  759. complete => 1,
  760. );
  761. push @paths, map Slic3r::ExtrusionPath->new(
  762. polyline => Slic3r::Polyline->new(@$_),
  763. role => EXTR_ROLE_SUPPORTMATERIAL,
  764. mm3_per_mm => $base_flow->mm3_per_mm,
  765. width => $base_flow->width,
  766. height => $layer->height,
  767. ), @$polylines;
  768. }
  769. $layer->support_fills->append(@paths);
  770. }
  771. if (0) {
  772. require "Slic3r/SVG.pm";
  773. Slic3r::SVG::output("islands_" . $z . ".svg",
  774. red_expolygons => union_ex($contact),
  775. green_expolygons => union_ex($interface),
  776. green_polylines => [ map $_->unpack->polyline, @{$layer->support_contact_fills} ],
  777. polylines => [ map $_->unpack->polyline, @{$layer->support_fills} ],
  778. );
  779. }
  780. };
  781. Slic3r::parallelize(
  782. threads => $self->print_config->threads,
  783. items => [ 0 .. $#{$object->support_layers} ],
  784. thread_cb => sub {
  785. my $q = shift;
  786. while (defined (my $layer_id = $q->dequeue)) {
  787. $process_layer->($layer_id);
  788. }
  789. },
  790. no_threads_cb => sub {
  791. $process_layer->($_) for 0 .. $#{$object->support_layers};
  792. },
  793. );
  794. }
  795. sub generate_pillars_shape {
  796. my ($self, $contact, $support_z, $shape) = @_;
  797. # this prevents supplying an empty point set to BoundingBox constructor
  798. return if !%$contact;
  799. my $pillar_size = scale PILLAR_SIZE;
  800. my $pillar_spacing = scale PILLAR_SPACING;
  801. # A regular grid of pillars, filling the 2D bounding box.
  802. # arrayref of polygons
  803. my $grid; # arrayref of polygons
  804. {
  805. # Rectangle with a side of 2.5x2.5mm.
  806. my $pillar = Slic3r::Polygon->new(
  807. [0,0],
  808. [$pillar_size, 0],
  809. [$pillar_size, $pillar_size],
  810. [0, $pillar_size],
  811. );
  812. # A regular grid of pillars, filling the 2D bounding box.
  813. my @pillars = ();
  814. # 2D bounding box of the projection of all contact polygons.
  815. my $bb = Slic3r::Geometry::BoundingBox->new_from_points([ map @$_, map @$_, values %$contact ]);
  816. for (my $x = $bb->x_min; $x <= $bb->x_max-$pillar_size; $x += $pillar_spacing) {
  817. for (my $y = $bb->y_min; $y <= $bb->y_max-$pillar_size; $y += $pillar_spacing) {
  818. push @pillars, my $p = $pillar->clone;
  819. $p->translate($x, $y);
  820. }
  821. }
  822. $grid = union(\@pillars);
  823. }
  824. # add pillars to every layer
  825. for my $i (0..$#$support_z) {
  826. $shape->[$i] = [ @$grid ];
  827. }
  828. # build capitals
  829. for my $i (0..$#$support_z) {
  830. my $z = $support_z->[$i];
  831. my $capitals = intersection(
  832. $grid,
  833. $contact->{$z} // [],
  834. );
  835. # work on one pillar at time (if any) to prevent the capitals from being merged
  836. # but store the contact area supported by the capital because we need to make
  837. # sure nothing is left
  838. my $contact_supported_by_capitals = [];
  839. foreach my $capital (@$capitals) {
  840. # enlarge capital tops
  841. $capital = offset([$capital], +($pillar_spacing - $pillar_size)/2);
  842. push @$contact_supported_by_capitals, @$capital;
  843. for (my $j = $i-1; $j >= 0; $j--) {
  844. my $jz = $support_z->[$j];
  845. $capital = offset($capital, -$self->interface_flow->scaled_width/2);
  846. last if !@$capitals;
  847. push @{ $shape->[$j] }, @$capital;
  848. }
  849. }
  850. # Capitals will not generally cover the whole contact area because there will be
  851. # remainders. For now we handle this situation by projecting such unsupported
  852. # areas to the ground, just like we would do with a normal support.
  853. my $contact_not_supported_by_capitals = diff(
  854. $contact->{$z} // [],
  855. $contact_supported_by_capitals,
  856. );
  857. if (@$contact_not_supported_by_capitals) {
  858. for (my $j = $i-1; $j >= 0; $j--) {
  859. push @{ $shape->[$j] }, @$contact_not_supported_by_capitals;
  860. }
  861. }
  862. }
  863. }
  864. sub clip_with_shape {
  865. my ($self, $support, $shape) = @_;
  866. foreach my $i (keys %$support) {
  867. # don't clip bottom layer with shape so that we
  868. # can generate a continuous base flange
  869. # also don't clip raft layers
  870. next if $i == 0;
  871. next if $i < $self->object_config->raft_layers;
  872. $support->{$i} = intersection(
  873. $support->{$i},
  874. $shape->[$i],
  875. );
  876. }
  877. }
  878. # this method returns the indices of the layers overlapping with the given one
  879. sub overlapping_layers {
  880. my ($self, $i, $support_z) = @_;
  881. my $zmax = $support_z->[$i];
  882. my $zmin = ($i == 0) ? 0 : $support_z->[$i-1];
  883. return grep {
  884. my $zmax2 = $support_z->[$_];
  885. my $zmin2 = ($_ == 0) ? 0 : $support_z->[$_-1];
  886. $zmax > $zmin2 && $zmin < $zmax2;
  887. } 0..$#$support_z;
  888. }
  889. sub contact_distance {
  890. my ($self, $layer_height, $nozzle_diameter) = @_;
  891. my $extra = $self->object_config->support_material_contact_distance;
  892. if ($extra == 0) {
  893. return $layer_height;
  894. } else {
  895. return $nozzle_diameter + $extra;
  896. }
  897. }
  898. 1;