SupportMaterial.pm 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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);
  7. use Slic3r::Geometry::Clipper qw(offset diff union union_ex intersection offset_ex offset2
  8. intersection_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. sub generate {
  21. my ($self, $object) = @_;
  22. # Determine the top surfaces of the support, defined as:
  23. # contact = overhangs - clearance + margin
  24. # This method is responsible for identifying what contact surfaces
  25. # should the support material expose to the object in order to guarantee
  26. # that it will be effective, regardless of how it's built below.
  27. my ($contact, $overhang) = $self->contact_area($object);
  28. # Determine the top surfaces of the object. We need these to determine
  29. # the layer heights of support material and to clip support to the object
  30. # silhouette.
  31. my ($top) = $self->object_top($object, $contact);
  32. # We now know the upper and lower boundaries for our support material object
  33. # (@$contact_z and @$top_z), so we can generate intermediate layers.
  34. my ($support_z) = $self->support_layers_z(
  35. [ sort keys %$contact ],
  36. [ sort keys %$top ],
  37. max(map $_->height, @{$object->layers})
  38. );
  39. # If we wanted to apply some special logic to the first support layers lying on
  40. # object's top surfaces this is the place to detect them
  41. # Propagate contact layers downwards to generate interface layers
  42. my ($interface) = $self->generate_interface_layers($support_z, $contact, $top);
  43. $self->clip_with_object($interface, $support_z, $object);
  44. # Propagate contact layers and interface layers downwards to generate
  45. # the main support layers.
  46. my ($base) = $self->generate_base_layers($support_z, $contact, $interface, $top);
  47. $self->clip_with_object($base, $support_z, $object);
  48. # Install support layers into object.
  49. push @{$object->support_layers}, map Slic3r::Layer::Support->new(
  50. object => $object,
  51. id => $_,
  52. height => ($_ == 0) ? $support_z->[$_] : ($support_z->[$_] - $support_z->[$_-1]),
  53. print_z => $support_z->[$_],
  54. slice_z => -1,
  55. slices => [],
  56. ), 0 .. $#$support_z;
  57. # Generate the actual toolpaths and save them into each layer.
  58. $self->generate_toolpaths($object, $overhang, $contact, $interface, $base);
  59. }
  60. sub contact_area {
  61. my ($self, $object) = @_;
  62. # if user specified a custom angle threshold, convert it to radians
  63. my $threshold_rad;
  64. if ($self->object_config->support_material_threshold) {
  65. $threshold_rad = deg2rad($self->object_config->support_material_threshold + 1); # +1 makes the threshold inclusive
  66. Slic3r::debugf "Threshold angle = %d°\n", rad2deg($threshold_rad);
  67. }
  68. # determine contact areas
  69. my %contact = (); # contact_z => [ polygons ]
  70. my %overhang = (); # contact_z => [ polygons ] - this stores the actual overhang supported by each contact layer
  71. for my $layer_id (0 .. $#{$object->layers}) {
  72. # note $layer_id might != $layer->id when raft_layers > 0
  73. # so $layer_id == 0 means first object layer
  74. # and $layer->id == 0 means first print layer (including raft)
  75. if ($self->object_config->raft_layers == 0) {
  76. next if $layer_id == 0;
  77. } elsif (!$self->object_config->support_material) {
  78. # if we are only going to generate raft just check
  79. # the 'overhangs' of the first object layer
  80. last if $layer_id > 0;
  81. }
  82. my $layer = $object->layers->[$layer_id];
  83. # detect overhangs and contact areas needed to support them
  84. my (@overhang, @contact) = ();
  85. if ($layer_id == 0) {
  86. # this is the first object layer, so we're here just to get the object
  87. # footprint for the raft
  88. push @overhang, map $_->clone, map @$_, @{$layer->slices};
  89. push @contact, @{offset(\@overhang, scale +MARGIN)};
  90. } else {
  91. my $lower_layer = $object->layers->[$layer_id-1];
  92. foreach my $layerm (@{$layer->regions}) {
  93. my $fw = $layerm->flow(FLOW_ROLE_PERIMETER)->scaled_width;
  94. my $diff;
  95. # If a threshold angle was specified, use a different logic for detecting overhangs.
  96. if (defined $threshold_rad
  97. || $layer_id < $self->object_config->support_material_enforce_layers
  98. || $self->object_config->raft_layers > 0) {
  99. my $d = defined $threshold_rad
  100. ? scale $lower_layer->height * ((cos $threshold_rad) / (sin $threshold_rad))
  101. : 0;
  102. $diff = diff(
  103. offset([ map $_->p, @{$layerm->slices} ], -$d),
  104. [ map @$_, @{$lower_layer->slices} ],
  105. );
  106. # only enforce spacing from the object ($fw/2) if the threshold angle
  107. # is not too high: in that case, $d will be very small (as we need to catch
  108. # very short overhangs), and such contact area would be eaten by the
  109. # enforced spacing, resulting in high threshold angles to be almost ignored
  110. $diff = diff(
  111. offset($diff, $d - $fw/2),
  112. [ map @$_, @{$lower_layer->slices} ],
  113. ) if $d > $fw/2;
  114. } else {
  115. $diff = diff(
  116. offset([ map $_->p, @{$layerm->slices} ], -$fw/2),
  117. [ map @$_, @{$lower_layer->slices} ],
  118. );
  119. # collapse very tiny spots
  120. $diff = offset2($diff, -$fw/10, +$fw/10);
  121. # $diff now contains the ring or stripe comprised between the boundary of
  122. # lower slices and the centerline of the last perimeter in this overhanging layer.
  123. # Void $diff means that there's no upper perimeter whose centerline is
  124. # outside the lower slice boundary, thus no overhang
  125. }
  126. # TODO: this is the place to remove bridged areas
  127. next if !@$diff;
  128. push @overhang, @$diff; # NOTE: this is not the full overhang as it misses the outermost half of the perimeter width!
  129. # Let's define the required contact area by using a max gap of half the upper
  130. # extrusion width and extending the area according to the configured margin.
  131. # We increment the area in steps because we don't want our support to overflow
  132. # on the other side of the object (if it's very thin).
  133. {
  134. my @slices_margin = @{offset([ map @$_, @{$lower_layer->slices} ], $fw/2)};
  135. for ($fw/2, map {scale MARGIN_STEP} 1..(MARGIN / MARGIN_STEP)) {
  136. $diff = diff(
  137. offset($diff, $_),
  138. \@slices_margin,
  139. );
  140. }
  141. }
  142. push @contact, @$diff;
  143. }
  144. }
  145. next if !@contact;
  146. # now apply the contact areas to the layer were they need to be made
  147. {
  148. # get the average nozzle diameter used on this layer
  149. my @nozzle_diameters = map $self->print_config->get_at('nozzle_diameter', $_),
  150. map { $_->config->perimeter_extruder-1, $_->config->infill_extruder-1 }
  151. @{$layer->regions};
  152. my $nozzle_diameter = sum(@nozzle_diameters)/@nozzle_diameters;
  153. my $contact_z = $layer->print_z - $nozzle_diameter * 1.5;
  154. ###$contact_z = $layer->print_z - $layer->height;
  155. # ignore this contact area if it's too low
  156. next if $contact_z < $self->object_config->get_value('first_layer_height');
  157. $contact{$contact_z} = [ @contact ];
  158. $overhang{$contact_z} = [ @overhang ];
  159. if (0) {
  160. require "Slic3r/SVG.pm";
  161. Slic3r::SVG::output("contact_" . $contact_z . ".svg",
  162. expolygons => union_ex(\@contact),
  163. red_expolygons => union_ex(\@overhang),
  164. );
  165. }
  166. }
  167. }
  168. return (\%contact, \%overhang);
  169. }
  170. sub object_top {
  171. my ($self, $object, $contact) = @_;
  172. # find object top surfaces
  173. # we'll use them to clip our support and detect where does it stick
  174. my %top = (); # print_z => [ expolygons ]
  175. my $projection = [];
  176. foreach my $layer (reverse @{$object->layers}) {
  177. if (my @top = map @{$_->slices->filter_by_type(S_TYPE_TOP)}, @{$layer->regions}) {
  178. # compute projection of the contact areas above this top layer
  179. # first add all the 'new' contact areas to the current projection
  180. # ('new' means all the areas that are lower than the last top layer
  181. # we considered)
  182. my $min_top = min(keys %top) // max(keys %$contact);
  183. # use <= instead of just < because otherwise we'd ignore any contact regions
  184. # having the same Z of top layers
  185. push @$projection, map @{$contact->{$_}}, grep { $_ > $layer->print_z && $_ <= $min_top } keys %$contact;
  186. # now find whether any projection falls onto this top surface
  187. my $touching = intersection($projection, [ map $_->p, @top ]);
  188. if (@$touching) {
  189. # grow top surfaces so that interface and support generation are generated
  190. # with some spacing from object - it looks we don't need the actual
  191. # top shapes so this can be done here
  192. $top{ $layer->print_z } = offset($touching, $self->flow->scaled_width);
  193. }
  194. # remove the areas that touched from the projection that will continue on
  195. # next, lower, top surfaces
  196. $projection = diff($projection, $touching);
  197. }
  198. }
  199. return \%top;
  200. }
  201. sub support_layers_z {
  202. my ($self, $contact_z, $top_z, $max_object_layer_height) = @_;
  203. # quick table to check whether a given Z is a top surface
  204. my %top = map { $_ => 1 } @$top_z;
  205. # determine layer height for any non-contact layer
  206. # we use max() to prevent many ultra-thin layers to be inserted in case
  207. # layer_height > nozzle_diameter * 0.75
  208. my $nozzle_diameter = $self->print_config->get_at('nozzle_diameter', $self->object_config->support_material_extruder-1);
  209. my $support_material_height = max($max_object_layer_height, $nozzle_diameter * 0.75);
  210. my @z = sort { $a <=> $b } @$contact_z, @$top_z, (map $_ + $nozzle_diameter, @$top_z);
  211. # enforce first layer height
  212. my $first_layer_height = $self->object_config->get_value('first_layer_height');
  213. shift @z while @z && $z[0] <= $first_layer_height;
  214. unshift @z, $first_layer_height;
  215. # add raft layers by dividing the space between first layer and
  216. # first contact layer evenly
  217. if ($self->object_config->raft_layers > 1 && @z >= 2) {
  218. # $z[1] is last raft layer (contact layer for the first layer object)
  219. my $height = ($z[1] - $z[0]) / ($self->object_config->raft_layers - 1);
  220. splice @z, 1, 0,
  221. map { int($_*100)/100 }
  222. map { $z[0] + $height * $_ }
  223. 0..($self->object_config->raft_layers - 1);
  224. }
  225. for (my $i = $#z; $i >= 0; $i--) {
  226. my $target_height = $support_material_height;
  227. if ($i > 0 && $top{ $z[$i-1] }) {
  228. $target_height = $nozzle_diameter;
  229. }
  230. # enforce first layer height
  231. if (($i == 0 && $z[$i] > $target_height + $first_layer_height)
  232. || ($z[$i] - $z[$i-1] > $target_height + Slic3r::Geometry::epsilon)) {
  233. splice @z, $i, 0, ($z[$i] - $target_height);
  234. $i++;
  235. }
  236. }
  237. # remove duplicates and make sure all 0.x values have the leading 0
  238. {
  239. my %sl = map { 1 * $_ => 1 } @z;
  240. @z = sort { $a <=> $b } keys %sl;
  241. }
  242. return \@z;
  243. }
  244. sub generate_interface_layers {
  245. my ($self, $support_z, $contact, $top) = @_;
  246. # let's now generate interface layers below contact areas
  247. my %interface = (); # layer_id => [ polygons ]
  248. my $interface_layers = $self->object_config->support_material_interface_layers;
  249. for my $layer_id (0 .. $#$support_z) {
  250. my $z = $support_z->[$layer_id];
  251. my $this = $contact->{$z} // next;
  252. # count contact layer as interface layer
  253. for (my $i = $layer_id-1; $i >= 0 && $i > $layer_id-$interface_layers; $i--) {
  254. $z = $support_z->[$i];
  255. my @overlapping_layers = $self->overlapping_layers($i, $support_z);
  256. my @overlapping_z = map $support_z->[$_], @overlapping_layers;
  257. # Compute interface area on this layer as diff of upper contact area
  258. # (or upper interface area) and layer slices.
  259. # This diff is responsible of the contact between support material and
  260. # the top surfaces of the object. We should probably offset the top
  261. # surfaces vertically before performing the diff, but this needs
  262. # investigation.
  263. $this = $interface{$i} = diff(
  264. [
  265. @$this, # clipped projection of the current contact regions
  266. @{ $interface{$i} || [] }, # interface regions already applied to this layer
  267. ],
  268. [
  269. (map @$_, map $top->{$_}, grep exists $top->{$_}, @overlapping_z), # top slices on this layer
  270. (map @$_, map $contact->{$_}, grep exists $contact->{$_}, @overlapping_z), # contact regions on this layer
  271. ],
  272. 1,
  273. );
  274. }
  275. }
  276. return \%interface;
  277. }
  278. sub generate_base_layers {
  279. my ($self, $support_z, $contact, $interface, $top) = @_;
  280. # let's now generate support layers under interface layers
  281. my $base = {}; # layer_id => [ polygons ]
  282. {
  283. for my $i (reverse 0 .. $#$support_z-1) {
  284. my $z = $support_z->[$i];
  285. my @overlapping_layers = $self->overlapping_layers($i, $support_z);
  286. my @overlapping_z = map $support_z->[$_], @overlapping_layers;
  287. # in case we have no interface layers, look at upper contact
  288. # (1 interface layer means we only have contact layer, so $interface->{$i+1} is empty)
  289. my @upper_contact = ();
  290. if ($self->object_config->support_material_interface_layers <= 1) {
  291. @upper_contact = @{ $contact->{$support_z->[$i+1]} || [] };
  292. }
  293. $base->{$i} = diff(
  294. [
  295. @{ $base->{$i+1} || [] }, # support regions on upper layer
  296. @{ $interface->{$i+1} || [] }, # interface regions on upper layer
  297. @upper_contact, # contact regions on upper layer
  298. ],
  299. [
  300. (map @$_, map $top->{$_}, grep exists $top->{$_}, @overlapping_z), # top slices on this layer
  301. (map @$_, map $interface->{$_}, grep exists $interface->{$_}, @overlapping_layers), # interface regions on this layer
  302. (map @$_, map $contact->{$_}, grep exists $contact->{$_}, @overlapping_z), # contact regions on this layer
  303. ],
  304. 1,
  305. );
  306. }
  307. }
  308. return $base;
  309. }
  310. sub clip_with_object {
  311. my ($self, $support, $support_z, $object) = @_;
  312. foreach my $i (keys %$support) {
  313. next if !@{$support->{$i}};
  314. my $zmax = $support_z->[$i];
  315. my $zmin = ($i == 0) ? 0 : $support_z->[$i-1];
  316. my @layers = grep { $_->print_z > $zmin && ($_->print_z - $_->height) < $zmax }
  317. @{$object->layers};
  318. $support->{$i} = diff(
  319. $support->{$i},
  320. offset([ map @$_, map @{$_->slices}, @layers ], +$self->flow->scaled_width),
  321. );
  322. }
  323. }
  324. sub generate_toolpaths {
  325. my ($self, $object, $overhang, $contact, $interface, $base) = @_;
  326. my $flow = $self->flow;
  327. my $interface_flow = $self->interface_flow;
  328. # shape of contact area
  329. my $contact_loops = 1;
  330. my $circle_radius = 1.5 * $interface_flow->scaled_width;
  331. my $circle_distance = 3 * $circle_radius;
  332. my $circle = Slic3r::Polygon->new(map [ $circle_radius * cos $_, $circle_radius * sin $_ ],
  333. (5*PI/3, 4*PI/3, PI, 2*PI/3, PI/3, 0));
  334. Slic3r::debugf "Generating patterns\n";
  335. # prepare fillers
  336. my $pattern = $self->object_config->support_material_pattern;
  337. my @angles = ($self->object_config->support_material_angle);
  338. if ($pattern eq 'rectilinear-grid') {
  339. $pattern = 'rectilinear';
  340. push @angles, $angles[0] + 90;
  341. }
  342. my %fillers = (
  343. interface => $object->fill_maker->filler('rectilinear'),
  344. support => $object->fill_maker->filler($pattern),
  345. );
  346. my $interface_angle = $self->object_config->support_material_angle + 90;
  347. my $interface_spacing = $self->object_config->support_material_interface_spacing + $interface_flow->spacing;
  348. my $interface_density = $interface_spacing == 0 ? 1 : $interface_flow->spacing / $interface_spacing;
  349. my $support_spacing = $self->object_config->support_material_spacing + $flow->spacing;
  350. my $support_density = $support_spacing == 0 ? 1 : $flow->spacing / $support_spacing;
  351. my $process_layer = sub {
  352. my ($layer_id) = @_;
  353. my $layer = $object->support_layers->[$layer_id];
  354. my $z = $layer->print_z;
  355. my $overhang = $overhang->{$z} || [];
  356. my $contact = $contact->{$z} || [];
  357. my $interface = $interface->{$layer_id} || [];
  358. my $base = $base->{$layer_id} || [];
  359. if (DEBUG_CONTACT_ONLY) {
  360. $interface = [];
  361. $base = [];
  362. }
  363. if (0) {
  364. require "Slic3r/SVG.pm";
  365. Slic3r::SVG::output("layer_" . $z . ".svg",
  366. red_expolygons => union_ex($contact),
  367. green_expolygons => union_ex($interface),
  368. );
  369. }
  370. # islands
  371. $layer->support_islands->append(@{union_ex([ @$interface, @$base, @$contact ])});
  372. # contact
  373. my $contact_infill = [];
  374. if ($self->object_config->support_material_interface_layers == 0) {
  375. # if no interface layers were requested we treat the contact layer
  376. # exactly as a generic base layer
  377. push @$base, @$contact;
  378. } elsif (@$contact && $contact_loops > 0) {
  379. # generate the outermost loop
  380. my @loops0 = ();
  381. {
  382. # find centerline of the external loop of the contours
  383. my @external_loops = @{offset($contact, -$interface_flow->scaled_width/2)};
  384. # only consider the loops facing the overhang
  385. {
  386. my $overhang_with_margin = offset($overhang, +$interface_flow->scaled_width/2);
  387. @external_loops = grep {
  388. @{intersection_pl(
  389. [ $_->split_at_first_point ],
  390. $overhang_with_margin,
  391. )}
  392. } @external_loops;
  393. }
  394. # apply a pattern to the loop
  395. my @positions = map @{Slic3r::Polygon->new(@$_)->equally_spaced_points($circle_distance)}, @external_loops;
  396. @loops0 = @{diff(
  397. [ @external_loops ],
  398. [ map { my $c = $circle->clone; $c->translate(@$_); $c } @positions ],
  399. )};
  400. }
  401. # make more loops
  402. my @loops = @loops0;
  403. for my $i (2..$contact_loops) {
  404. my $d = ($i-1) * $interface_flow->scaled_spacing;
  405. push @loops, @{offset2(\@loops0, -$d -0.5*$interface_flow->scaled_spacing, +0.5*$interface_flow->scaled_spacing)};
  406. }
  407. # clip such loops to the side oriented towards the object
  408. @loops = @{intersection_pl(
  409. [ map $_->split_at_first_point, @loops ],
  410. offset($overhang, +scale MARGIN),
  411. )};
  412. # add the contact infill area to the interface area
  413. # note that growing loops by $circle_radius ensures no tiny
  414. # extrusions are left inside the circles; however it creates
  415. # a very large gap between loops and contact_infill, so maybe another
  416. # solution should be found to achieve both goals
  417. $contact_infill = diff(
  418. $contact,
  419. [ map @{$_->grow($circle_radius*1.1)}, @loops ],
  420. );
  421. # transform loops into ExtrusionPath objects
  422. my $mm3_per_mm = $interface_flow->mm3_per_mm($layer->height);
  423. @loops = map Slic3r::ExtrusionPath->new(
  424. polyline => $_,
  425. role => EXTR_ROLE_SUPPORTMATERIAL,
  426. mm3_per_mm => $mm3_per_mm,
  427. ), @loops;
  428. $layer->support_interface_fills->append(@loops);
  429. }
  430. # interface and contact infill
  431. if (@$interface || @$contact_infill) {
  432. $fillers{interface}->angle($interface_angle);
  433. # join regions by offsetting them to ensure they're merged
  434. $interface = offset([ @$interface, @$contact_infill ], scaled_epsilon);
  435. # turn base support into interface when it's contained in our holes
  436. # (this way we get wider interface anchoring)
  437. {
  438. my @p = @$interface;
  439. @$interface = ();
  440. foreach my $p (@p) {
  441. if ($p->is_clockwise) {
  442. my $p2 = $p->clone;
  443. $p2->make_counter_clockwise;
  444. next if !@{diff([$p2], $base, 1)};
  445. }
  446. push @$interface, $p;
  447. }
  448. }
  449. $base = diff($base, $interface);
  450. my @paths = ();
  451. foreach my $expolygon (@{union_ex($interface)}) {
  452. my ($params, @p) = $fillers{interface}->fill_surface(
  453. Slic3r::Surface->new(expolygon => $expolygon, surface_type => S_TYPE_INTERNAL),
  454. density => $interface_density,
  455. flow => $interface_flow,
  456. complete => 1,
  457. );
  458. my $mm3_per_mm = $params->{flow}->mm3_per_mm($layer->height);
  459. push @paths, map Slic3r::ExtrusionPath->new(
  460. polyline => Slic3r::Polyline->new(@$_),
  461. role => EXTR_ROLE_SUPPORTMATERIAL,
  462. mm3_per_mm => $mm3_per_mm,
  463. ), @p;
  464. }
  465. $layer->support_interface_fills->append(@paths);
  466. }
  467. # support or flange
  468. if (@$base) {
  469. my $filler = $fillers{support};
  470. $filler->angle($angles[ ($layer_id) % @angles ]);
  471. my $density = $support_density;
  472. my $base_flow = $flow;
  473. # TODO: use offset2_ex()
  474. my $to_infill = union_ex($base, 1);
  475. my @paths = ();
  476. # base flange
  477. if ($layer_id == 0) {
  478. $filler = $fillers{interface};
  479. $filler->angle($self->object_config->support_material_angle + 90);
  480. $density = 0.5;
  481. $base_flow = $self->first_layer_flow;
  482. } else {
  483. # draw a perimeter all around support infill
  484. # TODO: use brim ordering algorithm
  485. my $mm3_per_mm = $flow->mm3_per_mm($layer->height);
  486. push @paths, map Slic3r::ExtrusionPath->new(
  487. polyline => $_->split_at_first_point,
  488. role => EXTR_ROLE_SUPPORTMATERIAL,
  489. mm3_per_mm => $mm3_per_mm,
  490. ), map @$_, @$to_infill;
  491. # TODO: use offset2_ex()
  492. $to_infill = offset_ex([ map @$_, @$to_infill ], -$flow->scaled_spacing);
  493. }
  494. foreach my $expolygon (@$to_infill) {
  495. my ($params, @p) = $filler->fill_surface(
  496. Slic3r::Surface->new(expolygon => $expolygon, surface_type => S_TYPE_INTERNAL),
  497. density => $density,
  498. flow => $base_flow,
  499. complete => 1,
  500. );
  501. my $mm3_per_mm = $params->{flow}->mm3_per_mm($layer->height);
  502. push @paths, map Slic3r::ExtrusionPath->new(
  503. polyline => Slic3r::Polyline->new(@$_),
  504. role => EXTR_ROLE_SUPPORTMATERIAL,
  505. mm3_per_mm => $mm3_per_mm,
  506. ), @p;
  507. }
  508. $layer->support_fills->append(@paths);
  509. }
  510. if (0) {
  511. require "Slic3r/SVG.pm";
  512. Slic3r::SVG::output("islands_" . $z . ".svg",
  513. red_expolygons => union_ex($contact),
  514. green_expolygons => union_ex($interface),
  515. green_polylines => [ map $_->unpack->polyline, @{$layer->support_contact_fills} ],
  516. polylines => [ map $_->unpack->polyline, @{$layer->support_fills} ],
  517. );
  518. }
  519. };
  520. Slic3r::parallelize(
  521. threads => $self->print_config->threads,
  522. items => [ 0 .. $#{$object->support_layers} ],
  523. thread_cb => sub {
  524. my $q = shift;
  525. while (defined (my $layer_id = $q->dequeue)) {
  526. $process_layer->($layer_id);
  527. }
  528. },
  529. no_threads_cb => sub {
  530. $process_layer->($_) for 0 .. $#{$object->support_layers};
  531. },
  532. );
  533. }
  534. # this method returns the indices of the layers overlapping with the given one
  535. sub overlapping_layers {
  536. my ($self, $i, $support_z) = @_;
  537. my $zmax = $support_z->[$i];
  538. my $zmin = ($i == 0) ? 0 : $support_z->[$i-1];
  539. return grep {
  540. my $zmax2 = $support_z->[$_];
  541. my $zmin2 = ($_ == 0) ? 0 : $support_z->[$_-1];
  542. $zmax > $zmin2 && $zmin < $zmax2;
  543. } 0..$#$support_z;
  544. }
  545. 1;