SupportMaterial.pm 36 KB

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