Ported ExPolygon->contour and ExPolygon->holes

This commit is contained in:
Alessandro Ranellucci 2013-08-26 23:09:18 +02:00
parent 85011dfef3
commit da0b85c0d9
7 changed files with 14 additions and 18 deletions

View file

@ -10,21 +10,11 @@ use Math::Geometry::Voronoi;
use Slic3r::Geometry qw(X Y A B point_in_polygon same_line epsilon); use Slic3r::Geometry qw(X Y A B point_in_polygon same_line epsilon);
use Slic3r::Geometry::Clipper qw(union_ex JT_MITER); use Slic3r::Geometry::Clipper qw(union_ex JT_MITER);
sub contour {
my $self = shift;
return $self->[0];
}
sub holes {
my $self = shift;
return @$self[1..$#$self];
}
sub is_valid { sub is_valid {
my $self = shift; my $self = shift;
return (!first { !$_->is_valid } @$self) return (!first { !$_->is_valid } @$self)
&& $self->contour->is_counter_clockwise && $self->contour->is_counter_clockwise
&& (!first { $_->is_counter_clockwise } $self->holes); && (!first { $_->is_counter_clockwise } @{$self->holes});
} }
# returns false if the expolygon is too tight to be printed # returns false if the expolygon is too tight to be printed

View file

@ -150,7 +150,7 @@ sub BUILD {
points => [ values %{$self->_pointmap} ], points => [ values %{$self->_pointmap} ],
no_arrows => 1, no_arrows => 1,
expolygons => $self->islands, expolygons => $self->islands,
#red_polygons => [ map $_->holes, map @$_, @{$self->_inner} ], #red_polygons => [ map @{$_->holes}, map @$_, @{$self->_inner} ],
#white_polygons => [ map @$_, @{$self->_outer} ], #white_polygons => [ map @$_, @{$self->_outer} ],
); );
printf "%d islands\n", scalar @{$self->islands}; printf "%d islands\n", scalar @{$self->islands};
@ -195,7 +195,7 @@ sub find_node {
# if we're inside a hole, move to a point on hole; # if we're inside a hole, move to a point on hole;
{ {
my $polygon = first { $_->encloses_point($point) } (map $_->holes, map @$_, @{$self->_inner}); my $polygon = first { $_->encloses_point($point) } (map @{$_->holes}, map @$_, @{$self->_inner});
return nearest_point($point, $polygon) if $polygon; return nearest_point($point, $polygon) if $polygon;
} }

View file

@ -159,7 +159,7 @@ sub _merge_loops {
$slices = offset2_ex($slices, +$safety_offset, -$safety_offset); $slices = offset2_ex($slices, +$safety_offset, -$safety_offset);
Slic3r::debugf " %d surface(s) having %d holes detected from %d polylines\n", Slic3r::debugf " %d surface(s) having %d holes detected from %d polylines\n",
scalar(@$slices), scalar(map $_->holes, @$slices), scalar(@$loops) if $Slic3r::debug; scalar(@$slices), scalar(map @{$_->holes}, @$slices), scalar(@$loops) if $Slic3r::debug;
return map Slic3r::Surface->new(expolygon => $_, surface_type => S_TYPE_INTERNAL), @$slices; return map Slic3r::Surface->new(expolygon => $_, surface_type => S_TYPE_INTERNAL), @$slices;
} }
@ -198,7 +198,7 @@ sub make_perimeters {
my @offsets = @{offset2_ex(\@last, -1.5*$spacing, +0.5*$spacing)}; my @offsets = @{offset2_ex(\@last, -1.5*$spacing, +0.5*$spacing)};
my @contours_offsets = map $_->contour, @offsets; my @contours_offsets = map $_->contour, @offsets;
my @holes_offsets = map $_->holes, @offsets; my @holes_offsets = map @{$_->holes}, @offsets;
@offsets = (@contours_offsets, @holes_offsets); # turn @offsets from ExPolygons to Polygons @offsets = (@contours_offsets, @holes_offsets); # turn @offsets from ExPolygons to Polygons
# where offset2() collapses the expolygon, then there's no room for an inner loop # where offset2() collapses the expolygon, then there's no room for an inner loop

View file

@ -531,7 +531,7 @@ EOF
my $expolygon = $slice->clone; my $expolygon = $slice->clone;
$expolygon->translate(@$copy); $expolygon->translate(@$copy);
$print_polygon->($expolygon->contour, 'contour'); $print_polygon->($expolygon->contour, 'contour');
$print_polygon->($_, 'hole') for $expolygon->holes; $print_polygon->($_, 'hole') for @{$expolygon->holes};
push @current_layer_slices, $expolygon; push @current_layer_slices, $expolygon;
} }
} }

View file

@ -245,7 +245,7 @@ sub slice {
]); ]);
my $diff = diff_ex( my $diff = diff_ex(
[ map @$_, @$union ], [ map @$_, @$union ],
[ map $_->expolygon->holes, @upper_surfaces, @lower_surfaces, ], [ map @{$_->expolygon->holes}, @upper_surfaces, @lower_surfaces, ],
); );
$layerm->slices->clear; $layerm->slices->clear;

View file

@ -4,7 +4,7 @@ use strict;
use warnings; use warnings;
use Slic3r::XS; use Slic3r::XS;
use Test::More tests => 18; use Test::More tests => 20;
use constant PI => 4 * atan2(1, 1); use constant PI => 4 * atan2(1, 1);
@ -28,6 +28,8 @@ is_deeply $expolygon->pp, [$square, $hole_in_square], 'expolygon roundtrip';
is ref($expolygon->arrayref), 'ARRAY', 'expolygon arrayref is unblessed'; is ref($expolygon->arrayref), 'ARRAY', 'expolygon arrayref is unblessed';
isa_ok $expolygon->[0], 'Slic3r::Polygon', 'expolygon polygon is blessed'; isa_ok $expolygon->[0], 'Slic3r::Polygon', 'expolygon polygon is blessed';
isa_ok $expolygon->contour, 'Slic3r::Polygon', 'expolygon contour is blessed';
isa_ok $expolygon->holes->[0], 'Slic3r::Polygon', 'expolygon hole is blessed';
isa_ok $expolygon->[0][0], 'Slic3r::Point', 'expolygon point is blessed'; isa_ok $expolygon->[0][0], 'Slic3r::Point', 'expolygon point is blessed';
{ {

View file

@ -13,6 +13,10 @@
%code{% RETVAL = THIS->to_SV(); %}; %code{% RETVAL = THIS->to_SV(); %};
SV* pp() SV* pp()
%code{% RETVAL = THIS->to_SV_pureperl(); %}; %code{% RETVAL = THIS->to_SV_pureperl(); %};
Polygon* contour()
%code{% const char* CLASS = "Slic3r::Polygon"; RETVAL = new Polygon(THIS->contour); %};
Polygons holes()
%code{% RETVAL = THIS->holes; %};
void scale(double factor); void scale(double factor);
void translate(double x, double y); void translate(double x, double y);
double area(); double area();