ExtrusionPath.pm 10.0 KB

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