Object.pm 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. package Slic3r::Print::Object;
  2. use Moo;
  3. use List::Util qw(min max sum first);
  4. use Slic3r::Geometry qw(X Y Z PI scale unscale deg2rad rad2deg scaled_epsilon chained_path_points);
  5. use Slic3r::Geometry::Clipper qw(diff diff_ex intersection intersection_ex union union_ex
  6. offset offset_ex offset2 offset2_ex CLIPPER_OFFSET_SCALE JT_MITER);
  7. use Slic3r::Surface ':types';
  8. has 'print' => (is => 'ro', weak_ref => 1, required => 1);
  9. has 'input_file' => (is => 'rw', required => 0);
  10. has 'meshes' => (is => 'rw', default => sub { [] }); # by region_id
  11. has 'size' => (is => 'rw', required => 1); # XYZ in scaled coordinates
  12. has 'copies' => (is => 'rw', trigger => 1); # in scaled coordinates
  13. has 'layers' => (is => 'rw', default => sub { [] });
  14. has 'support_layers' => (is => 'rw', default => sub { [] });
  15. has 'config_overrides' => (is => 'rw', default => sub { Slic3r::Config->new });
  16. has 'config' => (is => 'rw');
  17. has 'layer_height_ranges' => (is => 'rw', default => sub { [] }); # [ z_min, z_max, layer_height ]
  18. has 'fill_maker' => (is => 'lazy');
  19. sub BUILD {
  20. my $self = shift;
  21. $self->init_config;
  22. # make layers taking custom heights into account
  23. my $print_z = my $slice_z = my $height = 0;
  24. # add raft layers
  25. for my $id (0 .. $self->config->raft_layers-1) {
  26. $height = ($id == 0)
  27. ? $Slic3r::Config->get_value('first_layer_height')
  28. : $Slic3r::Config->layer_height;
  29. $print_z += $height;
  30. push @{$self->layers}, Slic3r::Layer->new(
  31. object => $self,
  32. id => $id,
  33. height => $height,
  34. print_z => $print_z,
  35. slice_z => -1,
  36. );
  37. }
  38. # loop until we have at least one layer and the max slice_z reaches the object height
  39. my $max_z = unscale $self->size->[Z];
  40. while (!@{$self->layers} || ($slice_z - $height) <= $max_z) {
  41. my $id = $#{$self->layers} + 1;
  42. # assign the default height to the layer according to the general settings
  43. $height = ($id == 0)
  44. ? $Slic3r::Config->get_value('first_layer_height')
  45. : $Slic3r::Config->layer_height;
  46. # look for an applicable custom range
  47. if (my $range = first { $_->[0] <= $slice_z && $_->[1] > $slice_z } @{$self->layer_height_ranges}) {
  48. $height = $range->[2];
  49. # if user set custom height to zero we should just skip the range and resume slicing over it
  50. if ($height == 0) {
  51. $slice_z += $range->[1] - $range->[0];
  52. next;
  53. }
  54. }
  55. $print_z += $height;
  56. $slice_z += $height/2;
  57. ### Slic3r::debugf "Layer %d: height = %s; slice_z = %s; print_z = %s\n", $id, $height, $slice_z, $print_z;
  58. push @{$self->layers}, Slic3r::Layer->new(
  59. object => $self,
  60. id => $id,
  61. height => $height,
  62. print_z => $print_z,
  63. slice_z => scale $slice_z,
  64. );
  65. $slice_z += $height/2; # add the other half layer
  66. }
  67. }
  68. sub _build_fill_maker {
  69. my $self = shift;
  70. return Slic3r::Fill->new(object => $self);
  71. }
  72. # This should be probably moved in Print.pm at the point where we sort Layer objects
  73. sub _trigger_copies {
  74. my $self = shift;
  75. return unless @{$self->copies} > 1;
  76. # order copies with a nearest neighbor search
  77. @{$self->copies} = @{chained_path_points($self->copies)}
  78. }
  79. sub init_config {
  80. my $self = shift;
  81. $self->config(Slic3r::Config->merge($self->print->config, $self->config_overrides));
  82. }
  83. sub layer_count {
  84. my $self = shift;
  85. return scalar @{ $self->layers };
  86. }
  87. sub bounding_box {
  88. my $self = shift;
  89. # since the object is aligned to origin, bounding box coincides with size
  90. return Slic3r::Geometry::BoundingBox->new_from_points([ map Slic3r::Point->new(@$_[X,Y]), [0,0], $self->size ]);
  91. }
  92. sub slice {
  93. my $self = shift;
  94. my %params = @_;
  95. # make sure all layers contain layer region objects for all regions
  96. my $regions_count = $self->print->regions_count;
  97. foreach my $layer (@{ $self->layers }) {
  98. $layer->region($_) for 0 .. ($regions_count-1);
  99. }
  100. # process facets
  101. for my $region_id (0 .. $#{$self->meshes}) {
  102. my $mesh = $self->meshes->[$region_id] // next; # ignore undef meshes
  103. $mesh->repair;
  104. {
  105. my $loops = $mesh->slice([ map $_->slice_z, @{$self->layers} ]);
  106. for my $layer_id (0..$#$loops) {
  107. my $layerm = $self->layers->[$layer_id]->regions->[$region_id];
  108. $layerm->make_surfaces($loops->[$layer_id]);
  109. }
  110. # TODO: read slicing_errors
  111. }
  112. # free memory
  113. undef $mesh;
  114. undef $self->meshes->[$region_id];return;
  115. }
  116. # free memory
  117. $self->meshes(undef);
  118. # remove last layer(s) if empty
  119. pop @{$self->layers} while @{$self->layers} && (!map @{$_->slices}, @{$self->layers->[-1]->regions});
  120. foreach my $layer (@{ $self->layers }) {
  121. # merge all regions' slices to get islands
  122. $layer->make_slices;
  123. }
  124. # detect slicing errors
  125. my $warning_thrown = 0;
  126. for my $i (0 .. $#{$self->layers}) {
  127. my $layer = $self->layers->[$i];
  128. next unless $layer->slicing_errors;
  129. if (!$warning_thrown) {
  130. warn "The model has overlapping or self-intersecting facets. I tried to repair it, "
  131. . "however you might want to check the results or repair the input file and retry.\n";
  132. $warning_thrown = 1;
  133. }
  134. # try to repair the layer surfaces by merging all contours and all holes from
  135. # neighbor layers
  136. Slic3r::debugf "Attempting to repair layer %d\n", $i;
  137. foreach my $region_id (0 .. $#{$layer->regions}) {
  138. my $layerm = $layer->region($region_id);
  139. my (@upper_surfaces, @lower_surfaces);
  140. for (my $j = $i+1; $j <= $#{$self->layers}; $j++) {
  141. if (!$self->layers->[$j]->slicing_errors) {
  142. @upper_surfaces = @{$self->layers->[$j]->region($region_id)->slices};
  143. last;
  144. }
  145. }
  146. for (my $j = $i-1; $j >= 0; $j--) {
  147. if (!$self->layers->[$j]->slicing_errors) {
  148. @lower_surfaces = @{$self->layers->[$j]->region($region_id)->slices};
  149. last;
  150. }
  151. }
  152. my $union = union_ex([
  153. map $_->expolygon->contour, @upper_surfaces, @lower_surfaces,
  154. ]);
  155. my $diff = diff_ex(
  156. [ map @$_, @$union ],
  157. [ map @{$_->expolygon->holes}, @upper_surfaces, @lower_surfaces, ],
  158. );
  159. $layerm->slices->clear;
  160. $layerm->slices->append(
  161. map Slic3r::Surface->new
  162. (expolygon => $_, surface_type => S_TYPE_INTERNAL),
  163. @$diff
  164. );
  165. }
  166. # update layer slices after repairing the single regions
  167. $layer->make_slices;
  168. }
  169. # remove empty layers from bottom
  170. my $first_object_layer_id = $self->config->raft_layers;
  171. while (@{$self->layers} && !@{$self->layers->[$first_object_layer_id]->slices} && !map @{$_->thin_walls}, @{$self->layers->[$first_object_layer_id]->regions}) {
  172. splice @{$self->layers}, $first_object_layer_id, 1;
  173. for (my $i = $first_object_layer_id; $i <= $#{$self->layers}; $i++) {
  174. $self->layers->[$i]->id($i);
  175. }
  176. }
  177. }
  178. sub make_perimeters {
  179. my $self = shift;
  180. # compare each layer to the one below, and mark those slices needing
  181. # one additional inner perimeter, like the top of domed objects-
  182. # this algorithm makes sure that at least one perimeter is overlapping
  183. # but we don't generate any extra perimeter if fill density is zero, as they would be floating
  184. # inside the object - infill_only_where_needed should be the method of choice for printing
  185. # hollow objects
  186. if ($self->config->extra_perimeters && $self->config->perimeters > 0 && $self->config->fill_density > 0) {
  187. for my $region_id (0 .. ($self->print->regions_count-1)) {
  188. for my $layer_id (0 .. $self->layer_count-2) {
  189. my $layerm = $self->layers->[$layer_id]->regions->[$region_id];
  190. my $upper_layerm = $self->layers->[$layer_id+1]->regions->[$region_id];
  191. my $perimeter_spacing = $layerm->perimeter_flow->scaled_spacing;
  192. my $overlap = $perimeter_spacing; # one perimeter
  193. my $diff = diff(
  194. offset([ map @{$_->expolygon}, @{$layerm->slices} ], -($self->config->perimeters * $perimeter_spacing)),
  195. offset([ map @{$_->expolygon}, @{$upper_layerm->slices} ], -$overlap),
  196. );
  197. next if !@$diff;
  198. # if we need more perimeters, $diff should contain a narrow region that we can collapse
  199. # we use a higher miterLimit here to handle areas with acute angles
  200. # in those cases, the default miterLimit would cut the corner and we'd
  201. # get a triangle that would trigger a non-needed extra perimeter
  202. $diff = diff(
  203. $diff,
  204. offset2($diff, -$perimeter_spacing, +$perimeter_spacing, CLIPPER_OFFSET_SCALE, JT_MITER, 5),
  205. 1,
  206. );
  207. next if !@$diff;
  208. # diff contains the collapsed area
  209. foreach my $slice (@{$layerm->slices}) {
  210. my $extra_perimeters = 0;
  211. CYCLE: while (1) {
  212. # compute polygons representing the thickness of the hypotetical new internal perimeter
  213. # of our slice
  214. $extra_perimeters++;
  215. my $hypothetical_perimeter = diff(
  216. offset($slice->expolygon->arrayref, -($perimeter_spacing * ($self->config->perimeters + $extra_perimeters-1))),
  217. offset($slice->expolygon->arrayref, -($perimeter_spacing * ($self->config->perimeters + $extra_perimeters))),
  218. );
  219. last CYCLE if !@$hypothetical_perimeter; # no extra perimeter is possible
  220. # only add the perimeter if there's an intersection with the collapsed area
  221. last CYCLE if !@{ intersection($diff, $hypothetical_perimeter) };
  222. Slic3r::debugf " adding one more perimeter at layer %d\n", $layer_id;
  223. $slice->extra_perimeters($extra_perimeters);
  224. }
  225. }
  226. }
  227. }
  228. }
  229. Slic3r::parallelize(
  230. items => sub { 0 .. ($self->layer_count-1) },
  231. thread_cb => sub {
  232. my $q = shift;
  233. while (defined (my $layer_id = $q->dequeue)) {
  234. $self->layers->[$layer_id]->make_perimeters;
  235. }
  236. },
  237. collect_cb => sub {},
  238. no_threads_cb => sub {
  239. $_->make_perimeters for @{$self->layers};
  240. },
  241. );
  242. }
  243. sub detect_surfaces_type {
  244. my $self = shift;
  245. Slic3r::debugf "Detecting solid surfaces...\n";
  246. for my $region_id (0 .. ($self->print->regions_count-1)) {
  247. for my $i (0 .. ($self->layer_count-1)) {
  248. my $layerm = $self->layers->[$i]->regions->[$region_id];
  249. # prepare a reusable subroutine to make surface differences
  250. my $difference = sub {
  251. my ($subject, $clip, $result_type) = @_;
  252. my $diff = diff(
  253. [ map @$_, @$subject ],
  254. [ map @$_, @$clip ],
  255. );
  256. # collapse very narrow parts (using the safety offset in the diff is not enough)
  257. my $offset = $layerm->perimeter_flow->scaled_width / 10;
  258. return map Slic3r::Surface->new(expolygon => $_, surface_type => $result_type),
  259. @{ offset2_ex($diff, -$offset, +$offset) };
  260. };
  261. # comparison happens against the *full* slices (considering all regions)
  262. my $upper_layer = $self->layers->[$i+1];
  263. my $lower_layer = $i > 0 ? $self->layers->[$i-1] : undef;
  264. my (@bottom, @top, @internal) = ();
  265. # find top surfaces (difference between current surfaces
  266. # of current layer and upper one)
  267. if ($upper_layer) {
  268. @top = $difference->(
  269. [ map $_->expolygon, @{$layerm->slices} ],
  270. $upper_layer->slices,
  271. S_TYPE_TOP,
  272. );
  273. } else {
  274. # if no upper layer, all surfaces of this one are solid
  275. # we clone surfaces because we're going to clear the slices collection
  276. @top = map $_->clone, @{$layerm->slices};
  277. $_->surface_type(S_TYPE_TOP) for @top;
  278. }
  279. # find bottom surfaces (difference between current surfaces
  280. # of current layer and lower one)
  281. if ($lower_layer) {
  282. # lower layer's slices are already Surface objects
  283. @bottom = $difference->(
  284. [ map $_->expolygon, @{$layerm->slices} ],
  285. $lower_layer->slices,
  286. S_TYPE_BOTTOM,
  287. );
  288. } else {
  289. # if no lower layer, all surfaces of this one are solid
  290. # we clone surfaces because we're going to clear the slices collection
  291. @bottom = map $_->clone, @{$layerm->slices};
  292. $_->surface_type(S_TYPE_BOTTOM) for @bottom;
  293. }
  294. # now, if the object contained a thin membrane, we could have overlapping bottom
  295. # and top surfaces; let's do an intersection to discover them and consider them
  296. # as bottom surfaces (to allow for bridge detection)
  297. if (@top && @bottom) {
  298. my $overlapping = intersection_ex([ map $_->p, @top ], [ map $_->p, @bottom ]);
  299. Slic3r::debugf " layer %d contains %d membrane(s)\n", $layerm->id, scalar(@$overlapping)
  300. if $Slic3r::debug;
  301. @top = $difference->([map $_->expolygon, @top], $overlapping, S_TYPE_TOP);
  302. }
  303. # find internal surfaces (difference between top/bottom surfaces and others)
  304. @internal = $difference->(
  305. [ map $_->expolygon, @{$layerm->slices} ],
  306. [ map $_->expolygon, @top, @bottom ],
  307. S_TYPE_INTERNAL,
  308. );
  309. # save surfaces to layer
  310. $layerm->slices->clear;
  311. $layerm->slices->append(@bottom, @top, @internal);
  312. Slic3r::debugf " layer %d has %d bottom, %d top and %d internal surfaces\n",
  313. $layerm->id, scalar(@bottom), scalar(@top), scalar(@internal) if $Slic3r::debug;
  314. }
  315. # clip surfaces to the fill boundaries
  316. foreach my $layer (@{$self->layers}) {
  317. my $layerm = $layer->regions->[$region_id];
  318. my $fill_boundaries = [ map $_->clone->p, @{$layerm->fill_surfaces} ];
  319. $layerm->fill_surfaces->clear;
  320. foreach my $surface (@{$layerm->slices}) {
  321. my $intersection = intersection_ex(
  322. [ $surface->p ],
  323. $fill_boundaries,
  324. );
  325. $layerm->fill_surfaces->append(map Slic3r::Surface->new
  326. (expolygon => $_, surface_type => $surface->surface_type),
  327. @$intersection);
  328. }
  329. }
  330. }
  331. }
  332. sub clip_fill_surfaces {
  333. my $self = shift;
  334. return unless $self->config->infill_only_where_needed;
  335. # We only want infill under ceilings; this is almost like an
  336. # internal support material.
  337. my $additional_margin = scale 3;
  338. my $overhangs = []; # arrayref of polygons
  339. for my $layer_id (reverse 0..$#{$self->layers}) {
  340. my $layer = $self->layers->[$layer_id];
  341. my @layer_internal = ();
  342. my @new_internal = ();
  343. # clip this layer's internal surfaces to @overhangs
  344. foreach my $layerm (@{$layer->regions}) {
  345. # we assume that this step is run before bridge_over_infill() and combine_infill()
  346. # so these are the only internal types we might have
  347. my (@internal, @other) = ();
  348. foreach my $surface (map $_->clone, @{$layerm->fill_surfaces}) {
  349. $surface->surface_type == S_TYPE_INTERNAL
  350. ? push @internal, $surface
  351. : push @other, $surface;
  352. }
  353. # keep all the original internal surfaces to detect overhangs in this layer
  354. push @layer_internal, @internal;
  355. push @new_internal, my @new = map Slic3r::Surface->new(
  356. expolygon => $_,
  357. surface_type => S_TYPE_INTERNAL,
  358. ),
  359. @{intersection_ex(
  360. $overhangs,
  361. [ map $_->p, @internal ],
  362. )};
  363. $layerm->fill_surfaces->clear;
  364. $layerm->fill_surfaces->append(@new, @other);
  365. }
  366. # get this layer's overhangs defined as the full slice minus the internal infill
  367. # (thus we also consider perimeters)
  368. if ($layer_id > 0) {
  369. my $solid = diff(
  370. [ map @$_, @{$layer->slices} ],
  371. \@layer_internal,
  372. );
  373. $overhangs = offset($solid, +$additional_margin);
  374. push @$overhangs, @new_internal; # propagate upper overhangs
  375. }
  376. }
  377. }
  378. sub bridge_over_infill {
  379. my $self = shift;
  380. return if $self->config->fill_density == 1;
  381. for my $layer_id (1..$#{$self->layers}) {
  382. my $layer = $self->layers->[$layer_id];
  383. my $lower_layer = $self->layers->[$layer_id-1];
  384. foreach my $layerm (@{$layer->regions}) {
  385. # compute the areas needing bridge math
  386. my @internal_solid = @{$layerm->fill_surfaces->filter_by_type(S_TYPE_INTERNALSOLID)};
  387. my @lower_internal = map @{$_->fill_surfaces->filter_by_type(S_TYPE_INTERNAL)}, @{$lower_layer->regions};
  388. my $to_bridge = intersection_ex(
  389. [ map $_->p, @internal_solid ],
  390. [ map $_->p, @lower_internal ],
  391. );
  392. next unless @$to_bridge;
  393. Slic3r::debugf "Bridging %d internal areas at layer %d\n", scalar(@$to_bridge), $layer_id;
  394. # build the new collection of fill_surfaces
  395. {
  396. my @new_surfaces = map $_->clone, grep $_->surface_type != S_TYPE_INTERNALSOLID, @{$layerm->fill_surfaces};
  397. push @new_surfaces, map Slic3r::Surface->new(
  398. expolygon => $_,
  399. surface_type => S_TYPE_INTERNALBRIDGE,
  400. ), @$to_bridge;
  401. push @new_surfaces, map Slic3r::Surface->new(
  402. expolygon => $_,
  403. surface_type => S_TYPE_INTERNALSOLID,
  404. ), @{diff_ex(
  405. [ map $_->p, @internal_solid ],
  406. [ map @$_, @$to_bridge ],
  407. 1,
  408. )};
  409. $layerm->fill_surfaces->clear;
  410. $layerm->fill_surfaces->append(@new_surfaces);
  411. }
  412. # exclude infill from the layers below if needed
  413. # see discussion at https://github.com/alexrj/Slic3r/issues/240
  414. # Update: do not exclude any infill. Sparse infill is able to absorb the excess material.
  415. if (0) {
  416. my $excess = $layerm->extruders->{infill}->bridge_flow->width - $layerm->height;
  417. for (my $i = $layer_id-1; $excess >= $self->layers->[$i]->height; $i--) {
  418. Slic3r::debugf " skipping infill below those areas at layer %d\n", $i;
  419. foreach my $lower_layerm (@{$self->layers->[$i]->regions}) {
  420. my @new_surfaces = ();
  421. # subtract the area from all types of surfaces
  422. foreach my $group (Slic3r::Surface->group(@{$lower_layerm->fill_surfaces})) {
  423. push @new_surfaces, map $group->[0]->clone(expolygon => $_),
  424. @{diff_ex(
  425. [ map $_->p, @$group ],
  426. [ map @$_, @$to_bridge ],
  427. )};
  428. push @new_surfaces, map Slic3r::Surface->new(
  429. expolygon => $_,
  430. surface_type => S_TYPE_INTERNALVOID,
  431. ), @{intersection_ex(
  432. [ map $_->p, @$group ],
  433. [ map @$_, @$to_bridge ],
  434. )};
  435. }
  436. $lower_layerm->fill_surfaces->clear;
  437. $lower_layerm->fill_surfaces->append(@new_surfaces);
  438. }
  439. $excess -= $self->layers->[$i]->height;
  440. }
  441. }
  442. }
  443. }
  444. }
  445. sub discover_horizontal_shells {
  446. my $self = shift;
  447. Slic3r::debugf "==> DISCOVERING HORIZONTAL SHELLS\n";
  448. for my $region_id (0 .. ($self->print->regions_count-1)) {
  449. for (my $i = 0; $i < $self->layer_count; $i++) {
  450. my $layerm = $self->layers->[$i]->regions->[$region_id];
  451. if ($self->config->solid_infill_every_layers && $self->config->fill_density > 0
  452. && ($i % $self->config->solid_infill_every_layers) == 0) {
  453. $_->surface_type(S_TYPE_INTERNALSOLID) for @{$layerm->fill_surfaces->filter_by_type(S_TYPE_INTERNAL)};
  454. }
  455. EXTERNAL: foreach my $type (S_TYPE_TOP, S_TYPE_BOTTOM) {
  456. # find slices of current type for current layer
  457. # use slices instead of fill_surfaces because they also include the perimeter area
  458. # which needs to be propagated in shells; we need to grow slices like we did for
  459. # fill_surfaces though. Using both ungrown slices and grown fill_surfaces will
  460. # not work in some situations, as there won't be any grown region in the perimeter
  461. # area (this was seen in a model where the top layer had one extra perimeter, thus
  462. # its fill_surfaces were thinner than the lower layer's infill), however it's the best
  463. # solution so far. Growing the external slices by EXTERNAL_INFILL_MARGIN will put
  464. # too much solid infill inside nearly-vertical slopes.
  465. my $solid = [
  466. (map $_->p, @{$layerm->slices->filter_by_type($type)}),
  467. (map $_->p, @{$layerm->fill_surfaces->filter_by_type($type)}),
  468. ];
  469. next if !@$solid;
  470. Slic3r::debugf "Layer %d has %s surfaces\n", $i, ($type == S_TYPE_TOP) ? 'top' : 'bottom';
  471. my $solid_layers = ($type == S_TYPE_TOP)
  472. ? $self->config->top_solid_layers
  473. : $self->config->bottom_solid_layers;
  474. NEIGHBOR: for (my $n = ($type == S_TYPE_TOP) ? $i-1 : $i+1;
  475. abs($n - $i) <= $solid_layers-1;
  476. ($type == S_TYPE_TOP) ? $n-- : $n++) {
  477. next if $n < 0 || $n >= $self->layer_count;
  478. Slic3r::debugf " looking for neighbors on layer %d...\n", $n;
  479. my $neighbor_fill_surfaces = $self->layers->[$n]->regions->[$region_id]->fill_surfaces;
  480. my @neighbor_fill_surfaces = map $_->clone, @$neighbor_fill_surfaces; # clone because we will use these surfaces even after clearing the collection
  481. # find intersection between neighbor and current layer's surfaces
  482. # intersections have contours and holes
  483. # we update $solid so that we limit the next neighbor layer to the areas that were
  484. # found on this one - in other words, solid shells on one layer (for a given external surface)
  485. # are always a subset of the shells found on the previous shell layer
  486. # this approach allows for DWIM in hollow sloping vases, where we want bottom
  487. # shells to be generated in the base but not in the walls (where there are many
  488. # narrow bottom surfaces): reassigning $solid will consider the 'shadow' of the
  489. # upper perimeter as an obstacle and shell will not be propagated to more upper layers
  490. my $new_internal_solid = $solid = intersection(
  491. $solid,
  492. [ map $_->p, grep { ($_->surface_type == S_TYPE_INTERNAL) || ($_->surface_type == S_TYPE_INTERNALSOLID) } @neighbor_fill_surfaces ],
  493. 1,
  494. );
  495. next EXTERNAL if !@$new_internal_solid;
  496. # make sure the new internal solid is wide enough, as it might get collapsed when
  497. # spacing is added in Fill.pm
  498. {
  499. # we use a higher miterLimit here to handle areas with acute angles
  500. # in those cases, the default miterLimit would cut the corner and we'd
  501. # get a triangle in $too_narrow; if we grow it below then the shell
  502. # would have a different shape from the external surface and we'd still
  503. # have the same angle, so the next shell would be grown even more and so on.
  504. my $margin = 3 * $layerm->solid_infill_flow->scaled_width; # require at least this size
  505. my $too_narrow = diff(
  506. $new_internal_solid,
  507. offset2($new_internal_solid, -$margin, +$margin, CLIPPER_OFFSET_SCALE, JT_MITER, 5),
  508. 1,
  509. );
  510. # if some parts are going to collapse, use a different strategy according to fill density
  511. if (@$too_narrow) {
  512. if ($self->config->fill_density > 0) {
  513. # if we have internal infill, grow the collapsing parts and add the extra area to
  514. # the neighbor layer as well as to our original surfaces so that we support this
  515. # additional area in the next shell too
  516. # make sure our grown surfaces don't exceed the fill area
  517. my @grown = @{intersection(
  518. offset($too_narrow, +$margin),
  519. [ map $_->p, @neighbor_fill_surfaces ],
  520. )};
  521. $new_internal_solid = $solid = [ @grown, @$new_internal_solid ];
  522. } else {
  523. # if we're printing a hollow object, we discard such small parts
  524. $new_internal_solid = $solid = diff(
  525. $new_internal_solid,
  526. $too_narrow,
  527. );
  528. }
  529. }
  530. }
  531. # internal-solid are the union of the existing internal-solid surfaces
  532. # and new ones
  533. my $internal_solid = union_ex([
  534. ( map $_->p, grep $_->surface_type == S_TYPE_INTERNALSOLID, @neighbor_fill_surfaces ),
  535. @$new_internal_solid,
  536. ]);
  537. # subtract intersections from layer surfaces to get resulting internal surfaces
  538. my $internal = diff_ex(
  539. [ map $_->p, grep $_->surface_type == S_TYPE_INTERNAL, @neighbor_fill_surfaces ],
  540. [ map @$_, @$internal_solid ],
  541. 1,
  542. );
  543. Slic3r::debugf " %d internal-solid and %d internal surfaces found\n",
  544. scalar(@$internal_solid), scalar(@$internal);
  545. # assign resulting internal surfaces to layer
  546. $neighbor_fill_surfaces->clear;
  547. $neighbor_fill_surfaces->append(map Slic3r::Surface->new
  548. (expolygon => $_, surface_type => S_TYPE_INTERNAL), @$internal);
  549. # assign new internal-solid surfaces to layer
  550. $neighbor_fill_surfaces->append(map Slic3r::Surface->new
  551. (expolygon => $_, surface_type => S_TYPE_INTERNALSOLID), @$internal_solid);
  552. # assign top and bottom surfaces to layer
  553. foreach my $s (Slic3r::Surface->group(grep { ($_->surface_type == S_TYPE_TOP) || ($_->surface_type == S_TYPE_BOTTOM) } @neighbor_fill_surfaces)) {
  554. my $solid_surfaces = diff_ex(
  555. [ map $_->p, @$s ],
  556. [ map @$_, @$internal_solid, @$internal ],
  557. 1,
  558. );
  559. $neighbor_fill_surfaces->append(map $s->[0]->clone(expolygon => $_), @$solid_surfaces);
  560. }
  561. }
  562. }
  563. }
  564. }
  565. }
  566. # combine fill surfaces across layers
  567. sub combine_infill {
  568. my $self = shift;
  569. return unless $self->config->infill_every_layers > 1 && $self->config->fill_density > 0;
  570. my $every = $self->config->infill_every_layers;
  571. my $layer_count = $self->layer_count;
  572. my @layer_heights = map $self->layers->[$_]->height, 0 .. $layer_count-1;
  573. for my $region_id (0 .. ($self->print->regions_count-1)) {
  574. # limit the number of combined layers to the maximum height allowed by this regions' nozzle
  575. my $nozzle_diameter = $self->print->regions->[$region_id]->extruders->{infill}->nozzle_diameter;
  576. # define the combinations
  577. my @combine = (); # layer_id => thickness in layers
  578. {
  579. my $current_height = my $layers = 0;
  580. for my $layer_id (1 .. $#layer_heights) {
  581. my $height = $self->layers->[$layer_id]->height;
  582. if ($current_height + $height >= $nozzle_diameter || $layers >= $every) {
  583. $combine[$layer_id-1] = $layers;
  584. $current_height = $layers = 0;
  585. }
  586. $current_height += $height;
  587. $layers++;
  588. }
  589. }
  590. # skip bottom layer
  591. for my $layer_id (1 .. $#combine) {
  592. next unless ($combine[$layer_id] // 1) > 1;
  593. my @layerms = map $self->layers->[$_]->regions->[$region_id],
  594. ($layer_id - ($combine[$layer_id]-1) .. $layer_id);
  595. # only combine internal infill
  596. for my $type (S_TYPE_INTERNAL) {
  597. # we need to perform a multi-layer intersection, so let's split it in pairs
  598. # initialize the intersection with the candidates of the lowest layer
  599. my $intersection = [ map $_->expolygon, @{$layerms[0]->fill_surfaces->filter_by_type($type)} ];
  600. # start looping from the second layer and intersect the current intersection with it
  601. for my $layerm (@layerms[1 .. $#layerms]) {
  602. $intersection = intersection_ex(
  603. [ map @$_, @$intersection ],
  604. [ map @{$_->expolygon}, @{$layerm->fill_surfaces->filter_by_type($type)} ],
  605. );
  606. }
  607. my $area_threshold = $layerms[0]->infill_area_threshold;
  608. @$intersection = grep $_->area > $area_threshold, @$intersection;
  609. next if !@$intersection;
  610. Slic3r::debugf " combining %d %s regions from layers %d-%d\n",
  611. scalar(@$intersection),
  612. ($type == S_TYPE_INTERNAL ? 'internal' : 'internal-solid'),
  613. $layer_id-($every-1), $layer_id;
  614. # $intersection now contains the regions that can be combined across the full amount of layers
  615. # so let's remove those areas from all layers
  616. my @intersection_with_clearance = map @{$_->offset(
  617. $layerms[-1]->solid_infill_flow->scaled_width / 2
  618. + $layerms[-1]->perimeter_flow->scaled_width / 2
  619. # Because fill areas for rectilinear and honeycomb are grown
  620. # later to overlap perimeters, we need to counteract that too.
  621. + (($type == S_TYPE_INTERNALSOLID || $self->config->fill_pattern =~ /(rectilinear|honeycomb)/)
  622. ? $layerms[-1]->solid_infill_flow->scaled_width * &Slic3r::INFILL_OVERLAP_OVER_SPACING
  623. : 0)
  624. )}, @$intersection;
  625. foreach my $layerm (@layerms) {
  626. my @this_type = @{$layerm->fill_surfaces->filter_by_type($type)};
  627. my @other_types = map $_->clone, grep $_->surface_type != $type, @{$layerm->fill_surfaces};
  628. my @new_this_type = map Slic3r::Surface->new(expolygon => $_, surface_type => $type),
  629. @{diff_ex(
  630. [ map $_->p, @this_type ],
  631. [ @intersection_with_clearance ],
  632. )};
  633. # apply surfaces back with adjusted depth to the uppermost layer
  634. if ($layerm->id == $layer_id) {
  635. push @new_this_type,
  636. map Slic3r::Surface->new(
  637. expolygon => $_,
  638. surface_type => $type,
  639. thickness => sum(map $_->height, @layerms),
  640. thickness_layers => scalar(@layerms),
  641. ),
  642. @$intersection;
  643. } else {
  644. # save void surfaces
  645. push @this_type,
  646. map Slic3r::Surface->new(expolygon => $_, surface_type => S_TYPE_INTERNALVOID),
  647. @{intersection_ex(
  648. [ map @{$_->expolygon}, @this_type ],
  649. [ @intersection_with_clearance ],
  650. )};
  651. }
  652. $layerm->fill_surfaces->clear;
  653. $layerm->fill_surfaces->append(@new_this_type, @other_types);
  654. }
  655. }
  656. }
  657. }
  658. }
  659. sub generate_support_material {
  660. my $self = shift;
  661. return unless ($self->config->support_material || $self->config->raft_layers > 0)
  662. && $self->layer_count >= 2;
  663. Slic3r::Print::SupportMaterial->new(object => $self)->generate;
  664. }
  665. 1;