ExtrusionPath.pm 10 KB

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