Further Perl unit test porting to C++ and Perl interface reduction:

Ported cooling, gap fill, thin walls and polyline unit tests.
This commit is contained in:
Vojtech Bubnik 2022-05-05 17:57:48 +02:00
parent 5a67d0e183
commit d4b8d4d0f3
46 changed files with 1080 additions and 1113 deletions

View file

@ -1,83 +0,0 @@
use Test::More;
use strict;
use warnings;
plan tests => 6;
BEGIN {
use FindBin;
use lib "$FindBin::Bin/../lib";
use local::lib "$FindBin::Bin/../local-lib";
}
use Slic3r;
{
my $polyline = Slic3r::Polyline->new(
[0,0],[1,0],[2,0],[2,1],[2,2],[1,2],[0,2],[0,1],[0,0],
);
$polyline->simplify(1);
is_deeply $polyline->pp, [ [0, 0], [2, 0], [2, 2], [0, 2], [0, 0] ], 'Douglas-Peucker';
}
{
my $polyline = Slic3r::Polyline->new(
[0,0], [50,50], [100,0], [125,-25], [150,50],
);
$polyline->simplify(25);
is_deeply $polyline->pp, [ [0, 0], [50, 50], [125, -25], [150, 50] ], 'Douglas-Peucker';
}
{
my $gear = Slic3r::Polygon->new_scale(
[144.9694,317.1543], [145.4181,301.5633], [146.3466,296.921], [131.8436,294.1643], [131.7467,294.1464],
[121.7238,291.5082], [117.1631,290.2776], [107.9198,308.2068], [100.1735,304.5101], [104.9896,290.3672],
[106.6511,286.2133], [93.453,279.2327], [81.0065,271.4171], [67.7886,286.5055], [60.7927,280.1127],
[69.3928,268.2566], [72.7271,264.9224], [61.8152,253.9959], [52.2273,242.8494], [47.5799,245.7224],
[34.6577,252.6559], [30.3369,245.2236], [42.1712,236.3251], [46.1122,233.9605], [43.2099,228.4876],
[35.0862,211.5672], [33.1441,207.0856], [13.3923,212.1895], [10.6572,203.3273], [6.0707,204.8561],
[7.2775,204.4259], [29.6713,196.3631], [25.9815,172.1277], [25.4589,167.2745], [19.8337,167.0129],
[5.0625,166.3346], [5.0625,156.9425], [5.3701,156.9282], [21.8636,156.1628], [25.3713,156.4613],
[25.4243,155.9976], [29.3432,155.8157], [30.3838,149.3549], [26.3596,147.8137], [27.1085,141.2604],
[29.8466,126.8337], [24.5841,124.9201], [10.6664,119.8989], [13.4454,110.9264], [33.1886,116.0691],
[38.817,103.1819], [45.8311,89.8133], [30.4286,76.81], [35.7686,70.0812], [48.0879,77.6873],
[51.564,81.1635], [61.9006,69.1791], [72.3019,58.7916], [60.5509,42.5416], [68.3369,37.1532],
[77.9524,48.1338], [80.405,52.2215], [92.5632,44.5992], [93.0123,44.3223], [106.3561,37.2056],
[100.8631,17.4679], [108.759,14.3778], [107.3148,11.1283], [117.0002,32.8627], [140.9109,27.3974],
[145.7004,26.4994], [145.1346,6.1011], [154.502,5.4063], [156.9398,25.6501], [171.0557,26.2017],
[181.3139,27.323], [186.2377,27.8532], [191.6031,8.5474], [200.6724,11.2756], [197.2362,30.2334],
[220.0789,39.1906], [224.3261,41.031], [236.3506,24.4291], [243.6897,28.6723], [234.2956,46.7747],
[245.6562,55.1643], [257.2523,65.0901], [261.4374,61.5679], [273.1709,52.8031], [278.555,59.5164],
[268.4334,69.8001], [264.1615,72.3633], [268.2763,77.9442], [278.8488,93.5305], [281.4596,97.6332],
[286.4487,95.5191], [300.2821,90.5903], [303.4456,98.5849], [286.4523,107.7253], [293.7063,131.1779],
[294.9748,135.8787], [314.918,133.8172], [315.6941,143.2589], [300.9234,146.1746], [296.6419,147.0309],
[297.1839,161.7052], [296.6136,176.3942], [302.1147,177.4857], [316.603,180.3608], [317.1658,176.7341],
[315.215,189.6589], [315.1749,189.6548], [294.9411,187.5222], [291.13,201.7233], [286.2615,215.5916],
[291.1944,218.2545], [303.9158,225.1271], [299.2384,233.3694], [285.7165,227.6001], [281.7091,225.1956],
[273.8981,237.6457], [268.3486,245.2248], [267.4538,246.4414], [264.8496,250.0221], [268.6392,253.896],
[278.5017,265.2131], [272.721,271.4403], [257.2776,258.3579], [234.4345,276.5687], [242.6222,294.8315],
[234.9061,298.5798], [227.0321,286.2841], [225.2505,281.8301], [211.5387,287.8187], [202.3025,291.0935],
[197.307,292.831], [199.808,313.1906], [191.5298,315.0787], [187.3082,299.8172], [186.4201,295.3766],
[180.595,296.0487], [161.7854,297.4248], [156.8058,297.6214], [154.3395,317.8592],
);
my $num_points = scalar @$gear;
my $simplified = $gear->simplify(1000);
ok @$simplified == 1, 'gear simplified to a single polygon';
###note sprintf "original points: %d\nnew points: %d", $num_points, scalar(@{$simplified->[0]});
ok @{$simplified->[0]} < $num_points, 'gear was further simplified using Douglas-Peucker';
}
{
my $hole_in_square = Slic3r::Polygon->new( # cw
[140, 140],
[140, 160],
[160, 160],
[160, 140],
);
my $simplified = $hole_in_square->simplify(2);
is scalar(@$simplified), 1, 'hole simplification returns one polygon';
ok $simplified->[0]->is_counter_clockwise, 'hole simplification turns cw polygon into ccw polygon';
}

