Skip to content

Commit

Permalink
New experimental --gcode-arcs options to generate G2/G3 commands. #23
Browse files Browse the repository at this point in the history
  • Loading branch information
alranel committed Oct 28, 2011
1 parent 7f341cf commit 6d65338
Show file tree
Hide file tree
Showing 13 changed files with 333 additions and 21 deletions.
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ lib/Slic3r/ExPolygon.pm
lib/Slic3r/Extruder.pm
lib/Slic3r/ExtrusionLoop.pm
lib/Slic3r/ExtrusionPath.pm
lib/Slic3r/ExtrusionPath/Arc.pm
lib/Slic3r/ExtrusionPath/Collection.pm
lib/Slic3r/Fill.pm
lib/Slic3r/Fill/Base.pm
Expand Down
72 changes: 72 additions & 0 deletions MANIFEST.SKIP
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@

#!start included /opt/local/lib/perl5/5.12.3/ExtUtils/MANIFEST.SKIP
# Avoid version control files.
\bRCS\b
\bCVS\b
\bSCCS\b
,v$
\B\.svn\b
\B\.git\b
\B\.gitignore\b
\b_darcs\b
\B\.cvsignore$

# Avoid VMS specific MakeMaker generated files
\bDescrip.MMS$
\bDESCRIP.MMS$
\bdescrip.mms$

# Avoid Makemaker generated and utility files.
\bMANIFEST\.bak
\bMakefile$
\bblib/
\bMakeMaker-\d
\bpm_to_blib\.ts$
\bpm_to_blib$
\bblibdirs\.ts$ # 6.18 through 6.25 generated this

# Avoid Module::Build generated and utility files.
\bBuild$
\b_build/
\bBuild.bat$
\bBuild.COM$
\bBUILD.COM$
\bbuild.com$

# Avoid temp and backup files.
~$
\.old$
\#$
\b\.#
\.bak$
\.tmp$
\.#
\.rej$

# Avoid OS-specific files/dirs
# Mac OSX metadata
\B\.DS_Store
# Mac OSX SMB mount metadata files
\B\._

# Avoid Devel::Cover files.
\bcover_db\b
#!end included /opt/local/lib/perl5/5.12.3/ExtUtils/MANIFEST.SKIP

# Avoid configuration metadata file
^MYMETA\.

# Avoid Module::Build generated and utility files.
\bBuild$
\bBuild.bat$
\b_build
\bBuild.COM$
\bBUILD.COM$
\bbuild.com$
^MANIFEST\.SKIP

# Avoid archives of this distribution
\bSlic3r-[\d\.\_]+

\.git$
^\.DS_Store$
3 changes: 2 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ Slic3r current features are:
* multiple solid layers near horizontal external surfaces;
* ability to scale, rotate and multiply input object;
* customizable initial and final GCODE (using command line only);
* use different speed for bottom layer and perimeters.
* use different speed for bottom layer and perimeters;
* experimental support for G2/G3 native arcs.

Roadmap includes the following goals:

Expand Down
4 changes: 3 additions & 1 deletion lib/Slic3r.pm
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use Slic3r::ExPolygon;
use Slic3r::Extruder;
use Slic3r::ExtrusionLoop;
use Slic3r::ExtrusionPath;
use Slic3r::ExtrusionPath::Arc;
use Slic3r::ExtrusionPath::Collection;
use Slic3r::Fill;
use Slic3r::Geometry;
Expand All @@ -34,7 +35,8 @@ use Slic3r::Surface::Bridge;
our $nozzle_diameter = 0.5;
our $print_center = [100,100]; # object will be centered around this point
our $use_relative_e_distances = 0;
our $z_offset = 0;
our $z_offset = 0;
our $gcode_arcs = 0;

# filament options
our $filament_diameter = 3; # mm
Expand Down
70 changes: 55 additions & 15 deletions lib/Slic3r/Extruder.pm
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ has 'retract_speed' => (
default => sub { $Slic3r::retract_speed * 60 }, # mm/min
);

use Slic3r::Geometry qw(points_coincide);
use XXX;

