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
|
|
|
|
2011-11-13 17:14:02 +00:00
|
|
|
use Slic3r::Geometry qw(polygon_lines polygon_remove_parallel_continuous_edges
|
2011-12-30 18:59:51 +00:00
|
|
|
scale polygon_remove_acute_vertices
|
2011-11-13 17:14:02 +00:00
|
|
|
polygon_segment_having_point point_in_polygon move_points rotate_points);
|
2011-11-19 15:08:00 +00:00
|
|
|
use Slic3r::Geometry::Clipper qw(JT_MITER);
|
2011-10-15 09:36:05 +00:00
|
|
|
|
|
|
|
# the constructor accepts an array(ref) of points
|
|
|
|
sub new {
|
|
|
|
my $class = shift;
|
|
|
|
my $self;
|
|
|
|
if (@_ == 1) {
|
|
|
|
$self = [ @{$_[0]} ];
|
|
|
|
} else {
|
|
|
|
$self = [ @_ ];
|
|
|
|
}
|
2011-11-11 09:21:48 +00:00
|
|
|
|
2011-10-15 09:36:05 +00:00
|
|
|
bless $self, $class;
|
2011-11-13 17:14:02 +00:00
|
|
|
bless $_, 'Slic3r::Point' for @$self;
|
2011-10-15 09:36:05 +00:00
|
|
|
$self;
|
|
|
|
}
|
|
|
|
|
2011-12-30 16:17:37 +00:00
|
|
|
sub clone {
|
|
|
|
my $self = shift;
|
|
|
|
return (ref $self)->new(map $_->clone, @$self);
|
|
|
|
}
|
|
|
|
|
2011-11-11 09:21:48 +00:00
|
|
|
sub lines {
|
|
|
|
my $self = shift;
|
2012-02-25 21:15:34 +00:00
|
|
|
my @lines = polygon_lines($self);
|
|
|
|
bless $_, 'Slic3r::Line' for @lines;
|
|
|
|
return @lines;
|
2011-11-11 09:21:48 +00:00
|
|
|
}
|
|
|
|
|
2011-10-15 09:36:05 +00:00
|
|
|
sub cleanup {
|
|
|
|
my $self = shift;
|
2011-12-30 18:59:51 +00:00
|
|
|
$self->merge_continuous_lines;
|
2012-01-21 17:44:24 +00:00
|
|
|
return @$self >= 3;
|
2011-12-30 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub merge_continuous_lines {
|
|
|
|
my $self = shift;
|
|
|
|
|
2011-10-15 09:36:05 +00:00
|
|
|
polygon_remove_parallel_continuous_edges($self);
|
2011-12-30 18:59:51 +00:00
|
|
|
bless $_, 'Slic3r::Point' for @$self;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub remove_acute_vertices {
|
|
|
|
my $self = shift;
|
|
|
|
polygon_remove_acute_vertices($self);
|
|
|
|
bless $_, 'Slic3r::Point' for @$self;
|
2011-10-15 09:36:05 +00:00
|
|
|
}
|
|
|
|
|
2011-11-13 17:14:02 +00:00
|
|
|
sub point_on_segment {
|
|
|
|
my $self = shift;
|
|
|
|
my ($point) = @_;
|
|
|
|
return polygon_segment_having_point($self, $point);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub encloses_point {
|
|
|
|
my $self = shift;
|
|
|
|
my ($point) = @_;
|
|
|
|
return point_in_polygon($point, $self);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub translate {
|
|
|
|
my $self = shift;
|
|
|
|
my ($x, $y) = @_;
|
|
|
|
@$self = move_points([$x, $y], @$self);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub rotate {
|
|
|
|
my $self = shift;
|
|
|
|
my ($angle, $center) = @_;
|
|
|
|
@$self = rotate_points($angle, $center, @$self);
|
|
|
|
}
|
|
|
|
|
2011-11-16 15:35:20 +00:00
|
|
|
sub area {
|
|
|
|
my $self = shift;
|
|
|
|
return Slic3r::Geometry::Clipper::area($self);
|
|
|
|
}
|
|
|
|
|
2011-11-17 09:34:40 +00:00
|
|
|
sub safety_offset {
|
|
|
|
my $self = shift;
|
|
|
|
return (ref $self)->new(Slic3r::Geometry::Clipper::safety_offset([$self])->[0]);
|
|
|
|
}
|
|
|
|
|
2011-11-19 15:08:00 +00:00
|
|
|
sub offset {
|
|
|
|
my $self = shift;
|
|
|
|
my ($distance, $scale, $joinType, $miterLimit) = @_;
|
|
|
|
$scale ||= $Slic3r::resolution * 1000000;
|
|
|
|
$joinType = JT_MITER if !defined $joinType;
|
|
|
|
$miterLimit ||= 2;
|
|
|
|
|
|
|
|
my $offsets = Math::Clipper::offset([$self], $distance, $scale, $joinType, $miterLimit);
|
|
|
|
return @$offsets;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-30 18:59:51 +00:00
|
|
|
# returns false if the polyline is too tight to be printed
|
|
|
|
sub is_printable {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
# try to get an inwards offset
|
|
|
|
# for a distance equal to half of the extrusion width;
|
|
|
|
# if no offset is possible, then polyline is not printable
|
|
|
|
my $p = $self->clone;
|
2012-02-21 19:16:03 +00:00
|
|
|
$p->make_counter_clockwise;
|
2011-12-30 18:59:51 +00:00
|
|
|
my $offsets = Math::Clipper::offset([$p], -(scale $Slic3r::flow_spacing / 2), $Slic3r::resolution * 100000, JT_MITER, 2);
|
|
|
|
return @$offsets ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub is_valid {
|
|
|
|
my $self = shift;
|
2012-01-27 13:29:06 +00:00
|
|
|
return @$self >= 3;
|
2011-12-30 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
2011-10-15 09:36:05 +00:00
|
|
|
1;
|