Layer.pm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. package Slic3r::Layer;
  2. use Moo;
  3. use Math::Clipper ':all';
  4. use Slic3r::Geometry qw(collinear X Y A B PI);
  5. use Slic3r::Geometry::Clipper qw(union_ex diff_ex intersection_ex PFT_EVENODD);
  6. use XXX;
  7. # a sequential number of layer, starting at 0
  8. has 'id' => (
  9. is => 'ro',
  10. #isa => 'Int',
  11. required => 1,
  12. );
  13. # collection of spare segments generated by slicing the original geometry;
  14. # these need to be merged in continuos (closed) polylines
  15. has 'lines' => (
  16. is => 'rw',
  17. #isa => 'ArrayRef[Slic3r::TriangleMesh::IntersectionLine]',
  18. default => sub { [] },
  19. );
  20. # collection of surfaces generated by slicing the original geometry
  21. has 'surfaces' => (
  22. is => 'rw',
  23. #isa => 'ArrayRef[Slic3r::Surface]',
  24. default => sub { [] },
  25. );
  26. # ordered collection of extrusion paths to build all perimeters
  27. has 'perimeters' => (
  28. is => 'rw',
  29. #isa => 'ArrayRef[Slic3r::ExtrusionLoop]',
  30. default => sub { [] },
  31. );
  32. # ordered collection of extrusion paths to build skirt loops
  33. has 'skirts' => (
  34. is => 'rw',
  35. #isa => 'ArrayRef[Slic3r::ExtrusionLoop]',
  36. default => sub { [] },
  37. );
  38. # collection of surfaces generated by offsetting the innermost perimeter(s)
  39. # they represent boundaries of areas to fill
  40. has 'fill_boundaries' => (
  41. is => 'rw',
  42. #isa => 'ArrayRef[Slic3r::Surface]',
  43. default => sub { [] },
  44. );
  45. # ordered collection of extrusion paths to fill surfaces
  46. has 'fills' => (
  47. is => 'rw',
  48. #isa => 'ArrayRef[Slic3r::ExtrusionPath]',
  49. default => sub { [] },
  50. );
  51. # Z used for slicing
  52. sub slice_z {
  53. my $self = shift;
  54. if ($self->id == 0) {
  55. return ($Slic3r::layer_height * $Slic3r::first_layer_height_ratio) / 2 / $Slic3r::resolution;
  56. }
  57. return (($Slic3r::layer_height * $Slic3r::first_layer_height_ratio)
  58. + (($self->id-1) * $Slic3r::layer_height)
  59. + ($Slic3r::layer_height/2)) / $Slic3r::resolution;
  60. }
  61. # Z used for printing
  62. sub print_z {
  63. my $self = shift;
  64. return (($Slic3r::layer_height * $Slic3r::first_layer_height_ratio)
  65. + ($self->id * $Slic3r::layer_height)) / $Slic3r::resolution;
  66. }
  67. sub add_surface {
  68. my $self = shift;
  69. my (@vertices) = @_;
  70. # convert arrayref points to Point objects
  71. @vertices = map Slic3r::Point->new($_), @vertices;
  72. my $surface = Slic3r::Surface->new(
  73. contour => Slic3r::Polyline::Closed->new(points => \@vertices),
  74. );
  75. push @{ $self->surfaces }, $surface;
  76. # make sure our contour has its points in counter-clockwise order
  77. $surface->contour->make_counter_clockwise;
  78. return $surface;
  79. }
  80. sub add_line {
  81. my $self = shift;
  82. my ($line) = @_;
  83. push @{ $self->lines }, $line;
  84. return $line;
  85. }
  86. # build polylines from lines
  87. sub make_surfaces {
  88. my $self = shift;
  89. my ($loops) = @_;
  90. {
  91. my $expolygons = union_ex($loops, PFT_EVENODD);
  92. Slic3r::debugf " %d surface(s) having %d holes detected from %d polylines\n",
  93. scalar(@$expolygons), scalar(map $_->holes, @$expolygons), scalar(@$loops);
  94. push @{$self->surfaces},
  95. map Slic3r::Surface->cast_from_expolygon($_, surface_type => 'internal'),
  96. @$expolygons;
  97. }
  98. #use Slic3r::SVG;
  99. #Slic3r::SVG::output(undef, "surfaces.svg",
  100. # polygons => [ map $_->contour->p, @{$self->surfaces} ],
  101. # red_polygons => [ map $_->p, map @{$_->holes}, @{$self->surfaces} ],
  102. #);
  103. }
  104. sub remove_small_surfaces {
  105. my $self = shift;
  106. my @good_surfaces = ();
  107. my $distance = ($Slic3r::flow_width / 2 / $Slic3r::resolution);
  108. my @surfaces = @{$self->surfaces};
  109. @{$self->surfaces} = ();
  110. foreach my $surface (@surfaces) {
  111. # offset inwards
  112. my @offsets = $surface->expolygon->offset_ex(-$distance);
  113. # offset the results outwards again and merge the results
  114. @offsets = map $_->offset_ex($distance), @offsets;
  115. @offsets = @{ union_ex([ map @$_, @offsets ]) };
  116. # the difference between $surface->expolygon and @offsets
  117. # is what we can't print since it's too small
  118. push @{$self->surfaces}, map Slic3r::Surface->cast_from_expolygon($_,
  119. surface_type => $surface->surface_type), @offsets;
  120. }
  121. Slic3r::debugf "removed %d small surfaces at layer %d\n",
  122. (@surfaces - @{$self->surfaces}), $self->id
  123. if @{$self->surfaces} != @surfaces;
  124. }
  125. sub remove_small_perimeters {
  126. my $self = shift;
  127. my @good_perimeters = grep $_->is_printable, @{$self->perimeters};
  128. Slic3r::debugf "removed %d unprintable perimeters at layer %d\n",
  129. (@{$self->perimeters} - @good_perimeters), $self->id
  130. if @good_perimeters != @{$self->perimeters};
  131. @{$self->perimeters} = @good_perimeters;
  132. }
  133. # make bridges printable
  134. sub process_bridges {
  135. my $self = shift;
  136. # no bridges are possible if we have no internal surfaces
  137. return if $Slic3r::fill_density == 0;
  138. my @bridges = ();
  139. # a bottom surface on a layer > 0 is either a bridge or a overhang
  140. # or a combination of both; any top surface is a candidate for
  141. # reverse bridge processing
  142. my @solid_surfaces = grep {
  143. ($_->surface_type eq 'bottom' && $self->id > 0) || $_->surface_type eq 'top'
  144. } @{$self->surfaces} or return;
  145. my @internal_surfaces = grep $_->surface_type =~ /internal/, @{$self->surfaces};
  146. SURFACE: foreach my $surface (@solid_surfaces) {
  147. my $expolygon = $surface->expolygon->safety_offset;
  148. my $description = $surface->surface_type eq 'bottom' ? 'bridge/overhang' : 'reverse bridge';
  149. # offset the contour and intersect it with the internal surfaces to discover
  150. # which of them has contact with our bridge
  151. my @supporting_surfaces = ();
  152. my ($contour_offset) = $expolygon->contour->offset($Slic3r::flow_width / $Slic3r::resolution);
  153. foreach my $internal_surface (@internal_surfaces) {
  154. my $intersection = intersection_ex([$contour_offset], [$internal_surface->contour->p]);
  155. if (@$intersection) {
  156. push @supporting_surfaces, $internal_surface;
  157. }
  158. }
  159. #use Slic3r::SVG;
  160. #Slic3r::SVG::output(undef, "bridge.svg",
  161. # green_polygons => [ map $_->p, @supporting_surfaces ],
  162. # red_polygons => [ @$expolygon ],
  163. #);
  164. next SURFACE unless @supporting_surfaces;
  165. Slic3r::debugf " Found $description on layer %d with %d support(s)\n",
  166. $self->id, scalar(@supporting_surfaces);
  167. my $bridge_angle = undef;
  168. if ($surface->surface_type eq 'bottom') {
  169. # detect optimal bridge angle
  170. my $bridge_over_hole = 0;
  171. my @edges = (); # edges are POLYLINES
  172. foreach my $supporting_surface (@supporting_surfaces) {
  173. my @surface_edges = $supporting_surface->contour->clip_with_polygon($contour_offset);
  174. if (@surface_edges == 1 && @{$supporting_surface->contour->p} == @{$surface_edges[0]->p}) {
  175. $bridge_over_hole = 1;
  176. } else {
  177. foreach my $edge (@surface_edges) {
  178. shift @{$edge->points};
  179. pop @{$edge->points};
  180. }
  181. @surface_edges = grep { @{$_->points} } @surface_edges;
  182. }
  183. push @edges, @surface_edges;
  184. }
  185. Slic3r::debugf " Bridge is supported on %d edge(s)\n", scalar(@edges);
  186. Slic3r::debugf " and covers a hole\n" if $bridge_over_hole;
  187. if (0) {
  188. require "Slic3r/SVG.pm";
  189. Slic3r::SVG::output(undef, "bridge.svg",
  190. polylines => [ map $_->p, @edges ],
  191. );
  192. }
  193. if (@edges == 2) {
  194. my @chords = map Slic3r::Line->new($_->points->[0], $_->points->[-1]), @edges;
  195. my @midpoints = map $_->midpoint, @chords;
  196. $bridge_angle = -Slic3r::Geometry::rad2deg(Slic3r::Geometry::line_atan(\@midpoints) + PI/2);
  197. Slic3r::debugf "Optimal infill angle of bridge on layer %d is %d degrees\n", $self->id, $bridge_angle;
  198. }
  199. }
  200. # now, extend our bridge by taking a portion of supporting surfaces
  201. {
  202. # offset the bridge by the specified amount of mm (minimum 3)
  203. my $bridge_overlap = 3 / $Slic3r::resolution;
  204. my ($bridge_offset) = $expolygon->contour->offset($bridge_overlap, $Slic3r::resolution * 100, JT_MITER, 2);
  205. # calculate the new bridge
  206. my $intersection = intersection_ex(
  207. [ @$expolygon, map $_->p, @supporting_surfaces ],
  208. [ $bridge_offset ],
  209. );
  210. push @bridges, map Slic3r::Surface->cast_from_expolygon($_,
  211. surface_type => $surface->surface_type,
  212. bridge_angle => $bridge_angle,
  213. ), @$intersection;
  214. }
  215. }
  216. # now we need to merge bridges to avoid overlapping
  217. {
  218. # build a list of unique bridge types
  219. my @surface_groups = Slic3r::Surface->group(@bridges);
  220. # merge bridges of the same type, removing any of the bridges already merged;
  221. # the order of @surface_groups determines the priority between bridges having
  222. # different surface_type or bridge_angle
  223. @bridges = ();
  224. foreach my $surfaces (@surface_groups) {
  225. my $union = union_ex([ map $_->p, @$surfaces ]);
  226. my $diff = diff_ex(
  227. [ map @$_, @$union ],
  228. [ map $_->p, @bridges ],
  229. );
  230. push @bridges, map Slic3r::Surface->cast_from_expolygon($_,
  231. surface_type => $surfaces->[0]->surface_type,
  232. bridge_angle => $surfaces->[0]->bridge_angle,
  233. ), @$union;
  234. }
  235. }
  236. # apply bridges to layer
  237. {
  238. my @surfaces = @{$self->surfaces};
  239. @{$self->surfaces} = ();
  240. # intersect layer surfaces with bridges to get actual bridges
  241. foreach my $bridge (@bridges) {
  242. my $actual_bridge = intersection_ex(
  243. [ map $_->p, @surfaces ],
  244. [ $bridge->p ],
  245. );
  246. push @{$self->surfaces}, map Slic3r::Surface->cast_from_expolygon($_,
  247. surface_type => $bridge->surface_type,
  248. bridge_angle => $bridge->bridge_angle,
  249. ), @$actual_bridge;
  250. }
  251. # difference between layer surfaces and bridges are the other surfaces
  252. foreach my $group (Slic3r::Surface->group(@surfaces)) {
  253. my $difference = diff_ex(
  254. [ map $_->p, @$group ],
  255. [ map $_->p, @bridges ],
  256. );
  257. push @{$self->surfaces}, map Slic3r::Surface->cast_from_expolygon($_,
  258. surface_type => $group->[0]->surface_type), @$difference;
  259. }
  260. }
  261. }
  262. 1;