Bugfix: wrong logic for concave_points() and convex_points()
This commit is contained in:
parent
7ea09a0071
commit
abdf6531f1
2 changed files with 55 additions and 13 deletions
|
@ -45,34 +45,52 @@ sub subdivide {
|
||||||
return Slic3r::Polygon->new(@new_points);
|
return Slic3r::Polygon->new(@new_points);
|
||||||
}
|
}
|
||||||
|
|
||||||
# angle is checked on the internal side of the polygon
|
|
||||||
sub concave_points {
|
sub concave_points {
|
||||||
my ($self, $angle) = @_;
|
my ($self, $angle) = @_;
|
||||||
|
|
||||||
$angle //= PI;
|
$angle //= PI;
|
||||||
|
|
||||||
|
# input angle threshold is checked on the internal side of the polygon
|
||||||
|
# but angle3points measures CCW angle, so we calculate the complementary angle
|
||||||
|
my $ccw_angle = 2*PI-$angle;
|
||||||
|
|
||||||
my @points = @$self;
|
my @points = @$self;
|
||||||
my @points_pp = @{$self->pp};
|
my @points_pp = @{$self->pp};
|
||||||
return [
|
|
||||||
map $points[$_],
|
my @concave = ();
|
||||||
grep Slic3r::Geometry::angle3points(@points_pp[$_, $_-1, $_+1]) < $angle,
|
for my $i (-1 .. ($#points-1)) {
|
||||||
-1 .. ($#points-1)
|
next if $points[$i-1]->coincides_with($points[$i]);
|
||||||
];
|
# angle is measured in ccw orientation
|
||||||
|
my $vertex_angle = Slic3r::Geometry::angle3points(@points_pp[$i, $i-1, $i+1]);
|
||||||
|
if ($vertex_angle <= $ccw_angle) {
|
||||||
|
push @concave, $points[$i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return [@concave];
|
||||||
}
|
}
|
||||||
|
|
||||||
# angle is checked on the internal side of the polygon
|
|
||||||
sub convex_points {
|
sub convex_points {
|
||||||
my ($self, $angle) = @_;
|
my ($self, $angle) = @_;
|
||||||
|
|
||||||
$angle //= PI;
|
$angle //= PI;
|
||||||
|
|
||||||
|
# input angle threshold is checked on the internal side of the polygon
|
||||||
|
# but angle3points measures CCW angle, so we calculate the complementary angle
|
||||||
|
my $ccw_angle = 2*PI-$angle;
|
||||||
|
|
||||||
my @points = @$self;
|
my @points = @$self;
|
||||||
my @points_pp = @{$self->pp};
|
my @points_pp = @{$self->pp};
|
||||||
return [
|
|
||||||
map $points[$_],
|
my @convex = ();
|
||||||
grep Slic3r::Geometry::angle3points(@points_pp[$_, $_-1, $_+1]) > $angle,
|
for my $i (-1 .. ($#points-1)) {
|
||||||
-1 .. ($#points-1)
|
next if $points[$i-1]->coincides_with($points[$i]);
|
||||||
];
|
# angle is measured in ccw orientation
|
||||||
|
my $vertex_angle = Slic3r::Geometry::angle3points(@points_pp[$i, $i-1, $i+1]);
|
||||||
|
if ($vertex_angle >= $ccw_angle) {
|
||||||
|
push @convex, $points[$i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return [@convex];
|
||||||
}
|
}
|
||||||
|
|
||||||
1;
|
1;
|
26
t/geometry.t
26
t/geometry.t
|
@ -2,7 +2,7 @@ use Test::More;
|
||||||
use strict;
|
use strict;
|
||||||
use warnings;
|
use warnings;
|
||||||
|
|
||||||
plan tests => 29;
|
plan tests => 33;
|
||||||
|
|
||||||
BEGIN {
|
BEGIN {
|
||||||
use FindBin;
|
use FindBin;
|
||||||
|
@ -189,3 +189,27 @@ my $polygons = [
|
||||||
}
|
}
|
||||||
|
|
||||||
#==========================================================
|
#==========================================================
|
||||||
|
|
||||||
|
{
|
||||||
|
my $square = Slic3r::Polygon->new(
|
||||||
|
[150,100],
|
||||||
|
[200,100],
|
||||||
|
[200,200],
|
||||||
|
[100,200],
|
||||||
|
[100,100],
|
||||||
|
);
|
||||||
|
is scalar(@{$square->concave_points(PI*4/3)}), 0, 'no concave vertices detected in convex polygon';
|
||||||
|
is scalar(@{$square->convex_points(PI*2/3)}), 4, 'four convex vertices detected in square';
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
my $square = Slic3r::Polygon->new(
|
||||||
|
[200,200],
|
||||||
|
[100,200],
|
||||||
|
[100,100],
|
||||||
|
[150,100],
|
||||||
|
[200,100],
|
||||||
|
);
|
||||||
|
is scalar(@{$square->concave_points(PI*4/3)}), 0, 'no concave vertices detected in convex polygon';
|
||||||
|
is scalar(@{$square->convex_points(PI*2/3)}), 4, 'four convex vertices detected in square';
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue