Merge branch 'better-brim'

Conflicts:
	lib/Slic3r/Geometry/Clipper.pm
	lib/Slic3r/Print.pm
This commit is contained in:
Alessandro Ranellucci 2013-05-11 09:19:23 +02:00
commit 7953dcfb12
5 changed files with 73 additions and 29 deletions

View File

@ -12,7 +12,7 @@ my $build = Module::Build->new(
'File::Basename' => '0',
'File::Spec' => '0',
'Getopt::Long' => '0',
'Math::Clipper' => '1.18',
'Math::Clipper' => '1.21',
'Math::ConvexHull::MonotoneChain' => '0.01',
'Math::Geometry::Voronoi' => '1.3',
'Math::PlanePath' => '53',

View File

@ -6,9 +6,9 @@ require Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(safety_offset safety_offset_ex offset offset_ex collapse_ex
diff_ex diff union_ex intersection_ex xor_ex PFT_EVENODD JT_MITER JT_ROUND
JT_SQUARE is_counter_clockwise);
JT_SQUARE is_counter_clockwise union_pt offset2 offset2_ex);
use Math::Clipper 1.17 qw(:cliptypes :polyfilltypes :jointypes is_counter_clockwise area);
use Math::Clipper 1.21 qw(:cliptypes :polyfilltypes :jointypes is_counter_clockwise area);
use Slic3r::Geometry qw(scale);
our $clipper = Math::Clipper->new;
@ -33,6 +33,16 @@ sub offset {
return @$offsets;
}
sub offset2 {
my ($polygons, $distance1, $distance2, $scale, $joinType, $miterLimit) = @_;
$scale ||= 100000;
$joinType //= JT_MITER;
$miterLimit //= 3;
my $offsets = Math::Clipper::int_offset2($polygons, $distance1, $distance2, $scale, $joinType, $miterLimit);
return @$offsets;
}
sub offset_ex {
my ($polygons, $distance, $scale, $joinType, $miterLimit) = @_;
$scale ||= 100000;
@ -43,6 +53,16 @@ sub offset_ex {
return map Slic3r::ExPolygon->new($_), @$offsets;
}
sub offset2_ex {
my ($polygons, $delta1, $delta2, $scale, $joinType, $miterLimit) = @_;
$scale ||= 100000;
$joinType //= JT_MITER;
$miterLimit //= 3;
my $offsets = Math::Clipper::ex_int_offset2($polygons, $delta1, $delta2, $scale, $joinType, $miterLimit);
return map Slic3r::ExPolygon->new($_), @$offsets;
}
sub diff_ex {
my ($subject, $clip, $safety_offset) = @_;
@ -78,6 +98,14 @@ sub union_ex {
];
}
sub union_pt {
my ($polygons, $jointype, $safety_offset) = @_;
$jointype = PFT_NONZERO unless defined $jointype;
$clipper->clear;
$clipper->add_subject_polygons($safety_offset ? safety_offset($polygons) : $polygons);
return $clipper->pt_execute(CT_UNION, $jointype, $jointype);
}
sub intersection_ex {
my ($subject, $clip, $jointype, $safety_offset) = @_;
$jointype = PFT_NONZERO unless defined $jointype;
@ -102,19 +130,9 @@ sub xor_ex {
];
}
sub ex_int_offset2 {
my ($polygons, $delta1, $delta2, $scale, $joinType, $miterLimit) = @_;
$scale ||= 100000;
$joinType //= JT_MITER;
$miterLimit //= 3;
my $offsets = Math::Clipper::ex_int_offset2($polygons, $delta1, $delta2, $scale, $joinType, $miterLimit);
return map Slic3r::ExPolygon->new($_), @$offsets;
}
sub collapse_ex {
my ($polygons, $width) = @_;
return [ ex_int_offset2($polygons, -$width/2, +$width/2) ];
return [ offset2_ex($polygons, -$width/2, +$width/2) ];
}
sub simplify_polygon {

View File

@ -4,7 +4,7 @@ use Moo;
use List::Util qw(sum first);
use Slic3r::ExtrusionPath ':roles';
use Slic3r::Geometry qw(PI X1 X2 Y1 Y2 A B scale chained_path_items points_coincide);
use Slic3r::Geometry::Clipper qw(safety_offset union_ex diff_ex intersection_ex);
use Slic3r::Geometry::Clipper qw(safety_offset union_ex diff_ex intersection_ex offset2_ex);
use Slic3r::Surface ':types';
has 'layer' => (
@ -97,7 +97,7 @@ sub make_surfaces {
{
my $width = $self->perimeter_flow->scaled_width;
my $outgrown = [
Slic3r::Geometry::Clipper::ex_int_offset2([ map @$_, map $_->expolygon, @{$self->slices} ], -$width, +$width),
offset2_ex([ map @$_, map $_->expolygon, @{$self->slices} ], -$width, +$width),
];
my $diff = diff_ex(
[ map $_->p, @{$self->slices} ],
@ -227,7 +227,7 @@ sub make_perimeters {
# offsetting a polygon can result in one or many offset polygons
my @new_offsets = ();
foreach my $expolygon (@last_offsets) {
my @offsets = Slic3r::Geometry::Clipper::ex_int_offset2($expolygon, -1.5*$spacing, +0.5*$spacing);
my @offsets = offset2_ex($expolygon, -1.5*$spacing, +0.5*$spacing);
push @new_offsets, @offsets;
# where the above check collapses the expolygon, then there's no room for an inner loop

View File

@ -6,8 +6,9 @@ use File::Spec;
use List::Util qw(max first);
use Math::ConvexHull::MonotoneChain qw(convex_hull);
use Slic3r::ExtrusionPath ':roles';
use Slic3r::Geometry qw(X Y Z X1 Y1 X2 Y2 MIN PI scale unscale move_points nearest_point);
use Slic3r::Geometry::Clipper qw(diff_ex union_ex intersection_ex offset JT_ROUND JT_SQUARE);
use Slic3r::Geometry qw(X Y Z X1 Y1 X2 Y2 MIN PI scale unscale move_points nearest_point chained_path_items);
use Slic3r::Geometry::Clipper qw(diff_ex union_ex union_pt intersection_ex offset
offset2 JT_ROUND JT_SQUARE PFT_EVENODD);
use Time::HiRes qw(gettimeofday tv_interval);
has 'config' => (is => 'rw', default => sub { Slic3r::Config->new_from_defaults }, trigger => 1);
@ -673,16 +674,39 @@ sub make_brim {
push @islands, map $_->unpack->split_at_first_point->polyline->grow($grow_distance), @{$self->skirt};
}
my @loops = ();
my $num_loops = sprintf "%.0f", $Slic3r::Config->brim_width / $flow->width;
for my $i (reverse 1 .. $num_loops) {
# JT_SQUARE ensures no vertex is outside the given offset distance
push @{$self->brim}, Slic3r::ExtrusionLoop->pack(
polygon => Slic3r::Polygon->new($_),
# -0.5 because islands are not represented by their centerlines
# TODO: we need the offset inwards/offset outwards logic to avoid overlapping extrusions
push @loops, offset2(\@islands, ($i - 2) * $flow->scaled_spacing, ($i + 1.5) * $flow->scaled_spacing, undef, JT_SQUARE);
}
# prepare a subroutine to traverse the tree and return inner perimeters first
my $traverse;
$traverse = sub {
my ($loops) = @_;
# use a nearest neighbor search to order these children
# TODO: supply second argument to chained_path_items() too?
@$loops = @{chained_path_items(
[ map [ ($_->{outer} ? $_->{outer}[0] : $_->{hole}[0]), $_ ], @$loops ],
)};
my @polygons = ();
foreach my $loop (@$loops) {
push @polygons, $traverse->($loop->{children});
push @polygons, Slic3r::ExtrusionLoop->pack(
polygon => Slic3r::Polygon->new($loop->{outer} // [ reverse @{$loop->{hole}} ]),
role => EXTR_ROLE_SKIRT,
flow_spacing => $flow->spacing,
) for Slic3r::Geometry::Clipper::offset(\@islands, ($i - 0.5) * $flow->scaled_spacing, undef, JT_SQUARE); # -0.5 because islands are not represented by their centerlines
# TODO: we need the offset inwards/offset outwards logic to avoid overlapping extrusions
);
}
return @polygons;
};
@{$self->brim} = reverse $traverse->( union_pt(\@loops, PFT_EVENODD) );
}
sub write_gcode {

View File

@ -30,21 +30,22 @@ use Math::Clipper ':all';
$clipper->add_subject_polygons([ $square, $hole_in_square ]);
$clipper->add_clip_polygons([ $square2 ]);
my $intersection = $clipper->ex_execute(CT_INTERSECTION, PFT_NONZERO, PFT_NONZERO);
is_deeply $intersection, [
{
holes => [
[
[14, 14],
[14, 16],
[16, 16],
[16, 14],
[14, 14],
],
],
outer => [
[10, 18],
[10, 12],
[20, 12],
[20, 18],
[10, 18],
[10, 12],
],
},
], 'hole is preserved after intersection';
@ -60,14 +61,15 @@ use Math::Clipper ':all';
my $clipper = Math::Clipper->new;
$clipper->add_subject_polygons([ $contour1, $contour2, $hole ]);
my $union = $clipper->ex_execute(CT_UNION, PFT_NONZERO, PFT_NONZERO);
is_deeply $union, [{ holes => [], outer => [ [0,40], [0,0], [40,0], [40,40] ] }],
is_deeply $union, [{ holes => [], outer => [ [40,0], [40,40], [0,40], [0,0] ] }],
'union of two ccw and one cw is a contour with no holes';
$clipper->clear;
$clipper->add_subject_polygons([ $contour1, $contour2 ]);
$clipper->add_clip_polygons([ $hole ]);
my $diff = $clipper->ex_execute(CT_DIFFERENCE, PFT_NONZERO, PFT_NONZERO);
is_deeply $diff, [{ holes => [[ [15,25], [25,25], [25,15], [15,15] ]], outer => [ [0,40], [0,0], [40,0], [40,40] ] }],
is_deeply $diff, [{ holes => [[ [15,15], [15,25], [25,25], [25,15] ]], outer => [ [40,0], [40,40], [0,40], [0,0] ] }],
'difference of a cw from two ccw is a contour with one hole';
}