SupportMaterial.pm 36 KB

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