View file

@ -107,60 +107,4 @@ plan tests => 8;
'infill combination is idempotent';
}
# the following needs to be adapted to the new API
if (0) {
my $config = Slic3r::Config::new_from_defaults;
$config->set('skirts', 0);
$config->set('solid_layers', 0);
$config->set('bottom_solid_layers', 0);
$config->set('top_solid_layers', 0);
$config->set('infill_every_layers', 6);
$config->set('layer_height', 0.06);
$config->set('perimeters', 1);
my $test = sub {
my ($shift) = @_;
my $self = Slic3r::Test::init_print('20mm_cube', config => $config);
$shift /= &Slic3r::SCALING_FACTOR;
my $scale = 4; # make room for fat infill lines with low layer height
# Put a slope on the box's sides by shifting x and y coords by $tilt * (z / boxheight).
# The test here is to put such a slight slope on the walls that it should
# not trigger any extra fill on fill layers that should be empty when
# combine infill is enabled.
$_->[0] += $shift * ($_->[2] / (20 / &Slic3r::SCALING_FACTOR)) for @{$self->objects->[0]->meshes->[0]->vertices};
$_->[1] += $shift * ($_->[2] / (20 / &Slic3r::SCALING_FACTOR)) for @{$self->objects->[0]->meshes->[0]->vertices};
$_ = [$_->[0]*$scale, $_->[1]*$scale, $_->[2]] for @{$self->objects->[0]->meshes->[0]->vertices};
# copy of Print::export_gcode() up to the point
# after fill surfaces are combined
$_->slice for @{$self->objects};
$_->make_perimeters for @{$self->objects};
$_->detect_surfaces_type for @{$self->objects};
$_->prepare_fill_surfaces for map @{$_->regions}, map @{$_->layers}, @{$self->objects};
$_->process_external_surfaces for map @{$_->regions}, map @{$_->layers}, @{$self->objects};
$_->discover_horizontal_shells for @{$self->objects};
$_->combine_infill for @{$self->objects};
# Only layers with id % 6 == 0 should have fill.
my $spurious_infill = 0;
foreach my $layer (map @{$_->layers}, @{$self->objects}) {
++$spurious_infill if ($layer->id % 6 && grep @{$_->fill_surfaces} > 0, @{$layer->regions});
}
$spurious_infill -= scalar(@{$self->objects->[0]->layers} - 1) % 6;
fail "spurious fill surfaces found on layers that should have none (walls " . sprintf("%.4f", Slic3r::Geometry::rad2deg(atan2($shift, 20/&Slic3r::SCALING_FACTOR))) . " degrees off vertical)"
unless $spurious_infill == 0;
1;
};
# Test with mm skew offsets for the top of the 20mm-high box
for my $shift (0, 0.0001, 1) {
ok $test->($shift), "no spurious fill surfaces with box walls " . sprintf("%.4f",Slic3r::Geometry::rad2deg(atan2($shift, 20))) . " degrees off of vertical";
}
}
__END__

