2011-10-10 15:27:00 +00:00
|
|
|
package Slic3r::Geometry::Clipper;
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
require Exporter;
|
|
|
|
our @ISA = qw(Exporter);
|
2011-11-16 08:52:09 +00:00
|
|
|
our @EXPORT_OK = qw(explode_expolygon explode_expolygons safety_offset offset
|
2011-12-24 10:01:28 +00:00
|
|
|
diff_ex diff union_ex intersection_ex xor_ex PFT_EVENODD JT_MITER JT_ROUND
|
2012-08-06 18:26:08 +00:00
|
|
|
JT_SQUARE is_counter_clockwise);
|
2011-10-10 15:27:00 +00:00
|
|
|
|
2012-06-21 08:53:04 +00:00
|
|
|
use Math::Clipper 1.09 ':all';
|
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 explode_expolygon {
|
|
|
|
my ($expolygon) = @_;
|
|
|
|
return ($expolygon->{outer}, @{ $expolygon->{holes} });
|
|
|
|
}
|
|
|
|
|
|
|
|
sub explode_expolygons {
|
|
|
|
my ($expolygons) = @_;
|
|
|
|
return map explode_expolygon($_), @$expolygons;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub safety_offset {
|
2012-06-23 18:08:08 +00:00
|
|
|
my ($polygons, $factor) = @_;
|
|
|
|
return Math::Clipper::offset($polygons, $factor || (scale 1e-05), 100, JT_MITER, 2);
|
2011-10-18 13:57:53 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2011-11-13 20:46:32 +00:00
|
|
|
return [ map @$_, diff_ex(@_) ];
|
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
|
|
|
}
|
|
|
|
|
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) },
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2011-10-10 15:27:00 +00:00
|
|
|
1;
|