Many fixes to the bridge direction detection algorithm and other things. #58

This commit is contained in:
Alessandro Ranellucci 2011-12-03 18:31:31 +01:00
parent 792960aae1
commit 4fe340cc56
7 changed files with 93 additions and 70 deletions

View file

@ -2,7 +2,7 @@ package Slic3r::Layer;
use Moo;
use Math::Clipper ':all';
use Slic3r::Geometry qw(scale collinear X Y A B PI rad2deg_dir);
use Slic3r::Geometry qw(scale collinear X Y A B PI rad2deg_dir bounding_box_center);
use Slic3r::Geometry::Clipper qw(union_ex diff_ex intersection_ex is_counter_clockwise);
use XXX;
@ -24,6 +24,21 @@ has 'lines' => (
);
# collection of surfaces generated by slicing the original geometry
has 'slices' => (
is => 'rw',
#isa => 'ArrayRef[Slic3r::Surface]',
default => sub { [] },
);
# collection of surfaces generated by offsetting the innermost perimeter(s)
# they represent boundaries of areas to fill
has 'fill_boundaries' => (
is => 'rw',
#isa => 'ArrayRef[Slic3r::Surface]',
default => sub { [] },
);
# collection of surfaces generated by clipping the slices to the fill boundaries
has 'surfaces' => (
is => 'rw',
#isa => 'ArrayRef[Slic3r::Surface]',
@ -51,14 +66,6 @@ has 'skirts' => (
default => sub { [] },
);
# collection of surfaces generated by offsetting the innermost perimeter(s)
# they represent boundaries of areas to fill
has 'fill_boundaries' => (
is => 'rw',
#isa => 'ArrayRef[Slic3r::Surface]',
default => sub { [] },
);
# ordered collection of extrusion paths to fill surfaces
has 'fills' => (
is => 'rw',
@ -110,7 +117,7 @@ sub make_surfaces {
Slic3r::debugf " %d surface(s) having %d holes detected from %d polylines\n",
scalar(@$expolygons), scalar(map $_->holes, @$expolygons), scalar(@$loops);
push @{$self->surfaces},
push @{$self->slices},
map Slic3r::Surface->cast_from_expolygon($_, surface_type => 'internal'),
@$expolygons;
}
@ -162,9 +169,8 @@ sub prepare_fill_surfaces {
sub remove_small_surfaces {
my $self = shift;
my @good_surfaces = ();
my $distance = ($Slic3r::flow_width / 2 / $Slic3r::resolution);
my $distance = scale $Slic3r::flow_width / 2;
my @surfaces = @{$self->fill_surfaces};
@{$self->fill_surfaces} = ();
@ -174,18 +180,26 @@ sub remove_small_surfaces {
# offset the results outwards again and merge the results
@offsets = map $_->offset_ex($distance), @offsets;
@offsets = @{ union_ex([ map @$_, @offsets ]) };
# the difference between $surface->expolygon and @offsets
# is what we can't print since it's too small
@offsets = @{ union_ex([ map @$_, @offsets ], undef, 1) };
push @{$self->fill_surfaces}, map Slic3r::Surface->cast_from_expolygon($_,
surface_type => $surface->surface_type), @offsets;
}
Slic3r::debugf "removed %d small surfaces at layer %d\n",
Slic3r::debugf "identified %d small surfaces at layer %d\n",
(@surfaces - @{$self->fill_surfaces}), $self->id
if @{$self->fill_surfaces} != @surfaces;
# the difference between @surfaces and $self->fill_surfaces
# is what's too small; we add it back as solid infill
{
my $diff = diff_ex(
[ map $_->p, @surfaces ],
[ map $_->p, @{$self->fill_surfaces} ],
);
push @{$self->fill_surfaces}, map Slic3r::Surface->cast_from_expolygon($_,
surface_type => 'internal-solid'), @$diff;
}
}
sub remove_small_perimeters {
@ -215,7 +229,7 @@ sub process_bridges {
($_->surface_type eq 'bottom' && $self->id > 0) || $_->surface_type eq 'top'
} @{$self->fill_surfaces} or return;
my @internal_surfaces = grep $_->surface_type =~ /internal/, @{$self->surfaces};
my @internal_surfaces = grep $_->surface_type =~ /internal/, @{$self->slices};
SURFACE: foreach my $surface (@solid_surfaces) {
my $expolygon = $surface->expolygon->safety_offset;
@ -234,16 +248,17 @@ sub process_bridges {
if (0) {
require "Slic3r/SVG.pm";
Slic3r::SVG::output(undef, "bridge.svg",
Slic3r::SVG::output(undef, "bridge_surfaces.svg",
green_polygons => [ map $_->p, @supporting_surfaces ],
#red_polygons => [ @$expolygon ],
red_polygons => [ @$expolygon ],
);
}
next SURFACE unless @supporting_surfaces;
Slic3r::debugf " Found $description on layer %d with %d support(s)\n",
Slic3r::debugf "Found $description on layer %d with %d support(s)\n",
$self->id, scalar(@supporting_surfaces);
next SURFACE unless @supporting_surfaces;
my $bridge_angle = undef;
if ($surface->surface_type eq 'bottom') {
# detect optimal bridge angle
@ -256,37 +271,34 @@ sub process_bridges {
&& @{$supporting_surface->contour->p} == @{$surface_edges[0]->p}) {
$bridge_over_hole = 1;
} else {
foreach my $edge (@surface_edges) {
next unless @{$edge->points} >= 4;
shift @{$edge->points};
pop @{$edge->points};
}
@surface_edges = grep { @{$_->points} } @surface_edges;
}
push @edges, @surface_edges;
}
Slic3r::debugf " Bridge is supported on %d edge(s)\n", scalar(@edges);
Slic3r::debugf " and covers a hole\n" if $bridge_over_hole;
Slic3r::debugf " Bridge is supported on %d edge(s)\n", scalar(@edges);
Slic3r::debugf " and covers a hole\n" if $bridge_over_hole;
if (0) {
require "Slic3r/SVG.pm";
Slic3r::SVG::output(undef, "bridge.svg",
Slic3r::SVG::output(undef, "bridge_edges.svg",
polylines => [ map $_->p, @edges ],
);
}
{
my $weighted_sum = 0;
my $total_length = 0;
foreach my $line (map $_->lines, @edges) {
if (@edges) {
my $center = bounding_box_center([ map @{$_->points}, @edges ]);
my $x = my $y = 0;
foreach my $point (map @{$_->points}, @edges) {
my $line = Slic3r::Line->new($center, $point);
my $dir = $line->direction;
my $len = $line->length;
$weighted_sum += $len * $line->direction;
$total_length += $len;
$x += cos($dir) * $len;
$y += sin($dir) * $len;
}
$bridge_angle = rad2deg_dir(($weighted_sum / $total_length) + PI/2);
$bridge_angle = rad2deg_dir(atan2($y, $x));
}
Slic3r::debugf "Optimal infill angle of bridge on layer %d is %d degrees\n",
Slic3r::debugf " Optimal infill angle of bridge on layer %d is %d degrees\n",
$self->id, $bridge_angle if defined $bridge_angle;
}