2016-03-18 21:19:49 +00:00
|
|
|
use Test::More tests => 16;
|
2014-04-08 11:08:42 +00:00
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
BEGIN {
|
|
|
|
use FindBin;
|
|
|
|
use lib "$FindBin::Bin/../lib";
|
2017-08-18 07:58:50 +00:00
|
|
|
use local::lib "$FindBin::Bin/../local-lib";
|
2014-04-08 11:08:42 +00:00
|
|
|
}
|
|
|
|
|
2014-04-24 11:43:24 +00:00
|
|
|
use List::Util qw(first sum);
|
2014-04-08 11:08:42 +00:00
|
|
|
use Slic3r;
|
2017-07-19 08:45:39 +00:00
|
|
|
use Slic3r::Geometry qw(scale epsilon deg2rad rad2deg);
|
2014-04-08 11:08:42 +00:00
|
|
|
use Slic3r::Test;
|
|
|
|
|
2014-04-21 22:59:14 +00:00
|
|
|
{
|
|
|
|
my $test = sub {
|
|
|
|
my ($bridge_size, $rotate, $expected_angle, $tolerance) = @_;
|
2014-04-09 10:09:25 +00:00
|
|
|
|
2014-04-21 22:59:14 +00:00
|
|
|
my ($x, $y) = @$bridge_size;
|
|
|
|
my $lower = Slic3r::ExPolygon->new(
|
|
|
|
Slic3r::Polygon->new_scale([-2,-2], [$x+2,-2], [$x+2,$y+2], [-2,$y+2]),
|
|
|
|
Slic3r::Polygon->new_scale([0,0], [0,$y], [$x,$y], [$x,0]),
|
2014-04-09 10:09:25 +00:00
|
|
|
);
|
2014-04-21 22:59:14 +00:00
|
|
|
$lower->translate(scale 20, scale 20); # avoid negative coordinates for easier SVG preview
|
|
|
|
$lower->rotate(deg2rad($rotate), [$x/2,$y/2]);
|
|
|
|
my $bridge = $lower->[1]->clone;
|
|
|
|
$bridge->reverse;
|
|
|
|
$bridge = Slic3r::ExPolygon->new($bridge);
|
|
|
|
|
|
|
|
ok check_angle([$lower], $bridge, $expected_angle, $tolerance), 'correct bridge angle for O-shaped overhang';
|
|
|
|
};
|
2014-04-09 10:09:25 +00:00
|
|
|
|
2014-04-23 09:40:37 +00:00
|
|
|
$test->([20,10], 0, 90);
|
|
|
|
$test->([10,20], 0, 0);
|
2014-04-21 22:59:14 +00:00
|
|
|
$test->([20,10], 45, 135, 20);
|
|
|
|
$test->([20,10], 135, 45, 20);
|
|
|
|
}
|
2014-04-08 11:08:42 +00:00
|
|
|
|
2014-04-21 22:59:14 +00:00
|
|
|
{
|
|
|
|
my $bridge = Slic3r::ExPolygon->new(
|
|
|
|
Slic3r::Polygon->new_scale([0,0], [20,0], [20,10], [0,10]),
|
|
|
|
);
|
|
|
|
my $lower = [
|
|
|
|
Slic3r::ExPolygon->new(
|
|
|
|
Slic3r::Polygon->new_scale([-2,0], [0,0], [0,10], [-2,10]),
|
|
|
|
),
|
|
|
|
];
|
|
|
|
$_->translate(scale 20, scale 20) for $bridge, @$lower; # avoid negative coordinates for easier SVG preview
|
2014-04-08 11:08:42 +00:00
|
|
|
|
2014-04-21 22:59:14 +00:00
|
|
|
$lower->[1] = $lower->[0]->clone;
|
|
|
|
$lower->[1]->translate(scale 22, 0);
|
|
|
|
|
|
|
|
ok check_angle($lower, $bridge, 0), 'correct bridge angle for two-sided bridge';
|
|
|
|
}
|
2014-04-08 13:18:37 +00:00
|
|
|
|
2014-04-21 22:59:14 +00:00
|
|
|
{
|
|
|
|
my $bridge = Slic3r::ExPolygon->new(
|
|
|
|
Slic3r::Polygon->new_scale([0,0], [20,0], [10,10], [0,10]),
|
|
|
|
);
|
|
|
|
my $lower = [
|
|
|
|
Slic3r::ExPolygon->new(
|
|
|
|
Slic3r::Polygon->new_scale([0,0], [0,10], [10,10], [10,12], [-2,12], [-2,-2], [22,-2], [22,0]),
|
|
|
|
),
|
|
|
|
];
|
|
|
|
$_->translate(scale 20, scale 20) for $bridge, @$lower; # avoid negative coordinates for easier SVG preview
|
|
|
|
|
|
|
|
ok check_angle($lower, $bridge, 135), 'correct bridge angle for C-shaped overhang';
|
|
|
|
}
|
2014-04-08 13:18:37 +00:00
|
|
|
|
2014-04-26 14:07:43 +00:00
|
|
|
{
|
|
|
|
my $bridge = Slic3r::ExPolygon->new(
|
|
|
|
Slic3r::Polygon->new_scale([10,10],[20,10],[20,20], [10,20]),
|
|
|
|
);
|
|
|
|
my $lower = [
|
|
|
|
Slic3r::ExPolygon->new(
|
2016-11-08 08:59:25 +00:00
|
|
|
Slic3r::Polygon->new_scale([10,10],[10,20],[20,20],[30,30],[0,30],[0,0]),
|
2014-04-26 14:07:43 +00:00
|
|
|
),
|
|
|
|
];
|
|
|
|
$_->translate(scale 20, scale 20) for $bridge, @$lower; # avoid negative coordinates for easier SVG preview
|
|
|
|
|
2014-04-26 14:09:00 +00:00
|
|
|
ok check_angle($lower, $bridge, 45, undef, $bridge->area/2), 'correct bridge angle for square overhang with L-shaped anchors';
|
2014-04-26 14:07:43 +00:00
|
|
|
}
|
|
|
|
|
2014-04-08 13:18:37 +00:00
|
|
|
sub check_angle {
|
2014-04-24 11:43:24 +00:00
|
|
|
my ($lower, $bridge, $expected, $tolerance, $expected_coverage) = @_;
|
|
|
|
|
2014-11-15 21:41:22 +00:00
|
|
|
if (ref($lower) eq 'ARRAY') {
|
|
|
|
$lower = Slic3r::ExPolygon::Collection->new(@$lower);
|
|
|
|
}
|
|
|
|
|
2014-04-24 11:43:24 +00:00
|
|
|
$expected_coverage //= -1;
|
|
|
|
$expected_coverage = $bridge->area if $expected_coverage == -1;
|
2014-04-21 22:59:14 +00:00
|
|
|
|
2014-11-15 21:41:22 +00:00
|
|
|
my $bd = Slic3r::BridgeDetector->new($bridge, $lower, scale 0.5);
|
2014-04-09 10:09:25 +00:00
|
|
|
|
2014-04-21 18:44:21 +00:00
|
|
|
$tolerance //= rad2deg($bd->resolution) + epsilon;
|
2014-11-15 21:41:22 +00:00
|
|
|
$bd->detect_angle;
|
|
|
|
my $result = $bd->angle;
|
2014-04-24 11:43:24 +00:00
|
|
|
my $coverage = $bd->coverage;
|
|
|
|
is sum(map $_->area, @$coverage), $expected_coverage, 'correct coverage area';
|
2014-04-08 13:18:37 +00:00
|
|
|
|
|
|
|
# our epsilon is equal to the steps used by the bridge detection algorithm
|
|
|
|
###use XXX; YYY [ rad2deg($result), $expected ];
|
2014-04-23 09:40:37 +00:00
|
|
|
# returned value must be non-negative, check for that too
|
|
|
|
my $delta=rad2deg($result) - $expected;
|
|
|
|
$delta-=180 if $delta>=180 - epsilon;
|
|
|
|
return defined $result && $result>=0 && abs($delta) < $tolerance;
|
2014-04-08 13:18:37 +00:00
|
|
|
}
|
|
|
|
|
2016-03-18 21:19:49 +00:00
|
|
|
{
|
|
|
|
my $config = Slic3r::Config->new_from_defaults;
|
|
|
|
$config->set('top_solid_layers', 0); # to prevent bridging on sparse infill
|
|
|
|
$config->set('bridge_speed', 99);
|
|
|
|
|
|
|
|
my $print = Slic3r::Test::init_print('bridge', config => $config);
|
|
|
|
|
|
|
|
my %extrusions = (); # angle => length
|
|
|
|
Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
|
|
|
|
my ($self, $cmd, $args, $info) = @_;
|
|
|
|
|
|
|
|
if ($cmd eq 'G1' && ($args->{F} // $self->F)/60 == $config->bridge_speed) {
|
|
|
|
my $line = Slic3r::Line->new_scale(
|
|
|
|
[ $self->X, $self->Y ],
|
|
|
|
[ $info->{new_X}, $info->{new_Y} ],
|
|
|
|
);
|
|
|
|
my $angle = $line->direction;
|
|
|
|
$extrusions{$angle} //= 0;
|
|
|
|
$extrusions{$angle} += $line->length;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
ok !!%extrusions, "bridge is generated";
|
|
|
|
my ($main_angle) = sort { $extrusions{$b} <=> $extrusions{$a} } keys %extrusions;
|
|
|
|
is $main_angle, 0, "bridge has the expected direction";
|
|
|
|
}
|
|
|
|
|
2014-04-08 11:08:42 +00:00
|
|
|
__END__
|