Port several rectilinear infill tests from perl.

This commit is contained in:
Joseph Lenox 2018-07-30 22:38:43 -05:00
parent 4d257930da
commit 02acbd625a

View File

@ -1,108 +1,110 @@
#include <catch.hpp>
#include "test_data.hpp"
#include "Fill/Fill.hpp"
#include "Print.hpp"
#include "Geometry.hpp"
#include "Flow.hpp"
using namespace Slic3r;
using namespace Slic3r::Geometry;
TEST_CASE("Fill: adjusted solid distance") {
Print print;
int surface_width {250};
int distance {Slic3r::Flow::solid_spacing(surface_width, 47)};
REQUIRE(distance == Approx(50));
REQUIRE(surface_width % distance == 0);
}
TEST_CASE("Fill: Pattern Path Length") {
auto filler {Slic3r::Fill::new_from_type("rectilinear")};
filler->angle = -(PI)/2.0;
filler->min_spacing = 5;
filler->dont_adjust = true;
filler->endpoints_overlap = false;
filler->density = filler->min_spacing / 50.0;
auto test {[filler] (const ExPolygon& poly) -> Polylines {
auto surface {Slic3r::Surface(stTop, poly)};
return filler->fill_surface(surface);
}};
SECTION("Square") {
Points test_set;
test_set.reserve(4);
Pointfs points {Pointf(0,0), Pointf(100,0), Pointf(100,100), Pointf(0,100)};
for (size_t i = 0; i < 4; ++i) {
std::transform(points.cbegin()+i, points.cend(), std::back_inserter(test_set), [] (const Pointf& a) -> Point { return Point::new_scale(a); } );
std::transform(points.cbegin(), points.cbegin()+i, std::back_inserter(test_set), [] (const Pointf& a) -> Point { return Point::new_scale(a); } );
Polylines paths {test(Slic3r::ExPolygon(test_set))};
REQUIRE(paths.size() == 1); // one continuous path
// TODO: determine what the "Expected length" should be for rectilinear fill of a 100x100 polygon.
// This check only checks that it's above scale(3*100 + 2*50) + scaled_epsilon.
// ok abs($paths->[0]->length - scale(3*100 + 2*50)) - scaled_epsilon, 'path has expected length';
REQUIRE(std::abs(paths[0].length() - static_cast<double>(scale_(3*100 + 2*50))) - SCALED_EPSILON > 0); // path has expected length
test_set.clear();
}
}
SECTION("Diamond with endpoints on grid") {
Pointfs points {Pointf(0,0), Pointf(100,0), Pointf(150,50), Pointf(100,100), Pointf(0,100), Pointf(-50,50)};
Points test_set;
test_set.reserve(6);
std::transform(points.cbegin(), points.cend(), std::back_inserter(test_set), [] (const Pointf& a) -> Point { return Point::new_scale(a); } );
Polylines paths {test(Slic3r::ExPolygon(test_set))};
REQUIRE(paths.size() == 1); // one continuous path
}
SECTION("Square with hole") {
Pointfs square { Pointf(0,0), Pointf(100,0), Pointf(100,100), Pointf(0,100)};
Pointfs hole {Pointf(25,25), Pointf(75,25), Pointf(75,75), Pointf(25,75) };
std::reverse(hole.begin(), hole.end());
Points test_hole;
Points test_square;
std::transform(square.cbegin(), square.cend(), std::back_inserter(test_square), [] (const Pointf& a) -> Point { return Point::new_scale(a); } );
std::transform(hole.cbegin(), hole.cend(), std::back_inserter(test_hole), [] (const Pointf& a) -> Point { return Point::new_scale(a); } );
for (double angle : {-(PI/2.0), -(PI/4.0), -(PI), PI/2.0, PI}) {
for (double spacing : {25.0, 5.0, 7.5, 8.5}) {
filler->density = filler->min_spacing / spacing;
filler->angle = angle;
ExPolygon e(test_square, test_hole);
Polylines paths {test(e)};
REQUIRE((paths.size() >= 2 && paths.size() <= 3));
// paths don't cross hole
REQUIRE(diff_pl(paths, offset(e, +SCALED_EPSILON*10)).size() == 0);
}
}
}
SECTION("Rotated Square") {
filler->angle = (PI/4.0);
filler->dont_adjust = false;
filler->min_spacing = 0.654498;
filler->endpoints_overlap = unscale(359974);
filler->density = 1;
filler->layer_id = 66;
filler->z = 20.15;
Points points {Point(25771516,14142125),Point(14142138,25771515),Point(2512749,14142131),Point(14142125,2512749)};
Polylines paths {test(Slic3r::ExPolygon(points))};
REQUIRE(paths.size() == 1); // one continuous path
// TODO: determine what the "Expected length" should be for rectilinear fill of a 100x100 polygon.
// This check only checks that it's above scale(3*100 + 2*50) + scaled_epsilon.
// ok abs($paths->[0]->length - scale(3*100 + 2*50)) - scaled_epsilon, 'path has expected length';
REQUIRE(std::abs(paths[0].length() - static_cast<double>(scale_(3*100 + 2*50))) - SCALED_EPSILON > 0); // path has expected length
}
}
/*
BEGIN {
use FindBin;
use lib "$FindBin::Bin/../lib";
use local::lib "$FindBin::Bin/../local-lib";
}
use List::Util qw(first sum max);
use Slic3r;
use Slic3r::Geometry qw(PI X Y scaled_epsilon scale unscale convex_hull);
use Slic3r::Geometry::Clipper qw(union diff diff_ex offset offset2_ex diff_pl union_ex);
use Slic3r::Surface qw(:types);
use Slic3r::Test;
sub scale_points (@) { map [scale $_->[X], scale $_->[Y]], @_ }
{
my $print = Slic3r::Print->new;
my $surface_width = 250;
my $distance = Slic3r::Flow::solid_spacing($surface_width, 47);
is $distance, 50, 'adjusted solid distance';
is $surface_width % $distance, 0, 'adjusted solid distance';
}
{
my $filler = Slic3r::Filler->new_from_type('rectilinear');
$filler->set_angle(-(PI)/2);
$filler->set_min_spacing(5);
$filler->set_dont_adjust(1);
$filler->set_endpoints_overlap(0);
my $test = sub {
my ($expolygon) = @_;
my $surface = Slic3r::Surface->new(
surface_type => S_TYPE_TOP,
expolygon => $expolygon,
);
return $filler->fill_surface($surface);
};
# square
$filler->set_density($filler->min_spacing / 50);
for my $i (0..3) {
# check that it works regardless of the points order
my @points = ([0,0], [100,0], [100,100], [0,100]);
@points = (@points[$i..$#points], @points[0..($i-1)]);
my $paths = $test->(my $e = Slic3r::ExPolygon->new([ scale_points @points ]));
is(scalar @$paths, 1, 'one continuous path') or done_testing, exit;
ok abs($paths->[0]->length - scale(3*100 + 2*50)) - scaled_epsilon, 'path has expected length';
}
# diamond with endpoints on grid
{
my $paths = $test->(my $e = Slic3r::ExPolygon->new([ scale_points [0,0], [100,0], [150,50], [100,100], [0,100], [-50,50] ]));
is(scalar @$paths, 1, 'one continuous path') or done_testing, exit;
}
# square with hole
for my $angle (-(PI/2), -(PI/4), -(PI), PI/2, PI) {
for my $spacing (25, 5, 7.5, 8.5) {
$filler->set_density($filler->min_spacing / $spacing);
$filler->set_angle($angle);
my $paths = $test->(my $e = Slic3r::ExPolygon->new(
[ scale_points [0,0], [100,0], [100,100], [0,100] ],
[ scale_points reverse [25,25], [75,25], [75,75], [25,75] ],
));
if (0) {
require "Slic3r/SVG.pm";
Slic3r::SVG::output(
"fill.svg",
no_arrows => 1,
expolygons => [$e],
polylines => $paths,
);
}
ok(@$paths >= 2 && @$paths <= 3, '2 or 3 continuous paths') or done_testing, exit;
ok(!@{diff_pl($paths->arrayref, offset(\@$e, +scaled_epsilon*10))},
'paths don\'t cross hole') or done_testing, exit;
}
}
# rotated square
$filler->set_angle(PI/4);
$filler->set_dont_adjust(0);
$filler->set_min_spacing(0.654498);
$filler->set_endpoints_overlap(unscale(359974));
$filler->set_density(1);
$filler->set_layer_id(66);
$filler->set_z(20.15);
{
my $e = Slic3r::ExPolygon->new(
Slic3r::Polygon->new([25771516,14142125],[14142138,25771515],[2512749,14142131],[14142125,2512749]),
);
my $paths = $test->($e);
is(scalar @$paths, 1, 'one continuous path') or done_testing, exit;
ok abs($paths->[0]->length - scale(3*100 + 2*50)) - scaled_epsilon, 'path has expected length';
}
}
{
my $expolygon = Slic3r::ExPolygon->new([ scale_points [0,0], [50,0], [50,50], [0,50] ]);
my $filler = Slic3r::Filler->new_from_type('rectilinear');