View file

@ -1,20 +0,0 @@
use Test::More tests => 1;
use strict;
use warnings;
BEGIN {
use FindBin;
use lib "$FindBin::Bin/../lib";
use local::lib "$FindBin::Bin/../local-lib";
}
use Slic3r;
use Slic3r::Test;
{
my $config = Slic3r::Config::new_from_defaults;
$config->set('perimeter_extrusion_width', '250%');
ok $config->validate, 'percent extrusion width is validated';
}
__END__

View file

@ -1,214 +0,0 @@
use Test::More;
use strict;
use warnings;
plan tests => 14;
BEGIN {
use FindBin;
use lib "$FindBin::Bin/../lib";
use local::lib "$FindBin::Bin/../local-lib";
}
use List::Util qw(none all);
use Slic3r;
use Slic3r::Test;
my $gcodegen;
sub buffer {
my $config = shift;
if (defined($config)) {
$config = $config->clone();
} else {
$config = Slic3r::Config->new;
}
my $config_override = shift;
foreach my $key (keys %{$config_override}) {
$config->set($key, ${$config_override}{$key});
}
my $print_config = Slic3r::Config::Print->new;
$print_config->apply_dynamic($config);
$gcodegen = Slic3r::GCode->new;
$gcodegen->apply_print_config($print_config);
$gcodegen->set_layer_count(10);
my $extruders_ref = shift;
$extruders_ref = [ 0 ] if !defined $extruders_ref;
$gcodegen->set_extruders($extruders_ref);
return Slic3r::GCode::CoolingBuffer->new($gcodegen);
}
my $gcode1 = "G1 X100 E1 F3000\n";
my $print_time1 = 100 / (3000 / 60); # 2 sec
my $gcode2 = $gcode1 . "G1 X0 E1 F3000\n";
my $print_time2 = 2 * $print_time1; # 4 sec
my $config = Slic3r::Config::new_from_defaults;
# Default cooling settings.
$config->set('bridge_fan_speed', [ 100 ]);
$config->set('cooling', [ 1 ]);
$config->set('fan_always_on', [ 0 ]);
$config->set('fan_below_layer_time', [ 60 ]);
$config->set('max_fan_speed', [ 100 ]);
$config->set('min_print_speed', [ 10 ]);
$config->set('slowdown_below_layer_time', [ 5 ]);
# Default print speeds.
$config->set('bridge_speed', 60);
$config->set('external_perimeter_speed', '50%');
$config->set('first_layer_speed', 30);
$config->set('gap_fill_speed', 20);
$config->set('infill_speed', 80);
$config->set('perimeter_speed', 60);
$config->set('small_perimeter_speed', 15);
$config->set('solid_infill_speed', 20);
$config->set('top_solid_infill_speed', 15);
$config->set('max_print_speed', 80);
# Override for tests.
$config->set('disable_fan_first_layers', [ 0 ]);
{
my $gcode_src = "G1 F3000;_EXTRUDE_SET_SPEED\nG1 X100 E1";
# Print time of $gcode.
my $print_time = 100 / (3000 / 60);
my $buffer = buffer($config, { 'slowdown_below_layer_time' => [ $print_time * 0.999 ] });
my $gcode = $buffer->process_layer($gcode_src, 0);
like $gcode, qr/F3000/, 'speed is not altered when elapsed time is greater than slowdown threshold';
}
{
my $gcode_src =
"G1 X50 F2500\n" .
"G1 F3000;_EXTRUDE_SET_SPEED\n" .
"G1 X100 E1\n" .
";_EXTRUDE_END\n" .
"G1 E4 F400",
# Print time of $gcode.
my $print_time = 50 / (2500 / 60) + 100 / (3000 / 60) + 4 / (400 / 60);
my $buffer = buffer($config, { 'slowdown_below_layer_time' => [ $print_time * 1.001 ] });
my $gcode = $buffer->process_layer($gcode_src, 0);
unlike $gcode, qr/F3000/, 'speed is altered when elapsed time is lower than slowdown threshold';
like $gcode, qr/F2500/, 'speed is not altered for travel moves';
like $gcode, qr/F400/, 'speed is not altered for extruder-only moves';
}
{
my $buffer = buffer($config, {
'fan_below_layer_time' => [ $print_time1 * 0.88 ],
'slowdown_below_layer_time' => [ $print_time1 * 0.99 ]
});
my $gcode = $buffer->process_layer($gcode1, 0);
unlike $gcode, qr/M106/, 'fan is not activated when elapsed time is greater than fan threshold';
}
{
my $gcode .= buffer($config, { 'slowdown_below_layer_time', [ $print_time2 * 0.99 ] })->process_layer($gcode2, 0);
like $gcode, qr/F3000/, 'slowdown is computed on all objects printing at the same Z';
}
{
# use an elapsed time which is < the threshold but greater than it when summed twice
my $buffer = buffer($config, {
'fan_below_layer_time' => [ $print_time2 * 0.65],
'slowdown_below_layer_time' => [ $print_time2 * 0.7 ]
});
my $gcode = $buffer->process_layer($gcode2, 0) .
$buffer->process_layer($gcode2, 1);
unlike $gcode, qr/M106/, 'fan is not activated on all objects printing at different Z';
}
{
# use an elapsed time which is < the threshold even when summed twice
my $buffer = buffer($config, {
'fan_below_layer_time' => [ $print_time2 + 1 ],
'slowdown_below_layer_time' => [ $print_time2 + 2 ]
});
my $gcode = $buffer->process_layer($gcode2, 0) .
$buffer->process_layer($gcode2, 1);
like $gcode, qr/M106/, 'fan is activated on all objects printing at different Z';
}
{
my $buffer = buffer($config, {
'cooling' => [ 1 , 0 ],
'fan_below_layer_time' => [ $print_time2 + 1, $print_time2 + 1 ],
'slowdown_below_layer_time' => [ $print_time2 + 2, $print_time2 + 2 ]
},
[ 0, 1]);
my $gcode = $buffer->process_layer($gcode1 . "T1\nG1 X0 E1 F3000\n", 0);
like $gcode, qr/^M106/, 'fan is activated for the 1st tool';
like $gcode, qr/.*M107/, 'fan is disabled for the 2nd tool';
}
{
my $config = Slic3r::Config::new_from_defaults;
$config->set('cooling', [ 1 ]);
$config->set('bridge_fan_speed', [ 100 ]);
$config->set('fan_below_layer_time', [ 0 ]);
$config->set('slowdown_below_layer_time', [ 0 ]);
$config->set('bridge_speed', 99);
$config->set('top_solid_layers', 1); # internal bridges use solid_infil speed
$config->set('bottom_solid_layers', 1); # internal bridges use solid_infil speed
my $print = Slic3r::Test::init_print('overhang', config => $config);
my $fan = 0;
my $fan_with_incorrect_speeds = my $fan_with_incorrect_print_speeds = 0;
my $bridge_with_no_fan = 0;
Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
my ($self, $cmd, $args, $info) = @_;
if ($cmd eq 'M106') {
$fan = $args->{S};
$fan_with_incorrect_speeds++ if $fan != 255;
} elsif ($cmd eq 'M107') {
$fan = 0;
} elsif ($info->{extruding} && $info->{dist_XY} > 0) {
$fan_with_incorrect_print_speeds++
if ($fan > 0) && ($args->{F} // $self->F) != 60*$config->bridge_speed;
$bridge_with_no_fan++
if !$fan && ($args->{F} // $self->F) == 60*$config->bridge_speed;
}
});
ok !$fan_with_incorrect_speeds, 'bridge fan speed is applied correctly';
ok !$fan_with_incorrect_print_speeds, 'bridge fan is only turned on for bridges';
ok !$bridge_with_no_fan, 'bridge fan is turned on for all bridges';
}
{
my $config = Slic3r::Config::new_from_defaults;
$config->set('cooling', [ 1 ]);
$config->set('fan_below_layer_time', [ 0 ]);
$config->set('slowdown_below_layer_time', [ 10 ]);
$config->set('min_print_speed', [ 0 ]);
$config->set('start_gcode', '');
$config->set('first_layer_speed', '100%');
$config->set('external_perimeter_speed', 99);
my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
my @layer_times = (0); # in seconds
my %layer_external = (); # z => 1
Slic3r::GCode::Reader->new->parse(my $gcode = Slic3r::Test::gcode($print), sub {
my ($self, $cmd, $args, $info) = @_;
if ($cmd eq 'G1') {
if ($info->{dist_Z}) {
push @layer_times, 0;
$layer_external{ $args->{Z} } = 0;
}
$layer_times[-1] += abs($info->{dist_XY} || $info->{dist_E} || $info->{dist_Z} || 0) / ($args->{F} // $self->F) * 60;
if ($args->{F} && $args->{F} == $config->external_perimeter_speed*60) {
$layer_external{ $self->Z }++;
}
}
});
@layer_times = grep $_, @layer_times;
my $all_below = none { $_ < $config->slowdown_below_layer_time->[0] } @layer_times;
ok $all_below, 'slowdown_below_layer_time is honored';
# check that all layers have at least one unaltered external perimeter speed
# my $external = all { $_ > 0 } values %layer_external;
# ok $external, 'slowdown_below_layer_time does not alter external perimeters';
}
__END__

View file

@ -1,83 +0,0 @@
use Test::More tests => 6;
use strict;
use warnings;
BEGIN {
use FindBin;
use lib "$FindBin::Bin/../lib";
use local::lib "$FindBin::Bin/../local-lib";
}
use List::Util qw(first sum);
use Slic3r;
use Slic3r::Geometry qw(scale PI);
use Slic3r::Test;
{
my $config = Slic3r::Config::new_from_defaults;
$config->set('skirts', 1);
$config->set('brim_width', 2);
$config->set('perimeters', 3);
$config->set('fill_density', 0.4);
$config->set('bottom_solid_layers', 1);
$config->set('first_layer_extrusion_width', 2);
$config->set('first_layer_height', $config->layer_height);
$config->set('filament_diameter', [ 3.0 ]);
$config->set('nozzle_diameter', [ 0.5 ]);
my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
my @E_per_mm = ();
Slic3r::GCode::Reader->new->parse(my $gcode = Slic3r::Test::gcode($print), sub {
my ($self, $cmd, $args, $info) = @_;
if ($self->Z == $config->layer_height) { # only consider first layer
if ($info->{extruding} && $info->{dist_XY} > 0) {
push @E_per_mm, $info->{dist_E} / $info->{dist_XY};
}
}
});
my $E_per_mm_avg = sum(@E_per_mm) / @E_per_mm;
# allow some tolerance because solid rectilinear infill might be adjusted/stretched
ok !(defined first { abs($_ - $E_per_mm_avg) > 0.015 } @E_per_mm),
'first_layer_extrusion_width applies to everything on first layer';
}
{
my $config = Slic3r::Config::new_from_defaults;
$config->set('bridge_speed', 99);
$config->set('bridge_flow_ratio', 1);
$config->set('cooling', [ 0 ]); # to prevent speeds from being altered
$config->set('first_layer_speed', '100%'); # to prevent speeds from being altered
my $test = sub {
my $print = Slic3r::Test::init_print('overhang', config => $config);
my @E_per_mm = ();
Slic3r::GCode::Reader->new->parse(my $gcode = Slic3r::Test::gcode($print), sub {
my ($self, $cmd, $args, $info) = @_;
if ($info->{extruding} && $info->{dist_XY} > 0) {
if (($args->{F} // $self->F) == $config->bridge_speed*60) {
push @E_per_mm, $info->{dist_E} / $info->{dist_XY};
}
}
});
my $expected_mm3_per_mm = ($config->nozzle_diameter->[0]**2) * PI/4 * $config->bridge_flow_ratio;
my $expected_E_per_mm = $expected_mm3_per_mm / ((($config->filament_diameter->[0]/2)**2)*PI);
ok !(defined first { abs($_ - $expected_E_per_mm) > 0.01 } @E_per_mm),
'expected flow when using bridge_flow_ratio = ' . $config->bridge_flow_ratio;
};
$config->set('bridge_flow_ratio', 0.5);
$test->();
$config->set('bridge_flow_ratio', 2);
$test->();
$config->set('extrusion_width', 0.4);
$config->set('bridge_flow_ratio', 1);
$test->();
$config->set('bridge_flow_ratio', 0.5);
$test->();
$config->set('bridge_flow_ratio', 2);
$test->();
}
__END__

View file

@ -1,59 +0,0 @@
use Test::More tests => 1;
use strict;
use warnings;
BEGIN {
use FindBin;
use lib "$FindBin::Bin/../lib";
use local::lib "$FindBin::Bin/../local-lib";
}
use List::Util qw(first);
use Slic3r;
use Slic3r::Geometry qw(PI scale unscale convex_hull);
use Slic3r::Surface ':types';
use Slic3r::Test;
{
my $config = Slic3r::Config::new_from_defaults;
$config->set('skirts', 0);
$config->set('perimeter_speed', 66);
$config->set('external_perimeter_speed', 66);
$config->set('small_perimeter_speed', 66);
$config->set('gap_fill_speed', 99);
$config->set('perimeters', 1);
$config->set('cooling', [ 0 ]); # to prevent speeds from being altered
$config->set('first_layer_speed', '100%'); # to prevent speeds from being altered
$config->set('perimeter_extrusion_width', 0.35);
$config->set('first_layer_extrusion_width', 0.35);
my $print = Slic3r::Test::init_print('two_hollow_squares', config => $config);
my @perimeter_points = ();
my $last = ''; # perimeter | gap
my $gap_fills_outside_last_perimeters = 0;
Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
my ($self, $cmd, $args, $info) = @_;
if ($info->{extruding} && $info->{dist_XY} > 0) {
my $F = $args->{F} // $self->F;
my $point = Slic3r::Point->new_scale($info->{new_X}, $info->{new_Y});
if ($F == $config->perimeter_speed*60) {
if ($last eq 'gap') {
@perimeter_points = ();
}
push @perimeter_points, $point;
$last = 'perimeter';
} elsif ($F == $config->gap_fill_speed*60) {
my $convex_hull = convex_hull(\@perimeter_points);
if (!$convex_hull->contains_point($point)) {
$gap_fills_outside_last_perimeters++;
}
$last = 'gap';
}
}
});
is $gap_fills_outside_last_perimeters, 0, 'gap fills are printed before leaving islands';
}
__END__

