ExtrusionPath.pm 9.3 KB

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