Object.pm 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. package Slic3r::Print::Object;
  2. use Moo;
  3. use Slic3r::ExtrusionPath ':roles';
  4. use Slic3r::Geometry qw(scale unscale deg2rad 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 layer_count {
  14. my $self = shift;
  15. return scalar @{ $self->layers };
  16. }
  17. sub layer {
  18. my $self = shift;
  19. my ($layer_id) = @_;
  20. # extend our print by creating all necessary layers
  21. for (my $i = $self->layer_count; $i <= $layer_id; $i++) {
  22. push @{ $self->layers }, Slic3r::Layer->new(id => $i, object => $self);
  23. }
  24. return $self->layers->[$layer_id];
  25. }
  26. sub slice {
  27. my $self = shift;
  28. my %params = @_;
  29. # process facets
  30. for my $region_id (0 .. $#{$self->meshes}) {
  31. my $mesh = $self->meshes->[$region_id]; # ignore undef meshes
  32. my $apply_lines = sub {
  33. my $lines = shift;
  34. foreach my $layer_id (keys %$lines) {
  35. my $layerm = $self->layer($layer_id)->region($region_id);
  36. push @{$layerm->lines}, @{$lines->{$layer_id}};
  37. }
  38. };
  39. Slic3r::parallelize(
  40. disable => ($#{$mesh->facets} < 500), # don't parallelize when too few facets
  41. items => [ 0..$#{$mesh->facets} ],
  42. thread_cb => sub {
  43. my $q = shift;
  44. my $result_lines = {};
  45. while (defined (my $facet_id = $q->dequeue)) {
  46. my $lines = $mesh->slice_facet($self, $facet_id);
  47. foreach my $layer_id (keys %$lines) {
  48. $result_lines->{$layer_id} ||= [];
  49. push @{ $result_lines->{$layer_id} }, @{ $lines->{$layer_id} };
  50. }
  51. }
  52. return $result_lines;
  53. },
  54. collect_cb => sub {
  55. $apply_lines->($_[0]);
  56. },
  57. no_threads_cb => sub {
  58. for (0..$#{$mesh->facets}) {
  59. my $lines = $mesh->slice_facet($self, $_);
  60. $apply_lines->($lines);
  61. }
  62. },
  63. );
  64. }
  65. die "Invalid input file\n" if !@{$self->layers};
  66. # free memory
  67. $self->meshes(undef) unless $params{keep_meshes};
  68. # remove last layer if empty
  69. # (we might have created it because of the $max_layer = ... + 1 code in TriangleMesh)
  70. pop @{$self->layers} if !map @{$_->lines}, @{$self->layers->[-1]->regions};
  71. foreach my $layer (@{ $self->layers }) {
  72. # make sure all layers contain layer region objects for all regions
  73. $layer->region($_) for 0 .. ($self->print->regions_count-1);
  74. Slic3r::debugf "Making surfaces for layer %d (slice z = %f):\n",
  75. $layer->id, unscale $layer->slice_z if $Slic3r::debug;
  76. # layer currently has many lines representing intersections of
  77. # model facets with the layer plane. there may also be lines
  78. # that we need to ignore (for example, when two non-horizontal
  79. # facets share a common edge on our plane, we get a single line;
  80. # however that line has no meaning for our layer as it's enclosed
  81. # inside a closed polyline)
  82. # build surfaces from sparse lines
  83. foreach my $layerm (@{$layer->regions}) {
  84. my ($slicing_errors, $loops) = Slic3r::TriangleMesh::make_loops($layerm->lines);
  85. $layer->slicing_errors(1) if $slicing_errors;
  86. $layerm->make_surfaces($loops);
  87. # free memory
  88. $layerm->lines(undef);
  89. }
  90. # merge all regions' slices to get islands
  91. $layer->make_slices;
  92. }
  93. # detect slicing errors
  94. my $warning_thrown = 0;
  95. for my $i (0 .. $#{$self->layers}) {
  96. my $layer = $self->layers->[$i];
  97. next unless $layer->slicing_errors;
  98. if (!$warning_thrown) {
  99. warn "The model has overlapping or self-intersecting facets. I tried to repair it, "
  100. . "however you might want to check the results or repair the input file and retry.\n";
  101. $warning_thrown = 1;
  102. }
  103. # try to repair the layer surfaces by merging all contours and all holes from
  104. # neighbor layers
  105. Slic3r::debugf "Attempting to repair layer %d\n", $i;
  106. foreach my $region_id (0 .. $#{$layer->regions}) {
  107. my $layerm = $layer->region($region_id);
  108. my (@upper_surfaces, @lower_surfaces);
  109. for (my $j = $i+1; $j <= $#{$self->layers}; $j++) {
  110. if (!$self->layers->[$j]->slicing_errors) {
  111. @upper_surfaces = @{$self->layers->[$j]->region($region_id)->slices};
  112. last;
  113. }
  114. }
  115. for (my $j = $i-1; $j >= 0; $j--) {
  116. if (!$self->layers->[$j]->slicing_errors) {
  117. @lower_surfaces = @{$self->layers->[$j]->region($region_id)->slices};
  118. last;
  119. }
  120. }
  121. my $union = union_ex([
  122. map $_->expolygon->contour, @upper_surfaces, @lower_surfaces,
  123. ]);
  124. my $diff = diff_ex(
  125. [ map @$_, @$union ],
  126. [ map $_->expolygon->holes, @upper_surfaces, @lower_surfaces, ],
  127. );
  128. @{$layerm->slices} = map Slic3r::Surface->new
  129. (expolygon => $_, surface_type => S_TYPE_INTERNAL),
  130. @$diff;
  131. }
  132. # update layer slices after repairing the single regions
  133. $layer->make_slices;
  134. }
  135. # remove empty layers from bottom
  136. while (@{$self->layers} && !@{$self->layers->[0]->slices} && !map @{$_->thin_walls}, @{$self->layers->[0]->regions}) {
  137. shift @{$self->layers};
  138. for (my $i = 0; $i <= $#{$self->layers}; $i++) {
  139. $self->layers->[$i]->id($i);
  140. }
  141. }
  142. warn "No layers were detected. You might want to repair your STL file and retry.\n"
  143. if !@{$self->layers};
  144. }
  145. sub make_perimeters {
  146. my $self = shift;
  147. # compare each layer to the one below, and mark those slices needing
  148. # one additional inner perimeter, like the top of domed objects-
  149. # this algorithm makes sure that almost one perimeter is overlapping
  150. if ($Slic3r::Config->extra_perimeters && $Slic3r::Config->perimeters > 0) {
  151. for my $region_id (0 .. ($self->print->regions_count-1)) {
  152. for my $layer_id (0 .. $self->layer_count-2) {
  153. my $layerm = $self->layers->[$layer_id]->regions->[$region_id];
  154. my $upper_layerm = $self->layers->[$layer_id+1]->regions->[$region_id];
  155. my $perimeter_flow = $layerm->perimeter_flow;
  156. my $overlap = $perimeter_flow->spacing; # one perimeter
  157. # compute polygons representing the thickness of the first external perimeter of
  158. # the upper layer slices
  159. my $upper = diff_ex(
  160. [ map @$_, map $_->expolygon->offset_ex(+ 0.5 * $perimeter_flow->scaled_spacing), @{$upper_layerm->slices} ],
  161. [ map @$_, map $_->expolygon->offset_ex(- scale($overlap) + (0.5 * $perimeter_flow->scaled_spacing)), @{$upper_layerm->slices} ],
  162. );
  163. next if !@$upper;
  164. # we need to limit our detection to the areas which would actually benefit from
  165. # more perimeters. so, let's compute the area we want to ignore
  166. my $ignore = [];
  167. {
  168. my $diff = diff_ex(
  169. [ map @$_, map $_->expolygon->offset_ex(- ($Slic3r::Config->perimeters-0.5) * $perimeter_flow->scaled_spacing), @{$layerm->slices} ],
  170. [ map @{$_->expolygon}, @{$upper_layerm->slices} ],
  171. );
  172. $ignore = [ map @$_, map $_->offset_ex($perimeter_flow->scaled_spacing), @$diff ];
  173. }
  174. foreach my $slice (@{$layerm->slices}) {
  175. my $hypothetical_perimeter_num = $Slic3r::Config->perimeters + 1;
  176. CYCLE: while (1) {
  177. # compute polygons representing the thickness of the hypotetical new internal perimeter
  178. # of our slice
  179. my $hypothetical_perimeter;
  180. {
  181. my $outer = [ map @$_, $slice->expolygon->offset_ex(- ($hypothetical_perimeter_num-1.5) * $perimeter_flow->scaled_spacing - scaled_epsilon) ];
  182. last CYCLE if !@$outer;
  183. my $inner = [ map @$_, $slice->expolygon->offset_ex(- ($hypothetical_perimeter_num-0.5) * $perimeter_flow->scaled_spacing) ];
  184. last CYCLE if !@$inner;
  185. $hypothetical_perimeter = diff_ex($outer, $inner);
  186. }
  187. last CYCLE if !@$hypothetical_perimeter;
  188. my $intersection = intersection_ex([ map @$_, @$upper ], [ map @$_, @$hypothetical_perimeter ]);
  189. $intersection = diff_ex([ map @$_, @$intersection ], $ignore) if @$ignore;
  190. last CYCLE if !@{ $intersection };
  191. Slic3r::debugf " adding one more perimeter at layer %d\n", $layer_id;
  192. $slice->additional_inner_perimeters(($slice->additional_inner_perimeters || 0) + 1);
  193. $hypothetical_perimeter_num++;
  194. }
  195. }
  196. }
  197. }
  198. }
  199. $_->make_perimeters for @{$self->layers};
  200. }
  201. sub detect_surfaces_type {
  202. my $self = shift;
  203. Slic3r::debugf "Detecting solid surfaces...\n";
  204. # prepare a reusable subroutine to make surface differences
  205. my $surface_difference = sub {
  206. my ($subject_surfaces, $clip_surfaces, $result_type, $layerm) = @_;
  207. my $expolygons = diff_ex(
  208. [ map { ref $_ eq 'ARRAY' ? $_ : ref $_ eq 'Slic3r::ExPolygon' ? @$_ : $_->p } @$subject_surfaces ],
  209. [ map { ref $_ eq 'ARRAY' ? $_ : ref $_ eq 'Slic3r::ExPolygon' ? @$_ : $_->p } @$clip_surfaces ],
  210. 1,
  211. );
  212. return grep $_->contour->is_printable($layerm->flow->width),
  213. map Slic3r::Surface->new(expolygon => $_, surface_type => $result_type),
  214. @$expolygons;
  215. };
  216. for my $region_id (0 .. ($self->print->regions_count-1)) {
  217. for (my $i = 0; $i < $self->layer_count; $i++) {
  218. my $layerm = $self->layers->[$i]->regions->[$region_id];
  219. # comparison happens against the *full* slices (considering all regions)
  220. my $upper_layer = $self->layers->[$i+1];
  221. my $lower_layer = $i > 0 ? $self->layers->[$i-1] : undef;
  222. my (@bottom, @top, @internal) = ();
  223. # find top surfaces (difference between current surfaces
  224. # of current layer and upper one)
  225. if ($upper_layer) {
  226. @top = $surface_difference->($layerm->slices, $upper_layer->slices, S_TYPE_TOP, $layerm);
  227. } else {
  228. # if no upper layer, all surfaces of this one are solid
  229. @top = @{$layerm->slices};
  230. $_->surface_type(S_TYPE_TOP) for @top;
  231. }
  232. # find bottom surfaces (difference between current surfaces
  233. # of current layer and lower one)
  234. if ($lower_layer) {
  235. @bottom = $surface_difference->($layerm->slices, $lower_layer->slices, S_TYPE_BOTTOM, $layerm);
  236. } else {
  237. # if no lower layer, all surfaces of this one are solid
  238. @bottom = @{$layerm->slices};
  239. $_->surface_type(S_TYPE_BOTTOM) for @bottom;
  240. }
  241. # now, if the object contained a thin membrane, we could have overlapping bottom
  242. # and top surfaces; let's do an intersection to discover them and consider them
  243. # as bottom surfaces (to allow for bridge detection)
  244. if (@top && @bottom) {
  245. my $overlapping = intersection_ex([ map $_->p, @top ], [ map $_->p, @bottom ]);
  246. Slic3r::debugf " layer %d contains %d membrane(s)\n", $layerm->id, scalar(@$overlapping);
  247. @top = $surface_difference->([@top], $overlapping, S_TYPE_TOP, $layerm);
  248. }
  249. # find internal surfaces (difference between top/bottom surfaces and others)
  250. @internal = $surface_difference->($layerm->slices, [@top, @bottom], S_TYPE_INTERNAL, $layerm);
  251. # save surfaces to layer
  252. @{$layerm->slices} = (@bottom, @top, @internal);
  253. Slic3r::debugf " layer %d has %d bottom, %d top and %d internal surfaces\n",
  254. $layerm->id, scalar(@bottom), scalar(@top), scalar(@internal);
  255. }
  256. # clip surfaces to the fill boundaries
  257. foreach my $layer (@{$self->layers}) {
  258. my $layerm = $layer->regions->[$region_id];
  259. my $fill_boundaries = [ map @$_, @{$layerm->surfaces} ];
  260. @{$layerm->surfaces} = ();
  261. foreach my $surface (@{$layerm->slices}) {
  262. my $intersection = intersection_ex(
  263. [ $surface->p ],
  264. $fill_boundaries,
  265. );
  266. push @{$layerm->surfaces}, map Slic3r::Surface->new
  267. (expolygon => $_, surface_type => $surface->surface_type),
  268. @$intersection;
  269. }
  270. }
  271. }
  272. }
  273. sub discover_horizontal_shells {
  274. my $self = shift;
  275. Slic3r::debugf "==> DISCOVERING HORIZONTAL SHELLS\n";
  276. my $area_threshold = $Slic3r::flow->scaled_spacing ** 2;
  277. for my $region_id (0 .. ($self->print->regions_count-1)) {
  278. for (my $i = 0; $i < $self->layer_count; $i++) {
  279. my $layerm = $self->layers->[$i]->regions->[$region_id];
  280. if ($Slic3r::Config->solid_infill_every_layers && ($i % $Slic3r::Config->solid_infill_every_layers) == 0) {
  281. $_->surface_type(S_TYPE_INTERNALSOLID)
  282. for grep $_->surface_type == S_TYPE_INTERNAL, @{$layerm->fill_surfaces};
  283. }
  284. foreach my $type (S_TYPE_TOP, S_TYPE_BOTTOM) {
  285. # find surfaces of current type for current layer
  286. # and offset them to take perimeters into account
  287. my @surfaces = map $_->offset($Slic3r::Config->perimeters * $layerm->perimeter_flow->scaled_width),
  288. grep $_->surface_type == $type, @{$layerm->fill_surfaces} or next;
  289. my $surfaces_p = [ map $_->p, @surfaces ];
  290. Slic3r::debugf "Layer %d has %d surfaces of type '%s'\n",
  291. $i, scalar(@surfaces), ($type == S_TYPE_TOP ? 'top' : 'bottom');
  292. my $solid_layers = ($type == S_TYPE_TOP)
  293. ? $Slic3r::Config->top_solid_layers
  294. : $Slic3r::Config->bottom_solid_layers;
  295. for (my $n = $type == S_TYPE_TOP ? $i-1 : $i+1;
  296. abs($n - $i) <= $solid_layers-1;
  297. $type == S_TYPE_TOP ? $n-- : $n++) {
  298. next if $n < 0 || $n >= $self->layer_count;
  299. Slic3r::debugf " looking for neighbors on layer %d...\n", $n;
  300. my @neighbor_surfaces = @{$self->layers->[$n]->regions->[$region_id]->surfaces};
  301. my @neighbor_fill_surfaces = @{$self->layers->[$n]->regions->[$region_id]->fill_surfaces};
  302. # find intersection between neighbor and current layer's surfaces
  303. # intersections have contours and holes
  304. my $new_internal_solid = intersection_ex(
  305. $surfaces_p,
  306. [ map $_->p, grep { $_->surface_type == S_TYPE_INTERNAL || $_->surface_type == S_TYPE_INTERNALSOLID } @neighbor_surfaces ],
  307. undef, 1,
  308. );
  309. next if !@$new_internal_solid;
  310. # internal-solid are the union of the existing internal-solid surfaces
  311. # and new ones
  312. my $internal_solid = union_ex([
  313. ( map $_->p, grep $_->surface_type == S_TYPE_INTERNALSOLID, @neighbor_fill_surfaces ),
  314. ( map @$_, @$new_internal_solid ),
  315. ]);
  316. # subtract intersections from layer surfaces to get resulting inner surfaces
  317. my $internal = diff_ex(
  318. [ map $_->p, grep $_->surface_type == S_TYPE_INTERNAL, @neighbor_fill_surfaces ],
  319. [ map @$_, @$internal_solid ],
  320. 1,
  321. );
  322. Slic3r::debugf " %d internal-solid and %d internal surfaces found\n",
  323. scalar(@$internal_solid), scalar(@$internal);
  324. # Note: due to floating point math we're going to get some very small
  325. # polygons as $internal; they will be removed by removed_small_features()
  326. # assign resulting inner surfaces to layer
  327. my $neighbor_fill_surfaces = $self->layers->[$n]->regions->[$region_id]->fill_surfaces;
  328. @$neighbor_fill_surfaces = ();
  329. push @$neighbor_fill_surfaces, Slic3r::Surface->new
  330. (expolygon => $_, surface_type => S_TYPE_INTERNAL)
  331. for @$internal;
  332. # assign new internal-solid surfaces to layer
  333. push @$neighbor_fill_surfaces, Slic3r::Surface->new
  334. (expolygon => $_, surface_type => S_TYPE_INTERNALSOLID)
  335. for @$internal_solid;
  336. # assign top and bottom surfaces to layer
  337. foreach my $s (Slic3r::Surface->group(grep { $_->surface_type == S_TYPE_TOP || $_->surface_type == S_TYPE_BOTTOM } @neighbor_fill_surfaces)) {
  338. my $solid_surfaces = diff_ex(
  339. [ map $_->p, @$s ],
  340. [ map @$_, @$internal_solid, @$internal ],
  341. 1,
  342. );
  343. push @$neighbor_fill_surfaces, Slic3r::Surface->new
  344. (expolygon => $_, surface_type => $s->[0]->surface_type, bridge_angle => $s->[0]->bridge_angle)
  345. for @$solid_surfaces;
  346. }
  347. }
  348. }
  349. @{$layerm->fill_surfaces} = grep $_->expolygon->area > $area_threshold, @{$layerm->fill_surfaces};
  350. }
  351. }
  352. }
  353. # combine fill surfaces across layers
  354. sub combine_infill {
  355. my $self = shift;
  356. return unless $Slic3r::Config->infill_every_layers > 1 && $Slic3r::Config->fill_density > 0;
  357. my $area_threshold = $Slic3r::flow->scaled_spacing ** 2;
  358. for my $region_id (0 .. ($self->print->regions_count-1)) {
  359. # start from bottom, skip first layer
  360. for (my $i = 1; $i < $self->layer_count; $i++) {
  361. my $layerm = $self->layers->[$i]->regions->[$region_id];
  362. # skip layer if no internal fill surfaces
  363. next if !grep $_->surface_type == S_TYPE_INTERNAL, @{$layerm->fill_surfaces};
  364. # for each possible depth, look for intersections with the lower layer
  365. # we do this from the greater depth to the smaller
  366. for (my $d = $Slic3r::Config->infill_every_layers - 1; $d >= 1; $d--) {
  367. next if ($i - $d) <= 0; # do not combine infill for bottom layer
  368. my $lower_layerm = $self->layer($i - 1)->regions->[$region_id];
  369. # select surfaces of the lower layer having the depth we're looking for
  370. my @lower_surfaces = grep $_->depth_layers == $d && $_->surface_type == S_TYPE_INTERNAL,
  371. @{$lower_layerm->fill_surfaces};
  372. next if !@lower_surfaces;
  373. # calculate intersection between our surfaces and theirs
  374. my $intersection = intersection_ex(
  375. [ map $_->p, grep $_->depth_layers <= $d, @lower_surfaces ],
  376. [ map $_->p, grep $_->surface_type == S_TYPE_INTERNAL, @{$layerm->fill_surfaces} ],
  377. undef, 1,
  378. );
  379. # purge intersections, skip tiny regions
  380. @$intersection = grep $_->area > $area_threshold, @$intersection;
  381. next if !@$intersection;
  382. # new fill surfaces of the current layer are:
  383. # - any non-internal surface
  384. # - intersections found (with a $d + 1 depth)
  385. # - any internal surface not belonging to the intersection (with its original depth)
  386. {
  387. my @new_surfaces = ();
  388. push @new_surfaces, grep $_->surface_type != S_TYPE_INTERNAL, @{$layerm->fill_surfaces};
  389. push @new_surfaces, map Slic3r::Surface->new
  390. (expolygon => $_, surface_type => S_TYPE_INTERNAL, depth_layers => $d + 1), @$intersection;
  391. foreach my $depth (reverse $d..$Slic3r::Config->infill_every_layers) {
  392. push @new_surfaces, map Slic3r::Surface->new
  393. (expolygon => $_, surface_type => S_TYPE_INTERNAL, depth_layers => $depth),
  394. # difference between our internal layers with depth == $depth
  395. # and the intersection found
  396. @{diff_ex(
  397. [
  398. map $_->p, grep $_->surface_type == S_TYPE_INTERNAL && $_->depth_layers == $depth,
  399. @{$layerm->fill_surfaces},
  400. ],
  401. [ map @$_, @$intersection ],
  402. 1,
  403. )};
  404. }
  405. @{$layerm->fill_surfaces} = @new_surfaces;
  406. }
  407. # now we remove the intersections from lower layer
  408. {
  409. my @new_surfaces = ();
  410. push @new_surfaces, grep $_->surface_type != S_TYPE_INTERNAL, @{$lower_layerm->fill_surfaces};
  411. foreach my $depth (1..$Slic3r::Config->infill_every_layers) {
  412. push @new_surfaces, map Slic3r::Surface->new
  413. (expolygon => $_, surface_type => S_TYPE_INTERNAL, depth_layers => $depth),
  414. # difference between internal layers with depth == $depth
  415. # and the intersection found
  416. @{diff_ex(
  417. [
  418. map $_->p, grep $_->surface_type == S_TYPE_INTERNAL && $_->depth_layers == $depth,
  419. @{$lower_layerm->fill_surfaces},
  420. ],
  421. [ map @$_, @$intersection ],
  422. 1,
  423. )};
  424. }
  425. @{$lower_layerm->fill_surfaces} = @new_surfaces;
  426. }
  427. }
  428. }
  429. }
  430. }
  431. sub generate_support_material {
  432. my $self = shift;
  433. my $flow = $self->print->support_material_flow;
  434. my $threshold_rad = deg2rad($Slic3r::Config->support_material_threshold + 1); # +1 makes the threshold inclusive
  435. my $overhang_width = $threshold_rad == 0 ? undef : scale $Slic3r::Config->layer_height * ((cos $threshold_rad) / (sin $threshold_rad));
  436. my $distance_from_object = 1.5 * $flow->scaled_width;
  437. # determine support regions in each layer (for upper layers)
  438. Slic3r::debugf "Detecting regions\n";
  439. my %layers = (); # this represents the areas of each layer having to support upper layers (excluding interfaces)
  440. my %layers_interfaces = (); # this represents the areas of each layer having an overhang in the immediately upper layer
  441. {
  442. my @current_support_regions = (); # expolygons we've started to support (i.e. below the empty interface layers)
  443. my @queue = (); # the number of items of this array determines the number of empty interface layers
  444. for my $i (reverse 0 .. $#{$self->layers}) {
  445. my $layer = $self->layers->[$i];
  446. my $lower_layer = $i > 0 ? $self->layers->[$i-1] : undef;
  447. # $queue[-1] contains the overhangs of the upper layer, regardless of any empty interface layers
  448. # $queue[0] contains the overhangs of the first upper layer above the empty interface layers
  449. $layers_interfaces{$i} = [@{ $queue[-1] || [] }];
  450. # step 1: generate support material in current layer (for upper layers)
  451. push @current_support_regions, @{ shift @queue } if @queue && $i < $#{$self->layers};
  452. @current_support_regions = @{diff_ex(
  453. [ map @$_, @current_support_regions ],
  454. [ map @$_, @{$layer->slices} ],
  455. )};
  456. $layers{$i} = diff_ex(
  457. [ map @$_, @current_support_regions ],
  458. [
  459. (map @$_, map $_->offset_ex($distance_from_object), @{$layer->slices}),
  460. (map @$_, @{ $layers_interfaces{$i} }),
  461. ],
  462. );
  463. $_->simplify($flow->scaled_spacing * 2) for @{$layers{$i}};
  464. # step 2: get layer overhangs and put them into queue for adding support inside lower layers
  465. # we need an angle threshold for this
  466. my @overhangs = ();
  467. if ($lower_layer) {
  468. @overhangs = map $_->offset_ex(2 * $overhang_width), @{diff_ex(
  469. [ map @$_, map $_->offset_ex(-$overhang_width), @{$layer->slices} ],
  470. [ map @$_, @{$lower_layer->slices} ],
  471. 1,
  472. )};
  473. }
  474. push @queue, [@overhangs];
  475. }
  476. }
  477. return if !map @$_, values %layers;
  478. # generate paths for the pattern that we're going to use
  479. Slic3r::debugf "Generating patterns\n";
  480. my $support_patterns = []; # in case we want cross-hatching
  481. {
  482. # 0.5 makes sure the paths don't get clipped externally when applying them to layers
  483. my @support_material_areas = map $_->offset_ex(- 0.5 * $flow->scaled_width),
  484. @{union_ex([ map $_->contour, map @$_, values %layers ])};
  485. my $fill = Slic3r::Fill->new(print => $self->print);
  486. my $filler = $fill->filler($Slic3r::Config->support_material_pattern);
  487. $filler->angle($Slic3r::Config->support_material_angle);
  488. {
  489. my @patterns = ();
  490. foreach my $expolygon (@support_material_areas) {
  491. my @paths = $filler->fill_surface(
  492. Slic3r::Surface->new(expolygon => $expolygon),
  493. density => $flow->spacing / $Slic3r::Config->support_material_spacing,
  494. flow_spacing => $flow->spacing,
  495. );
  496. my $params = shift @paths;
  497. push @patterns,
  498. map Slic3r::ExtrusionPath->new(
  499. polyline => Slic3r::Polyline->new(@$_),
  500. role => EXTR_ROLE_SUPPORTMATERIAL,
  501. height => undef,
  502. flow_spacing => $params->{flow_spacing},
  503. ), @paths;
  504. }
  505. push @$support_patterns, [@patterns];
  506. }
  507. if (0) {
  508. require "Slic3r/SVG.pm";
  509. Slic3r::SVG::output(undef, "support_$_.svg",
  510. polylines => [ map $_->polyline, map @$_, $support_patterns->[$_] ],
  511. polygons => [ map @$_, @support_material_areas ],
  512. ) for 0 .. $#$support_patterns;
  513. }
  514. }
  515. # apply the pattern to layers
  516. Slic3r::debugf "Applying patterns\n";
  517. {
  518. my $clip_pattern = sub {
  519. my ($layer_id, $expolygons, $height) = @_;
  520. my @paths = ();
  521. foreach my $expolygon (@$expolygons) {
  522. push @paths,
  523. map $_->pack,
  524. map {
  525. $_->height($height);
  526. $_->flow_spacing($self->print->first_layer_support_material_flow->spacing)
  527. if $layer_id == 0;
  528. $_;
  529. }
  530. map $_->clip_with_expolygon($expolygon),
  531. map $_->clip_with_polygon($expolygon->bounding_box_polygon),
  532. @{$support_patterns->[ $layer_id % @$support_patterns ]};
  533. };
  534. return @paths;
  535. };
  536. my %layer_paths = ();
  537. my %layer_interface_paths = ();
  538. my $process_layer = sub {
  539. my ($layer_id) = @_;
  540. my $layer = $self->layers->[$layer_id];
  541. my $paths = [ $clip_pattern->($layer_id, $layers{$layer_id}, $layer->height) ];
  542. my $interface_paths = [ $clip_pattern->($layer_id, $layers_interfaces{$layer_id}, $layer->support_material_interface_height) ];
  543. return ($paths, $interface_paths);
  544. };
  545. Slic3r::parallelize(
  546. items => [ keys %layers ],
  547. thread_cb => sub {
  548. my $q = shift;
  549. my $paths = {};
  550. my $interface_paths = {};
  551. while (defined (my $layer_id = $q->dequeue)) {
  552. ($paths->{$layer_id}, $interface_paths->{$layer_id}) = $process_layer->($layer_id);
  553. }
  554. return [ $paths, $interface_paths ];
  555. },
  556. collect_cb => sub {
  557. my $paths = shift;
  558. ($layer_paths{$_}, $layer_interface_paths{$_}) = @{ $paths->{$_} } for keys %$paths;
  559. },
  560. no_threads_cb => sub {
  561. ($layer_paths{$_}, $layer_interface_paths{$_}) = $process_layer->($_) for keys %layers;
  562. },
  563. );
  564. foreach my $layer_id (keys %layer_paths) {
  565. my $layer = $self->layers->[$layer_id];
  566. $layer->support_fills(Slic3r::ExtrusionPath::Collection->new);
  567. $layer->support_interface_fills(Slic3r::ExtrusionPath::Collection->new);
  568. push @{$layer->support_fills->paths}, @{$layer_paths{$layer_id}};
  569. push @{$layer->support_interface_fills->paths}, @{$layer_interface_paths{$layer_id}};
  570. }
  571. }
  572. }
  573. 1;