ExtrusionPath.pm 9.7 KB

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