2011-10-10 15:27:00 +00:00
|
|
|
package Slic3r::Geometry::Clipper;
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
require Exporter;
|
|
|
|
our @ISA = qw(Exporter);
|
2013-03-26 12:57:37 +00:00
|
|
|
our @EXPORT_OK = qw(safety_offset safety_offset_ex offset offset_ex collapse_ex
|
2011-12-24 10:01:28 +00:00
|
|
|
diff_ex diff union_ex intersection_ex xor_ex PFT_EVENODD JT_MITER JT_ROUND
|
2013-05-11 07:24:48 +00:00
|
|
|
JT_SQUARE is_counter_clockwise union_pt offset2 offset2_ex traverse_pt);
|
2011-10-10 15:27:00 +00:00
|
|
|
|
2013-05-11 07:19:23 +00:00
|
|
|
use Math::Clipper 1.21 qw(:cliptypes :polyfilltypes :jointypes is_counter_clockwise area);
|
2011-12-02 22:35:39 +00:00
|
|
|
use Slic3r::Geometry qw(scale);
|
2011-10-10 15:27:00 +00:00
|
|
|
our $clipper = Math::Clipper->new;
|
|
|
|
|
2011-10-18 13:57:53 +00:00
|
|
|
sub safety_offset {
|
2012-06-23 18:08:08 +00:00
|
|
|
my ($polygons, $factor) = @_;
|
2013-03-31 17:40:25 +00:00
|
|
|
return Math::Clipper::int_offset($polygons, $factor // (scale 1e-05), 100000, JT_MITER, 2);
|
2011-10-18 13:57:53 +00:00
|
|
|
}
|
|
|
|
|
2013-03-26 12:57:37 +00:00
|
|
|
sub safety_offset_ex {
|
2013-03-31 17:40:25 +00:00
|
|
|
my ($polygons, $factor) = @_;
|
|
|
|
return map Slic3r::ExPolygon->new($_),
|
|
|
|
@{Math::Clipper::ex_int_offset($polygons, $factor // (scale 1e-05), 100000, JT_MITER, 2)};
|
2013-03-26 12:57:37 +00:00
|
|
|
}
|
|
|
|
|
2012-08-25 18:04:29 +00:00
|
|
|
sub offset {
|
|
|
|
my ($polygons, $distance, $scale, $joinType, $miterLimit) = @_;
|
2012-11-15 09:09:29 +00:00
|
|
|
$scale ||= 100000;
|
2012-11-04 23:17:46 +00:00
|
|
|
$joinType //= JT_MITER;
|
2013-01-13 10:50:49 +00:00
|
|
|
$miterLimit //= 3;
|
2012-08-25 18:04:29 +00:00
|
|
|
|
2013-03-31 17:40:25 +00:00
|
|
|
my $offsets = Math::Clipper::int_offset($polygons, $distance, $scale, $joinType, $miterLimit);
|
2012-08-25 18:04:29 +00:00
|
|
|
return @$offsets;
|
|
|
|
}
|
|
|
|
|
2013-05-10 11:47:40 +00:00
|
|
|
sub offset2 {
|
|
|
|
my ($polygons, $distance1, $distance2, $scale, $joinType, $miterLimit) = @_;
|
|
|
|
$scale ||= 100000;
|
|
|
|
$joinType //= JT_MITER;
|
|
|
|
$miterLimit //= 3;
|
|
|
|
|
|
|
|
my $offsets = Math::Clipper::int_offset2($polygons, $distance1, $distance2, $scale, $joinType, $miterLimit);
|
|
|
|
return @$offsets;
|
|
|
|
}
|
|
|
|
|
2012-08-25 18:04:29 +00:00
|
|
|
sub offset_ex {
|
2013-03-31 17:40:25 +00:00
|
|
|
my ($polygons, $distance, $scale, $joinType, $miterLimit) = @_;
|
|
|
|
$scale ||= 100000;
|
|
|
|
$joinType //= JT_MITER;
|
|
|
|
$miterLimit //= 3;
|
|
|
|
|
|
|
|
my $offsets = Math::Clipper::ex_int_offset($polygons, $distance, $scale, $joinType, $miterLimit);
|
|
|
|
return map Slic3r::ExPolygon->new($_), @$offsets;
|
2012-08-25 18:04:29 +00:00
|
|
|
}
|
|
|
|
|
2013-05-10 11:56:29 +00:00
|
|
|
sub offset2_ex {
|
|
|
|
my ($polygons, $delta1, $delta2, $scale, $joinType, $miterLimit) = @_;
|
|
|
|
$scale ||= 100000;
|
|
|
|
$joinType //= JT_MITER;
|
|
|
|
$miterLimit //= 3;
|
|
|
|
|
|
|
|
my $offsets = Math::Clipper::ex_int_offset2($polygons, $delta1, $delta2, $scale, $joinType, $miterLimit);
|
|
|
|
return map Slic3r::ExPolygon->new($_), @$offsets;
|
|
|
|
}
|
|
|
|
|
2011-10-10 15:27:00 +00:00
|
|
|
sub diff_ex {
|
2011-12-02 22:35:39 +00:00
|
|
|
my ($subject, $clip, $safety_offset) = @_;
|
2011-10-10 15:27:00 +00:00
|
|
|
|
|
|
|
$clipper->clear;
|
|
|
|
$clipper->add_subject_polygons($subject);
|
2011-12-02 22:35:39 +00:00
|
|
|
$clipper->add_clip_polygons($safety_offset ? safety_offset($clip) : $clip);
|
2011-11-13 20:46:32 +00:00
|
|
|
return [
|
|
|
|
map Slic3r::ExPolygon->new($_),
|
|
|
|
@{ $clipper->ex_execute(CT_DIFFERENCE, PFT_NONZERO, PFT_NONZERO) },
|
|
|
|
];
|
2011-10-10 15:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub diff {
|
2013-03-13 13:55:58 +00:00
|
|
|
my ($subject, $clip, $safety_offset) = @_;
|
|
|
|
|
|
|
|
$clipper->clear;
|
|
|
|
$clipper->add_subject_polygons($subject);
|
|
|
|
$clipper->add_clip_polygons($safety_offset ? safety_offset($clip) : $clip);
|
|
|
|
return [
|
|
|
|
map Slic3r::Polygon->new($_),
|
|
|
|
@{ $clipper->execute(CT_DIFFERENCE, PFT_NONZERO, PFT_NONZERO) },
|
|
|
|
];
|
2011-10-10 15:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub union_ex {
|
2011-12-02 22:35:39 +00:00
|
|
|
my ($polygons, $jointype, $safety_offset) = @_;
|
2011-11-11 09:21:48 +00:00
|
|
|
$jointype = PFT_NONZERO unless defined $jointype;
|
2011-10-10 15:27:00 +00:00
|
|
|
$clipper->clear;
|
2011-12-02 22:35:39 +00:00
|
|
|
$clipper->add_subject_polygons($safety_offset ? safety_offset($polygons) : $polygons);
|
2011-11-11 09:21:48 +00:00
|
|
|
return [
|
|
|
|
map Slic3r::ExPolygon->new($_),
|
|
|
|
@{ $clipper->ex_execute(CT_UNION, $jointype, $jointype) },
|
|
|
|
];
|
2011-10-10 15:27:00 +00:00
|
|
|
}
|
|
|
|
|
2013-05-09 12:52:56 +00:00
|
|
|
sub union_pt {
|
|
|
|
my ($polygons, $jointype, $safety_offset) = @_;
|
|
|
|
$jointype = PFT_NONZERO unless defined $jointype;
|
|
|
|
$clipper->clear;
|
|
|
|
$clipper->add_subject_polygons($safety_offset ? safety_offset($polygons) : $polygons);
|
|
|
|
return $clipper->pt_execute(CT_UNION, $jointype, $jointype);
|
|
|
|
}
|
|
|
|
|
2011-10-18 13:57:53 +00:00
|
|
|
sub intersection_ex {
|
2011-12-02 22:35:39 +00:00
|
|
|
my ($subject, $clip, $jointype, $safety_offset) = @_;
|
2011-11-12 10:05:32 +00:00
|
|
|
$jointype = PFT_NONZERO unless defined $jointype;
|
2011-10-18 13:57:53 +00:00
|
|
|
$clipper->clear;
|
|
|
|
$clipper->add_subject_polygons($subject);
|
2011-12-02 22:35:39 +00:00
|
|
|
$clipper->add_clip_polygons($safety_offset ? safety_offset($clip) : $clip);
|
2011-11-12 10:05:32 +00:00
|
|
|
return [
|
|
|
|
map Slic3r::ExPolygon->new($_),
|
|
|
|
@{ $clipper->ex_execute(CT_INTERSECTION, $jointype, $jointype) },
|
|
|
|
];
|
2011-10-18 13:57:53 +00:00
|
|
|
}
|
|
|
|
|
2011-12-24 10:01:28 +00:00
|
|
|
sub xor_ex {
|
|
|
|
my ($subject, $clip, $jointype) = @_;
|
|
|
|
$jointype = PFT_NONZERO unless defined $jointype;
|
|
|
|
$clipper->clear;
|
|
|
|
$clipper->add_subject_polygons($subject);
|
|
|
|
$clipper->add_clip_polygons($clip);
|
|
|
|
return [
|
|
|
|
map Slic3r::ExPolygon->new($_),
|
|
|
|
@{ $clipper->ex_execute(CT_XOR, $jointype, $jointype) },
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2013-03-14 13:27:08 +00:00
|
|
|
sub collapse_ex {
|
2013-04-18 17:49:02 +00:00
|
|
|
my ($polygons, $width) = @_;
|
2013-05-10 11:56:29 +00:00
|
|
|
return [ offset2_ex($polygons, -$width/2, +$width/2) ];
|
2013-03-14 13:27:08 +00:00
|
|
|
}
|
|
|
|
|
2013-03-16 17:42:56 +00:00
|
|
|
sub simplify_polygon {
|
|
|
|
my ($polygon, $pft) = @_;
|
|
|
|
return @{ Math::Clipper::simplify_polygon($polygon, $pft // PFT_NONZERO) };
|
|
|
|
}
|
|
|
|
|
|
|
|
sub simplify_polygons {
|
|
|
|
my ($polygons, $pft) = @_;
|
|
|
|
return @{ Math::Clipper::simplify_polygons($polygons, $pft // PFT_NONZERO) };
|
|
|
|
}
|
|
|
|
|
2013-05-11 07:24:48 +00:00
|
|
|
sub traverse_pt {
|
2013-05-11 19:30:26 +00:00
|
|
|
my ($polynodes) = @_;
|
2013-05-11 07:24:48 +00:00
|
|
|
|
|
|
|
# use a nearest neighbor search to order these children
|
|
|
|
# TODO: supply second argument to chained_path_items() too?
|
|
|
|
my @nodes = @{Slic3r::Geometry::chained_path_items(
|
|
|
|
[ map [ ($_->{outer} ? $_->{outer}[0] : $_->{hole}[0]), $_ ], @$polynodes ],
|
|
|
|
)};
|
|
|
|
|
|
|
|
my @polygons = ();
|
|
|
|
foreach my $polynode (@$polynodes) {
|
2013-05-11 19:05:29 +00:00
|
|
|
# traverse the next depth
|
2013-05-11 19:30:26 +00:00
|
|
|
push @polygons, traverse_pt($polynode->{children});
|
|
|
|
push @polygons, $polynode->{outer} // [ reverse @{$polynode->{hole}} ];
|
2013-05-11 07:24:48 +00:00
|
|
|
}
|
|
|
|
return @polygons;
|
|
|
|
}
|
|
|
|
|
2011-10-10 15:27:00 +00:00
|
|
|
1;
|