SupportMaterial.pm 35 KB

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