View file

@ -1,4 +1,4 @@
use Test::More tests => 24;
use Test::More tests => 23;
use strict;
use warnings;
@ -13,13 +13,6 @@ use Slic3r;
use Slic3r::Geometry qw(scale convex_hull);
use Slic3r::Test;
{
my $gcodegen = Slic3r::GCode->new();
$gcodegen->set_layer_count(1);
$gcodegen->set_origin(Slic3r::Pointf->new(10, 10));
is_deeply $gcodegen->last_pos->arrayref, [scale -10, scale -10], 'last_pos is shifted correctly';
}
{
my $config = Slic3r::Config::new_from_defaults;
$config->set('wipe', [1]);

View file

@ -1,57 +0,0 @@
use Test::More;
use strict;
use warnings;
plan skip_all => 'temporarily disabled';
plan tests => 4;
BEGIN {
use FindBin;
use lib "$FindBin::Bin/../lib";
use local::lib "$FindBin::Bin/../local-lib";
}
use Slic3r;
use Slic3r::Test;
{
# We only need to slice at one height, so we'll build a non-manifold mesh
# that still produces complete loops at that height. Triangular walls are
# enough for this purpose.
# Basically we want to check what happens when three concentric loops happen
# to be at the same height, the two external ones being ccw and the other being
# a hole, thus cw.
my (@vertices, @facets) = ();
Slic3r::Test::add_facet($_, \@vertices, \@facets) for
# external surface below the slicing Z
[ [0,0,0], [20,0,10], [0,0,10] ],
[ [20,0,0], [20,20,10], [20,0,10] ],
[ [20,20,0], [0,20,10], [20,20,10] ],
[ [0,20,0], [0,0,10], [0,20,10] ],
# external insetted surface above the slicing Z
[ [2,2,10], [18,2,10], [2,2,20] ],
[ [18,2,10], [18,18,10], [18,2,20] ],
[ [18,18,10], [2,18,10], [18,18,20] ],
[ [2,18,10], [2,2,10], [2,18,20] ],
# insetted hole below the slicing Z
[ [15,5,0], [5,5,10], [15,5,10] ],
[ [15,15,0], [15,5,10], [15,15,10] ],
[ [5,15,0], [15,15,10], [5,15,10] ],
[ [5,5,0], [5,15,10], [5,5,10] ];
my $mesh = Slic3r::TriangleMesh->new;
$mesh->ReadFromPerl(\@vertices, \@facets);
$mesh->analyze;
my @lines = map $mesh->intersect_facet($_, 10), 0..$#facets;
my $loops = Slic3r::TriangleMesh::make_loops(\@lines);
is scalar(@$loops), 3, 'correct number of loops detected';
is scalar(grep $_->is_counter_clockwise, @$loops), 2, 'correct number of ccw loops detected';
my @surfaces = Slic3r::Layer::Region::_merge_loops($loops, 0);
is scalar(@surfaces), 1, 'one surface detected';
is scalar(@{$surfaces[0]->expolygon})-1, 1, 'surface has one hole';
}
__END__

185
t/thin.t
View file

@ -1,185 +0,0 @@
use Test::More tests => 23;
use strict;
use warnings;
BEGIN {
use FindBin;
use lib "$FindBin::Bin/../lib";
use local::lib "$FindBin::Bin/../local-lib";
}
use Slic3r;
use List::Util qw(first sum none);
use Slic3r::Geometry qw(epsilon scale unscale scaled_epsilon Y);
use Slic3r::Test;
# Disable this until a more robust implementation is provided. It currently
# fails on Linux 32bit because some spurious extrudates are generated.
if (0) {
my $config = Slic3r::Config::new_from_defaults;
$config->set('layer_height', 0.2);
$config->set('first_layer_height', $config->layer_height);
$config->set('extrusion_width', 0.5);
$config->set('first_layer_extrusion_width', '200%'); # check this one too
$config->set('skirts', 0);
$config->set('thin_walls', 1);
my $print = Slic3r::Test::init_print('gt2_teeth', config => $config);
my %extrusion_paths = (); # Z => count of continuous extrusions
my $extruding = 0;
Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
my ($self, $cmd, $args, $info) = @_;
if ($cmd eq 'G1') {
if ($info->{extruding} && $info->{dist_XY}) {
if (!$extruding) {
$extrusion_paths{$self->Z} //= 0;
$extrusion_paths{$self->Z}++;
}
$extruding = 1;
} else {
$extruding = 0;
}
}
});
ok !(first { $_ != 3 } values %extrusion_paths),
'no superfluous thin walls are generated for toothed profile';
}
{
my $square = Slic3r::Polygon->new_scale( # ccw
[100, 100],
[200, 100],
[200, 200],
[100, 200],
);
my $hole_in_square = Slic3r::Polygon->new_scale( # cw
[140, 140],
[140, 160],
[160, 160],
[160, 140],
);
my $expolygon = Slic3r::ExPolygon->new($square, $hole_in_square);
my $res = $expolygon->medial_axis(scale 40, scale 0.5);
is scalar(@$res), 1, 'medial axis of a square shape is a single path';
isa_ok $res->[0], 'Slic3r::Polyline', 'medial axis result is a polyline';
ok $res->[0]->first_point->coincides_with($res->[0]->last_point), 'polyline forms a closed loop';
ok $res->[0]->length > $hole_in_square->length && $res->[0]->length < $square->length,
'medial axis loop has reasonable length';
}
{
my $expolygon = Slic3r::ExPolygon->new(Slic3r::Polygon->new_scale(
[100, 100],
[120, 100],
[120, 200],
[100, 200],
));
my $res = $expolygon->medial_axis(scale 20, scale 0.5);
is scalar(@$res), 1, 'medial axis of a narrow rectangle is a single line';
ok unscale($res->[0]->length) >= (200-100 - (120-100)) - epsilon, 'medial axis has reasonable length';
$expolygon = Slic3r::ExPolygon->new(Slic3r::Polygon->new_scale(
[100, 100],
[120, 100],
[120, 200],
[105, 200], # extra point in the short side
[100, 200],
));
my $res2 = $expolygon->medial_axis(scale 1, scale 0.5);
is scalar(@$res), 1, 'medial axis of a narrow rectangle with an extra vertex is still a single line';
ok unscale($res->[0]->length) >= (200-100 - (120-100)) - epsilon, 'medial axis has still a reasonable length';
ok !(grep { abs($_ - scale 150) < scaled_epsilon } map $_->[Y], map @$_, @$res2), "extra vertices don't influence medial axis";
}
{
my $expolygon = Slic3r::ExPolygon->new(
Slic3r::Polygon->new([1185881,829367],[1421988,1578184],[1722442,2303558],[2084981,2999998],[2506843,3662186],[2984809,4285086],[3515250,4863959],[4094122,5394400],[4717018,5872368],[5379210,6294226],[6075653,6656769],[6801033,6957229],[7549842,7193328],[8316383,7363266],[9094809,7465751],[9879211,7500000],[10663611,7465750],[11442038,7363265],[12208580,7193327],[12957389,6957228],[13682769,6656768],[14379209,6294227],[15041405,5872366],[15664297,5394401],[16243171,4863960],[16758641,4301424],[17251579,3662185],[17673439,3000000],[18035980,2303556],[18336441,1578177],[18572539,829368],[18750748,0],[19758422,0],[19727293,236479],[19538467,1088188],[19276136,1920196],[18942292,2726179],[18539460,3499999],[18070731,4235755],[17539650,4927877],[16950279,5571067],[16307090,6160437],[15614974,6691519],[14879209,7160248],[14105392,7563079],[13299407,7896927],[12467399,8159255],[11615691,8348082],[10750769,8461952],[9879211,8500000],[9007652,8461952],[8142729,8348082],[7291022,8159255],[6459015,7896927],[5653029,7563079],[4879210,7160247],[4143447,6691519],[3451331,6160437],[2808141,5571066],[2218773,4927878],[1687689,4235755],[1218962,3499999],[827499,2748020],[482284,1920196],[219954,1088186],[31126,236479],[0,0],[1005754,0]),
);
my $res = $expolygon->medial_axis(scale 1.324888, scale 0.25);
is scalar(@$res), 1, 'medial axis of a semicircumference is a single line';
# check whether turns are all CCW or all CW
my @lines = @{$res->[0]->lines};
my @angles = map { $lines[$_-1]->ccw($lines[$_]->b) } 1..$#lines;
ok !!(none { $_ < 0 } @angles) || (none { $_ > 0 } @angles),
'all medial axis segments of a semicircumference have the same orientation';
}
{
my $expolygon = Slic3r::ExPolygon->new(Slic3r::Polygon->new_scale(
[100, 100],
[120, 100],
[112, 200],
[108, 200],
));
my $res = $expolygon->medial_axis(scale 20, scale 0.5);
is scalar(@$res), 1, 'medial axis of a narrow trapezoid is a single line';
ok unscale($res->[0]->length) >= (200-100 - (120-100)) - epsilon, 'medial axis has reasonable length';
}
{
my $expolygon = Slic3r::ExPolygon->new(Slic3r::Polygon->new_scale(
[100, 100],
[120, 100],
[120, 180],
[200, 180],
[200, 200],
[100, 200],
));
my $res = $expolygon->medial_axis(scale 20, scale 0.5);
is scalar(@$res), 1, 'medial axis of a L shape is a single polyline';
my $len = unscale($res->[0]->length) + 20; # 20 is the thickness of the expolygon, which is subtracted from the ends
ok $len > 80*2 && $len < 100*2, 'medial axis has reasonable length';
}
{
my $expolygon = Slic3r::ExPolygon->new(Slic3r::Polygon->new(
[-203064906,-51459966],[-219312231,-51459966],[-219335477,-51459962],[-219376095,-51459962],[-219412047,-51459966],
[-219572094,-51459966],[-219624814,-51459962],[-219642183,-51459962],[-219656665,-51459966],[-220815482,-51459966],
[-220815482,-37738966],[-221117540,-37738966],[-221117540,-51762024],[-203064906,-51762024],
));
my $polylines = $expolygon->medial_axis(819998, 102499.75);
my $perimeter = $expolygon->contour->split_at_first_point->length;
ok sum(map $_->length, @$polylines) > $perimeter/2/4*3, 'medial axis has a reasonable length';
}
{
my $expolygon = Slic3r::ExPolygon->new(Slic3r::Polygon->new_scale(
[50, 100],
[1000, 102],
[50, 104],
));
my $res = $expolygon->medial_axis(scale 4, scale 0.5);
is scalar(@$res), 1, 'medial axis of a narrow triangle is a single line';
ok unscale($res->[0]->length) >= (200-100 - (120-100)) - epsilon, 'medial axis has reasonable length';
}
{
# GH #2474
my $expolygon = Slic3r::ExPolygon->new(Slic3r::Polygon->new(
[91294454,31032190],[11294481,31032190],[11294481,29967810],[44969182,29967810],[89909960,29967808],[91294454,29967808]
));
my $polylines = $expolygon->medial_axis(1871238, 500000);
is scalar(@$polylines), 1, 'medial axis is a single polyline';
my $polyline = $polylines->[0];
my $expected_y = $expolygon->bounding_box->center->y; #;;
ok abs(sum(map $_->y, @$polyline) / @$polyline - $expected_y) < scaled_epsilon, #,,
'medial axis is horizontal and is centered';
# order polyline from left to right
$polyline->reverse if $polyline->first_point->x > $polyline->last_point->x;
my $polyline_bb = $polyline->bounding_box;
is $polyline->first_point->x, $polyline_bb->x_min, 'expected x_min';
is $polyline->last_point->x, $polyline_bb->x_max, 'expected x_max';
is_deeply [ map $_->x, @$polyline ], [ sort map $_->x, @$polyline ],
'medial axis is not self-overlapping';
}
__END__