SupportMaterial.pm 36 KB

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