Fill.pm 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package Slic3r::Fill;
  2. use Moo;
  3. use Slic3r::Fill::ArchimedeanChords;
  4. use Slic3r::Fill::Base;
  5. use Slic3r::Fill::Concentric;
  6. use Slic3r::Fill::Flowsnake;
  7. use Slic3r::Fill::HilbertCurve;
  8. use Slic3r::Fill::Honeycomb;
  9. use Slic3r::Fill::Line;
  10. use Slic3r::Fill::OctagramSpiral;
  11. use Slic3r::Fill::PlanePath;
  12. use Slic3r::Fill::Rectilinear;
  13. use Slic3r::ExtrusionPath ':roles';
  14. use Slic3r::Geometry qw(X Y PI scale chained_path);
  15. use Slic3r::Geometry::Clipper qw(union_ex diff diff_ex intersection_ex offset);
  16. use Slic3r::Surface ':types';
  17. has 'object' => (is => 'ro', required => 1, weak_ref => 1);
  18. has 'fillers' => (is => 'rw', default => sub { {} });
  19. our %FillTypes = (
  20. archimedeanchords => 'Slic3r::Fill::ArchimedeanChords',
  21. rectilinear => 'Slic3r::Fill::Rectilinear',
  22. flowsnake => 'Slic3r::Fill::Flowsnake',
  23. octagramspiral => 'Slic3r::Fill::OctagramSpiral',
  24. hilbertcurve => 'Slic3r::Fill::HilbertCurve',
  25. line => 'Slic3r::Fill::Line',
  26. concentric => 'Slic3r::Fill::Concentric',
  27. honeycomb => 'Slic3r::Fill::Honeycomb',
  28. );
  29. sub filler {
  30. my $self = shift;
  31. my ($filler) = @_;
  32. if (!ref $self) {
  33. return $FillTypes{$filler}->new;
  34. }
  35. $self->fillers->{$filler} ||= $FillTypes{$filler}->new(
  36. bounding_box => $self->object->bounding_box,
  37. );
  38. return $self->fillers->{$filler};
  39. }
  40. sub make_fill {
  41. my $self = shift;
  42. my ($layerm) = @_;
  43. Slic3r::debugf "Filling layer %d:\n", $layerm->id;
  44. my @surfaces = ();
  45. # if hollow object is requested, remove internal surfaces
  46. # (this needs to be done after internal-solid shells are created)
  47. if ($Slic3r::Config->fill_density == 0) {
  48. @surfaces = grep $_->surface_type != S_TYPE_INTERNAL, @surfaces;
  49. }
  50. # merge adjacent surfaces
  51. # in case of bridge surfaces, the ones with defined angle will be attached to the ones
  52. # without any angle (shouldn't this logic be moved to process_external_surfaces()?)
  53. {
  54. my @surfaces_with_bridge_angle = grep defined $_->bridge_angle, @{$layerm->fill_surfaces};
  55. # give priority to bridges
  56. my @groups = Slic3r::Surface->group({merge_solid => 1}, @{$layerm->fill_surfaces});
  57. @groups = sort { defined $a->[0]->bridge_angle ? -1 : 0 } @groups;
  58. foreach my $group (@groups) {
  59. my $union = union_ex([ map $_->p, @$group ], 1);
  60. # subtract surfaces having a defined bridge_angle from any other
  61. if (@surfaces_with_bridge_angle && !defined $group->[0]->bridge_angle) {
  62. $union = diff_ex(
  63. [ map @$_, @$union ],
  64. [ map $_->p, @surfaces_with_bridge_angle ],
  65. 1,
  66. );
  67. }
  68. # subtract any other surface already processed
  69. $union = diff_ex(
  70. [ map @$_, @$union ],
  71. [ map $_->p, @surfaces ],
  72. 1,
  73. );
  74. push @surfaces, map $group->[0]->clone(expolygon => $_), @$union;
  75. }
  76. }
  77. # we need to detect any narrow surfaces that might collapse
  78. # when adding spacing below
  79. # such narrow surfaces are often generated in sloping walls
  80. # by bridge_over_infill() and combine_infill() as a result of the
  81. # subtraction of the combinable area from the layer infill area,
  82. # which leaves small areas near the perimeters
  83. # we are going to grow such regions by overlapping them with the void (if any)
  84. # TODO: detect and investigate whether there could be narrow regions without
  85. # any void neighbors
  86. my $distance_between_surfaces = $layerm->solid_infill_flow->scaled_spacing;
  87. {
  88. my $collapsed = diff(
  89. [ map @{$_->expolygon}, @surfaces ],
  90. offset(
  91. offset([ map @{$_->expolygon}, @surfaces ], -$distance_between_surfaces/2),
  92. +$distance_between_surfaces/2
  93. ),
  94. 1,
  95. );
  96. push @surfaces, map Slic3r::Surface->new(
  97. expolygon => $_,
  98. surface_type => S_TYPE_INTERNALSOLID,
  99. ), @{intersection_ex(
  100. offset($collapsed, $distance_between_surfaces),
  101. [
  102. (map @{$_->expolygon}, grep $_->surface_type == S_TYPE_INTERNALVOID, @surfaces),
  103. (@$collapsed),
  104. ],
  105. 1,
  106. )};
  107. }
  108. # add spacing between surfaces
  109. @surfaces = map @{$_->offset(-$distance_between_surfaces / 2 * &Slic3r::INFILL_OVERLAP_OVER_SPACING)}, @surfaces;
  110. my @fills = ();
  111. my @fills_ordering_points = ();
  112. SURFACE: foreach my $surface (@surfaces) {
  113. next if $surface->surface_type == S_TYPE_INTERNALVOID;
  114. my $filler = $Slic3r::Config->fill_pattern;
  115. my $density = $Slic3r::Config->fill_density;
  116. my $flow = ($surface->surface_type == S_TYPE_TOP)
  117. ? $layerm->top_infill_flow
  118. : $surface->is_solid
  119. ? $layerm->solid_infill_flow
  120. : $layerm->infill_flow;
  121. my $flow_spacing = $flow->spacing;
  122. my $is_bridge = $layerm->id > 0 && $surface->is_bridge;
  123. my $is_solid = $surface->is_solid;
  124. # force 100% density and rectilinear fill for external surfaces
  125. if ($surface->surface_type != S_TYPE_INTERNAL) {
  126. $density = 1;
  127. $filler = $Slic3r::Config->solid_fill_pattern;
  128. if ($is_bridge) {
  129. $filler = 'rectilinear';
  130. $flow_spacing = $layerm->extruders->{infill}->bridge_flow->spacing;
  131. } elsif ($surface->surface_type == S_TYPE_INTERNALSOLID) {
  132. $filler = 'rectilinear';
  133. }
  134. } else {
  135. next SURFACE unless $density > 0;
  136. }
  137. my $f = $self->filler($filler);
  138. $f->layer_id($layerm->id);
  139. my ($params, @polylines) = $f->fill_surface(
  140. $surface,
  141. density => $density,
  142. flow_spacing => $flow_spacing,
  143. );
  144. next unless @polylines;
  145. # ugly hack(tm) to get the right amount of flow (GCode.pm should be fixed)
  146. $params->{flow_spacing} = $layerm->extruders->{infill}->bridge_flow->width if $is_bridge;
  147. # save into layer
  148. push @fills, my $collection = Slic3r::ExtrusionPath::Collection->new;
  149. $collection->no_sort($params->{no_sort});
  150. $collection->append(
  151. map Slic3r::ExtrusionPath->new(
  152. polyline => Slic3r::Polyline->new(@$_),
  153. role => ($surface->surface_type == S_TYPE_INTERNALBRIDGE
  154. ? EXTR_ROLE_INTERNALBRIDGE
  155. : $is_bridge
  156. ? EXTR_ROLE_BRIDGE
  157. : $is_solid
  158. ? (($surface->surface_type == S_TYPE_TOP) ? EXTR_ROLE_TOPSOLIDFILL : EXTR_ROLE_SOLIDFILL)
  159. : EXTR_ROLE_FILL),
  160. height => $surface->thickness,
  161. flow_spacing => $params->{flow_spacing} || (warn "Warning: no flow_spacing was returned by the infill engine, please report this to the developer\n"),
  162. ), @polylines,
  163. );
  164. push @fills_ordering_points, $polylines[0][0];
  165. }
  166. # add thin fill regions
  167. if (@{ $layerm->thin_fills }) {
  168. push @fills, Slic3r::ExtrusionPath::Collection->new(@{$layerm->thin_fills});
  169. push @fills_ordering_points, $fills[-1]->first_point;
  170. }
  171. # organize infill paths using a nearest-neighbor search
  172. @fills = @fills[ chained_path(\@fills_ordering_points) ];
  173. return @fills;
  174. }
  175. 1;