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
|
2012-09-12 14:30:44 +00:00
|
|
|
polygon_remove_acute_vertices polygon_segment_having_point point_in_polygon);
|
2011-11-19 15:08:00 +00:00
|
|
|
use Slic3r::Geometry::Clipper qw(JT_MITER);
|
2011-10-15 09:36:05 +00:00
|
|
|
|
2011-11-11 09:21:48 +00:00
|
|
|
sub lines {
|
|
|
|
my $self = shift;
|
2012-07-01 22:14:04 +00:00
|
|
|
return polygon_lines($self);
|
2011-11-11 09:21:48 +00:00
|
|
|
}
|
|
|
|
|
2012-06-14 11:33:59 +00:00
|
|
|
sub boost_linestring {
|
|
|
|
my $self = shift;
|
|
|
|
return Boost::Geometry::Utils::linestring([@$self, $self->[0]]);
|
|
|
|
}
|
|
|
|
|
2012-11-06 19:31:50 +00:00
|
|
|
sub wkt {
|
|
|
|
my $self = shift;
|
|
|
|
return sprintf "POLYGON((%s))", join ',', map "$_->[0] $_->[1]", @$self;
|
|
|
|
}
|
|
|
|
|
2012-04-02 09:40:12 +00:00
|
|
|
sub is_counter_clockwise {
|
|
|
|
my $self = shift;
|
2012-11-04 23:17:46 +00:00
|
|
|
return Slic3r::Geometry::Clipper::is_counter_clockwise($self);
|
2012-04-02 09:40:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub make_counter_clockwise {
|
|
|
|
my $self = shift;
|
2012-12-05 10:31:35 +00:00
|
|
|
if (!$self->is_counter_clockwise) {
|
|
|
|
$self->reverse;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2012-04-02 09:40:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub make_clockwise {
|
|
|
|
my $self = shift;
|
2012-12-05 10:31:35 +00:00
|
|
|
if ($self->is_counter_clockwise) {
|
|
|
|
$self->reverse;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2012-04-02 09:40:12 +00:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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;
|
2012-08-25 18:04:29 +00:00
|
|
|
return map Slic3r::Polygon->new($_), Slic3r::Geometry::Clipper::offset([$self], @_);
|
2011-11-19 15:08:00 +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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2012-06-06 15:29:12 +00:00
|
|
|
my ($flow_width) = @_;
|
2011-12-30 18:59:51 +00:00
|
|
|
|
|
|
|
# try to get an inwards offset
|
|
|
|
# for a distance equal to half of the extrusion width;
|
2012-05-05 15:07:38 +00:00
|
|
|
# if no offset is possible, then polyline is not printable.
|
|
|
|
# we use flow_width here because this has to be consistent
|
|
|
|
# with the thin wall detection in Layer->make_surfaces,
|
|
|
|
# otherwise we could lose surfaces as that logic wouldn't
|
|
|
|
# detect them and we would be discarding them.
|
2011-12-30 18:59:51 +00:00
|
|
|
my $p = $self->clone;
|
2012-02-21 19:16:03 +00:00
|
|
|
$p->make_counter_clockwise;
|
2012-09-12 14:30:44 +00:00
|
|
|
return $p->offset(Slic3r::Geometry::scale($flow_width || $Slic3r::flow->width) / 2) ? 1 : 0;
|
2011-12-30 18:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-10-30 14:29:13 +00:00
|
|
|
sub split_at_index {
|
|
|
|
my $self = shift;
|
|
|
|
my ($index) = @_;
|
|
|
|
|
|
|
|
return (ref $self)->new(
|
|
|
|
@$self[$index .. $#$self],
|
|
|
|
@$self[0 .. $index],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub split_at {
|
|
|
|
my $self = shift;
|
|
|
|
my ($point) = @_;
|
|
|
|
|
|
|
|
# find index of point
|
|
|
|
my $i = -1;
|
|
|
|
for (my $n = 0; $n <= $#$self; $n++) {
|
|
|
|
if (Slic3r::Geometry::same_point($point, $self->[$n])) {
|
|
|
|
$i = $n;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
die "Point not found" if $i == -1;
|
|
|
|
|
|
|
|
return $self->split_at_index($i);
|
|
|
|
}
|
|
|
|
|
2012-10-31 18:08:29 +00:00
|
|
|
sub split_at_first_point {
|
|
|
|
my $self = shift;
|
|
|
|
return $self->split_at_index(0);
|
|
|
|
}
|
|
|
|
|
2011-10-15 09:36:05 +00:00
|
|
|
1;
|