Files
OrcaSlicer-bambulab/lib/Slic3r/Geometry/Clipper.pm
T

55 lines
1.7 KiB
Perl
Raw Normal View History

2011-10-10 17:27:00 +02:00
package Slic3r::Geometry::Clipper;
use strict;
use warnings;
require Exporter;
our @ISA = qw(Exporter);
2013-08-26 17:58:37 +02:00
our @EXPORT_OK = qw(offset offset_ex
diff_ex diff union_ex intersection_ex xor_ex PFT_EVENODD JT_MITER JT_ROUND
2013-05-31 14:30:07 +02:00
JT_SQUARE is_counter_clockwise union_pt offset2 offset2_ex traverse_pt
2013-07-29 20:49:54 +02:00
intersection union);
2011-10-10 17:27:00 +02:00
use Math::Clipper 1.22 qw(:cliptypes :polyfilltypes :jointypes is_counter_clockwise area);
use Slic3r::Geometry qw(scale);
2011-10-10 17:27:00 +02:00
our $clipper = Math::Clipper->new;
2013-08-26 17:58:37 +02:00
sub _safety_offset {
my ($polygons, $factor) = @_;
return [ map Slic3r::Polygon->new(@$_),
@{Math::Clipper::int_offset(_convert($polygons), $factor // (scale 1e-05), 100000, JT_MITER, 2)} ];
}
2013-05-09 14:52:56 +02:00
sub union_pt {
my ($polygons, $jointype, $safety_offset) = @_;
$jointype = PFT_NONZERO unless defined $jointype;
$clipper->clear;
2013-08-26 17:58:37 +02:00
$clipper->add_subject_polygons($safety_offset ? _convert(_safety_offset($polygons)) : _convert($polygons));
2013-05-09 14:52:56 +02:00
return $clipper->pt_execute(CT_UNION, $jointype, $jointype);
}
2013-05-11 09:24:48 +02:00
sub traverse_pt {
2013-05-11 21:30:26 +02:00
my ($polynodes) = @_;
2013-05-11 09:24:48 +02: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 21:05:29 +02:00
# traverse the next depth
2013-05-11 21:30:26 +02:00
push @polygons, traverse_pt($polynode->{children});
push @polygons, $polynode->{outer} // [ reverse @{$polynode->{hole}} ];
2013-05-11 09:24:48 +02:00
}
return @polygons;
}
2013-07-15 20:31:43 +02:00
sub _convert {
my $p = shift;
$p = $p->pp if ref($p) ne 'ARRAY' && $p->can('pp');
2013-07-16 17:13:01 +02:00
return [ map { (ref($_) ne 'ARRAY' && $_->can('pp')) ? $_->pp : $_ } @$p ];
2013-07-15 20:31:43 +02:00
}
2011-10-10 17:27:00 +02:00
1;