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-07-16 07:49:34 +00:00
|
|
|
use Slic3r::Geometry qw(polygon_remove_parallel_continuous_edges
|
|
|
|
polygon_remove_acute_vertices polygon_segment_having_point
|
2013-06-22 22:20:52 +00:00
|
|
|
PI X1 X2 Y1 Y2 epsilon);
|
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;
|
|
|
|
}
|
|
|
|
|
2011-12-30 18:59:51 +00:00
|
|
|
sub merge_continuous_lines {
|
|
|
|
my $self = shift;
|
|
|
|
|
2013-07-16 07:49:34 +00:00
|
|
|
my $p = $self->pp;
|
|
|
|
polygon_remove_parallel_continuous_edges($p);
|
|
|
|
return (ref $self)->new(@$p);
|
2011-12-30 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub remove_acute_vertices {
|
|
|
|
my $self = shift;
|
|
|
|
polygon_remove_acute_vertices($self);
|
2011-10-15 09:36:05 +00:00
|
|
|
}
|
|
|
|
|
2011-11-13 17:14:02 +00:00
|
|
|
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]);
|
2011-11-13 17:14:02 +00:00
|
|
|
}
|
|
|
|
|
2013-03-09 16:07:11 +00:00
|
|
|
sub grow {
|
|
|
|
my $self = shift;
|
|
|
|
return $self->split_at_first_point->grow(@_);
|
|
|
|
}
|
|
|
|
|
2013-07-20 10:22:41 +00:00
|
|
|
# NOTE that this will turn the polygon to ccw regardless of its
|
|
|
|
# original orientation
|
2013-03-16 17:42:56 +00:00
|
|
|
sub simplify {
|
|
|
|
my $self = shift;
|
2013-08-08 00:10:34 +00:00
|
|
|
return @{Slic3r::Geometry::Clipper::simplify_polygons([ $self->SUPER::simplify(@_) ])};
|
2013-03-16 17:42:56 +00:00
|
|
|
}
|
|
|
|
|
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) = @_;
|
|
|
|
|
|
|
|
for (my $i = 0; $i <= $#$self; $i++) {
|
|
|
|
my $len = Slic3r::Geometry::line_length([ $self->[$i-1], $self->[$i] ]);
|
|
|
|
my $num_points = int($len / $max_length) - 1;
|
|
|
|
$num_points++ if $len % $max_length;
|
|
|
|
|
|
|
|
# $num_points is the number of points to add between $i-1 and $i
|
2012-03-03 20:25:26 +00:00
|
|
|
next if $num_points == -1;
|
2011-12-30 16:17:37 +00:00
|
|
|
my $spacing = $len / ($num_points + 1);
|
2011-12-30 18:59:51 +00:00
|
|
|
my @new_points = map Slic3r::Point->new($_),
|
|
|
|
map Slic3r::Geometry::point_along_segment($self->[$i-1], $self->[$i], $spacing * $_),
|
2011-12-30 16:17:37 +00:00
|
|
|
1..$num_points;
|
|
|
|
|
|
|
|
splice @$self, $i, 0, @new_points;
|
|
|
|
$i += @new_points;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07-16 15:13:01 +00:00
|
|
|
my @points = @{$self->pp};
|
2013-06-21 17:43:15 +00:00
|
|
|
return map $self->[$_],
|
2013-07-16 15:13:01 +00:00
|
|
|
grep Slic3r::Geometry::angle3points(@points[$_, $_-1, $_+1]) < PI - epsilon,
|
|
|
|
-1 .. ($#points-1);
|
2013-06-21 17:43:15 +00:00
|
|
|
}
|
|
|
|
|
2011-10-15 09:36:05 +00:00
|
|
|
1;
|