SupportMaterial.pm 44 KB

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