Bugfix: fill escapes perimeters. #139

This commit is contained in:
Alessandro Ranellucci 2012-03-04 11:26:11 +01:00
parent 4a4c4b4e7d
commit be9886eac3
3 changed files with 20 additions and 9 deletions

View File

@ -5,7 +5,7 @@ use warnings;
# an ExPolygon is a polygon with holes
use Math::Geometry::Voronoi;
use Slic3r::Geometry qw(X Y A B point_in_polygon);
use Slic3r::Geometry qw(X Y A B point_in_polygon same_line);
use Slic3r::Geometry::Clipper qw(union_ex JT_MITER);
# the constructor accepts an array of polygons
@ -93,6 +93,14 @@ sub encloses_point {
|| grep($_->point_on_segment($point), $self->holes));
}
sub encloses_line {
my $self = shift;
my ($line) = @_;
my $clip = $self->clip_line($line);
return @$clip == 1 && same_line($clip->[0], $line);
}
sub point_on_segment {
my $self = shift;
my ($point) = @_;

View File

@ -15,9 +15,8 @@ sub fill_surface {
my $rotate_vector = $self->infill_direction($surface);
$self->rotate_points($expolygon, $rotate_vector);
my $bounding_box = [ $expolygon->bounding_box ];
$bounding_box->[X1] += scale 0.1;
$bounding_box->[X2] -= scale 0.1;
my ($expolygon_off) = $expolygon->offset_ex(scale 0.2);
my $bounding_box = [ $expolygon_off->bounding_box ];
my $min_spacing = scale $params{flow_spacing};
my $distance_between_lines = $min_spacing / $params{density};
@ -59,11 +58,9 @@ sub fill_surface {
);
@paths = ();
# the 1 below disables the fix for connection lines being extruded outside $expolygon
# (this happens to gears, for example). it works, but it's 30% slower with it enabled
my $can_connect = $is_line_pattern
? sub { $_[X] <= (abs((($_[2][Y] - $bounding_box->[Y1])*(2 * $line_oscillation)/($bounding_box->[Y2] - $bounding_box->[Y1])) - $line_oscillation) + $distance_between_lines) && $_[Y] <= $distance_between_lines * 5 }
: sub { ($_[X] >= $distance_between_lines - epsilon) && ($_[X] <= $distance_between_lines + epsilon) && ($_[Y] <= $distance_between_lines * 5) && (1 || $expolygon->encloses_point(Slic3r::Line->new(@_[2,3])->midpoint)) };
: sub { ($_[X] >= $distance_between_lines - epsilon) && ($_[X] <= $distance_between_lines + epsilon) && ($_[Y] <= $distance_between_lines * 5) };
foreach my $path ($collection->shortest_path) {
if (@paths) {
@ -71,7 +68,8 @@ sub fill_surface {
# TODO: we should also check that both points are on a fill_boundary to avoid
# connecting paths on the boundaries of internal regions
if ($can_connect->(@distance, $paths[-1][-1], $path->points->[0])) {
if ($can_connect->(@distance, $paths[-1][-1], $path->points->[0])
&& $expolygon_off->encloses_line([ $paths[-1][-1], $path->points->[0] ])) {
push @{$paths[-1]}, @{$path->points};
next;
}

View File

@ -13,7 +13,7 @@ our @EXPORT_OK = qw(
polygon_has_vertex polyline_length can_connect_points deg2rad rad2deg
rotate_points move_points remove_coinciding_points clip_segment_polygon
sum_vectors multiply_vector subtract_vectors dot perp polygon_points_visibility
line_intersection bounding_box bounding_box_intersect same_point
line_intersection bounding_box bounding_box_intersect same_point same_line
longest_segment angle3points three_points_aligned line_direction
polyline_remove_parallel_continuous_edges polyline_remove_acute_vertices
polygon_remove_acute_vertices polygon_remove_parallel_continuous_edges
@ -103,6 +103,11 @@ sub same_point {
return $p1->[X] == $p2->[X] && $p1->[Y] == $p2->[Y];
}
sub same_line {
my ($line1, $line2) = @_;
return same_point($line1->[A], $line2->[A]) && same_point($line1->[B], $line2->[B]);
}
sub distance_between_points {
my ($p1, $p2) = @_;
return sqrt((($p1->[X] - $p2->[X])**2) + ($p1->[Y] - $p2->[Y])**2);