ExtrusionPath.pm 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. package Slic3r::ExtrusionPath;
  2. use Moo;
  3. require Exporter;
  4. our @ISA = qw(Exporter);
  5. our @EXPORT_OK = qw(EXTR_ROLE_PERIMETER EXTR_ROLE_SMALLPERIMETER EXTR_ROLE_CONTOUR_INTERNAL_PERIMETER
  6. EXTR_ROLE_FILL EXTR_ROLE_SOLIDFILL EXTR_ROLE_TOPSOLIDFILL EXTR_ROLE_BRIDGE EXTR_ROLE_SKIRT
  7. EXTR_ROLE_SUPPORTMATERIAL);
  8. our %EXPORT_TAGS = (roles => \@EXPORT_OK);
  9. use Slic3r::Geometry qw(PI X Y epsilon deg2rad rotate_points);
  10. # the underlying Slic3r::Polyline objects holds the geometry
  11. has 'polyline' => (
  12. is => 'rw',
  13. required => 1,
  14. handles => [qw(merge_continuous_lines lines length reverse)],
  15. );
  16. # this integer represents the vertical thickness of the extrusion
  17. # expressed in layers
  18. has 'depth_layers' => (is => 'ro', default => sub {1});
  19. has 'flow_spacing' => (is => 'rw');
  20. has 'role' => (is => 'rw', required => 1);
  21. use constant EXTR_ROLE_PERIMETER => 0;
  22. use constant EXTR_ROLE_SMALLPERIMETER => 1;
  23. use constant EXTR_ROLE_CONTOUR_INTERNAL_PERIMETER => 2;
  24. use constant EXTR_ROLE_FILL => 3;
  25. use constant EXTR_ROLE_SOLIDFILL => 4;
  26. use constant EXTR_ROLE_TOPSOLIDFILL => 5;
  27. use constant EXTR_ROLE_BRIDGE => 6;
  28. use constant EXTR_ROLE_SKIRT => 7;
  29. use constant EXTR_ROLE_SUPPORTMATERIAL => 8;
  30. sub BUILD {
  31. my $self = shift;
  32. bless $self->polyline, 'Slic3r::Polyline';
  33. $self->polyline($self->polyline->serialize);
  34. }
  35. sub deserialize {
  36. my $self = shift;
  37. $self->polyline($self->polyline->deserialize);
  38. }
  39. sub clip_end {
  40. my $self = shift;
  41. my ($distance) = @_;
  42. while ($distance > 0) {
  43. my $last_point = pop @{$self->points};
  44. last if !@{$self->points};
  45. my $last_segment_length = $last_point->distance_to($self->points->[-1]);
  46. if ($last_segment_length <= $distance) {
  47. $distance -= $last_segment_length;
  48. next;
  49. }
  50. my $new_point = Slic3r::Geometry::point_along_segment($last_point, $self->points->[-1], $distance);
  51. push @{$self->points}, Slic3r::Point->new($new_point);
  52. $distance = 0;
  53. }
  54. }
  55. sub clip_with_polygon {
  56. my $self = shift;
  57. my ($polygon) = @_;
  58. return $self->clip_with_expolygon(Slic3r::ExPolygon->new($polygon));
  59. }
  60. sub clip_with_expolygon {
  61. my $self = shift;
  62. my ($expolygon) = @_;
  63. my @paths = ();
  64. foreach my $polyline ($self->polyline->clip_with_expolygon($expolygon)) {
  65. push @paths, (ref $self)->new(
  66. polyline => $polyline,
  67. depth_layers => $self->depth_layers,
  68. flow_spacing => $self->flow_spacing,
  69. role => $self->role,
  70. );
  71. }
  72. return @paths;
  73. }
  74. sub points {
  75. my $self = shift;
  76. return $self->polyline;
  77. }
  78. sub endpoints {
  79. my $self = shift;
  80. return ($self->points->[0], $self->points->[-1]);
  81. }
  82. sub is_printable { 1 }
  83. sub split_at_acute_angles {
  84. my $self = shift;
  85. # calculate angle limit
  86. my $angle_limit = abs(Slic3r::Geometry::deg2rad(40));
  87. my @points = @{$self->p};
  88. my @paths = ();
  89. # take first two points
  90. my @p = splice @points, 0, 2;
  91. # loop until we have one spare point
  92. while (my $p3 = shift @points) {
  93. my $angle = abs(Slic3r::Geometry::angle3points($p[-1], $p[-2], $p3));
  94. $angle = 2*PI - $angle if $angle > PI;
  95. if ($angle < $angle_limit) {
  96. # if the angle between $p[-2], $p[-1], $p3 is too acute
  97. # then consider $p3 only as a starting point of a new
  98. # path and stop the current one as it is
  99. push @paths, (ref $self)->new(
  100. polyline => Slic3r::Polyline->new(\@p),
  101. role => $self->role,
  102. depth_layers => $self->depth_layers,
  103. );
  104. @p = ($p3);
  105. push @p, grep $_, shift @points or last;
  106. } else {
  107. push @p, $p3;
  108. }
  109. }
  110. push @paths, (ref $self)->new(
  111. polyline => Slic3r::Polyline->new(\@p),
  112. role => $self->role,
  113. depth_layers => $self->depth_layers,
  114. ) if @p > 1;
  115. return @paths;
  116. }
  117. sub detect_arcs {
  118. my $self = shift;
  119. my ($max_angle, $len_epsilon) = @_;
  120. $self->deserialize;
  121. $max_angle = deg2rad($max_angle || 15);
  122. $len_epsilon ||= 10 / $Slic3r::scaling_factor;
  123. my @points = @{$self->points};
  124. my @paths = ();
  125. # we require at least 3 consecutive segments to form an arc
  126. CYCLE: while (@points >= 4) {
  127. POINT: for (my $i = 0; $i <= $#points - 3; $i++) {
  128. my $s1 = Slic3r::Line->new($points[$i], $points[$i+1]);
  129. my $s2 = Slic3r::Line->new($points[$i+1], $points[$i+2]);
  130. my $s3 = Slic3r::Line->new($points[$i+2], $points[$i+3]);
  131. my $s1_len = $s1->length;
  132. my $s2_len = $s2->length;
  133. my $s3_len = $s3->length;
  134. # segments must have the same length
  135. if (abs($s3_len - $s2_len) > $len_epsilon) {
  136. # optimization: skip a cycle
  137. $i++;
  138. next;
  139. }
  140. next if abs($s2_len - $s1_len) > $len_epsilon;
  141. # segments must have the same relative angle
  142. my $s1_angle = $s1->atan;
  143. my $s2_angle = $s2->atan;
  144. my $s3_angle = $s3->atan;
  145. $s1_angle += 2*PI if $s1_angle < 0;
  146. $s2_angle += 2*PI if $s2_angle < 0;
  147. $s3_angle += 2*PI if $s3_angle < 0;
  148. my $s1s2_angle = $s2_angle - $s1_angle;
  149. my $s2s3_angle = $s3_angle - $s2_angle;
  150. next if abs($s1s2_angle - $s2s3_angle) > $Slic3r::Geometry::parallel_degrees_limit;
  151. next if abs($s1s2_angle) < $Slic3r::Geometry::parallel_degrees_limit; # ignore parallel lines
  152. next if $s1s2_angle > $max_angle; # ignore too sharp vertices
  153. my @arc_points = ($points[$i], $points[$i+3]), # first and last points
  154. # now look for more points
  155. my $last_line_angle = $s3_angle;
  156. my $last_j = $i+3;
  157. for (my $j = $i+3; $j < $#points; $j++) {
  158. my $line = Slic3r::Line->new($points[$j], $points[$j+1]);
  159. last if abs($line->length - $s1_len) > $len_epsilon;
  160. my $line_angle = $line->atan;
  161. $line_angle += 2*PI if $line_angle < 0;
  162. my $anglediff = $line_angle - $last_line_angle;
  163. last if abs($s1s2_angle - $anglediff) > $Slic3r::Geometry::parallel_degrees_limit;
  164. # point $j+1 belongs to the arc
  165. $arc_points[-1] = $points[$j+1];
  166. $last_j = $j+1;
  167. $last_line_angle = $line_angle;
  168. }
  169. # s1, s2, s3 form an arc
  170. my $orientation = $s1->point_on_left($points[$i+2]) ? 'ccw' : 'cw';
  171. # to find the center, we intersect the perpendicular lines
  172. # passing by midpoints of $s1 and last segment
  173. # a better method would be to draw all the perpendicular lines
  174. # and find the centroid of the enclosed polygon, or to
  175. # intersect multiple lines and find the centroid of the convex hull
  176. # around the intersections
  177. my $arc_center;
  178. {
  179. my $s1_mid = $s1->midpoint;
  180. my $last_mid = Slic3r::Line->new($points[$last_j-1], $points[$last_j])->midpoint;
  181. my $rotation_angle = PI/2 * ($orientation eq 'ccw' ? -1 : 1);
  182. my $ray1 = Slic3r::Line->new($s1_mid, rotate_points($rotation_angle, $s1_mid, $points[$i+1]));
  183. my $last_ray = Slic3r::Line->new($last_mid, rotate_points($rotation_angle, $last_mid, $points[$last_j]));
  184. $arc_center = $ray1->intersection($last_ray, 0) or next POINT;
  185. }
  186. my $arc = Slic3r::ExtrusionPath::Arc->new(
  187. polyline => Slic3r::Polyline->new(\@arc_points),
  188. role => $self->role,
  189. orientation => $orientation,
  190. center => $arc_center,
  191. radius => $arc_center->distance_to($points[$i]),
  192. );
  193. # points 0..$i form a linear path
  194. push @paths, (ref $self)->new(
  195. polyline => Slic3r::Polyline->new(@points[0..$i]),
  196. role => $self->role,
  197. depth_layers => $self->depth_layers,
  198. ) if $i > 0;
  199. # add our arc
  200. push @paths, $arc;
  201. Slic3r::debugf "ARC DETECTED\n";
  202. # remove arc points from path, leaving one
  203. splice @points, 0, $last_j, ();
  204. next CYCLE;
  205. }
  206. last;
  207. }
  208. # remaining points form a linear path
  209. push @paths, (ref $self)->new(
  210. polyline => Slic3r::Polyline->new(\@points),
  211. role => $self->role,
  212. depth_layers => $self->depth_layers,
  213. ) if @points > 1;
  214. return @paths;
  215. }
  216. 1;