Region.pm 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. package Slic3r::Layer::Region;
  2. use strict;
  3. use warnings;
  4. use List::Util qw(sum first);
  5. use Slic3r::ExtrusionLoop ':roles';
  6. use Slic3r::ExtrusionPath ':roles';
  7. use Slic3r::Flow ':roles';
  8. use Slic3r::Geometry qw(PI A B scale unscale chained_path);
  9. use Slic3r::Geometry::Clipper qw(union_ex diff_ex intersection_ex
  10. offset offset_ex offset2 offset2_ex union_pt diff intersection
  11. union diff intersection_ppl diff_ppl);
  12. use Slic3r::Surface ':types';
  13. # TODO: lazy
  14. sub infill_area_threshold {
  15. my $self = shift;
  16. return $self->flow(FLOW_ROLE_SOLID_INFILL)->scaled_spacing ** 2;
  17. }
  18. sub id { return $_[0]->layer->id; }
  19. sub slice_z { return $_[0]->layer->slice_z; }
  20. sub print_z { return $_[0]->layer->print_z; }
  21. sub height { return $_[0]->layer->height; }
  22. sub object { return $_[0]->layer->object; }
  23. sub print { return $_[0]->layer->print; }
  24. sub config { return $_[0]->region->config; }
  25. sub merge_slices {
  26. my ($self) = @_;
  27. my $expolygons = union_ex([ map $_->p, @{$self->slices} ]);
  28. $self->slices->clear;
  29. $self->slices->append(Slic3r::Surface->new(
  30. expolygon => $_,
  31. surface_type => S_TYPE_INTERNAL,
  32. )) for @$expolygons;
  33. }
  34. sub make_perimeters {
  35. my ($self, $slices, $fill_surfaces) = @_;
  36. # other perimeters
  37. my $perimeter_flow = $self->flow(FLOW_ROLE_PERIMETER);
  38. my $mm3_per_mm = $perimeter_flow->mm3_per_mm;
  39. my $pwidth = $perimeter_flow->scaled_width;
  40. my $pspacing = $perimeter_flow->scaled_spacing;
  41. # external perimeters
  42. my $ext_perimeter_flow = $self->flow(FLOW_ROLE_EXTERNAL_PERIMETER);
  43. my $ext_mm3_per_mm = $ext_perimeter_flow->mm3_per_mm;
  44. my $ext_pwidth = $ext_perimeter_flow->scaled_width;
  45. my $ext_pspacing = scale($ext_perimeter_flow->spacing_to($perimeter_flow));
  46. # overhang perimeters
  47. my $overhang_flow = $self->region->flow(FLOW_ROLE_PERIMETER, -1, 1, 0, -1, $self->layer->object);
  48. my $mm3_per_mm_overhang = $overhang_flow->mm3_per_mm;
  49. # solid infill
  50. my $solid_infill_flow = $self->flow(FLOW_ROLE_SOLID_INFILL);
  51. my $ispacing = $solid_infill_flow->scaled_spacing;
  52. my $gap_area_threshold = $pwidth ** 2;
  53. # Calculate the minimum required spacing between two adjacent traces.
  54. # This should be equal to the nominal flow spacing but we experiment
  55. # with some tolerance in order to avoid triggering medial axis when
  56. # some squishing might work. Loops are still spaced by the entire
  57. # flow spacing; this only applies to collapsing parts.
  58. my $min_spacing = $pspacing * (1 - &Slic3r::INSET_OVERLAP_TOLERANCE);
  59. my $ext_min_spacing = $ext_pspacing * (1 - &Slic3r::INSET_OVERLAP_TOLERANCE);
  60. $self->perimeters->clear;
  61. $self->thin_fills->clear;
  62. my @contours = (); # array of Polygons with ccw orientation
  63. my @holes = (); # array of Polygons with cw orientation
  64. my @thin_walls = (); # array of ExPolygons
  65. # we need to process each island separately because we might have different
  66. # extra perimeters for each one
  67. foreach my $surface (@$slices) {
  68. # detect how many perimeters must be generated for this island
  69. my $loop_number = $self->config->perimeters + ($surface->extra_perimeters || 0);
  70. my @last = @{$surface->expolygon};
  71. my @gaps = (); # array of ExPolygons
  72. if ($loop_number > 0) {
  73. # we loop one time more than needed in order to find gaps after the last perimeter was applied
  74. for my $i (1 .. ($loop_number+1)) { # outer loop is 1
  75. my @offsets = ();
  76. if ($i == 1) {
  77. # the minimum thickness of a single loop is:
  78. # ext_width/2 + ext_spacing/2 + spacing/2 + width/2
  79. if ($self->config->thin_walls) {
  80. @offsets = @{offset2(
  81. \@last,
  82. -(0.5*$ext_pwidth + 0.5*$ext_min_spacing - 1),
  83. +(0.5*$ext_min_spacing - 1),
  84. )};
  85. } else {
  86. @offsets = @{offset(
  87. \@last,
  88. -0.5*$ext_pwidth,
  89. )};
  90. }
  91. # look for thin walls
  92. if ($self->config->thin_walls) {
  93. my $diff = diff_ex(
  94. \@last,
  95. offset(\@offsets, +0.5*$ext_pwidth),
  96. 1, # medial axis requires non-overlapping geometry
  97. );
  98. push @thin_walls, @$diff;
  99. }
  100. } else {
  101. my $distance = ($i == 2) ? $ext_pspacing : $pspacing;
  102. if ($self->config->thin_walls) {
  103. @offsets = @{offset2(
  104. \@last,
  105. -($distance + 0.5*$min_spacing - 1),
  106. +(0.5*$min_spacing - 1),
  107. )};
  108. } else {
  109. @offsets = @{offset(
  110. \@last,
  111. -$distance,
  112. )};
  113. }
  114. # look for gaps
  115. if ($self->region->config->gap_fill_speed > 0 && $self->config->fill_density > 0) {
  116. # not using safety offset here would "detect" very narrow gaps
  117. # (but still long enough to escape the area threshold) that gap fill
  118. # won't be able to fill but we'd still remove from infill area
  119. my $diff = diff_ex(
  120. offset(\@last, -0.5*$pspacing),
  121. offset(\@offsets, +0.5*$pspacing + 10), # safety offset
  122. );
  123. push @gaps, grep abs($_->area) >= $gap_area_threshold, @$diff;
  124. }
  125. }
  126. last if !@offsets;
  127. last if $i > $loop_number; # we were only looking for gaps this time
  128. # clone polygons because these ExPolygons will go out of scope very soon
  129. @last = @offsets;
  130. foreach my $polygon (@offsets) {
  131. if ($polygon->is_counter_clockwise) {
  132. push @contours, $polygon;
  133. } else {
  134. push @holes, $polygon;
  135. }
  136. }
  137. }
  138. }
  139. # fill gaps
  140. if (@gaps) {
  141. if (0) {
  142. require "Slic3r/SVG.pm";
  143. Slic3r::SVG::output(
  144. "gaps.svg",
  145. expolygons => \@gaps,
  146. );
  147. }
  148. # where $pwidth < thickness < 2*$pspacing, infill with width = 1.5*$pwidth
  149. # where 0.5*$pwidth < thickness < $pwidth, infill with width = 0.5*$pwidth
  150. my @gap_sizes = (
  151. [ $pwidth, 2*$pspacing, unscale 1.5*$pwidth ],
  152. [ 0.5*$pwidth, $pwidth, unscale 0.5*$pwidth ],
  153. );
  154. foreach my $gap_size (@gap_sizes) {
  155. my @gap_fill = $self->_fill_gaps(@$gap_size, \@gaps);
  156. $self->thin_fills->append(@gap_fill);
  157. # Make sure we don't infill narrow parts that are already gap-filled
  158. # (we only consider this surface's gaps to reduce the diff() complexity).
  159. # Growing actual extrusions ensures that gaps not filled by medial axis
  160. # are not subtracted from fill surfaces (they might be too short gaps
  161. # that medial axis skips but infill might join with other infill regions
  162. # and use zigzag).
  163. my $w = $gap_size->[2];
  164. my @filled = map {
  165. @{($_->isa('Slic3r::ExtrusionLoop') ? $_->polygon->split_at_first_point : $_->polyline)
  166. ->grow(scale $w/2)};
  167. } @gap_fill;
  168. @last = @{diff(\@last, \@filled)};
  169. }
  170. }
  171. # create one more offset to be used as boundary for fill
  172. # we offset by half the perimeter spacing (to get to the actual infill boundary)
  173. # and then we offset back and forth by half the infill spacing to only consider the
  174. # non-collapsing regions
  175. my $min_perimeter_infill_spacing = $ispacing * (1 - &Slic3r::INSET_OVERLAP_TOLERANCE);
  176. $fill_surfaces->append(
  177. map Slic3r::Surface->new(expolygon => $_, surface_type => S_TYPE_INTERNAL), # use a bogus surface type
  178. @{offset2_ex(
  179. [ map @{$_->simplify_p(&Slic3r::SCALED_RESOLUTION)}, @{union_ex(\@last)} ],
  180. -($pspacing/2 + $min_perimeter_infill_spacing/2),
  181. +$min_perimeter_infill_spacing/2,
  182. )}
  183. );
  184. }
  185. # process thin walls by collapsing slices to single passes
  186. my @thin_wall_polylines = ();
  187. if (@thin_walls) {
  188. # the following offset2 ensures almost nothing in @thin_walls is narrower than $min_width
  189. # (actually, something larger than that still may exist due to mitering or other causes)
  190. my $min_width = $pwidth / 4;
  191. @thin_walls = @{offset2_ex([ map @$_, @thin_walls ], -$min_width/2, +$min_width/2)};
  192. # the maximum thickness of our thin wall area is equal to the minimum thickness of a single loop
  193. @thin_wall_polylines = map @{$_->medial_axis($pwidth + $pspacing, $min_width)}, @thin_walls;
  194. Slic3r::debugf " %d thin walls detected\n", scalar(@thin_wall_polylines) if $Slic3r::debug;
  195. if (0) {
  196. require "Slic3r/SVG.pm";
  197. Slic3r::SVG::output(
  198. "medial_axis.svg",
  199. no_arrows => 1,
  200. expolygons => \@thin_walls,
  201. green_polylines => [ map $_->polygon->split_at_first_point, @{$self->perimeters} ],
  202. red_polylines => \@thin_wall_polylines,
  203. );
  204. }
  205. }
  206. # find nesting hierarchies separately for contours and holes
  207. my $contours_pt = union_pt(\@contours);
  208. my $holes_pt = union_pt(\@holes);
  209. # prepare grown lower layer slices for overhang detection
  210. my $lower_slices = Slic3r::ExPolygon::Collection->new;
  211. if ($self->layer->lower_layer && $self->region->config->overhangs) {
  212. # We consider overhang any part where the entire nozzle diameter is not supported by the
  213. # lower layer, so we take lower slices and offset them by half the nozzle diameter used
  214. # in the current layer
  215. my $nozzle_diameter = $self->layer->print->config->get_at('nozzle_diameter', $self->region->config->perimeter_extruder-1);
  216. $lower_slices->append(
  217. @{offset_ex([ map @$_, @{$self->layer->lower_layer->slices} ], scale +$nozzle_diameter/2)},
  218. );
  219. }
  220. my $lower_slices_p = $lower_slices->polygons;
  221. # prepare a coderef for traversing the PolyTree object
  222. # external contours are root items of $contours_pt
  223. # internal contours are the ones next to external
  224. my $traverse;
  225. $traverse = sub {
  226. my ($polynodes, $depth, $is_contour) = @_;
  227. # convert all polynodes to ExtrusionLoop objects
  228. my $collection = Slic3r::ExtrusionPath::Collection->new;
  229. my @children = ();
  230. foreach my $polynode (@$polynodes) {
  231. my $polygon = ($polynode->{outer} // $polynode->{hole})->clone;
  232. my $role = EXTR_ROLE_PERIMETER;
  233. my $loop_role = EXTRL_ROLE_DEFAULT;
  234. my $root_level = $depth == 0;
  235. my $no_children = !@{ $polynode->{children} };
  236. my $is_external = $is_contour ? $root_level : $no_children;
  237. my $is_internal = $is_contour ? $no_children : $root_level;
  238. if ($is_external) {
  239. # external perimeters are root level in case of contours
  240. # and items with no children in case of holes
  241. $role = EXTR_ROLE_EXTERNAL_PERIMETER;
  242. $loop_role = EXTRL_ROLE_EXTERNAL_PERIMETER;
  243. } elsif ($is_contour && $is_internal) {
  244. # internal perimeters are root level in case of holes
  245. # and items with no children in case of contours
  246. $loop_role = EXTRL_ROLE_CONTOUR_INTERNAL_PERIMETER;
  247. }
  248. # detect overhanging/bridging perimeters
  249. my @paths = ();
  250. if ($self->region->config->overhangs && $lower_slices->count > 0) {
  251. # get non-overhang paths by intersecting this loop with the grown lower slices
  252. foreach my $polyline (@{ intersection_ppl([ $polygon ], $lower_slices_p) }) {
  253. push @paths, Slic3r::ExtrusionPath->new(
  254. polyline => $polyline,
  255. role => $role,
  256. mm3_per_mm => ($is_external ? $ext_mm3_per_mm : $mm3_per_mm),
  257. width => ($is_external ? $ext_perimeter_flow->width : $perimeter_flow->width),
  258. height => $self->height,
  259. );
  260. }
  261. # get overhang paths by checking what parts of this loop fall
  262. # outside the grown lower slices (thus where the distance between
  263. # the loop centerline and original lower slices is >= half nozzle diameter
  264. foreach my $polyline (@{ diff_ppl([ $polygon ], $lower_slices_p) }) {
  265. push @paths, Slic3r::ExtrusionPath->new(
  266. polyline => $polyline,
  267. role => EXTR_ROLE_OVERHANG_PERIMETER,
  268. mm3_per_mm => $mm3_per_mm_overhang,
  269. width => $overhang_flow->width,
  270. height => $self->height,
  271. );
  272. }
  273. # reapply the nearest point search for starting point
  274. # (clone because the collection gets DESTROY'ed)
  275. # We allow polyline reversal because Clipper may have randomly
  276. # reversed polylines during clipping.
  277. my $collection = Slic3r::ExtrusionPath::Collection->new(@paths);
  278. @paths = map $_->clone, @{$collection->chained_path(0)};
  279. } else {
  280. push @paths, Slic3r::ExtrusionPath->new(
  281. polyline => $polygon->split_at_first_point,
  282. role => $role,
  283. mm3_per_mm => $mm3_per_mm,
  284. width => $perimeter_flow->width,
  285. height => $self->height,
  286. );
  287. }
  288. my $loop = Slic3r::ExtrusionLoop->new_from_paths(@paths);
  289. $loop->role($loop_role);
  290. # return ccw contours and cw holes
  291. # GCode.pm will convert all of them to ccw, but it needs to know
  292. # what the holes are in order to compute the correct inwards move
  293. # We do this on the final Loop object instead of the polygon because
  294. # overhang clipping might have reversed its order since Clipper does
  295. # not preserve polyline orientation.
  296. if ($is_contour) {
  297. $loop->make_counter_clockwise;
  298. } else {
  299. $loop->make_clockwise;
  300. }
  301. $collection->append($loop);
  302. # save the children
  303. push @children, $polynode->{children};
  304. }
  305. # if we're handling the top-level contours, add thin walls as candidates too
  306. # in order to include them in the nearest-neighbor search
  307. if ($is_contour && $depth == 0) {
  308. foreach my $polyline (@thin_wall_polylines) {
  309. $collection->append(Slic3r::ExtrusionPath->new(
  310. polyline => $polyline,
  311. role => EXTR_ROLE_EXTERNAL_PERIMETER,
  312. mm3_per_mm => $mm3_per_mm,
  313. width => $perimeter_flow->width,
  314. height => $self->height,
  315. ));
  316. }
  317. }
  318. # use a nearest neighbor search to order these children
  319. # TODO: supply second argument to chained_path() too?
  320. # Optimization: since islands are going to be sorted by slice anyway in the
  321. # G-code export process, we skip chained_path here
  322. my ($sorted_collection, @orig_indices);
  323. if ($is_contour && $depth == 0) {
  324. $sorted_collection = $collection;
  325. @orig_indices = (0..$#$sorted_collection);
  326. } else {
  327. $sorted_collection = $collection->chained_path_indices(0);
  328. @orig_indices = @{$sorted_collection->orig_indices};
  329. }
  330. my @loops = ();
  331. foreach my $loop (@$sorted_collection) {
  332. my $orig_index = shift @orig_indices;
  333. if ($loop->isa('Slic3r::ExtrusionPath')) {
  334. push @loops, $loop->clone;
  335. } else {
  336. # if this is an external contour find all holes belonging to this contour(s)
  337. # and prepend them
  338. if ($is_contour && $depth == 0) {
  339. # $loop is the outermost loop of an island
  340. my @holes = ();
  341. for (my $i = 0; $i <= $#$holes_pt; $i++) {
  342. if ($loop->polygon->contains_point($holes_pt->[$i]{outer}->first_point)) {
  343. push @holes, splice @$holes_pt, $i, 1; # remove from candidates to reduce complexity
  344. $i--;
  345. }
  346. }
  347. # order holes efficiently
  348. @holes = @holes[@{chained_path([ map {($_->{outer} // $_->{hole})->first_point} @holes ])}];
  349. push @loops, reverse map $traverse->([$_], 0, 0), @holes;
  350. }
  351. # traverse children and prepend them to this loop
  352. push @loops, $traverse->($children[$orig_index], $depth+1, $is_contour);
  353. push @loops, $loop->clone;
  354. }
  355. }
  356. return @loops;
  357. };
  358. # order loops from inner to outer (in terms of object slices)
  359. my @loops = $traverse->($contours_pt, 0, 1);
  360. # if brim will be printed, reverse the order of perimeters so that
  361. # we continue inwards after having finished the brim
  362. # TODO: add test for perimeter order
  363. @loops = reverse @loops
  364. if $self->region->config->external_perimeters_first
  365. || ($self->layer->id == 0 && $self->print->config->brim_width > 0);
  366. # append perimeters
  367. $self->perimeters->append(@loops);
  368. }
  369. sub _fill_gaps {
  370. my ($self, $min, $max, $w, $gaps) = @_;
  371. my $this = diff_ex(
  372. offset2([ map @$_, @$gaps ], -$min/2, +$min/2),
  373. offset2([ map @$_, @$gaps ], -$max/2, +$max/2),
  374. 1,
  375. );
  376. my $flow = $self->flow(FLOW_ROLE_SOLID_INFILL, 0, $w);
  377. my %path_args = (
  378. role => EXTR_ROLE_GAPFILL,
  379. mm3_per_mm => $flow->mm3_per_mm,
  380. width => $flow->width,
  381. height => $self->height,
  382. );
  383. my @polylines = map @{$_->medial_axis($max, $min/2)}, @$this;
  384. Slic3r::debugf " %d gaps filled with extrusion width = %s\n", scalar @$this, $w
  385. if @$this;
  386. for my $i (0..$#polylines) {
  387. if ($polylines[$i]->isa('Slic3r::Polygon')) {
  388. my $loop = Slic3r::ExtrusionLoop->new;
  389. $loop->append(Slic3r::ExtrusionPath->new(polyline => $polylines[$i]->split_at_first_point, %path_args));
  390. $polylines[$i] = $loop;
  391. } elsif ($polylines[$i]->is_valid && $polylines[$i]->first_point->coincides_with($polylines[$i]->last_point)) {
  392. # since medial_axis() now returns only Polyline objects, detect loops here
  393. my $loop = Slic3r::ExtrusionLoop->new;
  394. $loop->append(Slic3r::ExtrusionPath->new(polyline => $polylines[$i], %path_args));
  395. $polylines[$i] = $loop;
  396. } else {
  397. $polylines[$i] = Slic3r::ExtrusionPath->new(polyline => $polylines[$i], %path_args);
  398. }
  399. }
  400. return @polylines;
  401. }
  402. sub prepare_fill_surfaces {
  403. my $self = shift;
  404. # Note: in order to make the psPrepareInfill step idempotent, we should never
  405. # alter fill_surfaces boundaries on which our idempotency relies since that's
  406. # the only meaningful information returned by psPerimeters.
  407. # if no solid layers are requested, turn top/bottom surfaces to internal
  408. if ($self->config->top_solid_layers == 0) {
  409. $_->surface_type(S_TYPE_INTERNAL) for @{$self->fill_surfaces->filter_by_type(S_TYPE_TOP)};
  410. }
  411. if ($self->config->bottom_solid_layers == 0) {
  412. $_->surface_type(S_TYPE_INTERNAL)
  413. for @{$self->fill_surfaces->filter_by_type(S_TYPE_BOTTOM)}, @{$self->fill_surfaces->filter_by_type(S_TYPE_BOTTOMBRIDGE)};
  414. }
  415. # turn too small internal regions into solid regions according to the user setting
  416. if ($self->config->fill_density > 0) {
  417. my $min_area = scale scale $self->config->solid_infill_below_area; # scaling an area requires two calls!
  418. $_->surface_type(S_TYPE_INTERNALSOLID)
  419. for grep { $_->area <= $min_area } @{$self->fill_surfaces->filter_by_type(S_TYPE_INTERNAL)};
  420. }
  421. }
  422. sub process_external_surfaces {
  423. my ($self, $lower_layer) = @_;
  424. my @surfaces = @{$self->fill_surfaces};
  425. my $margin = scale &Slic3r::EXTERNAL_INFILL_MARGIN;
  426. my @bottom = ();
  427. foreach my $surface (grep $_->is_bottom, @surfaces) {
  428. my $grown = $surface->expolygon->offset_ex(+$margin);
  429. # detect bridge direction before merging grown surfaces otherwise adjacent bridges
  430. # would get merged into a single one while they need different directions
  431. # also, supply the original expolygon instead of the grown one, because in case
  432. # of very thin (but still working) anchors, the grown expolygon would go beyond them
  433. my $angle;
  434. if ($lower_layer) {
  435. my $bridge_detector = Slic3r::Layer::BridgeDetector->new(
  436. expolygon => $surface->expolygon,
  437. lower_slices => $lower_layer->slices,
  438. extrusion_width => $self->flow(FLOW_ROLE_INFILL, $self->height, 1)->scaled_width,
  439. );
  440. Slic3r::debugf "Processing bridge at layer %d:\n", $self->id;
  441. $angle = $bridge_detector->detect_angle;
  442. if (defined $angle && $self->object->config->support_material) {
  443. $self->bridged->append(@{ $bridge_detector->coverage($angle) });
  444. $self->unsupported_bridge_edges->append(@{ $bridge_detector->unsupported_edges });
  445. }
  446. }
  447. push @bottom, map $surface->clone(expolygon => $_, bridge_angle => $angle), @$grown;
  448. }
  449. my @top = ();
  450. foreach my $surface (grep $_->surface_type == S_TYPE_TOP, @surfaces) {
  451. # give priority to bottom surfaces
  452. my $grown = diff_ex(
  453. $surface->expolygon->offset(+$margin),
  454. [ map $_->p, @bottom ],
  455. );
  456. push @top, map $surface->clone(expolygon => $_), @$grown;
  457. }
  458. # if we're slicing with no infill, we can't extend external surfaces
  459. # over non-existent infill
  460. my @fill_boundaries = $self->config->fill_density > 0
  461. ? @surfaces
  462. : grep $_->surface_type != S_TYPE_INTERNAL, @surfaces;
  463. # intersect the grown surfaces with the actual fill boundaries
  464. my @new_surfaces = ();
  465. foreach my $group (@{Slic3r::Surface::Collection->new(@top, @bottom)->group}) {
  466. push @new_surfaces,
  467. map $group->[0]->clone(expolygon => $_),
  468. @{intersection_ex(
  469. [ map $_->p, @$group ],
  470. [ map $_->p, @fill_boundaries ],
  471. 1, # to ensure adjacent expolygons are unified
  472. )};
  473. }
  474. # subtract the new top surfaces from the other non-top surfaces and re-add them
  475. my @other = grep $_->surface_type != S_TYPE_TOP && !$_->is_bottom, @surfaces;
  476. foreach my $group (@{Slic3r::Surface::Collection->new(@other)->group}) {
  477. push @new_surfaces, map $group->[0]->clone(expolygon => $_), @{diff_ex(
  478. [ map $_->p, @$group ],
  479. [ map $_->p, @new_surfaces ],
  480. )};
  481. }
  482. $self->fill_surfaces->clear;
  483. $self->fill_surfaces->append(@new_surfaces);
  484. }
  485. 1;