PrusaSlicer-NonPlainar/lib/Slic3r/Polygon.pm

78 lines
2.1 KiB
Perl
Raw Normal View History

package Slic3r::Polygon;
use strict;
use warnings;
# a polygon is a closed polyline.
use parent 'Slic3r::Polyline';
use Slic3r::Geometry qw(polygon_remove_parallel_continuous_edges
polygon_remove_acute_vertices polygon_segment_having_point
PI X1 X2 Y1 Y2 epsilon);
2012-11-06 19:31:50 +00:00
sub wkt {
my $self = shift;
return sprintf "POLYGON((%s))", join ',', map "$_->[0] $_->[1]", @$self;
}
sub merge_continuous_lines {
my $self = shift;
my $p = $self->pp;
polygon_remove_parallel_continuous_edges($p);
return __PACKAGE__->new(@$p);
}
sub remove_acute_vertices {
my $self = shift;
polygon_remove_acute_vertices($self);
}
sub encloses_point {
my $self = shift;
my ($point) = @_;
2013-07-16 15:13:01 +00:00
return Boost::Geometry::Utils::point_covered_by_polygon($point->pp, [$self->pp]);
}
sub grow {
my $self = shift;
return $self->split_at_first_point->grow(@_);
}
# NOTE that this will turn the polygon to ccw regardless of its
# original orientation
sub simplify {
my $self = shift;
return @{Slic3r::Geometry::Clipper::simplify_polygons([ $self->SUPER::simplify(@_) ])};
}
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
}
# for cw polygons this will return convex points!
sub concave_points {
my $self = shift;
2013-07-16 15:13:01 +00:00
my @points = @{$self->pp};
return map $self->[$_],
2013-07-16 15:13:01 +00:00
grep Slic3r::Geometry::angle3points(@points[$_, $_-1, $_+1]) < PI - epsilon,
-1 .. ($#points-1);
}
1;