Refactoring: move direction math into a single function. Includes some fixes and adjustments

This commit is contained in:
Alessandro Ranellucci 2014-05-02 18:46:22 +02:00
parent 8240f71d07
commit cb1527f7ef
9 changed files with 59 additions and 13 deletions

View file

@ -20,7 +20,7 @@ our @EXPORT_OK = qw(
rad2deg_dir bounding_box_center line_intersects_any douglas_peucker
polyline_remove_short_segments normal triangle_normal polygon_is_convex
scaled_epsilon bounding_box_3D size_3D size_2D
convex_hull
convex_hull directions_parallel directions_parallel_within
);

View file

@ -2,7 +2,7 @@ package Slic3r::Layer::BridgeDetector;
use Moo;
use List::Util qw(first sum max min);
use Slic3r::Geometry qw(PI unscale scaled_epsilon rad2deg epsilon);
use Slic3r::Geometry qw(PI unscale scaled_epsilon rad2deg epsilon directions_parallel_within);
use Slic3r::Geometry::Clipper qw(intersection_pl intersection_ex union offset diff_pl union_ex);
has 'expolygon' => (is => 'ro', required => 1);
@ -84,10 +84,11 @@ sub detect_angle {
# remove duplicates
my $min_resolution = PI/180; # 1 degree
@angles = map { ($_ >= &PI-&epsilon) ? ($_-&PI) : $_ } @angles;
@angles = sort @angles;
for (my $i = 1; $i <= $#angles; ++$i) {
if (abs($angles[$i] - $angles[$i-1]) < $min_resolution) {
# proceed in reverse order so that when we compare first value with last one (-1)
# we remove the greatest one (PI) in case they are parallel (PI, 0)
@angles = reverse sort @angles;
for (my $i = 0; $i <= $#angles; ++$i) {
if (directions_parallel_within($angles[$i], $angles[$i-1], $min_resolution)) {
splice @angles, $i, 1;
--$i;
}
@ -246,8 +247,13 @@ sub unsupported_edges {
);
# split into individual segments and filter out edges parallel to the bridging angle
# TODO: angle tolerance should probably be based on segment length and flow width,
# so that we build supports whenever there's a chance that at least one or two bridge
# extrusions would be anchored within such length (i.e. a slightly non-parallel bridging
# direction might still benefit from anchors if long enough)
my $angle_tolerance = PI/180*5;
@$unsupported = map $_->as_polyline,
grep !$_->parallel_to($angle),
grep !directions_parallel_within($_->direction, $angle, $angle_tolerance),
map @{$_->lines},
@$unsupported;

View file

@ -34,7 +34,7 @@ sub is_straight {
# first point and last point. (Checking each line against the previous
# one would have caused the error to accumulate.)
my $dir = Slic3r::Line->new($self->first_point, $self->last_point)->direction;
return !defined first { abs($_->direction - $dir) > epsilon } @{$self->lines};
return !defined first { !$_->parallel_to($dir) } @{$self->lines};
}
1;