SupportMaterial.pm 36 KB

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