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
|
|
|
|
2013-11-22 15:19:15 +00:00
|
|
|
use Slic3r::Geometry qw(
|
|
|
|
polygon_segment_having_point
|
2013-06-22 22:20:52 +00:00
|
|
|
PI X1 X2 Y1 Y2 epsilon);
|
2014-04-08 12:51:55 +00:00
|
|
|
use Slic3r::Geometry::Clipper qw(intersection_pl);
|
2011-10-15 09:36:05 +00:00
|
|
|
|
2012-11-06 19:31:50 +00:00
|
|
|
sub wkt {
|
|
|
|
my $self = shift;
|
|
|
|
return sprintf "POLYGON((%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-22 13:22:58 +00:00
|
|
|
# for cw polygons this will return convex points!
|
2013-06-21 17:43:15 +00:00
|
|
|
sub concave_points {
|
|
|
|
my $self = shift;
|
|
|
|
|
2013-09-16 16:42:28 +00:00
|
|
|
my @points = @$self;
|
|
|
|
my @points_pp = @{$self->pp};
|
|
|
|
return map $points[$_],
|
|
|
|
grep Slic3r::Geometry::angle3points(@points_pp[$_, $_-1, $_+1]) < PI - epsilon,
|
2013-07-16 15:13:01 +00:00
|
|
|
-1 .. ($#points-1);
|
2013-06-21 17:43:15 +00:00
|
|
|
}
|
|
|
|
|
2014-04-08 12:51:55 +00:00
|
|
|
sub clip_as_polyline {
|
|
|
|
my ($self, $polygons) = @_;
|
|
|
|
|
|
|
|
my $self_pl = $self->split_at_first_point;
|
|
|
|
|
|
|
|
# Clipper will remove a polyline segment if first point coincides with last one.
|
|
|
|
# Until that bug is not fixed upstream, we move one of those points slightly.
|
|
|
|
$self_pl->[0]->translate(1, 0);
|
|
|
|
|
|
|
|
my @polylines = @{intersection_pl([$self_pl], $polygons)};
|
2014-04-08 13:18:37 +00:00
|
|
|
if (@polylines == 1) {
|
|
|
|
if ($polylines[0][0]->coincides_with($self_pl->[0])) {
|
|
|
|
# compensate the above workaround for Clipper bug
|
|
|
|
$polylines[0][0]->translate(-1, 0);
|
|
|
|
}
|
|
|
|
} elsif (@polylines == 2) {
|
2014-04-08 12:51:55 +00:00
|
|
|
# If the split_at_first_point() call above happens to split the polygon inside the clipping area
|
|
|
|
# we would get two consecutive polylines instead of a single one, so we use this ugly hack to
|
|
|
|
# recombine them back into a single one in order to trigger the @edges == 2 logic below.
|
|
|
|
# This needs to be replaced with something way better.
|
2014-04-08 22:36:13 +00:00
|
|
|
if ($polylines[0][-1]->coincides_with($self_pl->[-1]) && $polylines[-1][0]->coincides_with($self_pl->[0])) {
|
2014-04-08 12:51:55 +00:00
|
|
|
my $p = $polylines[0]->clone;
|
|
|
|
$p->pop_back;
|
|
|
|
$p->append(@{$polylines[-1]});
|
|
|
|
return [$p];
|
|
|
|
}
|
2014-04-08 22:36:13 +00:00
|
|
|
if ($polylines[0][0]->coincides_with($self_pl->[0]) && $polylines[-1][-1]->coincides_with($self_pl->[-1])) {
|
2014-04-08 12:51:55 +00:00
|
|
|
my $p = $polylines[-1]->clone;
|
|
|
|
$p->pop_back;
|
|
|
|
$p->append(@{$polylines[0]});
|
|
|
|
return [$p];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return [ @polylines ];
|
|
|
|
}
|
|
|
|
|
2011-10-15 09:36:05 +00:00
|
|
|
1;
|