adaptive_width.t 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. use Test::More;
  2. use strict;
  3. use warnings;
  4. plan tests => 32;
  5. BEGIN {
  6. use FindBin;
  7. use lib "$FindBin::Bin/../lib";
  8. use local::lib "$FindBin::Bin/../local-lib";
  9. }
  10. use List::Util qw(first);
  11. use Slic3r;
  12. use Slic3r::Geometry qw(X Y scale epsilon);
  13. use Slic3r::Surface ':types';
  14. sub scale_points (@) { map [scale $_->[X], scale $_->[Y]], @_ }
  15. {
  16. my $test = sub {
  17. my ($expolygon, $fw, $expected, $descr) = @_;
  18. my $flow = Slic3r::Flow->new(
  19. nozzle_diameter => 0.5,
  20. height => 0.4,
  21. width => $fw,
  22. );
  23. my $slices = Slic3r::Surface::Collection->new;
  24. $slices->append(Slic3r::Surface->new(
  25. surface_type => S_TYPE_INTERNAL,
  26. expolygon => $expolygon,
  27. ));
  28. my $config = Slic3r::Config::Full->new;
  29. my $loops = Slic3r::ExtrusionPath::Collection->new;
  30. my $gap_fill = Slic3r::ExtrusionPath::Collection->new;
  31. my $fill_surfaces = Slic3r::Surface::Collection->new;
  32. my $pg = Slic3r::Layer::PerimeterGenerator->new(
  33. $slices, $flow->height, $flow,
  34. $config, $config, $config,
  35. $loops, $gap_fill, $fill_surfaces,
  36. );
  37. $pg->process;
  38. $loops = $loops->flatten;
  39. my @single = grep $_->isa('Slic3r::ExtrusionPath'), @$loops;
  40. my @loops = grep $_->isa('Slic3r::ExtrusionLoop'), @$loops;
  41. if (0) {
  42. require "Slic3r/SVG.pm";
  43. Slic3r::SVG::output(
  44. "output.svg",
  45. expolygons => [$expolygon],
  46. polylines => [ map $_->isa('Slic3r::ExtrusionPath') ? $_->polyline : $_->polygon->split_at_first_point, @$loops ],
  47. red_polylines => [ map $_->polyline, @$gap_fill ],
  48. );
  49. }
  50. is scalar(@single), $expected->{single} // 0, "expected number of single lines ($descr)";
  51. is scalar(@loops), $expected->{loops} // 0, "expected number of loops ($descr)";
  52. is scalar(@$gap_fill), $expected->{gaps} // 0, "expected number of gap fills ($descr)";
  53. if ($expected->{single}) {
  54. ok abs($loops->[0]->width - $expected->{width}) < epsilon, "single line covers the full width ($descr)";
  55. }
  56. if ($expected->{loops}) {
  57. my $loop_width = $loops[0][0]->width;
  58. my $gap_fill_width = @$gap_fill ? $gap_fill->[0]->width : 0;
  59. ok $loop_width * $expected->{loops} * 2 + $gap_fill_width > $expected->{width} - epsilon,
  60. "loop total width + gap fill covers the full width ($descr)";
  61. }
  62. };
  63. my $fw = 0.7;
  64. my $test_rect = sub {
  65. my ($width, $expected) = @_;
  66. my $e = Slic3r::ExPolygon->new([ scale_points [0,0], [100,0], [100,$width], [0,$width] ]);
  67. $expected->{width} = $width;
  68. $test->($e, $fw, $expected, $width);
  69. };
  70. $test_rect->($fw * 1, { single => 1, gaps => 0 });
  71. $test_rect->($fw * 1.3, { single => 1, gaps => 0 });
  72. $test_rect->($fw * 1.5, { single => 1, gaps => 0 });
  73. $test_rect->($fw * 2, { loops => 1, gaps => 0 });
  74. $test_rect->($fw * 2.5, { loops => 1, gaps => 1 });
  75. $test_rect->($fw * 3, { loops => 1, gaps => 1 });
  76. $test_rect->($fw * 3.5, { loops => 2, gaps => 0 });
  77. $test_rect->($fw * 4, { loops => 2, gaps => 1 });
  78. }
  79. __END__