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