SupportMaterial.pm 42 KB

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