2011-10-15 09:36:05 +00:00
|
|
|
package Slic3r::ExPolygon;
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
# an ExPolygon is a polygon with holes
|
|
|
|
|
2012-04-09 09:04:32 +00:00
|
|
|
use Boost::Geometry::Utils;
|
2013-03-14 13:27:08 +00:00
|
|
|
use List::Util qw(first);
|
2011-12-30 16:17:37 +00:00
|
|
|
use Math::Geometry::Voronoi;
|
2013-03-31 17:50:22 +00:00
|
|
|
use Slic3r::Geometry qw(X Y A B point_in_polygon same_line epsilon);
|
2011-11-19 15:08:00 +00:00
|
|
|
use Slic3r::Geometry::Clipper qw(union_ex JT_MITER);
|
2013-07-05 12:46:32 +00:00
|
|
|
use Storable qw();
|
2011-10-15 09:36:05 +00:00
|
|
|
|
|
|
|
# the constructor accepts an array of polygons
|
|
|
|
# or a Math::Clipper ExPolygon (hashref)
|
|
|
|
sub new {
|
|
|
|
my $class = shift;
|
|
|
|
my $self;
|
|
|
|
if (@_ == 1 && ref $_[0] eq 'HASH') {
|
|
|
|
$self = [
|
2013-07-05 12:29:57 +00:00
|
|
|
Slic3r::Polygon->new(@{$_[0]{outer}}),
|
|
|
|
map Slic3r::Polygon->new(@$_), @{$_[0]{holes}},
|
2011-10-15 09:36:05 +00:00
|
|
|
];
|
|
|
|
} else {
|
2013-07-05 12:29:57 +00:00
|
|
|
$self = [ map Slic3r::Polygon->new(@$_), @_ ];
|
2011-10-15 09:36:05 +00:00
|
|
|
}
|
|
|
|
bless $self, $class;
|
|
|
|
$self;
|
|
|
|
}
|
|
|
|
|
2011-12-30 16:17:37 +00:00
|
|
|
sub clone {
|
2013-07-05 12:46:32 +00:00
|
|
|
Storable::dclone($_[0])
|
2011-12-30 16:17:37 +00:00
|
|
|
}
|
|
|
|
|
2013-07-06 10:14:49 +00:00
|
|
|
sub threadsafe_clone {
|
|
|
|
my $self = shift;
|
|
|
|
return (ref $self)->new(map $_->threadsafe_clone, @$self);
|
|
|
|
}
|
|
|
|
|
2011-10-15 09:36:05 +00:00
|
|
|
sub contour {
|
|
|
|
my $self = shift;
|
|
|
|
return $self->[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
sub holes {
|
|
|
|
my $self = shift;
|
|
|
|
return @$self[1..$#$self];
|
|
|
|
}
|
|
|
|
|
2011-12-30 18:59:51 +00:00
|
|
|
sub lines {
|
|
|
|
my $self = shift;
|
|
|
|
return map $_->lines, @$self;
|
|
|
|
}
|
|
|
|
|
2011-10-15 09:36:05 +00:00
|
|
|
sub clipper_expolygon {
|
|
|
|
my $self = shift;
|
|
|
|
return {
|
|
|
|
outer => $self->contour,
|
|
|
|
holes => [ $self->holes ],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-03-14 13:27:08 +00:00
|
|
|
sub is_valid {
|
|
|
|
my $self = shift;
|
|
|
|
return (!first { !$_->is_valid } @$self)
|
|
|
|
&& $self->contour->is_counter_clockwise
|
|
|
|
&& (!first { $_->is_counter_clockwise } $self->holes);
|
|
|
|
}
|
|
|
|
|
2013-03-30 10:22:12 +00:00
|
|
|
# returns false if the expolygon is too tight to be printed
|
|
|
|
sub is_printable {
|
|
|
|
my $self = shift;
|
|
|
|
my ($width) = @_;
|
|
|
|
|
|
|
|
# try to get an inwards offset
|
|
|
|
# for a distance equal to half of the extrusion width;
|
|
|
|
# if no offset is possible, then expolygon is not printable.
|
|
|
|
return Slic3r::Geometry::Clipper::offset($self, -$width / 2) ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2013-01-27 23:02:34 +00:00
|
|
|
sub wkt {
|
|
|
|
my $self = shift;
|
|
|
|
return sprintf "POLYGON(%s)",
|
|
|
|
join ',', map "($_)", map { join ',', map "$_->[0] $_->[1]", @$_ } @$self;
|
|
|
|
}
|
|
|
|
|
2011-10-15 09:36:05 +00:00
|
|
|
sub offset {
|
|
|
|
my $self = shift;
|
2012-08-25 18:04:29 +00:00
|
|
|
return Slic3r::Geometry::Clipper::offset($self, @_);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub offset_ex {
|
|
|
|
my $self = shift;
|
|
|
|
return Slic3r::Geometry::Clipper::offset_ex($self, @_);
|
2011-11-16 15:35:20 +00:00
|
|
|
}
|
|
|
|
|
2011-11-20 21:09:59 +00:00
|
|
|
sub safety_offset {
|
|
|
|
my $self = shift;
|
2013-03-26 12:57:37 +00:00
|
|
|
return Slic3r::Geometry::Clipper::safety_offset_ex($self, @_);
|
2011-11-20 21:09:59 +00:00
|
|
|
}
|
|
|
|
|
2012-08-25 12:23:46 +00:00
|
|
|
sub noncollapsing_offset_ex {
|
|
|
|
my $self = shift;
|
|
|
|
my ($distance, @params) = @_;
|
|
|
|
|
|
|
|
return $self->offset_ex($distance + 1, @params);
|
|
|
|
}
|
|
|
|
|
2011-11-13 17:14:02 +00:00
|
|
|
sub encloses_point {
|
|
|
|
my $self = shift;
|
|
|
|
my ($point) = @_;
|
2013-06-20 16:48:11 +00:00
|
|
|
return Boost::Geometry::Utils::point_covered_by_polygon($point, $self);
|
2011-11-13 17:14:02 +00:00
|
|
|
}
|
|
|
|
|
2012-06-26 22:11:46 +00:00
|
|
|
# A version of encloses_point for use when hole borders do not matter.
|
2013-06-20 16:48:11 +00:00
|
|
|
# Useful because point_on_segment is probably slower (this was true
|
|
|
|
# before the switch to Boost.Geometry, not sure about now)
|
2012-06-26 22:11:46 +00:00
|
|
|
sub encloses_point_quick {
|
|
|
|
my $self = shift;
|
|
|
|
my ($point) = @_;
|
2013-06-20 16:48:11 +00:00
|
|
|
return Boost::Geometry::Utils::point_within_polygon($point, $self);
|
2012-06-26 22:11:46 +00:00
|
|
|
}
|
|
|
|
|
2012-03-04 10:26:11 +00:00
|
|
|
sub encloses_line {
|
|
|
|
my $self = shift;
|
2012-08-24 16:59:23 +00:00
|
|
|
my ($line, $tolerance) = @_;
|
2012-03-04 10:26:11 +00:00
|
|
|
my $clip = $self->clip_line($line);
|
2012-08-24 16:59:23 +00:00
|
|
|
if (!defined $tolerance) {
|
|
|
|
# optimization
|
|
|
|
return @$clip == 1 && same_line($clip->[0], $line);
|
|
|
|
} else {
|
2013-03-31 17:50:22 +00:00
|
|
|
return @$clip == 1 && abs(Boost::Geometry::Utils::linestring_length($clip->[0]) - $line->length) < $tolerance;
|
2012-08-24 16:59:23 +00:00
|
|
|
}
|
2012-03-04 10:26:11 +00:00
|
|
|
}
|
|
|
|
|
2011-11-13 17:14:02 +00:00
|
|
|
sub bounding_box {
|
|
|
|
my $self = shift;
|
2013-06-16 10:21:25 +00:00
|
|
|
return $self->contour->bounding_box;
|
2012-02-25 13:46:21 +00:00
|
|
|
}
|
|
|
|
|
2011-11-13 17:14:02 +00:00
|
|
|
sub clip_line {
|
|
|
|
my $self = shift;
|
2012-02-25 21:15:34 +00:00
|
|
|
my ($line) = @_; # line must be a Slic3r::Line object
|
2011-11-13 17:14:02 +00:00
|
|
|
|
2013-01-27 23:32:19 +00:00
|
|
|
return Boost::Geometry::Utils::polygon_multi_linestring_intersection($self, [$line]);
|
2011-11-13 17:14:02 +00:00
|
|
|
}
|
|
|
|
|
2013-07-20 10:22:41 +00:00
|
|
|
sub simplify_as_polygons {
|
2012-02-25 13:46:21 +00:00
|
|
|
my $self = shift;
|
2013-03-16 17:42:56 +00:00
|
|
|
my ($tolerance) = @_;
|
|
|
|
|
|
|
|
# it would be nice to have a multilinestring_simplify method in B::G::U
|
2013-07-20 10:22:41 +00:00
|
|
|
return Slic3r::Geometry::Clipper::simplify_polygons(
|
2013-03-16 17:42:56 +00:00
|
|
|
[ map Boost::Geometry::Utils::linestring_simplify($_, $tolerance), @$self ],
|
|
|
|
);
|
2013-07-20 10:22:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub simplify {
|
|
|
|
my $self = shift;
|
|
|
|
my ($tolerance) = @_;
|
|
|
|
|
|
|
|
return @{ Slic3r::Geometry::Clipper::union_ex([ $self->simplify_as_polygons($tolerance) ]) };
|
2012-02-25 13:46:21 +00:00
|
|
|
}
|
|
|
|
|
2012-11-23 23:13:04 +00:00
|
|
|
sub scale {
|
|
|
|
my $self = shift;
|
|
|
|
$_->scale(@_) for @$self;
|
|
|
|
}
|
|
|
|
|
2011-11-13 17:14:02 +00:00
|
|
|
sub translate {
|
|
|
|
my $self = shift;
|
|
|
|
$_->translate(@_) for @$self;
|
2012-08-23 13:42:58 +00:00
|
|
|
$self;
|
2011-11-13 17:14:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub rotate {
|
|
|
|
my $self = shift;
|
|
|
|
$_->rotate(@_) for @$self;
|
2012-08-23 13:42:58 +00:00
|
|
|
$self;
|
2011-10-15 09:36:05 +00:00
|
|
|
}
|
|
|
|
|
2011-12-24 10:01:28 +00:00
|
|
|
sub area {
|
|
|
|
my $self = shift;
|
|
|
|
my $area = $self->contour->area;
|
|
|
|
$area -= $_->area for $self->holes;
|
|
|
|
return $area;
|
|
|
|
}
|
|
|
|
|
2011-12-30 16:17:37 +00:00
|
|
|
# this method only works for expolygons having only a contour or
|
|
|
|
# a contour and a hole, and not being thicker than the supplied
|
|
|
|
# width. it returns a polyline or a polygon
|
|
|
|
sub medial_axis {
|
|
|
|
my $self = shift;
|
|
|
|
my ($width) = @_;
|
|
|
|
|
|
|
|
my @self_lines = map $_->lines, @$self;
|
|
|
|
my $expolygon = $self->clone;
|
|
|
|
my @points = ();
|
|
|
|
foreach my $polygon (@$expolygon) {
|
|
|
|
Slic3r::Geometry::polyline_remove_short_segments($polygon, $width / 2);
|
|
|
|
|
|
|
|
# subdivide polygon segments so that we don't have anyone of them
|
|
|
|
# being longer than $width / 2
|
|
|
|
$polygon->subdivide($width/2);
|
|
|
|
|
|
|
|
push @points, @$polygon;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $voronoi = Math::Geometry::Voronoi->new(points => \@points);
|
|
|
|
$voronoi->compute;
|
|
|
|
|
|
|
|
my @skeleton_lines = ();
|
|
|
|
|
|
|
|
my $vertices = $voronoi->vertices;
|
|
|
|
my $edges = $voronoi->edges;
|
|
|
|
foreach my $edge (@$edges) {
|
|
|
|
# ignore lines going to infinite
|
|
|
|
next if $edge->[1] == -1 || $edge->[2] == -1;
|
|
|
|
|
|
|
|
my ($a, $b);
|
|
|
|
$a = $vertices->[$edge->[1]];
|
|
|
|
$b = $vertices->[$edge->[2]];
|
|
|
|
|
2012-06-26 22:11:46 +00:00
|
|
|
next if !$self->encloses_point_quick($a) || !$self->encloses_point_quick($b);
|
2011-12-30 16:17:37 +00:00
|
|
|
|
|
|
|
push @skeleton_lines, [$edge->[1], $edge->[2]];
|
|
|
|
}
|
|
|
|
|
|
|
|
# remove leafs (lines not connected to other lines at one of their endpoints)
|
|
|
|
{
|
|
|
|
my %pointmap = ();
|
|
|
|
$pointmap{$_}++ for map @$_, @skeleton_lines;
|
|
|
|
@skeleton_lines = grep {
|
|
|
|
$pointmap{$_->[A]} >= 2 && $pointmap{$_->[B]} >= 2
|
|
|
|
} @skeleton_lines;
|
|
|
|
}
|
2012-06-07 13:31:51 +00:00
|
|
|
return () if !@skeleton_lines;
|
2011-12-30 16:17:37 +00:00
|
|
|
|
2012-06-06 19:27:39 +00:00
|
|
|
# now walk along the medial axis and build continuos polylines or polygons
|
|
|
|
my @polylines = ();
|
2011-12-30 16:17:37 +00:00
|
|
|
{
|
2012-06-06 19:27:39 +00:00
|
|
|
# build a map of line endpoints
|
|
|
|
my %pointmap = (); # point_idx => [line_idx, line_idx ...]
|
|
|
|
for my $line_idx (0 .. $#skeleton_lines) {
|
|
|
|
for my $point_idx (@{$skeleton_lines[$line_idx]}) {
|
|
|
|
$pointmap{$point_idx} ||= [];
|
|
|
|
push @{$pointmap{$point_idx}}, $line_idx;
|
2011-12-30 16:17:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-06 19:27:39 +00:00
|
|
|
# build the list of available lines
|
|
|
|
my %spare_lines = map {$_ => 1} (0 .. $#skeleton_lines);
|
|
|
|
|
|
|
|
CYCLE: while (%spare_lines) {
|
|
|
|
push @polylines, [];
|
|
|
|
my $polyline = $polylines[-1];
|
|
|
|
|
|
|
|
# start from a random line
|
|
|
|
my $first_line_idx = +(keys %spare_lines)[0];
|
|
|
|
delete $spare_lines{$first_line_idx};
|
|
|
|
push @$polyline, @{ $skeleton_lines[$first_line_idx] };
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
my $last_point_id = $polyline->[-1];
|
|
|
|
my $lines_starting_here = $pointmap{$last_point_id};
|
|
|
|
|
|
|
|
# remove all the visited lines from the array
|
|
|
|
shift @$lines_starting_here
|
|
|
|
while @$lines_starting_here && !$spare_lines{$lines_starting_here->[0]};
|
|
|
|
|
|
|
|
# do we have a line starting here?
|
|
|
|
my $next_line_idx = shift @$lines_starting_here;
|
|
|
|
if (!defined $next_line_idx) {
|
|
|
|
delete $pointmap{$last_point_id};
|
|
|
|
next CYCLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
# line is not available anymore
|
|
|
|
delete $spare_lines{$next_line_idx};
|
|
|
|
|
|
|
|
# add the other point to our polyline and continue walking
|
|
|
|
push @$polyline, grep $_ ne $last_point_id, @{$skeleton_lines[$next_line_idx]};
|
2011-12-30 16:17:37 +00:00
|
|
|
}
|
|
|
|
}
|
2012-06-06 19:27:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
my @result = ();
|
|
|
|
foreach my $polyline (@polylines) {
|
|
|
|
next unless @$polyline >= 2;
|
2011-12-30 16:17:37 +00:00
|
|
|
|
2012-06-06 19:27:39 +00:00
|
|
|
# now replace point indexes with coordinates
|
|
|
|
@$polyline = map $vertices->[$_], @$polyline;
|
2011-12-30 16:17:37 +00:00
|
|
|
|
2012-06-06 19:27:39 +00:00
|
|
|
# cleanup
|
|
|
|
$polyline = Slic3r::Geometry::douglas_peucker($polyline, $width / 7);
|
|
|
|
|
|
|
|
if (Slic3r::Geometry::same_point($polyline->[0], $polyline->[-1])) {
|
|
|
|
next if @$polyline == 2;
|
|
|
|
push @result, Slic3r::Polygon->new(@$polyline[0..$#$polyline-1]);
|
|
|
|
} else {
|
2013-07-05 12:29:57 +00:00
|
|
|
push @result, Slic3r::Polyline->new(@$polyline);
|
2011-12-30 16:17:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-06 19:27:39 +00:00
|
|
|
return @result;
|
2011-12-30 16:17:37 +00:00
|
|
|
}
|
|
|
|
|
2012-11-23 23:13:04 +00:00
|
|
|
package Slic3r::ExPolygon::Collection;
|
|
|
|
use Moo;
|
|
|
|
use Slic3r::Geometry qw(X1 Y1);
|
|
|
|
|
|
|
|
has 'expolygons' => (is => 'ro', default => sub { [] });
|
|
|
|
|
|
|
|
sub clone {
|
|
|
|
my $self = shift;
|
|
|
|
return (ref $self)->new(
|
2013-07-06 10:14:49 +00:00
|
|
|
expolygons => [ map $_->threadsafe_clone, @{$self->expolygons} ],
|
2012-11-23 23:13:04 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub align_to_origin {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
my @bb = Slic3r::Geometry::bounding_box([ map @$_, map @$_, @{$self->expolygons} ]);
|
|
|
|
$_->translate(-$bb[X1], -$bb[Y1]) for @{$self->expolygons};
|
2013-06-07 21:16:02 +00:00
|
|
|
$self;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub scale {
|
|
|
|
my $self = shift;
|
|
|
|
$_->scale(@_) for @{$self->expolygons};
|
|
|
|
$self;
|
2012-11-23 23:13:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub rotate {
|
|
|
|
my $self = shift;
|
|
|
|
$_->rotate(@_) for @{$self->expolygons};
|
2013-06-07 21:16:02 +00:00
|
|
|
$self;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub translate {
|
|
|
|
my $self = shift;
|
|
|
|
$_->translate(@_) for @{$self->expolygons};
|
|
|
|
$self;
|
2012-11-23 23:13:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub size {
|
|
|
|
my $self = shift;
|
|
|
|
return [ Slic3r::Geometry::size_2D([ map @$_, map @$_, @{$self->expolygons} ]) ];
|
|
|
|
}
|
|
|
|
|
2011-10-15 09:36:05 +00:00
|
|
|
1;
|