use constant PI => 4 * atan2(1, 1);
Expand Down Expand Up @@ -67,7 +68,13 @@ sub extrude_loop {

sub extrude {
my $self = shift;
my ($path, $description) = @_;
my ($path, $description, $recursive) = @_;

if ($Slic3r::gcode_arcs && !$recursive) {
my $gcode = "";
$gcode .= $self->extrude($_, $description, 1) for $path->detect_arcs;
return $gcode;
}

my $gcode = "";

Expand All @@ -80,23 +87,28 @@ sub extrude {
}

# go to first point of extrusion path
$gcode .= $self->G1($path->points->[0], undef, 0, "move to first $description point");
$gcode .= $self->G1($path->points->[0], undef, 0, "move to first $description point")
if !points_coincide($self->last_pos, $path->points->[0]);

# compensate retraction
$gcode .= $self->unretract if $self->retracted;
XXX "yes!\n" if $path->depth_layers > 1;
# extrude while going to next points
foreach my $line ($path->lines) {
# calculate how much filament to drive into the extruder
# to get the desired amount of extruded plastic
my $e = $line->a->distance_to($line->b) * $Slic3r::resolution
* (($Slic3r::nozzle_diameter**2) / ($Slic3r::filament_diameter ** 2))
* $Slic3r::thickness_ratio
* $self->flow_ratio
* $Slic3r::filament_packing_density
* $path->depth_layers;

$gcode .= $self->G1($line->b, undef, $e, $description);

# calculate extrusion length per distance unit
my $e = $Slic3r::resolution
* (($Slic3r::nozzle_diameter**2) / ($Slic3r::filament_diameter ** 2))
* $Slic3r::thickness_ratio
* $self->flow_ratio
* $Slic3r::filament_packing_density
* $path->depth_layers;

# extrude arc or line
if ($path->isa('Slic3r::ExtrusionPath::Arc')) {
$gcode .= $self->G2_G3($path->points->[-1], $path->orientation,
$path->center, $e * $path->length, $description);
} else {
foreach my $line ($path->lines) {
$gcode .= $self->G1($line->b, undef, $e * $line->length, $description);
}
}

return $gcode;
Expand Down Expand Up @@ -145,6 +157,34 @@ sub G1 {
$gcode .= sprintf " Z%.${dec}f", $z;
}

return $self->_Gx($gcode, $e, $comment);
}

sub G2_G3 {
my $self = shift;
my ($point, $orientation, $center, $e, $comment) = @_;
my $dec = $self->dec;

my $gcode = $orientation eq 'cw' ? "G2" : "G3";

$gcode .= sprintf " X%.${dec}f Y%.${dec}f",
($point->x * $Slic3r::resolution) + $self->shift_x,
($point->y * $Slic3r::resolution) + $self->shift_y; #**
$self->last_pos($point);

# XY distance of the center from the start position
$gcode .= sprintf " I%.${dec}f J%.${dec}f",
($point->[X] - $self->last_pos->[X]) * $Slic3r::resolution + $self->shift_x,
($point->[Y] - $self->last_pos->[Y]) * $Slic3r::resolution + $self->shift_y;

return $self->_Gx($gcode, $e, $comment);
}

sub _Gx {
my $self = shift;
my ($gcode, $e, $comment) = @_;
my $dec = $self->dec;

# apply the speed reduction for print moves on bottom layer
my $speed_multiplier = $e && $self->z == $Slic3r::z_offset
? $Slic3r::bottom_layer_speed_ratio
Expand Down
112 changes: 111 additions & 1 deletion lib/Slic3r/ExtrusionPath.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ extends 'Slic3r::Polyline';
# expressed in layers
has 'depth_layers' => (is => 'ro', default => sub {1});

use constant PI => 4 * atan2(1, 1);
use constant X => 0;
use constant Y => 1;

use Slic3r::Geometry qw(PI epsilon deg2rad rotate_points);
use XXX;

sub clip_end {
my $self = shift;
Expand Down Expand Up @@ -70,4 +74,110 @@ sub split_at_acute_angles {
return @paths;
}

sub detect_arcs {
my $self = shift;

my $max_angle = deg2rad(40);
my $len_epsilon = 1000000;

my @points = @{$self->points};
my @paths = ();

# we require at least 3 consecutive segments to form an arc
CYCLE: while (@points >= 4) {
for (my $i = 0; $i <= $#points - 3; $i++) {
my $s1 = Slic3r::Line->new($points[$i], $points[$i+1]);
my $s2 = Slic3r::Line->new($points[$i+1], $points[$i+2]);
my $s3 = Slic3r::Line->new($points[$i+2], $points[$i+3]);
my $s1_len = $s1->length;
my $s2_len = $s2->length;
my $s3_len = $s3->length;

# segments must have the same length
if (abs($s3_len - $s2_len) > $len_epsilon) {
# optimization: skip a cycle
$i++;
next;
}
next if abs($s2_len - $s1_len) > $len_epsilon;

# segments must have the same relative angle
my $s1_angle = $s1->atan;
my $s2_angle = $s2->atan;
my $s3_angle = $s3->atan;
$s1_angle += 2*PI if $s1_angle < 0;
$s2_angle += 2*PI if $s2_angle < 0;
$s3_angle += 2*PI if $s3_angle < 0;
my $s1s2_angle = $s2_angle - $s1_angle;
my $s2s3_angle = $s3_angle - $s2_angle;
next if abs($s1s2_angle - $s2s3_angle) > $Slic3r::Geometry::parallel_degrees_limit;
next if $s1s2_angle < $Slic3r::Geometry::parallel_degrees_limit; # ignore parallel lines
next if $s1s2_angle > $max_angle; # ignore too sharp vertices

# s1, s2, s3 form an arc
my $orientation = $s1->point_on_left($points[$i+2]) ? 'ccw' : 'cw';

# to find the center, we intersect the perpendicular lines
# passing by midpoints of $s1 and $s3
my $arc_center;
{
my $s1_mid = $s1->midpoint;
my $s3_mid = $s2->midpoint;
my $rotation_angle = PI/2 * ($orientation eq 'ccw' ? -1 : 1);
my $ray1 = Slic3r::Line->new($s1_mid, rotate_points($rotation_angle, $s1_mid, $points[$i+1]));
my $ray3 = Slic3r::Line->new($s3_mid, rotate_points($rotation_angle, $s3_mid, $points[$i+3]));
$arc_center = $ray1->intersection($ray3, 0);
}

my $arc = Slic3r::ExtrusionPath::Arc->new(
points => [$points[$i], $points[$i+3]], # first and last points
orientation => $orientation,
center => $arc_center,
radius => $arc_center->distance_to($points[$i]),
);

# now look for more points
my $last_line_angle = $s3_angle;
my $last_j = $points[$i+3];
for (my $j = $i+3; $j < $#points; $j++) {
my $line = Slic3r::Line->new($points[$j], $points[$j+1]);
last if abs($line->length - $s1_len) > $len_epsilon;
my $line_angle = $line->atan;
$line_angle += 2*PI if $line_angle < 0;
my $anglediff = $line_angle - $last_line_angle;
last if abs($s1s2_angle - $anglediff) > $Slic3r::Geometry::parallel_degrees_limit;

# point $j+1 belongs to the arc
$arc->points->[-1] = $points[$j+1];
$last_j = $j+1;

$last_line_angle = $line_angle;
}

# points 0..$i form a linear path
push @paths, (ref $self)->new(
points => [ @points[0..$i] ],
depth_layers => $self->depth_layers,
) if $i > 0;

# add our arc
push @paths, $arc;
print "ARC DETECTED\n";
# remove arc points from path, leaving one
splice @points, 0, $last_j, ();

next CYCLE;
}
last;
}

# remaining points form a linear path
push @paths, (ref $self)->new(
points => [@points],
depth_layers => $self->depth_layers
) if @points;

return @paths;
}

1;
23 changes: 23 additions & 0 deletions lib/Slic3r/ExtrusionPath/Arc.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package Slic3r::ExtrusionPath::Arc;
use Moo;

extends 'Slic3r::ExtrusionPath';

has 'center' => (is => 'ro', required => 1);
has 'radius' => (is => 'ro', required => 1);
has 'orientation' => (is => 'ro', required => 1); # cw/ccw

use Slic3r::Geometry qw(PI angle3points);

sub angle {
my $self = shift;
return angle3points($self->center, @{$self->points});
}

sub length {
my $self = shift;

return $self->radius * $self->angle;
}

1;
5 changes: 5 additions & 0 deletions lib/Slic3r/ExtrusionPath/Collection.pm
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,9 @@ sub cleanup {
@{$self->paths} = map $_->split_at_acute_angles, @{$self->paths};
}

sub detect_arcs {
my $self = shift;
@{$self->paths} = map $_->detect_arcs, @{$self->paths};
}

1;
4 changes: 2 additions & 2 deletions lib/Slic3r/Geometry.pm
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ sub _line_intersection {
# Take this test away and the line segments are
# turned into lines going from infinite to another.
# bounding_box_intersect() defined later in this chapter.
return "out of bounding box" unless bounding_box_intersect( 2, @box_a, @box_b );
###return "out of bounding box" unless bounding_box_intersect( 2, @box_a, @box_b );
}
elsif ( @_ == 4 ) { # The parametric form.
$x0 = $x2 = 0;
Expand Down Expand Up @@ -509,7 +509,7 @@ sub _line_intersection {
my $h10 = $dx10 ? ($x - $x0) / $dx10 : ($dy10 ? ($y - $y0) / $dy10 : 1);
my $h32 = $dx32 ? ($x - $x2) / $dx32 : ($dy32 ? ($y - $y2) / $dy32 : 1);

return [[$x, $y], $h10 >= 0 && $h10 <= 1 && $h32 >= 0 && $h32 <= 1];
return [Slic3r::Point->new($x, $y), $h10 >= 0 && $h10 <= 1 && $h32 >= 0 && $h32 <= 1];
}

# 2D
Expand Down
Loading

0 comments on commit 6d65338

Please sign in to comment.