Object.pm 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. package Slic3r::Print::Object;
  2. use Moo;
  3. use Slic3r::ExtrusionPath ':roles';
  4. use Slic3r::Geometry qw(Z PI scale unscale deg2rad rad2deg scaled_epsilon);
  5. use Slic3r::Geometry::Clipper qw(diff_ex intersection_ex union_ex);
  6. use Slic3r::Surface ':types';
  7. has 'print' => (is => 'ro', weak_ref => 1, required => 1);
  8. has 'input_file' => (is => 'rw', required => 0);
  9. has 'meshes' => (is => 'rw', default => sub { [] }); # by region_id
  10. has 'size' => (is => 'rw', required => 1);
  11. has 'copies' => (is => 'rw', default => sub {[ [0,0] ]});
  12. has 'layers' => (is => 'rw', default => sub { [] });
  13. sub BUILD {
  14. my $self = shift;
  15. # make layers
  16. while (!@{$self->layers} || $self->layers->[-1]->slice_z < $self->size->[Z]) {
  17. push @{$self->layers}, Slic3r::Layer->new(
  18. object => $self,
  19. id => $#{$self->layers} + 1,
  20. );
  21. }
  22. }
  23. sub layer_count {
  24. my $self = shift;
  25. return scalar @{ $self->layers };
  26. }
  27. sub get_layer_range {
  28. my $self = shift;
  29. my ($min_z, $max_z) = @_;
  30. my ($min_layer, $max_layer) = (0, undef);
  31. for my $layer (@{$self->layers}) {
  32. $min_layer = $layer->id if $layer->slice_z <= $min_z;
  33. if ($layer->slice_z >= $max_z) {
  34. $max_layer = $layer->id;
  35. last;
  36. }
  37. }
  38. return ($min_layer, $max_layer);
  39. }
  40. sub slice {
  41. my $self = shift;
  42. my %params = @_;
  43. # process facets
  44. for my $region_id (0 .. $#{$self->meshes}) {
  45. my $mesh = $self->meshes->[$region_id]; # ignore undef meshes
  46. my $apply_lines = sub {
  47. my $lines = shift;
  48. foreach my $layer_id (keys %$lines) {
  49. my $layerm = $self->layers->[$layer_id]->region($region_id);
  50. push @{$layerm->lines}, @{$lines->{$layer_id}};
  51. }
  52. };
  53. Slic3r::parallelize(
  54. disable => ($#{$mesh->facets} < 500), # don't parallelize when too few facets
  55. items => [ 0..$#{$mesh->facets} ],
  56. thread_cb => sub {
  57. my $q = shift;
  58. my $result_lines = {};
  59. while (defined (my $facet_id = $q->dequeue)) {
  60. my $lines = $mesh->slice_facet($self, $facet_id);
  61. foreach my $layer_id (keys %$lines) {
  62. $result_lines->{$layer_id} ||= [];
  63. push @{ $result_lines->{$layer_id} }, @{ $lines->{$layer_id} };
  64. }
  65. }
  66. return $result_lines;
  67. },
  68. collect_cb => sub {
  69. $apply_lines->($_[0]);
  70. },
  71. no_threads_cb => sub {
  72. for (0..$#{$mesh->facets}) {
  73. my $lines = $mesh->slice_facet($self, $_);
  74. $apply_lines->($lines);
  75. }
  76. },
  77. );
  78. }
  79. die "Invalid input file\n" if !@{$self->layers};
  80. # free memory
  81. $self->meshes(undef) unless $params{keep_meshes};
  82. # remove last layer if empty
  83. # (we might have created it because of the $max_layer = ... + 1 code in TriangleMesh)
  84. pop @{$self->layers} if !map @{$_->lines}, @{$self->layers->[-1]->regions};
  85. foreach my $layer (@{ $self->layers }) {
  86. # make sure all layers contain layer region objects for all regions
  87. $layer->region($_) for 0 .. ($self->print->regions_count-1);
  88. Slic3r::debugf "Making surfaces for layer %d (slice z = %f):\n",
  89. $layer->id, unscale $layer->slice_z if $Slic3r::debug;
  90. # layer currently has many lines representing intersections of
  91. # model facets with the layer plane. there may also be lines
  92. # that we need to ignore (for example, when two non-horizontal
  93. # facets share a common edge on our plane, we get a single line;
  94. # however that line has no meaning for our layer as it's enclosed
  95. # inside a closed polyline)
  96. # build surfaces from sparse lines
  97. foreach my $layerm (@{$layer->regions}) {
  98. my ($slicing_errors, $loops) = Slic3r::TriangleMesh::make_loops($layerm->lines);
  99. $layer->slicing_errors(1) if $slicing_errors;
  100. $layerm->make_surfaces($loops);
  101. # free memory
  102. $layerm->lines(undef);
  103. }
  104. # merge all regions' slices to get islands
  105. $layer->make_slices;
  106. }
  107. # detect slicing errors
  108. my $warning_thrown = 0;
  109. for my $i (0 .. $#{$self->layers}) {
  110. my $layer = $self->layers->[$i];
  111. next unless $layer->slicing_errors;
  112. if (!$warning_thrown) {
  113. warn "The model has overlapping or self-intersecting facets. I tried to repair it, "
  114. . "however you might want to check the results or repair the input file and retry.\n";
  115. $warning_thrown = 1;
  116. }
  117. # try to repair the layer surfaces by merging all contours and all holes from
  118. # neighbor layers
  119. Slic3r::debugf "Attempting to repair layer %d\n", $i;
  120. foreach my $region_id (0 .. $#{$layer->regions}) {
  121. my $layerm = $layer->region($region_id);
  122. my (@upper_surfaces, @lower_surfaces);
  123. for (my $j = $i+1; $j <= $#{$self->layers}; $j++) {
  124. if (!$self->layers->[$j]->slicing_errors) {
  125. @upper_surfaces = @{$self->layers->[$j]->region($region_id)->slices};
  126. last;
  127. }
  128. }
  129. for (my $j = $i-1; $j >= 0; $j--) {
  130. if (!$self->layers->[$j]->slicing_errors) {
  131. @lower_surfaces = @{$self->layers->[$j]->region($region_id)->slices};
  132. last;
  133. }
  134. }
  135. my $union = union_ex([
  136. map $_->expolygon->contour, @upper_surfaces, @lower_surfaces,
  137. ]);
  138. my $diff = diff_ex(
  139. [ map @$_, @$union ],
  140. [ map $_->expolygon->holes, @upper_surfaces, @lower_surfaces, ],
  141. );
  142. @{$layerm->slices} = map Slic3r::Surface->new
  143. (expolygon => $_, surface_type => S_TYPE_INTERNAL),
  144. @$diff;
  145. }
  146. # update layer slices after repairing the single regions
  147. $layer->make_slices;
  148. }
  149. # remove empty layers from bottom
  150. my $first_object_layer_id = $Slic3r::Config->raft_layers;
  151. while (@{$self->layers} && !@{$self->layers->[$first_object_layer_id]->slices} && !map @{$_->thin_walls}, @{$self->layers->[$first_object_layer_id]->regions}) {
  152. splice @{$self->layers}, $first_object_layer_id, 1;
  153. for (my $i = $first_object_layer_id; $i <= $#{$self->layers}; $i++) {
  154. $self->layers->[$i]->id($i);
  155. }
  156. }
  157. warn "No layers were detected. You might want to repair your STL file and retry.\n"
  158. if !@{$self->layers};
  159. }
  160. sub make_perimeters {
  161. my $self = shift;
  162. # compare each layer to the one below, and mark those slices needing
  163. # one additional inner perimeter, like the top of domed objects-
  164. # this algorithm makes sure that almost one perimeter is overlapping
  165. if ($Slic3r::Config->extra_perimeters && $Slic3r::Config->perimeters > 0) {
  166. for my $region_id (0 .. ($self->print->regions_count-1)) {
  167. for my $layer_id (0 .. $self->layer_count-2) {
  168. my $layerm = $self->layers->[$layer_id]->regions->[$region_id];
  169. my $upper_layerm = $self->layers->[$layer_id+1]->regions->[$region_id];
  170. my $perimeter_flow = $layerm->perimeter_flow;
  171. my $overlap = $perimeter_flow->spacing; # one perimeter
  172. # compute polygons representing the thickness of the first external perimeter of
  173. # the upper layer slices
  174. my $upper = diff_ex(
  175. [ map @$_, map $_->expolygon->offset_ex(+ 0.5 * $perimeter_flow->scaled_spacing), @{$upper_layerm->slices} ],
  176. [ map @$_, map $_->expolygon->offset_ex(- scale($overlap) + (0.5 * $perimeter_flow->scaled_spacing)), @{$upper_layerm->slices} ],
  177. );
  178. next if !@$upper;
  179. # we need to limit our detection to the areas which would actually benefit from
  180. # more perimeters. so, let's compute the area we want to ignore
  181. my $ignore = [];
  182. {
  183. my $diff = diff_ex(
  184. [ map @$_, map $_->expolygon->offset_ex(- ($Slic3r::Config->perimeters-0.5) * $perimeter_flow->scaled_spacing), @{$layerm->slices} ],
  185. [ map @{$_->expolygon}, @{$upper_layerm->slices} ],
  186. );
  187. $ignore = [ map @$_, map $_->offset_ex($perimeter_flow->scaled_spacing), @$diff ];
  188. }
  189. foreach my $slice (@{$layerm->slices}) {
  190. my $hypothetical_perimeter_num = $Slic3r::Config->perimeters + 1;
  191. CYCLE: while (1) {
  192. # compute polygons representing the thickness of the hypotetical new internal perimeter
  193. # of our slice
  194. my $hypothetical_perimeter;
  195. {
  196. my $outer = [ map @$_, $slice->expolygon->offset_ex(- ($hypothetical_perimeter_num-1.5) * $perimeter_flow->scaled_spacing - scaled_epsilon) ];
  197. last CYCLE if !@$outer;
  198. my $inner = [ map @$_, $slice->expolygon->offset_ex(- ($hypothetical_perimeter_num-0.5) * $perimeter_flow->scaled_spacing) ];
  199. last CYCLE if !@$inner;
  200. $hypothetical_perimeter = diff_ex($outer, $inner);
  201. }
  202. last CYCLE if !@$hypothetical_perimeter;
  203. my $intersection = intersection_ex([ map @$_, @$upper ], [ map @$_, @$hypothetical_perimeter ]);
  204. $intersection = diff_ex([ map @$_, @$intersection ], $ignore) if @$ignore;
  205. last CYCLE if !@{ $intersection };
  206. Slic3r::debugf " adding one more perimeter at layer %d\n", $layer_id;
  207. $slice->additional_inner_perimeters(($slice->additional_inner_perimeters || 0) + 1);
  208. $hypothetical_perimeter_num++;
  209. }
  210. }
  211. }
  212. }
  213. }
  214. Slic3r::parallelize(
  215. items => sub { 0 .. ($self->layer_count-1) },
  216. thread_cb => sub {
  217. my $q = shift;
  218. $Slic3r::Geometry::Clipper::clipper = Math::Clipper->new;
  219. my $result = {};
  220. while (defined (my $layer_id = $q->dequeue)) {
  221. my $layer = $self->layers->[$layer_id];
  222. $layer->make_perimeters;
  223. $result->{$layer_id} ||= {};
  224. foreach my $region_id (0 .. $#{$layer->regions}) {
  225. my $layerm = $layer->regions->[$region_id];
  226. $result->{$layer_id}{$region_id} = {
  227. perimeters => $layerm->perimeters,
  228. fill_surfaces => $layerm->fill_surfaces,
  229. thin_fills => $layerm->thin_fills,
  230. };
  231. }
  232. }
  233. return $result;
  234. },
  235. collect_cb => sub {
  236. my $result = shift;
  237. foreach my $layer_id (keys %$result) {
  238. foreach my $region_id (keys %{$result->{$layer_id}}) {
  239. $self->layers->[$layer_id]->regions->[$region_id]->$_($result->{$layer_id}{$region_id}{$_})
  240. for qw(perimeters fill_surfaces thin_fills);
  241. }
  242. }
  243. },
  244. no_threads_cb => sub {
  245. $_->make_perimeters for @{$self->layers};
  246. },
  247. );
  248. }
  249. sub detect_surfaces_type {
  250. my $self = shift;
  251. Slic3r::debugf "Detecting solid surfaces...\n";
  252. # prepare a reusable subroutine to make surface differences
  253. my $surface_difference = sub {
  254. my ($subject_surfaces, $clip_surfaces, $result_type, $layerm) = @_;
  255. my $expolygons = diff_ex(
  256. [ map { ref $_ eq 'ARRAY' ? $_ : ref $_ eq 'Slic3r::ExPolygon' ? @$_ : $_->p } @$subject_surfaces ],
  257. [ map { ref $_ eq 'ARRAY' ? $_ : ref $_ eq 'Slic3r::ExPolygon' ? @$_ : $_->p } @$clip_surfaces ],
  258. 1,
  259. );
  260. return grep $_->contour->is_printable($layerm->flow->width),
  261. map Slic3r::Surface->new(expolygon => $_, surface_type => $result_type),
  262. @$expolygons;
  263. };
  264. for my $region_id (0 .. ($self->print->regions_count-1)) {
  265. for (my $i = 0; $i < $self->layer_count; $i++) {
  266. my $layerm = $self->layers->[$i]->regions->[$region_id];
  267. # comparison happens against the *full* slices (considering all regions)
  268. my $upper_layer = $self->layers->[$i+1];
  269. my $lower_layer = $i > 0 ? $self->layers->[$i-1] : undef;
  270. my (@bottom, @top, @internal) = ();
  271. # find top surfaces (difference between current surfaces
  272. # of current layer and upper one)
  273. if ($upper_layer) {
  274. @top = $surface_difference->($layerm->slices, $upper_layer->slices, S_TYPE_TOP, $layerm);
  275. } else {
  276. # if no upper layer, all surfaces of this one are solid
  277. @top = @{$layerm->slices};
  278. $_->surface_type(S_TYPE_TOP) for @top;
  279. }
  280. # find bottom surfaces (difference between current surfaces
  281. # of current layer and lower one)
  282. if ($lower_layer) {
  283. @bottom = $surface_difference->($layerm->slices, $lower_layer->slices, S_TYPE_BOTTOM, $layerm);
  284. } else {
  285. # if no lower layer, all surfaces of this one are solid
  286. @bottom = @{$layerm->slices};
  287. $_->surface_type(S_TYPE_BOTTOM) for @bottom;
  288. }
  289. # now, if the object contained a thin membrane, we could have overlapping bottom
  290. # and top surfaces; let's do an intersection to discover them and consider them
  291. # as bottom surfaces (to allow for bridge detection)
  292. if (@top && @bottom) {
  293. my $overlapping = intersection_ex([ map $_->p, @top ], [ map $_->p, @bottom ]);
  294. Slic3r::debugf " layer %d contains %d membrane(s)\n", $layerm->id, scalar(@$overlapping);
  295. @top = $surface_difference->([@top], $overlapping, S_TYPE_TOP, $layerm);
  296. }
  297. # find internal surfaces (difference between top/bottom surfaces and others)
  298. @internal = $surface_difference->($layerm->slices, [@top, @bottom], S_TYPE_INTERNAL, $layerm);
  299. # save surfaces to layer
  300. @{$layerm->slices} = (@bottom, @top, @internal);
  301. Slic3r::debugf " layer %d has %d bottom, %d top and %d internal surfaces\n",
  302. $layerm->id, scalar(@bottom), scalar(@top), scalar(@internal);
  303. }
  304. # clip surfaces to the fill boundaries
  305. foreach my $layer (@{$self->layers}) {
  306. my $layerm = $layer->regions->[$region_id];
  307. my $fill_boundaries = [ map @$_, @{$layerm->fill_surfaces} ];
  308. @{$layerm->fill_surfaces} = ();
  309. foreach my $surface (@{$layerm->slices}) {
  310. my $intersection = intersection_ex(
  311. [ $surface->p ],
  312. $fill_boundaries,
  313. );
  314. push @{$layerm->fill_surfaces}, map Slic3r::Surface->new
  315. (expolygon => $_, surface_type => $surface->surface_type),
  316. @$intersection;
  317. }
  318. }
  319. }
  320. }
  321. sub discover_horizontal_shells {
  322. my $self = shift;
  323. Slic3r::debugf "==> DISCOVERING HORIZONTAL SHELLS\n";
  324. my $area_threshold = $Slic3r::flow->scaled_spacing ** 2;
  325. for my $region_id (0 .. ($self->print->regions_count-1)) {
  326. for (my $i = 0; $i < $self->layer_count; $i++) {
  327. my $layerm = $self->layers->[$i]->regions->[$region_id];
  328. if ($Slic3r::Config->solid_infill_every_layers && ($i % $Slic3r::Config->solid_infill_every_layers) == 0) {
  329. $_->surface_type(S_TYPE_INTERNALSOLID)
  330. for grep $_->surface_type == S_TYPE_INTERNAL, @{$layerm->fill_surfaces};
  331. }
  332. foreach my $type (S_TYPE_TOP, S_TYPE_BOTTOM) {
  333. # find slices of current type for current layer
  334. my @surfaces = grep $_->surface_type == $type, @{$layerm->slices} or next;
  335. my $surfaces_p = [ map $_->p, @surfaces ];
  336. Slic3r::debugf "Layer %d has %d surfaces of type '%s'\n",
  337. $i, scalar(@surfaces), ($type == S_TYPE_TOP ? 'top' : 'bottom');
  338. my $solid_layers = ($type == S_TYPE_TOP)
  339. ? $Slic3r::Config->top_solid_layers
  340. : $Slic3r::Config->bottom_solid_layers;
  341. for (my $n = $type == S_TYPE_TOP ? $i-1 : $i+1;
  342. abs($n - $i) <= $solid_layers-1;
  343. $type == S_TYPE_TOP ? $n-- : $n++) {
  344. next if $n < 0 || $n >= $self->layer_count;
  345. Slic3r::debugf " looking for neighbors on layer %d...\n", $n;
  346. my @neighbor_fill_surfaces = @{$self->layers->[$n]->regions->[$region_id]->fill_surfaces};
  347. # find intersection between neighbor and current layer's surfaces
  348. # intersections have contours and holes
  349. my $new_internal_solid = intersection_ex(
  350. $surfaces_p,
  351. [ map $_->p, grep { $_->surface_type == S_TYPE_INTERNAL || $_->surface_type == S_TYPE_INTERNALSOLID } @neighbor_fill_surfaces ],
  352. undef, 1,
  353. );
  354. next if !@$new_internal_solid;
  355. # internal-solid are the union of the existing internal-solid surfaces
  356. # and new ones
  357. my $internal_solid = union_ex([
  358. ( map $_->p, grep $_->surface_type == S_TYPE_INTERNALSOLID, @neighbor_fill_surfaces ),
  359. ( map @$_, @$new_internal_solid ),
  360. ]);
  361. # subtract intersections from layer surfaces to get resulting inner surfaces
  362. my $internal = diff_ex(
  363. [ map $_->p, grep $_->surface_type == S_TYPE_INTERNAL, @neighbor_fill_surfaces ],
  364. [ map @$_, @$internal_solid ],
  365. 1,
  366. );
  367. Slic3r::debugf " %d internal-solid and %d internal surfaces found\n",
  368. scalar(@$internal_solid), scalar(@$internal);
  369. # Note: due to floating point math we're going to get some very small
  370. # polygons as $internal; they will be removed by removed_small_features()
  371. # assign resulting inner surfaces to layer
  372. my $neighbor_fill_surfaces = $self->layers->[$n]->regions->[$region_id]->fill_surfaces;
  373. @$neighbor_fill_surfaces = ();
  374. push @$neighbor_fill_surfaces, Slic3r::Surface->new
  375. (expolygon => $_, surface_type => S_TYPE_INTERNAL)
  376. for @$internal;
  377. # assign new internal-solid surfaces to layer
  378. push @$neighbor_fill_surfaces, Slic3r::Surface->new
  379. (expolygon => $_, surface_type => S_TYPE_INTERNALSOLID)
  380. for @$internal_solid;
  381. # assign top and bottom surfaces to layer
  382. foreach my $s (Slic3r::Surface->group(grep { $_->surface_type == S_TYPE_TOP || $_->surface_type == S_TYPE_BOTTOM } @neighbor_fill_surfaces)) {
  383. my $solid_surfaces = diff_ex(
  384. [ map $_->p, @$s ],
  385. [ map @$_, @$internal_solid, @$internal ],
  386. 1,
  387. );
  388. push @$neighbor_fill_surfaces, Slic3r::Surface->new
  389. (expolygon => $_, surface_type => $s->[0]->surface_type, bridge_angle => $s->[0]->bridge_angle)
  390. for @$solid_surfaces;
  391. }
  392. }
  393. }
  394. @{$layerm->fill_surfaces} = grep $_->expolygon->area > $area_threshold, @{$layerm->fill_surfaces};
  395. }
  396. for (my $i = 0; $i < $self->layer_count; $i++) {
  397. my $layerm = $self->layers->[$i]->regions->[$region_id];
  398. # if hollow object is requested, remove internal surfaces
  399. if ($Slic3r::Config->fill_density == 0) {
  400. @{$layerm->fill_surfaces} = grep $_->surface_type != S_TYPE_INTERNAL, @{$layerm->fill_surfaces};
  401. }
  402. }
  403. }
  404. }
  405. # combine fill surfaces across layers
  406. sub combine_infill {
  407. my $self = shift;
  408. return unless $Slic3r::Config->infill_every_layers > 1 && $Slic3r::Config->fill_density > 0;
  409. my $area_threshold = $Slic3r::flow->scaled_spacing ** 2;
  410. for my $region_id (0 .. ($self->print->regions_count-1)) {
  411. # start from top, skip lowest layer
  412. for (my $i = $self->layer_count - 1; $i > 0; $i--) {
  413. my $layerm = $self->layers->[$i]->regions->[$region_id];
  414. # skip layer if no internal fill surfaces
  415. next if !grep $_->surface_type == S_TYPE_INTERNAL, @{$layerm->fill_surfaces};
  416. # for each possible depth, look for intersections with the lower layer
  417. # we do this from the greater depth to the smaller
  418. for (my $d = $Slic3r::Config->infill_every_layers - 1; $d >= 1; $d--) {
  419. next if ($i - $d) <= 0; # do not combine infill for bottom layer
  420. my $lower_layerm = $self->layers->[$i - 1]->regions->[$region_id];
  421. # select surfaces of the lower layer having the depth we're looking for
  422. my @lower_surfaces = grep $_->depth_layers == $d && $_->surface_type == S_TYPE_INTERNAL,
  423. @{$lower_layerm->fill_surfaces};
  424. next if !@lower_surfaces;
  425. # calculate intersection between our surfaces and theirs
  426. my $intersection = intersection_ex(
  427. [ map $_->p, grep $_->depth_layers <= $d, @lower_surfaces ],
  428. [ map $_->p, grep $_->surface_type == S_TYPE_INTERNAL, @{$layerm->fill_surfaces} ],
  429. undef, 1,
  430. );
  431. # purge intersections, skip tiny regions
  432. @$intersection = grep $_->area > $area_threshold, @$intersection;
  433. next if !@$intersection;
  434. # new fill surfaces of the current layer are:
  435. # - any non-internal surface
  436. # - intersections found (with a $d + 1 depth)
  437. # - any internal surface not belonging to the intersection (with its original depth)
  438. {
  439. my @new_surfaces = ();
  440. push @new_surfaces, grep $_->surface_type != S_TYPE_INTERNAL, @{$layerm->fill_surfaces};
  441. push @new_surfaces, map Slic3r::Surface->new
  442. (expolygon => $_, surface_type => S_TYPE_INTERNAL, depth_layers => $d + 1), @$intersection;
  443. foreach my $depth (reverse $d..$Slic3r::Config->infill_every_layers) {
  444. push @new_surfaces, map Slic3r::Surface->new
  445. (expolygon => $_, surface_type => S_TYPE_INTERNAL, depth_layers => $depth),
  446. # difference between our internal layers with depth == $depth
  447. # and the intersection found
  448. @{diff_ex(
  449. [
  450. map $_->p, grep $_->surface_type == S_TYPE_INTERNAL && $_->depth_layers == $depth,
  451. @{$layerm->fill_surfaces},
  452. ],
  453. [ map @$_, @$intersection ],
  454. 1,
  455. )};
  456. }
  457. @{$layerm->fill_surfaces} = @new_surfaces;
  458. }
  459. # now we remove the intersections from lower layer
  460. {
  461. my @new_surfaces = ();
  462. push @new_surfaces, grep $_->surface_type != S_TYPE_INTERNAL, @{$lower_layerm->fill_surfaces};
  463. # offset for the two different flow spacings
  464. $intersection = [ map $_->offset_ex(
  465. $lower_layerm->infill_flow->scaled_spacing / 2
  466. + $layerm->infill_flow->scaled_spacing / 2
  467. ), @$intersection];
  468. foreach my $depth (1..$Slic3r::Config->infill_every_layers) {
  469. push @new_surfaces, map Slic3r::Surface->new
  470. (expolygon => $_, surface_type => S_TYPE_INTERNAL, depth_layers => $depth),
  471. # difference between internal layers with depth == $depth
  472. # and the intersection found
  473. @{diff_ex(
  474. [
  475. map $_->p, grep $_->surface_type == S_TYPE_INTERNAL && $_->depth_layers == $depth,
  476. @{$lower_layerm->fill_surfaces},
  477. ],
  478. [ map @$_, @$intersection ],
  479. 1,
  480. )};
  481. }
  482. @{$lower_layerm->fill_surfaces} = @new_surfaces;
  483. }
  484. }
  485. }
  486. }
  487. }
  488. sub generate_support_material {
  489. my $self = shift;
  490. my $threshold_rad = $Slic3r::Config->support_material_threshold
  491. ? deg2rad($Slic3r::Config->support_material_threshold + 1) # +1 makes the threshold inclusive
  492. : PI/2 - atan2($self->layers->[1]->regions->[0]->perimeter_flow->width/$Slic3r::Config->layer_height/2, 1);
  493. Slic3r::debugf "Threshold angle = %d°\n", rad2deg($threshold_rad);
  494. my $flow = $self->print->support_material_flow;
  495. my $overhang_width = $threshold_rad == 0 ? undef : scale $Slic3r::Config->layer_height * ((cos $threshold_rad) / (sin $threshold_rad));
  496. my $distance_from_object = 1.5 * $flow->scaled_width;
  497. my $pattern_spacing = ($Slic3r::Config->support_material_spacing > $flow->spacing)
  498. ? $Slic3r::Config->support_material_spacing
  499. : $flow->spacing;
  500. # determine support regions in each layer (for upper layers)
  501. Slic3r::debugf "Detecting regions\n";
  502. my %layers = (); # this represents the areas of each layer having to support upper layers (excluding interfaces)
  503. my %layers_interfaces = (); # this represents the areas of each layer to be filled with interface pattern, excluding the contact areas which are stored separately
  504. my %layers_contact_areas = (); # this represents the areas of each layer having an overhang in the immediately upper layer
  505. {
  506. my @current_support_regions = (); # expolygons we've started to support (i.e. below the empty interface layers)
  507. my @upper_layers_overhangs = (map [], 1..$Slic3r::Config->support_material_interface_layers);
  508. for my $i (reverse 0 .. $#{$self->layers}) {
  509. next unless $Slic3r::Config->support_material
  510. || ($i <= $Slic3r::Config->raft_layers) # <= because we need to start from the first non-raft layer
  511. || ($i <= $Slic3r::Config->support_material_enforce_layers + $Slic3r::Config->raft_layers);
  512. my $layer = $self->layers->[$i];
  513. my $lower_layer = $i > 0 ? $self->layers->[$i-1] : undef;
  514. my @current_layer_offsetted_slices = map $_->offset_ex($distance_from_object), @{$layer->slices};
  515. # $upper_layers_overhangs[-1] contains the overhangs of the upper layer, regardless of any interface layers
  516. # $upper_layers_overhangs[0] contains the overhangs of the first upper layer above the interface layers
  517. # we only consider the overhangs of the upper layer to define contact areas of the current one
  518. $layers_contact_areas{$i} = diff_ex(
  519. [ map @$_, @{ $upper_layers_overhangs[-1] || [] } ],
  520. [ map @$_, @current_layer_offsetted_slices ],
  521. );
  522. $_->simplify($flow->scaled_spacing) for @{$layers_contact_areas{$i}};
  523. # to define interface regions of this layer we consider the overhangs of all the upper layers
  524. # minus the first one
  525. $layers_interfaces{$i} = diff_ex(
  526. [ map @$_, map @$_, @upper_layers_overhangs[0 .. $#upper_layers_overhangs-1] ],
  527. [
  528. (map @$_, @current_layer_offsetted_slices),
  529. (map @$_, @{ $layers_contact_areas{$i} }),
  530. ],
  531. );
  532. $_->simplify($flow->scaled_spacing) for @{$layers_interfaces{$i}};
  533. # generate support material in current layer (for upper layers)
  534. @current_support_regions = @{diff_ex(
  535. [
  536. (map @$_, @current_support_regions),
  537. (map @$_, @{ $upper_layers_overhangs[-1] || [] }), # only considering -1 instead of the whole array contents is just an optimization
  538. ],
  539. [ map @$_, @{$layer->slices} ],
  540. )};
  541. shift @upper_layers_overhangs;
  542. $layers{$i} = diff_ex(
  543. [ map @$_, @current_support_regions ],
  544. [
  545. (map @$_, @current_layer_offsetted_slices),
  546. (map @$_, @{ $layers_interfaces{$i} }),
  547. ],
  548. );
  549. $_->simplify($flow->scaled_spacing) for @{$layers{$i}};
  550. # get layer overhangs and put them into queue for adding support inside lower layers;
  551. # we need an angle threshold for this
  552. my @overhangs = ();
  553. if ($lower_layer) {
  554. # consider all overhangs regardless of their angle if we're told to enforce support on this layer
  555. my $distance = $i <= ($Slic3r::Config->support_material_enforce_layers + $Slic3r::Config->raft_layers)
  556. ? 0
  557. : $overhang_width;
  558. @overhangs = map $_->offset_ex(2 * $overhang_width), @{diff_ex(
  559. [ map @$_, map $_->offset_ex(-$overhang_width), @{$layer->slices} ],
  560. [ map @$_, @{$lower_layer->slices} ],
  561. 1,
  562. )};
  563. }
  564. push @upper_layers_overhangs, [@overhangs];
  565. if ($Slic3r::debug) {
  566. printf "Layer %d (z = %.2f) has %d generic support areas, %d normal interface areas, %d contact areas\n",
  567. $i, unscale($layer->print_z), scalar(@{$layers{$i}}), scalar(@{$layers_interfaces{$i}}), scalar(@{$layers_contact_areas{$i}});
  568. }
  569. }
  570. }
  571. return if !map @$_, values %layers;
  572. # generate paths for the pattern that we're going to use
  573. Slic3r::debugf "Generating patterns\n";
  574. my $support_patterns = [];
  575. my $support_interface_patterns = [];
  576. {
  577. # 0.5 ensures the paths don't get clipped externally when applying them to layers
  578. my @areas = map $_->offset_ex(- 0.5 * $flow->scaled_width),
  579. @{union_ex([ map $_->contour, map @$_, values %layers ])};
  580. my $pattern = $Slic3r::Config->support_material_pattern;
  581. my @angles = ($Slic3r::Config->support_material_angle);
  582. if ($pattern eq 'rectilinear-grid') {
  583. $pattern = 'rectilinear';
  584. push @angles, $angles[0] + 90;
  585. }
  586. my $filler = Slic3r::Fill->filler($pattern);
  587. my $make_pattern = sub {
  588. my ($expolygon, $density) = @_;
  589. my @paths = $filler->fill_surface(
  590. Slic3r::Surface->new(expolygon => $expolygon),
  591. density => $density,
  592. flow_spacing => $flow->spacing,
  593. );
  594. my $params = shift @paths;
  595. return map Slic3r::ExtrusionPath->new(
  596. polyline => Slic3r::Polyline->new(@$_),
  597. role => EXTR_ROLE_SUPPORTMATERIAL,
  598. height => undef,
  599. flow_spacing => $params->{flow_spacing},
  600. ), @paths;
  601. };
  602. foreach my $angle (@angles) {
  603. $filler->angle($angle);
  604. {
  605. my $density = $flow->spacing / $pattern_spacing;
  606. push @$support_patterns, [ map $make_pattern->($_, $density), @areas ];
  607. }
  608. if ($Slic3r::Config->support_material_interface_layers > 0) {
  609. # if pattern is not cross-hatched, rotate the interface pattern by 90° degrees
  610. $filler->angle($angle + 90) if @angles == 1;
  611. my $spacing = $Slic3r::Config->support_material_interface_spacing;
  612. my $density = $spacing == 0 ? 1 : $flow->spacing / $spacing;
  613. push @$support_interface_patterns, [ map $make_pattern->($_, $density), @areas ];
  614. }
  615. }
  616. if (0) {
  617. require "Slic3r/SVG.pm";
  618. Slic3r::SVG::output("support_$_.svg",
  619. polylines => [ map $_->polyline, map @$_, $support_patterns->[$_] ],
  620. red_polylines => [ map $_->polyline, map @$_, $support_interface_patterns->[$_] ],
  621. polygons => [ map @$_, @areas ],
  622. ) for 0 .. $#$support_patterns;
  623. }
  624. }
  625. # apply the pattern to layers
  626. Slic3r::debugf "Applying patterns\n";
  627. {
  628. my $clip_pattern = sub {
  629. my ($layer_id, $expolygons, $height, $is_interface) = @_;
  630. my @paths = ();
  631. foreach my $expolygon (@$expolygons) {
  632. push @paths,
  633. map $_->pack,
  634. map {
  635. $_->height($height);
  636. # useless line because this coderef isn't called for layer 0 anymore;
  637. # let's keep it here just in case we want to make the base flange optional
  638. # in the future
  639. $_->flow_spacing($self->print->first_layer_support_material_flow->spacing)
  640. if $layer_id == 0;
  641. $_;
  642. }
  643. map $_->clip_with_expolygon($expolygon),
  644. ###map $_->clip_with_polygon($expolygon->bounding_box_polygon), # currently disabled as a workaround for Boost failing at being idempotent
  645. ($is_interface && @$support_interface_patterns)
  646. ? @{$support_interface_patterns->[ $layer_id % @$support_interface_patterns ]}
  647. : @{$support_patterns->[ $layer_id % @$support_patterns ]};
  648. };
  649. return @paths;
  650. };
  651. my %layer_paths = ();
  652. my %layer_contact_paths = ();
  653. my %layer_islands = ();
  654. my $process_layer = sub {
  655. my ($layer_id) = @_;
  656. my $layer = $self->layers->[$layer_id];
  657. my ($paths, $contact_paths) = ([], []);
  658. my $islands = union_ex([ map @$_, map @$_, $layers{$layer_id}, $layers_contact_areas{$layer_id} ]);
  659. # make a solid base on bottom layer
  660. if ($layer_id == 0) {
  661. my $filler = Slic3r::Fill->filler('rectilinear');
  662. $filler->angle($Slic3r::Config->support_material_angle + 90);
  663. foreach my $expolygon (@$islands) {
  664. my @paths = $filler->fill_surface(
  665. Slic3r::Surface->new(expolygon => $expolygon),
  666. density => 0.5,
  667. flow_spacing => $self->print->first_layer_support_material_flow->spacing,
  668. );
  669. my $params = shift @paths;
  670. push @$paths, map Slic3r::ExtrusionPath->new(
  671. polyline => Slic3r::Polyline->new(@$_),
  672. role => EXTR_ROLE_SUPPORTMATERIAL,
  673. height => undef,
  674. flow_spacing => $params->{flow_spacing},
  675. ), @paths;
  676. }
  677. } else {
  678. $paths = [
  679. $clip_pattern->($layer_id, $layers{$layer_id}, $layer->height),
  680. $clip_pattern->($layer_id, $layers_interfaces{$layer_id}, $layer->height, 1),
  681. ];
  682. $contact_paths = [ $clip_pattern->($layer_id, $layers_contact_areas{$layer_id}, $layer->support_material_contact_height, 1) ];
  683. }
  684. return ($paths, $contact_paths, $islands);
  685. };
  686. Slic3r::parallelize(
  687. items => [ keys %layers ],
  688. thread_cb => sub {
  689. my $q = shift;
  690. $Slic3r::Geometry::Clipper::clipper = Math::Clipper->new;
  691. my $result = {};
  692. while (defined (my $layer_id = $q->dequeue)) {
  693. $result->{$layer_id} = [ $process_layer->($layer_id) ];
  694. }
  695. return $result;
  696. },
  697. collect_cb => sub {
  698. my $result = shift;
  699. ($layer_paths{$_}, $layer_contact_paths{$_}, $layer_islands{$_}) = @{$result->{$_}} for keys %$result;
  700. },
  701. no_threads_cb => sub {
  702. ($layer_paths{$_}, $layer_contact_paths{$_}, $layer_islands{$_}) = $process_layer->($_) for keys %layers;
  703. },
  704. );
  705. foreach my $layer_id (keys %layer_paths) {
  706. my $layer = $self->layers->[$layer_id];
  707. $layer->support_islands($layer_islands{$layer_id});
  708. $layer->support_fills(Slic3r::ExtrusionPath::Collection->new);
  709. $layer->support_contact_fills(Slic3r::ExtrusionPath::Collection->new);
  710. push @{$layer->support_fills->paths}, @{$layer_paths{$layer_id}};
  711. push @{$layer->support_contact_fills->paths}, @{$layer_contact_paths{$layer_id}};
  712. }
  713. }
  714. }
  715. 1;