Region.pm 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. package Slic3r::Layer::Region;
  2. use Moo;
  3. use List::Util qw(sum first);
  4. use Slic3r::ExtrusionPath ':roles';
  5. use Slic3r::Flow ':roles';
  6. use Slic3r::Geometry qw(PI A B scale unscale chained_path points_coincide);
  7. use Slic3r::Geometry::Clipper qw(union_ex diff_ex intersection_ex
  8. offset offset2 offset2_ex union_pt diff intersection
  9. union diff intersection_pl);
  10. use Slic3r::Surface ':types';
  11. has 'layer' => (
  12. is => 'ro',
  13. weak_ref => 1,
  14. required => 1,
  15. handles => [qw(id slice_z print_z height object print)],
  16. );
  17. has 'region' => (is => 'ro', required => 1, handles => [qw(config)]);
  18. has 'infill_area_threshold' => (is => 'lazy');
  19. has 'overhang_width' => (is => 'lazy');
  20. # collection of surfaces generated by slicing the original geometry
  21. # divided by type top/bottom/internal
  22. has 'slices' => (is => 'rw', default => sub { Slic3r::Surface::Collection->new });
  23. # collection of extrusion paths/loops filling gaps
  24. has 'thin_fills' => (is => 'rw', default => sub { Slic3r::ExtrusionPath::Collection->new });
  25. # collection of surfaces for infill generation
  26. has 'fill_surfaces' => (is => 'rw', default => sub { Slic3r::Surface::Collection->new });
  27. # ordered collection of extrusion paths/loops to build all perimeters
  28. has 'perimeters' => (is => 'rw', default => sub { Slic3r::ExtrusionPath::Collection->new });
  29. # ordered collection of extrusion paths to fill surfaces
  30. has 'fills' => (is => 'rw', default => sub { Slic3r::ExtrusionPath::Collection->new });
  31. sub _build_overhang_width {
  32. my $self = shift;
  33. my $threshold_rad = PI/2 - atan2($self->flow(FLOW_ROLE_PERIMETER)->width / $self->height / 2, 1);
  34. return scale($self->height * ((cos $threshold_rad) / (sin $threshold_rad)));
  35. }
  36. sub _build_infill_area_threshold {
  37. my $self = shift;
  38. return $self->flow(FLOW_ROLE_SOLID_INFILL)->scaled_spacing ** 2;
  39. }
  40. sub flow {
  41. my ($self, $role, $bridge, $width) = @_;
  42. return $self->region->flow(
  43. $role,
  44. $self->layer->height,
  45. $bridge // 0,
  46. $self->layer->id == 0,
  47. $width,
  48. );
  49. }
  50. sub make_perimeters {
  51. my $self = shift;
  52. my $perimeter_flow = $self->flow(FLOW_ROLE_PERIMETER);
  53. my $mm3_per_mm = $perimeter_flow->mm3_per_mm($self->height);
  54. my $pwidth = $perimeter_flow->scaled_width;
  55. my $pspacing = $perimeter_flow->scaled_spacing;
  56. my $ispacing = $self->flow(FLOW_ROLE_SOLID_INFILL)->scaled_spacing;
  57. my $gap_area_threshold = $pwidth ** 2;
  58. $self->perimeters->clear;
  59. $self->fill_surfaces->clear;
  60. $self->thin_fills->clear;
  61. my @contours = (); # array of Polygons with ccw orientation
  62. my @holes = (); # array of Polygons with cw orientation
  63. my @thin_walls = (); # array of ExPolygons
  64. my @gaps = (); # 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 (@{$self->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 @last_gaps = ();
  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. # width/2 + spacing/2 + spacing/2 + width/2
  79. @offsets = @{offset2(\@last, -(0.5*$pwidth + 0.5*$pspacing - 1), +(0.5*$pspacing - 1))};
  80. # look for thin walls
  81. if ($self->config->thin_walls) {
  82. my $diff = diff_ex(
  83. \@last,
  84. offset(\@offsets, +0.5*$pwidth),
  85. 1, # medial axis requires non-overlapping geometry
  86. );
  87. push @thin_walls, @$diff;
  88. }
  89. } else {
  90. @offsets = @{offset2(\@last, -(1.5*$pspacing - 1), +(0.5*$pspacing - 1))};
  91. # look for gaps
  92. if ($self->print->config->gap_fill_speed > 0 && $self->config->fill_density > 0) {
  93. my $diff = diff_ex(
  94. offset(\@last, -0.5*$pspacing),
  95. offset(\@offsets, +0.5*$pspacing),
  96. );
  97. push @gaps, @last_gaps = grep abs($_->area) >= $gap_area_threshold, @$diff;
  98. }
  99. }
  100. last if !@offsets;
  101. last if $i > $loop_number; # we were only looking for gaps this time
  102. # clone polygons because these ExPolygons will go out of scope very soon
  103. @last = @offsets;
  104. foreach my $polygon (@offsets) {
  105. if ($polygon->is_counter_clockwise) {
  106. push @contours, $polygon;
  107. } else {
  108. push @holes, $polygon;
  109. }
  110. }
  111. }
  112. }
  113. # make sure we don't infill narrow parts that are already gap-filled
  114. # (we only consider this surface's gaps to reduce the diff() complexity)
  115. @last = @{diff(\@last, [ map @$_, @last_gaps ])};
  116. # create one more offset to be used as boundary for fill
  117. # we offset by half the perimeter spacing (to get to the actual infill boundary)
  118. # and then we offset back and forth by half the infill spacing to only consider the
  119. # non-collapsing regions
  120. $self->fill_surfaces->append(
  121. map Slic3r::Surface->new(expolygon => $_, surface_type => S_TYPE_INTERNAL), # use a bogus surface type
  122. @{offset2_ex(
  123. [ map @{$_->simplify_p(&Slic3r::SCALED_RESOLUTION)}, @{union_ex(\@last)} ],
  124. -($pspacing/2 + $ispacing/2),
  125. +$ispacing/2,
  126. )}
  127. );
  128. }
  129. # find nesting hierarchies separately for contours and holes
  130. my $contours_pt = union_pt(\@contours);
  131. my $holes_pt = union_pt(\@holes);
  132. # prepare a coderef for traversing the PolyTree object
  133. # external contours are root items of $contours_pt
  134. # internal contours are the ones next to external
  135. my $traverse;
  136. $traverse = sub {
  137. my ($polynodes, $depth, $is_contour) = @_;
  138. # use a nearest neighbor search to order these children
  139. # TODO: supply second argument to chained_path() too?
  140. my @ordering_points = map { ($_->{outer} // $_->{hole})->first_point } @$polynodes;
  141. my @nodes = @$polynodes[@{chained_path(\@ordering_points)}];
  142. my @loops = ();
  143. foreach my $polynode (@nodes) {
  144. # if this is an external contour find all holes belonging to this contour(s)
  145. # and prepend them
  146. if ($is_contour && $depth == 0) {
  147. # $polynode is the outermost loop of an island
  148. my @holes = ();
  149. for (my $i = 0; $i <= $#$holes_pt; $i++) {
  150. if ($polynode->{outer}->contains_point($holes_pt->[$i]{outer}->first_point)) {
  151. push @holes, splice @$holes_pt, $i, 1; # remove from candidates to reduce complexity
  152. $i--;
  153. }
  154. }
  155. push @loops, reverse map $traverse->([$_], 0), @holes;
  156. }
  157. push @loops, $traverse->($polynode->{children}, $depth+1, $is_contour);
  158. # return ccw contours and cw holes
  159. # GCode.pm will convert all of them to ccw, but it needs to know
  160. # what the holes are in order to compute the correct inwards move
  161. my $polygon = ($polynode->{outer} // $polynode->{hole})->clone;
  162. $polygon->reverse if defined $polynode->{hole};
  163. $polygon->reverse if !$is_contour;
  164. my $role = EXTR_ROLE_PERIMETER;
  165. if ($is_contour ? $depth == 0 : !@{ $polynode->{children} }) {
  166. # external perimeters are root level in case of contours
  167. # and items with no children in case of holes
  168. $role = EXTR_ROLE_EXTERNAL_PERIMETER;
  169. } elsif ($depth == 1 && $is_contour) {
  170. $role = EXTR_ROLE_CONTOUR_INTERNAL_PERIMETER;
  171. }
  172. push @loops, Slic3r::ExtrusionLoop->new(
  173. polygon => $polygon,
  174. role => $role,
  175. mm3_per_mm => $mm3_per_mm,
  176. );
  177. }
  178. return @loops;
  179. };
  180. # order loops from inner to outer (in terms of object slices)
  181. my @loops = $traverse->($contours_pt, 0, 1);
  182. # if brim will be printed, reverse the order of perimeters so that
  183. # we continue inwards after having finished the brim
  184. # TODO: add test for perimeter order
  185. @loops = reverse @loops
  186. if $self->print->config->external_perimeters_first
  187. || ($self->layer->id == 0 && $self->print->config->brim_width > 0);
  188. # append perimeters
  189. $self->perimeters->append(@loops);
  190. # process thin walls by collapsing slices to single passes
  191. if (@thin_walls) {
  192. # the maximum thickness of our thin wall area is equal to the minimum thickness of a single loop
  193. my @p = map @{$_->medial_axis($pwidth + $pspacing)}, @thin_walls;
  194. if (0) {
  195. require "Slic3r/SVG.pm";
  196. Slic3r::SVG::output(
  197. "medial_axis.svg",
  198. no_arrows => 1,
  199. expolygons => \@thin_walls,
  200. green_polylines => [ map $_->polygon->split_at_first_point, @{$self->perimeters} ],
  201. red_polylines => \@p,
  202. );
  203. }
  204. my @paths = ();
  205. my $min_thin_wall_length = 2*$pwidth;
  206. for my $p (@p) {
  207. next if $p->length < $min_thin_wall_length;
  208. my %params = (
  209. role => EXTR_ROLE_EXTERNAL_PERIMETER,
  210. mm3_per_mm => $mm3_per_mm,
  211. );
  212. push @paths, $p->isa('Slic3r::Polygon')
  213. ? Slic3r::ExtrusionLoop->new(polygon => $p, %params)
  214. : Slic3r::ExtrusionPath->new(polyline => $p, %params);
  215. }
  216. $self->perimeters->append(
  217. map $_->clone, @{Slic3r::ExtrusionPath::Collection->new(@paths)->chained_path(0)}
  218. );
  219. Slic3r::debugf " %d thin walls detected\n", scalar(@paths) if $Slic3r::debug;
  220. }
  221. $self->_fill_gaps(\@gaps);
  222. }
  223. sub _fill_gaps {
  224. my $self = shift;
  225. my ($gaps) = @_;
  226. return unless @$gaps;
  227. my $filler = Slic3r::Fill->new->filler('rectilinear');
  228. $filler->angle($self->config->fill_angle);
  229. $filler->layer_id($self->layer->id);
  230. # we should probably use this code to handle thin walls
  231. # but we need to enable dynamic extrusion width before as we can't
  232. # use zigzag for thin walls.
  233. # medial axis-based gap fill should benefit from detection of larger gaps too, so
  234. # we could try with 1.5*$w for example, but that doesn't work well for zigzag fill
  235. # because it tends to create very sparse points along the gap when the infill direction
  236. # is not parallel to the gap (1.5*$w thus may only work well with a straight line)
  237. my $w = $self->flow(FLOW_ROLE_PERIMETER)->width;
  238. my @widths = ($w, 0.4 * $w); # worth trying 0.2 too?
  239. foreach my $width (@widths) {
  240. my $flow = $self->flow(FLOW_ROLE_PERIMETER, 0, $width);
  241. # extract the gaps having this width
  242. my @this_width = map @{$_->offset_ex(+0.5*$flow->scaled_width)},
  243. map @{$_->noncollapsing_offset_ex(-0.5*$flow->scaled_width)},
  244. @$gaps;
  245. if (0) { # remember to re-enable t/dynamic.t
  246. # fill gaps using dynamic extrusion width, by treating them like thin polygons,
  247. # thus generating the skeleton and using it to fill them
  248. my %path_args = (
  249. role => EXTR_ROLE_SOLIDFILL,
  250. mm3_per_mm => $flow->mm3_per_mm($self->height),
  251. );
  252. $self->thin_fills->append(map {
  253. $_->isa('Slic3r::Polygon')
  254. ? Slic3r::ExtrusionLoop->new(polygon => $_, %path_args)->split_at_first_point # we should keep these as loops
  255. : Slic3r::ExtrusionPath->new(polyline => $_, %path_args),
  256. } map @{$_->medial_axis($flow->scaled_width)}, @this_width);
  257. Slic3r::debugf " %d gaps filled with extrusion width = %s\n", scalar @this_width, $width
  258. if @{ $self->thin_fills };
  259. } else {
  260. # fill gaps using zigzag infill
  261. # since this is infill, we have to offset by half-extrusion width inwards
  262. my @infill = map @{$_->offset_ex(-0.5*$flow->scaled_width)}, @this_width;
  263. foreach my $expolygon (@infill) {
  264. my ($params, @paths) = $filler->fill_surface(
  265. Slic3r::Surface->new(expolygon => $expolygon, surface_type => S_TYPE_INTERNALSOLID),
  266. density => 1,
  267. flow => $flow,
  268. );
  269. my $mm3_per_mm = $params->{flow}->mm3_per_mm($self->height);
  270. # Split polylines into lines so that the chained_path() search
  271. # at the final stage has more freedom and will choose starting
  272. # points closer than last positions. OTOH, this will make such
  273. # search slower. Probably, ExtrusionPath objects should support
  274. # splitting nearby a given position so that we can choose the right
  275. # entry point even in the middle of the path without needing a
  276. # complex, slow, chained_path() search on all segments. TODO.
  277. # Such logic will also avoid all the small travel moves that this
  278. # line-splitting causes, and it will be applicable to other things
  279. # too.
  280. my @lines = map @{Slic3r::Polyline->new(@$_)->lines}, @paths;
  281. @paths = map Slic3r::ExtrusionPath->new(
  282. polyline => Slic3r::Polyline->new(@$_),
  283. role => EXTR_ROLE_GAPFILL,
  284. mm3_per_mm => $mm3_per_mm,
  285. ), @lines;
  286. $_->simplify($flow->scaled_width/3) for @paths;
  287. $self->thin_fills->append(@paths);
  288. }
  289. }
  290. # check what's left
  291. @$gaps = @{diff_ex(
  292. [ map @$_, @$gaps ],
  293. [ map @$_, @this_width ],
  294. )};
  295. }
  296. }
  297. sub prepare_fill_surfaces {
  298. my $self = shift;
  299. # if no solid layers are requested, turn top/bottom surfaces to internal
  300. if ($self->config->top_solid_layers == 0) {
  301. $_->surface_type(S_TYPE_INTERNAL) for @{$self->fill_surfaces->filter_by_type(S_TYPE_TOP)};
  302. }
  303. if ($self->config->bottom_solid_layers == 0) {
  304. $_->surface_type(S_TYPE_INTERNAL) for @{$self->fill_surfaces->filter_by_type(S_TYPE_BOTTOM)};
  305. }
  306. # turn too small internal regions into solid regions according to the user setting
  307. if ($self->config->fill_density > 0) {
  308. my $min_area = scale scale $self->config->solid_infill_below_area; # scaling an area requires two calls!
  309. $_->surface_type(S_TYPE_INTERNALSOLID)
  310. for grep { $_->area <= $min_area } @{$self->fill_surfaces->filter_by_type(S_TYPE_INTERNAL)};
  311. }
  312. }
  313. sub process_external_surfaces {
  314. my ($self, $lower_layer) = @_;
  315. my @surfaces = @{$self->fill_surfaces};
  316. my $margin = scale &Slic3r::EXTERNAL_INFILL_MARGIN;
  317. my @bottom = ();
  318. foreach my $surface (grep $_->surface_type == S_TYPE_BOTTOM, @surfaces) {
  319. my $grown = $surface->expolygon->offset_ex(+$margin);
  320. # detect bridge direction before merging grown surfaces otherwise adjacent bridges
  321. # would get merged into a single one while they need different directions
  322. # also, supply the original expolygon instead of the grown one, because in case
  323. # of very thin (but still working) anchors, the grown expolygon would go beyond them
  324. my $angle = $lower_layer
  325. ? $self->_detect_bridge_direction($surface->expolygon, $lower_layer)
  326. : undef;
  327. push @bottom, map $surface->clone(expolygon => $_, bridge_angle => $angle), @$grown;
  328. }
  329. my @top = ();
  330. foreach my $surface (grep $_->surface_type == S_TYPE_TOP, @surfaces) {
  331. # give priority to bottom surfaces
  332. my $grown = diff_ex(
  333. $surface->expolygon->offset(+$margin),
  334. [ map $_->p, @bottom ],
  335. );
  336. push @top, map $surface->clone(expolygon => $_), @$grown;
  337. }
  338. # if we're slicing with no infill, we can't extend external surfaces
  339. # over non-existent infill
  340. my @fill_boundaries = $self->config->fill_density > 0
  341. ? @surfaces
  342. : grep $_->surface_type != S_TYPE_INTERNAL, @surfaces;
  343. # intersect the grown surfaces with the actual fill boundaries
  344. my @new_surfaces = ();
  345. foreach my $group (@{Slic3r::Surface::Collection->new(@top, @bottom)->group}) {
  346. push @new_surfaces,
  347. map $group->[0]->clone(expolygon => $_),
  348. @{intersection_ex(
  349. [ map $_->p, @$group ],
  350. [ map $_->p, @fill_boundaries ],
  351. 1, # to ensure adjacent expolygons are unified
  352. )};
  353. }
  354. # subtract the new top surfaces from the other non-top surfaces and re-add them
  355. my @other = grep $_->surface_type != S_TYPE_TOP && $_->surface_type != S_TYPE_BOTTOM, @surfaces;
  356. foreach my $group (@{Slic3r::Surface::Collection->new(@other)->group}) {
  357. push @new_surfaces, map $group->[0]->clone(expolygon => $_), @{diff_ex(
  358. [ map $_->p, @$group ],
  359. [ map $_->p, @new_surfaces ],
  360. )};
  361. }
  362. $self->fill_surfaces->clear;
  363. $self->fill_surfaces->append(@new_surfaces);
  364. }
  365. sub _detect_bridge_direction {
  366. my ($self, $expolygon, $lower_layer) = @_;
  367. my $perimeter_flow = $self->flow(FLOW_ROLE_PERIMETER);
  368. my $infill_flow = $self->flow(FLOW_ROLE_INFILL);
  369. my $grown = $expolygon->offset(+$perimeter_flow->scaled_width);
  370. my @lower = @{$lower_layer->slices}; # expolygons
  371. # detect what edges lie on lower slices
  372. my @edges = (); # polylines
  373. foreach my $lower (@lower) {
  374. # turn bridge contour and holes into polylines and then clip them
  375. # with each lower slice's contour
  376. my @clipped = @{intersection_pl([ map $_->split_at_first_point, @$grown ], [$lower->contour])};
  377. if (@clipped == 2) {
  378. # If the split_at_first_point() call above happens to split the polygon inside the clipping area
  379. # we would get two consecutive polylines instead of a single one, so we use this ugly hack to
  380. # recombine them back into a single one in order to trigger the @edges == 2 logic below.
  381. # This needs to be replaced with something way better.
  382. if (points_coincide($clipped[0][0], $clipped[-1][-1])) {
  383. @clipped = (Slic3r::Polyline->new(@{$clipped[-1]}, @{$clipped[0]}));
  384. }
  385. if (points_coincide($clipped[-1][0], $clipped[0][-1])) {
  386. @clipped = (Slic3r::Polyline->new(@{$clipped[0]}, @{$clipped[1]}));
  387. }
  388. }
  389. push @edges, @clipped;
  390. }
  391. Slic3r::debugf "Found bridge on layer %d with %d support(s)\n", $self->id, scalar(@edges);
  392. return undef if !@edges;
  393. my $bridge_angle = undef;
  394. if (0) {
  395. require "Slic3r/SVG.pm";
  396. Slic3r::SVG::output("bridge_$expolygon.svg",
  397. expolygons => [ $expolygon ],
  398. red_expolygons => [ @lower ],
  399. polylines => [ @edges ],
  400. );
  401. }
  402. if (@edges == 2) {
  403. my @chords = map Slic3r::Line->new($_->[0], $_->[-1]), @edges;
  404. my @midpoints = map $_->midpoint, @chords;
  405. my $line_between_midpoints = Slic3r::Line->new(@midpoints);
  406. $bridge_angle = Slic3r::Geometry::rad2deg_dir($line_between_midpoints->direction);
  407. } elsif (@edges == 1) {
  408. # TODO: this case includes both U-shaped bridges and plain overhangs;
  409. # we need a trapezoidation algorithm to detect the actual bridged area
  410. # and separate it from the overhang area.
  411. # in the mean time, we're treating as overhangs all cases where
  412. # our supporting edge is a straight line
  413. if (@{$edges[0]} > 2) {
  414. my $line = Slic3r::Line->new($edges[0]->[0], $edges[0]->[-1]);
  415. $bridge_angle = Slic3r::Geometry::rad2deg_dir($line->direction);
  416. }
  417. } elsif (@edges) {
  418. # inset the bridge expolygon; we'll use this one to clip our test lines
  419. my $inset = $expolygon->offset_ex($infill_flow->scaled_width);
  420. # detect anchors as intersection between our bridge expolygon and the lower slices
  421. my $anchors = intersection_ex(
  422. $grown,
  423. [ map @$_, @lower ],
  424. 1, # safety offset required to avoid Clipper from detecting empty intersection while Boost actually found some @edges
  425. );
  426. if (@$anchors) {
  427. # we'll now try several directions using a rudimentary visibility check:
  428. # bridge in several directions and then sum the length of lines having both
  429. # endpoints within anchors
  430. my %directions = (); # angle => score
  431. my $angle_increment = PI/36; # 5°
  432. my $line_increment = $infill_flow->scaled_width;
  433. for (my $angle = 0; $angle <= PI; $angle += $angle_increment) {
  434. # rotate everything - the center point doesn't matter
  435. $_->rotate($angle, [0,0]) for @$inset, @$anchors;
  436. # generate lines in this direction
  437. my $bounding_box = Slic3r::Geometry::BoundingBox->new_from_points([ map @$_, map @$_, @$anchors ]);
  438. my @lines = ();
  439. for (my $x = $bounding_box->x_min; $x <= $bounding_box->x_max; $x += $line_increment) {
  440. push @lines, Slic3r::Polyline->new([$x, $bounding_box->y_min], [$x, $bounding_box->y_max]);
  441. }
  442. my @clipped_lines = map Slic3r::Line->new(@$_), @{ intersection_pl(\@lines, [ map @$_, @$inset ]) };
  443. # remove any line not having both endpoints within anchors
  444. # NOTE: these calls to contains_point() probably need to check whether the point
  445. # is on the anchor boundaries too
  446. @clipped_lines = grep {
  447. my $line = $_;
  448. !(first { $_->contains_point($line->a) } @$anchors)
  449. && !(first { $_->contains_point($line->b) } @$anchors);
  450. } @clipped_lines;
  451. # sum length of bridged lines
  452. $directions{-$angle} = sum(map $_->length, @clipped_lines) // 0;
  453. }
  454. # this could be slightly optimized with a max search instead of the sort
  455. my @sorted_directions = sort { $directions{$a} <=> $directions{$b} } keys %directions;
  456. # the best direction is the one causing most lines to be bridged
  457. $bridge_angle = Slic3r::Geometry::rad2deg_dir($sorted_directions[-1]);
  458. }
  459. }
  460. Slic3r::debugf " Optimal infill angle of bridge on layer %d is %d degrees\n",
  461. $self->id, $bridge_angle if defined $bridge_angle;
  462. return $bridge_angle;
  463. }
  464. 1;