Object.pm 41 KB

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