2011-10-15 09:36:05 +00:00
|
|
|
package Slic3r::Polygon;
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
# a polygon is a closed polyline.
|
2011-12-30 18:59:51 +00:00
|
|
|
use parent 'Slic3r::Polyline';
|
2011-10-15 09:36:05 +00:00
|
|
|
|
2014-11-15 22:06:15 +00:00
|
|
|
use Slic3r::Geometry qw(PI);
|
2012-11-06 19:31:50 +00:00
|
|
|
|
2014-05-12 20:28:26 +00:00
|
|
|
sub dump_perl {
|
|
|
|
my $self = shift;
|
|
|
|
return sprintf "[%s]", join ',', map "[$_->[0],$_->[1]]", @$self;
|
|
|
|
}
|
|
|
|
|
2013-03-09 16:07:11 +00:00
|
|
|
sub grow {
|
|
|
|
my $self = shift;
|
|
|
|
return $self->split_at_first_point->grow(@_);
|
|
|
|
}
|
|
|
|
|
2011-12-30 16:17:37 +00:00
|
|
|
# this method subdivides the polygon segments to that no one of them
|
|
|
|
# is longer than the length provided
|
|
|
|
sub subdivide {
|
|
|
|
my $self = shift;
|
|
|
|
my ($max_length) = @_;
|
|
|
|
|
2013-09-16 15:44:30 +00:00
|
|
|
my @points = @$self;
|
|
|
|
push @points, $points[0]; # append first point as this is a polygon
|
|
|
|
my @new_points = shift @points;
|
|
|
|
while (@points) {
|
|
|
|
while ($new_points[-1]->distance_to($points[0]) > $max_length) {
|
|
|
|
push @new_points, map Slic3r::Point->new(@$_),
|
|
|
|
Slic3r::Geometry::point_along_segment($new_points[-1], $points[0], $max_length);
|
|
|
|
}
|
|
|
|
push @new_points, shift @points;
|
2011-12-30 16:17:37 +00:00
|
|
|
}
|
2013-09-16 15:44:30 +00:00
|
|
|
pop @new_points; # remove last point as it coincides with first one
|
|
|
|
return Slic3r::Polygon->new(@new_points);
|
2011-12-30 16:17:37 +00:00
|
|
|
}
|
|
|
|
|
2013-06-21 17:43:15 +00:00
|
|
|
sub concave_points {
|
2014-05-22 17:34:49 +00:00
|
|
|
my ($self, $angle) = @_;
|
|
|
|
|
|
|
|
$angle //= PI;
|
|
|
|
|
2014-05-26 09:50:42 +00:00
|
|
|
# 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;
|
|
|
|
|
2014-07-25 10:04:33 +00:00
|
|
|
my @concave = ();
|
2014-05-22 17:34:49 +00:00
|
|
|
my @points = @$self;
|
|
|
|
my @points_pp = @{$self->pp};
|
2014-07-25 10:04:33 +00:00
|
|
|
|
2014-05-26 09:50:42 +00:00
|
|
|
for my $i (-1 .. ($#points-1)) {
|
|
|
|
# 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];
|
|
|
|
}
|
|
|
|
}
|
2014-07-25 10:04:33 +00:00
|
|
|
|
2014-05-26 09:50:42 +00:00
|
|
|
return [@concave];
|
2014-05-22 17:34:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub convex_points {
|
|
|
|
my ($self, $angle) = @_;
|
|
|
|
|
|
|
|
$angle //= PI;
|
2013-06-21 17:43:15 +00:00
|
|
|
|
2014-05-26 09:50:42 +00:00
|
|
|
# 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;
|
|
|
|
|
2014-07-25 10:04:33 +00:00
|
|
|
my @convex = ();
|
2013-09-16 16:42:28 +00:00
|
|
|
my @points = @$self;
|
|
|
|
my @points_pp = @{$self->pp};
|
2014-05-26 09:50:42 +00:00
|
|
|
|
|
|
|
for my $i (-1 .. ($#points-1)) {
|
|
|
|
# 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];
|
2013-06-21 17:43:15 +00:00
|
|
|
}
|
|
|
|
|
2011-10-15 09:36:05 +00:00
|
|
|
1;
|