mirror of
https://github.com/SoftFever/OrcaSlicer.git
synced 2025-08-06 21:44:08 -06:00
Refactored the BridgeDetector class to expose a cleaner API and make it stateful
This commit is contained in:
parent
bc101bd93e
commit
9989ebaabd
3 changed files with 110 additions and 112 deletions
|
@ -5,45 +5,61 @@ use List::Util qw(first sum max);
|
|||
use Slic3r::Geometry qw(PI unscale scaled_epsilon rad2deg epsilon);
|
||||
use Slic3r::Geometry::Clipper qw(intersection_pl intersection_ex);
|
||||
|
||||
has 'expolygon' => (is => 'ro', required => 1);
|
||||
has 'lower_slices' => (is => 'rw', required => 1); # ExPolygons or ExPolygonCollection
|
||||
has 'infill_flow' => (is => 'rw', required => 1);
|
||||
has 'extrusion_width' => (is => 'rw', required => 1); # scaled
|
||||
has 'resolution' => (is => 'rw', default => sub { PI/36 });
|
||||
|
||||
sub detect_angle {
|
||||
my ($self, $expolygon) = @_;
|
||||
has '_edges' => (is => 'rw'); # Polylines representing the supporting edges
|
||||
has '_anchors' => (is => 'rw'); # ExPolygons
|
||||
has 'angle' => (is => 'rw');
|
||||
|
||||
sub BUILD {
|
||||
my ($self) = @_;
|
||||
|
||||
my $anchors_offset = $self->infill_flow->scaled_width;
|
||||
|
||||
my $grown = $expolygon->offset(+$anchors_offset);
|
||||
my @lower = @{$self->lower_slices}; # expolygons
|
||||
# outset our bridge by an arbitrary amout; we'll use this outer margin
|
||||
# for detecting anchors
|
||||
my $grown = $self->expolygon->offset(+$self->extrusion_width);
|
||||
|
||||
# detect what edges lie on lower slices
|
||||
my @edges = (); # polylines
|
||||
foreach my $lower (@lower) {
|
||||
$self->_edges(my $edges = []);
|
||||
foreach my $lower (@{$self->lower_slices}) {
|
||||
# turn bridge contour and holes into polylines and then clip them
|
||||
# with each lower slice's contour
|
||||
push @edges, map @{$_->clip_as_polyline([$lower->contour])}, @$grown;
|
||||
push @$edges, map @{$_->clip_as_polyline([$lower->contour])}, @$grown;
|
||||
}
|
||||
Slic3r::debugf " bridge has %d support(s)\n", scalar(@$edges);
|
||||
|
||||
Slic3r::debugf " bridge has %d support(s)\n", scalar(@edges);
|
||||
return undef if !@edges;
|
||||
|
||||
my $bridge_angle = undef;
|
||||
# detect anchors as intersection between our bridge expolygon and the lower slices
|
||||
$self->_anchors(intersection_ex(
|
||||
$grown,
|
||||
[ map @$_, @{$self->lower_slices} ],
|
||||
1, # safety offset required to avoid Clipper from detecting empty intersection while Boost actually found some @edges
|
||||
));
|
||||
|
||||
if (0) {
|
||||
require "Slic3r/SVG.pm";
|
||||
Slic3r::SVG::output("bridge.svg",
|
||||
expolygons => [ $expolygon ],
|
||||
red_expolygons => [ @lower ],
|
||||
polylines => [ @edges ],
|
||||
expolygons => [ $self->expolygon ],
|
||||
red_expolygons => $self->lower_slices,
|
||||
polylines => $self->_edges,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
sub detect_angle {
|
||||
my ($self) = @_;
|
||||
|
||||
return undef if !@{$self->_edges};
|
||||
|
||||
my @edges = @{$self->_edges};
|
||||
my $anchors = $self->_anchors;
|
||||
|
||||
if (@edges == 2) {
|
||||
my @chords = map Slic3r::Line->new($_->[0], $_->[-1]), @edges;
|
||||
my @midpoints = map $_->midpoint, @chords;
|
||||
my $line_between_midpoints = Slic3r::Line->new(@midpoints);
|
||||
$bridge_angle = $line_between_midpoints->direction;
|
||||
$self->angle($line_between_midpoints->direction);
|
||||
} elsif (@edges == 1 && !$edges[0][0]->coincides_with($edges[0][-1])) {
|
||||
# Don't use this logic if $edges[0] is actually a closed loop
|
||||
# TODO: this case includes both U-shaped bridges and plain overhangs;
|
||||
|
@ -53,20 +69,13 @@ sub detect_angle {
|
|||
# our supporting edge is a straight line
|
||||
if (@{$edges[0]} > 2) {
|
||||
my $line = Slic3r::Line->new($edges[0]->[0], $edges[0]->[-1]);
|
||||
$bridge_angle = $line->direction;
|
||||
$self->angle($line->direction);
|
||||
}
|
||||
} elsif (@edges) {
|
||||
# Outset the bridge expolygon by half the amount we used for detecting anchors;
|
||||
# we'll use this one to clip our test lines and be sure that their endpoints
|
||||
# are inside the anchors and not on their contours leading to false negatives.
|
||||
my $clip_area = $expolygon->offset_ex(+$anchors_offset/2);
|
||||
|
||||
# detect anchors as intersection between our bridge expolygon and the lower slices
|
||||
my $anchors = intersection_ex(
|
||||
$grown,
|
||||
[ map @$_, @lower ],
|
||||
1, # safety offset required to avoid Clipper from detecting empty intersection while Boost actually found some @edges
|
||||
);
|
||||
my $clip_area = $self->expolygon->offset_ex(+$self->extrusion_width/2);
|
||||
|
||||
if (@$anchors) {
|
||||
# we'll now try several directions using a rudimentary visibility check:
|
||||
|
@ -74,7 +83,7 @@ sub detect_angle {
|
|||
# endpoints within anchors
|
||||
my %directions_coverage = (); # angle => score
|
||||
my %directions_avg_length = (); # angle => score
|
||||
my $line_increment = $self->infill_flow->scaled_width;
|
||||
my $line_increment = $self->extrusion_width;
|
||||
for (my $angle = 0; $angle < PI; $angle += $self->resolution) {
|
||||
my $my_clip_area = [ map $_->clone, @$clip_area ];
|
||||
my $my_anchors = [ map $_->clone, @$anchors ];
|
||||
|
@ -113,12 +122,15 @@ sub detect_angle {
|
|||
$directions_avg_length{$angle} = @lengths ? (max(@lengths)) : -1;
|
||||
}
|
||||
|
||||
# if no direction produced coverage, then there's no bridge direction
|
||||
return undef if !defined first { $_ > 0 } values %directions_coverage;
|
||||
|
||||
# the best direction is the one causing most lines to be bridged (thus most coverage)
|
||||
# and shortest max line length
|
||||
my @sorted_directions = sort {
|
||||
my $cmp;
|
||||
my $coverage_diff = $directions_coverage{$a} - $directions_coverage{$b};
|
||||
if (abs($coverage_diff) < $self->infill_flow->scaled_width) {
|
||||
if (abs($coverage_diff) < $self->extrusion_width) {
|
||||
$cmp = $directions_avg_length{$b} <=> $directions_avg_length{$a};
|
||||
} else {
|
||||
$cmp = ($coverage_diff > 0) ? 1 : -1;
|
||||
|
@ -126,20 +138,19 @@ sub detect_angle {
|
|||
$cmp;
|
||||
} keys %directions_coverage;
|
||||
|
||||
$bridge_angle = $sorted_directions[-1];
|
||||
$self->angle($sorted_directions[-1]);
|
||||
}
|
||||
}
|
||||
|
||||
if (defined $bridge_angle) {
|
||||
if ($bridge_angle >= PI - epsilon) {
|
||||
$bridge_angle -= PI;
|
||||
if (defined $self->angle) {
|
||||
if ($self->angle >= PI - epsilon) {
|
||||
$self->angle($self->angle - PI);
|
||||
}
|
||||
|
||||
Slic3r::debugf " Optimal infill angle is %d degrees\n", rad2deg($bridge_angle)
|
||||
if defined $bridge_angle;
|
||||
Slic3r::debugf " Optimal infill angle is %d degrees\n", rad2deg($self->angle);
|
||||
}
|
||||
|
||||
return $bridge_angle;
|
||||
return $self->angle;
|
||||
}
|
||||
|
||||
1;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue