Object.pm 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. package Slic3r::Print::Object;
  2. # extends c++ class Slic3r::PrintObject (Print.xsp)
  3. use strict;
  4. use warnings;
  5. use List::Util qw(min max sum first);
  6. use Slic3r::Flow ':roles';
  7. use Slic3r::Geometry qw(X Y Z PI scale unscale chained_path epsilon);
  8. use Slic3r::Geometry::Clipper qw(diff diff_ex intersection intersection_ex union union_ex
  9. offset offset_ex offset2 offset2_ex JT_MITER);
  10. use Slic3r::Print::State ':steps';
  11. use Slic3r::Surface ':types';
  12. # If enabled, phases of prepare_infill will be written into SVG files to an "out" directory.
  13. our $SLIC3R_DEBUG_SLICE_PROCESSING = 0;
  14. sub layers {
  15. my $self = shift;
  16. return [ map $self->get_layer($_), 0..($self->layer_count - 1) ];
  17. }
  18. sub support_layers {
  19. my $self = shift;
  20. return [ map $self->get_support_layer($_), 0..($self->support_layer_count - 1) ];
  21. }
  22. # 1) Decides Z positions of the layers,
  23. # 2) Initializes layers and their regions
  24. # 3) Slices the object meshes
  25. # 4) Slices the modifier meshes and reclassifies the slices of the object meshes by the slices of the modifier meshes
  26. # 5) Applies size compensation (offsets the slices in XY plane)
  27. # 6) Replaces bad slices by the slices reconstructed from the upper/lower layer
  28. # Resulting expolygons of layer regions are marked as Internal.
  29. #
  30. # this should be idempotent
  31. sub slice {
  32. my $self = shift;
  33. return if $self->step_done(STEP_SLICE);
  34. $self->set_step_started(STEP_SLICE);
  35. $self->print->status_cb->(10, "Processing triangulated mesh");
  36. $self->_slice;
  37. my $warning = $self->_fix_slicing_errors;
  38. warn $warning if (defined($warning) && $warning ne '');
  39. # simplify slices if required
  40. $self->_simplify_slices(scale($self->print->config->resolution))
  41. if ($self->print->config->resolution);
  42. die "No layers were detected. You might want to repair your STL file(s) or check their size or thickness and retry.\n"
  43. if !@{$self->layers};
  44. $self->set_typed_slices(0);
  45. $self->set_step_done(STEP_SLICE);
  46. }
  47. # 1) Merges typed region slices into stInternal type.
  48. # 2) Increases an "extra perimeters" counter at region slices where needed.
  49. # 3) Generates perimeters, gap fills and fill regions (fill regions of type stInternal).
  50. sub make_perimeters {
  51. my ($self) = @_;
  52. # prerequisites
  53. $self->slice;
  54. if (! $self->step_done(STEP_PERIMETERS)) {
  55. $self->print->status_cb->(20, "Generating perimeters");
  56. $self->_make_perimeters;
  57. }
  58. }
  59. sub prepare_infill {
  60. my ($self) = @_;
  61. # prerequisites
  62. $self->make_perimeters;
  63. return if $self->step_done(STEP_PREPARE_INFILL);
  64. $self->set_step_started(STEP_PREPARE_INFILL);
  65. $self->print->status_cb->(30, "Preparing infill");
  66. # This will assign a type (top/bottom/internal) to $layerm->slices.
  67. # Then the classifcation of $layerm->slices is transfered onto
  68. # the $layerm->fill_surfaces by clipping $layerm->fill_surfaces
  69. # by the cummulative area of the previous $layerm->fill_surfaces.
  70. $self->detect_surfaces_type;
  71. # Mark the object to have the region slices classified (typed, which also means they are split based on whether they are supported, bridging, top layers etc.)
  72. $self->set_typed_slices(1);
  73. # Decide what surfaces are to be filled.
  74. # Here the S_TYPE_TOP / S_TYPE_BOTTOMBRIDGE / S_TYPE_BOTTOM infill is turned to just S_TYPE_INTERNAL if zero top / bottom infill layers are configured.
  75. # Also tiny S_TYPE_INTERNAL surfaces are turned to S_TYPE_INTERNAL_SOLID.
  76. # BOOST_LOG_TRIVIAL(info) << "Preparing fill surfaces...";
  77. $_->prepare_fill_surfaces for map @{$_->regions}, @{$self->layers};
  78. # this will detect bridges and reverse bridges
  79. # and rearrange top/bottom/internal surfaces
  80. # It produces enlarged overlapping bridging areas.
  81. #
  82. # 1) S_TYPE_BOTTOMBRIDGE / S_TYPE_BOTTOM infill is grown by 3mm and clipped by the total infill area. Bridges are detected. The areas may overlap.
  83. # 2) S_TYPE_TOP is grown by 3mm and clipped by the grown bottom areas. The areas may overlap.
  84. # 3) Clip the internal surfaces by the grown top/bottom surfaces.
  85. # 4) Merge surfaces with the same style. This will mostly get rid of the overlaps.
  86. #FIXME This does not likely merge surfaces, which are supported by a material with different colors, but same properties.
  87. $self->process_external_surfaces;
  88. # Add solid fills to ensure the shell vertical thickness.
  89. $self->discover_vertical_shells;
  90. # Debugging output.
  91. if ($SLIC3R_DEBUG_SLICE_PROCESSING) {
  92. for my $region_id (0 .. ($self->print->region_count-1)) {
  93. for (my $i = 0; $i < $self->layer_count; $i++) {
  94. my $layerm = $self->get_layer($i)->regions->[$region_id];
  95. $layerm->export_region_slices_to_svg_debug("6_discover_vertical_shells-final");
  96. $layerm->export_region_fill_surfaces_to_svg_debug("6_discover_vertical_shells-final");
  97. } # for each layer
  98. } # for each region
  99. }
  100. # Detect, which fill surfaces are near external layers.
  101. # They will be split in internal and internal-solid surfaces.
  102. # The purpose is to add a configurable number of solid layers to support the TOP surfaces
  103. # and to add a configurable number of solid layers above the BOTTOM / BOTTOMBRIDGE surfaces
  104. # to close these surfaces reliably.
  105. #FIXME Vojtech: Is this a good place to add supporting infills below sloping perimeters?
  106. $self->discover_horizontal_shells;
  107. if ($SLIC3R_DEBUG_SLICE_PROCESSING) {
  108. # Debugging output.
  109. for my $region_id (0 .. ($self->print->region_count-1)) {
  110. for (my $i = 0; $i < $self->layer_count; $i++) {
  111. my $layerm = $self->get_layer($i)->regions->[$region_id];
  112. $layerm->export_region_slices_to_svg_debug("7_discover_horizontal_shells-final");
  113. $layerm->export_region_fill_surfaces_to_svg_debug("7_discover_horizontal_shells-final");
  114. } # for each layer
  115. } # for each region
  116. }
  117. # Only active if config->infill_only_where_needed. This step trims the sparse infill,
  118. # so it acts as an internal support. It maintains all other infill types intact.
  119. # Here the internal surfaces and perimeters have to be supported by the sparse infill.
  120. #FIXME The surfaces are supported by a sparse infill, but the sparse infill is only as large as the area to support.
  121. # Likely the sparse infill will not be anchored correctly, so it will not work as intended.
  122. # Also one wishes the perimeters to be supported by a full infill.
  123. $self->clip_fill_surfaces;
  124. if ($SLIC3R_DEBUG_SLICE_PROCESSING) {
  125. # Debugging output.
  126. for my $region_id (0 .. ($self->print->region_count-1)) {
  127. for (my $i = 0; $i < $self->layer_count; $i++) {
  128. my $layerm = $self->get_layer($i)->regions->[$region_id];
  129. $layerm->export_region_slices_to_svg_debug("8_clip_surfaces-final");
  130. $layerm->export_region_fill_surfaces_to_svg_debug("8_clip_surfaces-final");
  131. } # for each layer
  132. } # for each region
  133. }
  134. # the following step needs to be done before combination because it may need
  135. # to remove only half of the combined infill
  136. $self->bridge_over_infill;
  137. # combine fill surfaces to honor the "infill every N layers" option
  138. $self->combine_infill;
  139. # Debugging output.
  140. if ($SLIC3R_DEBUG_SLICE_PROCESSING) {
  141. for my $region_id (0 .. ($self->print->region_count-1)) {
  142. for (my $i = 0; $i < $self->layer_count; $i++) {
  143. my $layerm = $self->get_layer($i)->regions->[$region_id];
  144. $layerm->export_region_slices_to_svg_debug("9_prepare_infill-final");
  145. $layerm->export_region_fill_surfaces_to_svg_debug("9_prepare_infill-final");
  146. } # for each layer
  147. } # for each region
  148. for (my $i = 0; $i < $self->layer_count; $i++) {
  149. my $layer = $self->get_layer($i);
  150. $layer->export_region_slices_to_svg_debug("9_prepare_infill-final");
  151. $layer->export_region_fill_surfaces_to_svg_debug("9_prepare_infill-final");
  152. } # for each layer
  153. }
  154. $self->set_step_done(STEP_PREPARE_INFILL);
  155. }
  156. sub infill {
  157. my ($self) = @_;
  158. # prerequisites
  159. $self->prepare_infill;
  160. $self->_infill;
  161. }
  162. sub generate_support_material {
  163. my $self = shift;
  164. # prerequisites
  165. $self->slice;
  166. return if $self->step_done(STEP_SUPPORTMATERIAL);
  167. $self->set_step_started(STEP_SUPPORTMATERIAL);
  168. $self->clear_support_layers;
  169. if (($self->config->support_material || $self->config->raft_layers > 0) && scalar(@{$self->layers}) > 1) {
  170. $self->print->status_cb->(85, "Generating support material");
  171. # New supports, C++ implementation.
  172. $self->_generate_support_material;
  173. }
  174. $self->set_step_done(STEP_SUPPORTMATERIAL);
  175. my $stats = sprintf "Weight: %.1fg, Cost: %.1f" , $self->print->total_weight, $self->print->total_cost;
  176. $self->print->status_cb->(85, $stats);
  177. }
  178. # Idempotence of this method is guaranteed by the fact that we don't remove things from
  179. # fill_surfaces but we only turn them into VOID surfaces, thus preserving the boundaries.
  180. sub clip_fill_surfaces {
  181. my $self = shift;
  182. return unless $self->config->infill_only_where_needed;
  183. # We only want infill under ceilings; this is almost like an
  184. # internal support material.
  185. # proceed top-down skipping bottom layer
  186. my $upper_internal = [];
  187. for my $layer_id (reverse 1..($self->layer_count - 1)) {
  188. my $layer = $self->get_layer($layer_id);
  189. my $lower_layer = $self->get_layer($layer_id-1);
  190. # detect things that we need to support
  191. my $overhangs = []; # Polygons
  192. # we need to support any solid surface
  193. push @$overhangs, map $_->p,
  194. grep $_->is_solid, map @{$_->fill_surfaces}, @{$layer->regions};
  195. # we also need to support perimeters when there's at least one full
  196. # unsupported loop
  197. {
  198. # get perimeters area as the difference between slices and fill_surfaces
  199. my $perimeters = diff(
  200. [ map @$_, @{$layer->slices} ],
  201. [ map $_->p, map @{$_->fill_surfaces}, @{$layer->regions} ],
  202. );
  203. # only consider the area that is not supported by lower perimeters
  204. $perimeters = intersection(
  205. $perimeters,
  206. [ map $_->p, map @{$_->fill_surfaces}, @{$lower_layer->regions} ],
  207. 1,
  208. );
  209. # only consider perimeter areas that are at least one extrusion width thick
  210. #FIXME Offset2 eats out from both sides, while the perimeters are create outside in.
  211. #Should the $pw not be half of the current value?
  212. my $pw = min(map $_->flow(FLOW_ROLE_PERIMETER)->scaled_width, @{$layer->regions});
  213. $perimeters = offset2($perimeters, -$pw, +$pw);
  214. # append such thick perimeters to the areas that need support
  215. push @$overhangs, @$perimeters;
  216. }
  217. # find new internal infill
  218. $upper_internal = my $new_internal = intersection(
  219. [
  220. @$overhangs,
  221. @$upper_internal,
  222. ],
  223. [
  224. # our current internal fill boundaries
  225. map $_->p,
  226. grep $_->surface_type == S_TYPE_INTERNAL || $_->surface_type == S_TYPE_INTERNALVOID,
  227. map @{$_->fill_surfaces}, @{$lower_layer->regions}
  228. ],
  229. );
  230. # apply new internal infill to regions
  231. foreach my $layerm (@{$lower_layer->regions}) {
  232. my (@internal, @other) = ();
  233. foreach my $surface (map $_->clone, @{$layerm->fill_surfaces}) {
  234. if ($surface->surface_type == S_TYPE_INTERNAL || $surface->surface_type == S_TYPE_INTERNALVOID) {
  235. push @internal, $surface;
  236. } else {
  237. push @other, $surface;
  238. }
  239. }
  240. my @new = map Slic3r::Surface->new(
  241. expolygon => $_,
  242. surface_type => S_TYPE_INTERNAL,
  243. ),
  244. @{intersection_ex(
  245. [ map $_->p, @internal ],
  246. $new_internal,
  247. 1,
  248. )};
  249. push @other, map Slic3r::Surface->new(
  250. expolygon => $_,
  251. surface_type => S_TYPE_INTERNALVOID,
  252. ),
  253. @{diff_ex(
  254. [ map $_->p, @internal ],
  255. $new_internal,
  256. 1,
  257. )};
  258. # If there are voids it means that our internal infill is not adjacent to
  259. # perimeters. In this case it would be nice to add a loop around infill to
  260. # make it more robust and nicer. TODO.
  261. $layerm->fill_surfaces->clear;
  262. $layerm->fill_surfaces->append($_) for (@new, @other);
  263. if ($SLIC3R_DEBUG_SLICE_PROCESSING) {
  264. $layerm->export_region_fill_surfaces_to_svg_debug("6_clip_fill_surfaces");
  265. }
  266. }
  267. }
  268. }
  269. sub discover_horizontal_shells {
  270. my $self = shift;
  271. Slic3r::debugf "==> DISCOVERING HORIZONTAL SHELLS\n";
  272. for my $region_id (0 .. ($self->print->region_count-1)) {
  273. for (my $i = 0; $i < $self->layer_count; $i++) {
  274. my $layerm = $self->get_layer($i)->regions->[$region_id];
  275. if ($layerm->region->config->solid_infill_every_layers && $layerm->region->config->fill_density > 0
  276. && ($i % $layerm->region->config->solid_infill_every_layers) == 0) {
  277. # This is the layer to put the sparse infill in. Mark S_TYPE_INTERNAL surfaces as S_TYPE_INTERNALSOLID or S_TYPE_INTERNALBRIDGE.
  278. # If the sparse infill is not active, the internal surfaces are of type S_TYPE_INTERNAL.
  279. my $type = $layerm->region->config->fill_density == 100 ? S_TYPE_INTERNALSOLID : S_TYPE_INTERNALBRIDGE;
  280. $_->surface_type($type) for @{$layerm->fill_surfaces->filter_by_type(S_TYPE_INTERNAL)};
  281. }
  282. # If ensure_vertical_shell_thickness, then the rest has already been performed by discover_vertical_shells().
  283. next if ($layerm->region->config->ensure_vertical_shell_thickness);
  284. EXTERNAL: foreach my $type (S_TYPE_TOP, S_TYPE_BOTTOM, S_TYPE_BOTTOMBRIDGE) {
  285. # find slices of current type for current layer
  286. # use slices instead of fill_surfaces because they also include the perimeter area
  287. # which needs to be propagated in shells; we need to grow slices like we did for
  288. # fill_surfaces though. Using both ungrown slices and grown fill_surfaces will
  289. # not work in some situations, as there won't be any grown region in the perimeter
  290. # area (this was seen in a model where the top layer had one extra perimeter, thus
  291. # its fill_surfaces were thinner than the lower layer's infill), however it's the best
  292. # solution so far. Growing the external slices by EXTERNAL_INFILL_MARGIN will put
  293. # too much solid infill inside nearly-vertical slopes.
  294. my $solid = [
  295. # Surfaces including the area of perimeters. Everything, that is visible from the top / bottom
  296. # (not covered by a layer above / below).
  297. # This does not contain the areas covered by perimeters!
  298. (map $_->p, @{$layerm->slices->filter_by_type($type)}),
  299. # Infill areas (slices without the perimeters).
  300. (map $_->p, @{$layerm->fill_surfaces->filter_by_type($type)}),
  301. ];
  302. next if !@$solid;
  303. Slic3r::debugf "Layer %d has %s surfaces\n", $i, ($type == S_TYPE_TOP) ? 'top' : 'bottom';
  304. my $solid_layers = ($type == S_TYPE_TOP)
  305. ? $layerm->region->config->top_solid_layers
  306. : $layerm->region->config->bottom_solid_layers;
  307. NEIGHBOR: for (my $n = ($type == S_TYPE_TOP) ? $i-1 : $i+1;
  308. abs($n - $i) < $solid_layers;
  309. ($type == S_TYPE_TOP) ? $n-- : $n++) {
  310. next if $n < 0 || $n >= $self->layer_count;
  311. Slic3r::debugf " looking for neighbors on layer %d...\n", $n;
  312. # Reference to the lower layer of a TOP surface, or an upper layer of a BOTTOM surface.
  313. my $neighbor_layerm = $self->get_layer($n)->regions->[$region_id];
  314. # Reference to the neighbour fill surfaces.
  315. my $neighbor_fill_surfaces = $neighbor_layerm->fill_surfaces;
  316. # Clone because we will use these surfaces even after clearing the collection.
  317. my @neighbor_fill_surfaces = map $_->clone, @$neighbor_fill_surfaces;
  318. # find intersection between neighbor and current layer's surfaces
  319. # intersections have contours and holes
  320. # we update $solid so that we limit the next neighbor layer to the areas that were
  321. # found on this one - in other words, solid shells on one layer (for a given external surface)
  322. # are always a subset of the shells found on the previous shell layer
  323. # this approach allows for DWIM in hollow sloping vases, where we want bottom
  324. # shells to be generated in the base but not in the walls (where there are many
  325. # narrow bottom surfaces): reassigning $solid will consider the 'shadow' of the
  326. # upper perimeter as an obstacle and shell will not be propagated to more upper layers
  327. #FIXME How does it work for S_TYPE_INTERNALBRIDGE? This is set for sparse infill. Likely this does not work.
  328. my $new_internal_solid = $solid = intersection(
  329. $solid,
  330. [ map $_->p, grep { ($_->surface_type == S_TYPE_INTERNAL) || ($_->surface_type == S_TYPE_INTERNALSOLID) } @neighbor_fill_surfaces ],
  331. 1,
  332. );
  333. next EXTERNAL if !@$new_internal_solid;
  334. if ($layerm->region->config->fill_density == 0) {
  335. # if we're printing a hollow object we discard any solid shell thinner
  336. # than a perimeter width, since it's probably just crossing a sloping wall
  337. # and it's not wanted in a hollow print even if it would make sense when
  338. # obeying the solid shell count option strictly (DWIM!)
  339. my $margin = $neighbor_layerm->flow(FLOW_ROLE_EXTERNAL_PERIMETER)->scaled_width;
  340. my $regularized = offset2($new_internal_solid, -$margin, +$margin, JT_MITER, 5);
  341. my $too_narrow = diff(
  342. $new_internal_solid,
  343. $regularized,
  344. 1,
  345. );
  346. # Trim the regularized region by the original region.
  347. $new_internal_solid = $solid = intersection(
  348. $new_internal_solid,
  349. $regularized,
  350. ) if @$too_narrow;
  351. }
  352. # make sure the new internal solid is wide enough, as it might get collapsed
  353. # when spacing is added in Fill.pm
  354. if ($layerm->region->config->ensure_vertical_shell_thickness) {
  355. # The possible thin sickles of top / bottom surfaces on steeply sloping surfaces touch
  356. # the projections of top / bottom perimeters, therefore they will be sufficiently inflated by
  357. # merging them with the projections of the top / bottom perimeters.
  358. } else {
  359. #FIXME Vojtech: Disable this and you will be sorry.
  360. # https://github.com/prusa3d/Slic3r/issues/26 bottom
  361. my $margin = 3 * $layerm->flow(FLOW_ROLE_SOLID_INFILL)->scaled_width; # require at least this size
  362. # we use a higher miterLimit here to handle areas with acute angles
  363. # in those cases, the default miterLimit would cut the corner and we'd
  364. # get a triangle in $too_narrow; if we grow it below then the shell
  365. # would have a different shape from the external surface and we'd still
  366. # have the same angle, so the next shell would be grown even more and so on.
  367. my $too_narrow = diff(
  368. $new_internal_solid,
  369. offset2($new_internal_solid, -$margin, +$margin, JT_MITER, 5),
  370. 1,
  371. );
  372. if (@$too_narrow) {
  373. # grow the collapsing parts and add the extra area to the neighbor layer
  374. # as well as to our original surfaces so that we support this
  375. # additional area in the next shell too
  376. # make sure our grown surfaces don't exceed the fill area
  377. my @grown = @{intersection(
  378. offset($too_narrow, +$margin),
  379. # Discard bridges as they are grown for anchoring and we can't
  380. # remove such anchors. (This may happen when a bridge is being
  381. # anchored onto a wall where little space remains after the bridge
  382. # is grown, and that little space is an internal solid shell so
  383. # it triggers this too_narrow logic.)
  384. [ map $_->p, grep { $_->is_internal && !$_->is_bridge } @neighbor_fill_surfaces ],
  385. )};
  386. $new_internal_solid = $solid = [ @grown, @$new_internal_solid ];
  387. }
  388. }
  389. # internal-solid are the union of the existing internal-solid surfaces
  390. # and new ones
  391. my $internal_solid = union_ex([
  392. ( map $_->p, grep $_->surface_type == S_TYPE_INTERNALSOLID, @neighbor_fill_surfaces ),
  393. @$new_internal_solid,
  394. ]);
  395. # subtract intersections from layer surfaces to get resulting internal surfaces
  396. my $internal = diff_ex(
  397. [ map $_->p, grep $_->surface_type == S_TYPE_INTERNAL, @neighbor_fill_surfaces ],
  398. [ map @$_, @$internal_solid ],
  399. 1,
  400. );
  401. Slic3r::debugf " %d internal-solid and %d internal surfaces found\n",
  402. scalar(@$internal_solid), scalar(@$internal);
  403. # assign resulting internal surfaces to layer
  404. $neighbor_fill_surfaces->clear;
  405. $neighbor_fill_surfaces->append($_)
  406. for map Slic3r::Surface->new(expolygon => $_, surface_type => S_TYPE_INTERNAL),
  407. @$internal;
  408. # assign new internal-solid surfaces to layer
  409. $neighbor_fill_surfaces->append($_)
  410. for map Slic3r::Surface->new(expolygon => $_, surface_type => S_TYPE_INTERNALSOLID),
  411. @$internal_solid;
  412. # assign top and bottom surfaces to layer
  413. foreach my $s (@{Slic3r::Surface::Collection->new(grep { ($_->surface_type == S_TYPE_TOP) || $_->is_bottom } @neighbor_fill_surfaces)->group}) {
  414. my $solid_surfaces = diff_ex(
  415. [ map $_->p, @$s ],
  416. [ map @$_, @$internal_solid, @$internal ],
  417. 1,
  418. );
  419. $neighbor_fill_surfaces->append($_)
  420. for map $s->[0]->clone(expolygon => $_), @$solid_surfaces;
  421. }
  422. }
  423. } # foreach my $type (S_TYPE_TOP, S_TYPE_BOTTOM, S_TYPE_BOTTOMBRIDGE)
  424. } # for each layer
  425. } # for each region
  426. # Debugging output.
  427. if ($SLIC3R_DEBUG_SLICE_PROCESSING) {
  428. for my $region_id (0 .. ($self->print->region_count-1)) {
  429. for (my $i = 0; $i < $self->layer_count; $i++) {
  430. my $layerm = $self->get_layer($i)->regions->[$region_id];
  431. $layerm->export_region_slices_to_svg_debug("5_discover_horizontal_shells");
  432. $layerm->export_region_fill_surfaces_to_svg_debug("5_discover_horizontal_shells");
  433. } # for each layer
  434. } # for each region
  435. }
  436. }
  437. # combine fill surfaces across layers to honor the "infill every N layers" option
  438. # Idempotence of this method is guaranteed by the fact that we don't remove things from
  439. # fill_surfaces but we only turn them into VOID surfaces, thus preserving the boundaries.
  440. sub combine_infill {
  441. my $self = shift;
  442. # define the type used for voids
  443. my %voidtype = (
  444. &S_TYPE_INTERNAL() => S_TYPE_INTERNALVOID,
  445. );
  446. # work on each region separately
  447. for my $region_id (0 .. ($self->print->region_count-1)) {
  448. my $region = $self->print->get_region($region_id);
  449. my $every = $region->config->infill_every_layers;
  450. next unless $every > 1 && $region->config->fill_density > 0;
  451. # limit the number of combined layers to the maximum height allowed by this regions' nozzle
  452. my $nozzle_diameter = min(
  453. $self->print->config->get_at('nozzle_diameter', $region->config->infill_extruder-1),
  454. $self->print->config->get_at('nozzle_diameter', $region->config->solid_infill_extruder-1),
  455. );
  456. # define the combinations
  457. my %combine = (); # layer_idx => number of additional combined lower layers
  458. {
  459. my $current_height = my $layers = 0;
  460. for my $layer_idx (0 .. ($self->layer_count-1)) {
  461. my $layer = $self->get_layer($layer_idx);
  462. next if $layer->id == 0; # skip first print layer (which may not be first layer in array because of raft)
  463. my $height = $layer->height;
  464. # check whether the combination of this layer with the lower layers' buffer
  465. # would exceed max layer height or max combined layer count
  466. if ($current_height + $height >= $nozzle_diameter + epsilon || $layers >= $every) {
  467. # append combination to lower layer
  468. $combine{$layer_idx-1} = $layers;
  469. $current_height = $layers = 0;
  470. }
  471. $current_height += $height;
  472. $layers++;
  473. }
  474. # append lower layers (if any) to uppermost layer
  475. $combine{$self->layer_count-1} = $layers;
  476. }
  477. # loop through layers to which we have assigned layers to combine
  478. for my $layer_idx (sort keys %combine) {
  479. next unless $combine{$layer_idx} > 1;
  480. # get all the LayerRegion objects to be combined
  481. my @layerms = map $self->get_layer($_)->get_region($region_id),
  482. ($layer_idx - ($combine{$layer_idx}-1) .. $layer_idx);
  483. # only combine internal infill
  484. for my $type (S_TYPE_INTERNAL) {
  485. # we need to perform a multi-layer intersection, so let's split it in pairs
  486. # initialize the intersection with the candidates of the lowest layer
  487. my $intersection = [ map $_->expolygon, @{$layerms[0]->fill_surfaces->filter_by_type($type)} ];
  488. # start looping from the second layer and intersect the current intersection with it
  489. for my $layerm (@layerms[1 .. $#layerms]) {
  490. $intersection = intersection_ex(
  491. [ map @$_, @$intersection ],
  492. [ map @{$_->expolygon}, @{$layerm->fill_surfaces->filter_by_type($type)} ],
  493. );
  494. }
  495. my $area_threshold = $layerms[0]->infill_area_threshold;
  496. @$intersection = grep $_->area > $area_threshold, @$intersection;
  497. next if !@$intersection;
  498. Slic3r::debugf " combining %d %s regions from layers %d-%d\n",
  499. scalar(@$intersection),
  500. ($type == S_TYPE_INTERNAL ? 'internal' : 'internal-solid'),
  501. $layer_idx-($every-1), $layer_idx;
  502. # $intersection now contains the regions that can be combined across the full amount of layers
  503. # so let's remove those areas from all layers
  504. my @intersection_with_clearance = map @{$_->offset(
  505. $layerms[-1]->flow(FLOW_ROLE_SOLID_INFILL)->scaled_width / 2
  506. + $layerms[-1]->flow(FLOW_ROLE_PERIMETER)->scaled_width / 2
  507. # Because fill areas for rectilinear and honeycomb are grown
  508. # later to overlap perimeters, we need to counteract that too.
  509. + (($type == S_TYPE_INTERNALSOLID || $region->config->fill_pattern =~ /(rectilinear|grid|line|honeycomb)/)
  510. ? $layerms[-1]->flow(FLOW_ROLE_SOLID_INFILL)->scaled_width
  511. : 0)
  512. )}, @$intersection;
  513. foreach my $layerm (@layerms) {
  514. my @this_type = @{$layerm->fill_surfaces->filter_by_type($type)};
  515. my @other_types = map $_->clone, grep $_->surface_type != $type, @{$layerm->fill_surfaces};
  516. my @new_this_type = map Slic3r::Surface->new(expolygon => $_, surface_type => $type),
  517. @{diff_ex(
  518. [ map $_->p, @this_type ],
  519. [ @intersection_with_clearance ],
  520. )};
  521. # apply surfaces back with adjusted depth to the uppermost layer
  522. if ($layerm->layer->id == $self->get_layer($layer_idx)->id) {
  523. push @new_this_type,
  524. map Slic3r::Surface->new(
  525. expolygon => $_,
  526. surface_type => $type,
  527. thickness => sum(map $_->layer->height, @layerms),
  528. thickness_layers => scalar(@layerms),
  529. ),
  530. @$intersection;
  531. } else {
  532. # save void surfaces
  533. push @new_this_type,
  534. map Slic3r::Surface->new(expolygon => $_, surface_type => $voidtype{$type}),
  535. @{intersection_ex(
  536. [ map @{$_->expolygon}, @this_type ],
  537. [ @intersection_with_clearance ],
  538. )};
  539. }
  540. $layerm->fill_surfaces->clear;
  541. $layerm->fill_surfaces->append($_) for (@new_this_type, @other_types);
  542. }
  543. }
  544. }
  545. }
  546. }
  547. 1;