Fill.pm 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. package Slic3r::Fill;
  2. use Moo;
  3. use Slic3r::ExtrusionPath ':roles';
  4. use Slic3r::Fill::3DHoneycomb;
  5. use Slic3r::Fill::ArchimedeanChords;
  6. use Slic3r::Fill::Base;
  7. use Slic3r::Fill::Concentric;
  8. use Slic3r::Fill::Flowsnake;
  9. use Slic3r::Fill::HilbertCurve;
  10. use Slic3r::Fill::Honeycomb;
  11. use Slic3r::Fill::Line;
  12. use Slic3r::Fill::OctagramSpiral;
  13. use Slic3r::Fill::PlanePath;
  14. use Slic3r::Fill::Rectilinear;
  15. use Slic3r::Flow ':roles';
  16. use Slic3r::Geometry qw(X Y PI scale chained_path deg2rad);
  17. use Slic3r::Geometry::Clipper qw(union union_ex diff diff_ex intersection_ex offset offset2);
  18. use Slic3r::Surface ':types';
  19. has 'bounding_box' => (is => 'ro', required => 0);
  20. has 'fillers' => (is => 'rw', default => sub { {} });
  21. our %FillTypes = (
  22. archimedeanchords => 'Slic3r::Fill::ArchimedeanChords',
  23. rectilinear => 'Slic3r::Fill::Rectilinear',
  24. flowsnake => 'Slic3r::Fill::Flowsnake',
  25. octagramspiral => 'Slic3r::Fill::OctagramSpiral',
  26. hilbertcurve => 'Slic3r::Fill::HilbertCurve',
  27. line => 'Slic3r::Fill::Line',
  28. concentric => 'Slic3r::Fill::Concentric',
  29. honeycomb => 'Slic3r::Fill::Honeycomb',
  30. '3dhoneycomb' => 'Slic3r::Fill::3DHoneycomb',
  31. );
  32. sub filler {
  33. my $self = shift;
  34. my ($filler) = @_;
  35. if (!ref $self) {
  36. return $FillTypes{$filler}->new;
  37. }
  38. $self->fillers->{$filler} ||= $FillTypes{$filler}->new(
  39. bounding_box => $self->bounding_box,
  40. );
  41. return $self->fillers->{$filler};
  42. }
  43. sub make_fill {
  44. my $self = shift;
  45. my ($layerm) = @_;
  46. Slic3r::debugf "Filling layer %d:\n", $layerm->id;
  47. my $fill_density = $layerm->config->fill_density;
  48. my $infill_flow = $layerm->flow(FLOW_ROLE_INFILL);
  49. my $solid_infill_flow = $layerm->flow(FLOW_ROLE_SOLID_INFILL);
  50. my @surfaces = ();
  51. # merge adjacent surfaces
  52. # in case of bridge surfaces, the ones with defined angle will be attached to the ones
  53. # without any angle (shouldn't this logic be moved to process_external_surfaces()?)
  54. {
  55. my @surfaces_with_bridge_angle = grep { $_->bridge_angle >= 0 } @{$layerm->fill_surfaces};
  56. # group surfaces by distinct properties
  57. my @groups = @{$layerm->fill_surfaces->group};
  58. # merge compatible groups (we can generate continuous infill for them)
  59. {
  60. # cache flow widths and patterns used for all solid groups
  61. # (we'll use them for comparing compatible groups)
  62. my @is_solid = my @fw = my @pattern = ();
  63. for (my $i = 0; $i <= $#groups; $i++) {
  64. # we can only merge solid non-bridge surfaces, so discard
  65. # non-solid surfaces
  66. if ($groups[$i][0]->is_solid && (!$groups[$i][0]->is_bridge || $layerm->id == 0)) {
  67. $is_solid[$i] = 1;
  68. $fw[$i] = ($groups[$i][0]->surface_type == S_TYPE_TOP)
  69. ? $layerm->flow(FLOW_ROLE_TOP_SOLID_INFILL)->width
  70. : $solid_infill_flow->width;
  71. $pattern[$i] = $groups[$i][0]->is_external
  72. ? $layerm->config->external_fill_pattern
  73. : 'rectilinear';
  74. } else {
  75. $is_solid[$i] = 0;
  76. $fw[$i] = 0;
  77. $pattern[$i] = 'none';
  78. }
  79. }
  80. # loop through solid groups
  81. for (my $i = 0; $i <= $#groups; $i++) {
  82. next if !$is_solid[$i];
  83. # find compatible groups and append them to this one
  84. for (my $j = $i+1; $j <= $#groups; $j++) {
  85. next if !$is_solid[$j];
  86. if ($fw[$i] == $fw[$j] && $pattern[$i] eq $pattern[$j]) {
  87. # groups are compatible, merge them
  88. push @{$groups[$i]}, @{$groups[$j]};
  89. splice @groups, $j, 1;
  90. splice @is_solid, $j, 1;
  91. splice @fw, $j, 1;
  92. splice @pattern, $j, 1;
  93. }
  94. }
  95. }
  96. }
  97. # give priority to bridges
  98. @groups = sort { ($a->[0]->bridge_angle >= 0) ? -1 : 0 } @groups;
  99. foreach my $group (@groups) {
  100. my $union_p = union([ map $_->p, @$group ], 1);
  101. # subtract surfaces having a defined bridge_angle from any other
  102. if (@surfaces_with_bridge_angle && $group->[0]->bridge_angle < 0) {
  103. $union_p = diff(
  104. $union_p,
  105. [ map $_->p, @surfaces_with_bridge_angle ],
  106. 1,
  107. );
  108. }
  109. # subtract any other surface already processed
  110. my $union = diff_ex(
  111. $union_p,
  112. [ map $_->p, @surfaces ],
  113. 1,
  114. );
  115. push @surfaces, map $group->[0]->clone(expolygon => $_), @$union;
  116. }
  117. }
  118. # we need to detect any narrow surfaces that might collapse
  119. # when adding spacing below
  120. # such narrow surfaces are often generated in sloping walls
  121. # by bridge_over_infill() and combine_infill() as a result of the
  122. # subtraction of the combinable area from the layer infill area,
  123. # which leaves small areas near the perimeters
  124. # we are going to grow such regions by overlapping them with the void (if any)
  125. # TODO: detect and investigate whether there could be narrow regions without
  126. # any void neighbors
  127. my $distance_between_surfaces = $infill_flow->scaled_spacing * &Slic3r::INFILL_OVERLAP_OVER_SPACING;
  128. {
  129. my $collapsed = diff(
  130. [ map @{$_->expolygon}, @surfaces ],
  131. offset2([ map @{$_->expolygon}, @surfaces ], -$distance_between_surfaces/2, +$distance_between_surfaces/2),
  132. 1,
  133. );
  134. push @surfaces, map Slic3r::Surface->new(
  135. expolygon => $_,
  136. surface_type => S_TYPE_INTERNALSOLID,
  137. ), @{intersection_ex(
  138. offset($collapsed, $distance_between_surfaces),
  139. [
  140. (map @{$_->expolygon}, grep $_->surface_type == S_TYPE_INTERNALVOID, @surfaces),
  141. (@$collapsed),
  142. ],
  143. 1,
  144. )};
  145. }
  146. # add spacing between surfaces
  147. @surfaces = map @{$_->offset(-$distance_between_surfaces / 2)}, @surfaces;
  148. if (0) {
  149. require "Slic3r/SVG.pm";
  150. Slic3r::SVG::output("fill_" . $layerm->print_z . ".svg",
  151. expolygons => [ map $_->expolygon, grep !$_->is_solid, @surfaces ],
  152. red_expolygons => [ map $_->expolygon, grep $_->is_solid, @surfaces ],
  153. );
  154. }
  155. my @fills = ();
  156. my @fills_ordering_points = ();
  157. SURFACE: foreach my $surface (@surfaces) {
  158. next if $surface->surface_type == S_TYPE_INTERNALVOID;
  159. my $filler = $layerm->config->fill_pattern;
  160. my $density = $fill_density;
  161. my $role = ($surface->surface_type == S_TYPE_TOP) ? FLOW_ROLE_TOP_SOLID_INFILL
  162. : $surface->is_solid ? FLOW_ROLE_SOLID_INFILL
  163. : FLOW_ROLE_INFILL;
  164. my $is_bridge = $layerm->id > 0 && $surface->is_bridge;
  165. my $is_solid = $surface->is_solid;
  166. if ($surface->is_solid) {
  167. $density = 100;
  168. $filler = 'rectilinear';
  169. if ($surface->is_external) {
  170. $filler = $layerm->config->external_fill_pattern;
  171. }
  172. } else {
  173. next SURFACE unless $density > 0;
  174. }
  175. my $h = $surface->thickness == -1 ? $layerm->height : $surface->thickness;
  176. my $flow = $layerm->region->flow(
  177. $role,
  178. $h,
  179. $is_bridge,
  180. $layerm->id == 0,
  181. -1,
  182. $layerm->object,
  183. );
  184. my $f = $self->filler($filler);
  185. $f->layer_id($layerm->id);
  186. $f->z($layerm->print_z);
  187. $f->angle(deg2rad($layerm->config->fill_angle));
  188. my ($params, @polylines) = $f->fill_surface(
  189. $surface,
  190. density => $density/100,
  191. flow => $flow,
  192. layer_height => $h,
  193. );
  194. next unless @polylines;
  195. my $mm3_per_mm = $flow->mm3_per_mm;
  196. # save into layer
  197. push @fills, my $collection = Slic3r::ExtrusionPath::Collection->new;
  198. $collection->no_sort($params->{no_sort});
  199. $collection->append(
  200. map Slic3r::ExtrusionPath->new(
  201. polyline => $_,
  202. role => ($is_bridge
  203. ? EXTR_ROLE_BRIDGE
  204. : $is_solid
  205. ? (($surface->surface_type == S_TYPE_TOP) ? EXTR_ROLE_TOPSOLIDFILL : EXTR_ROLE_SOLIDFILL)
  206. : EXTR_ROLE_FILL),
  207. mm3_per_mm => $mm3_per_mm,
  208. width => $flow->width,
  209. height => ($is_bridge ? $flow->width : $h),
  210. ), @polylines,
  211. );
  212. push @fills_ordering_points, $polylines[0]->first_point;
  213. }
  214. # add thin fill regions
  215. foreach my $thin_fill (@{$layerm->thin_fills}) {
  216. push @fills, Slic3r::ExtrusionPath::Collection->new($thin_fill);
  217. push @fills_ordering_points, $thin_fill->first_point;
  218. }
  219. # organize infill paths using a nearest-neighbor search
  220. @fills = @fills[ @{chained_path(\@fills_ordering_points)} ];
  221. return @fills;
  222. }
  223. 1;