adaptive_width.t 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. $config->set('thin_walls_overlap',0);
  30. my $loops = Slic3r::ExtrusionPath::Collection->new;
  31. my $gap_fill = Slic3r::ExtrusionPath::Collection->new;
  32. my $fill_surfaces = Slic3r::Surface::Collection->new;
  33. my $pg = Slic3r::Layer::PerimeterGenerator->new(
  34. $slices, $flow->height, $flow,
  35. $config, $config, $config,
  36. $loops, $gap_fill, $fill_surfaces,
  37. );
  38. $pg->process;
  39. $loops = $loops->flatten;
  40. my @single = grep $_->isa('Slic3r::ExtrusionPath'), @$loops;
  41. my @loops = grep $_->isa('Slic3r::ExtrusionLoop'), @$loops;
  42. if (0) {
  43. require "Slic3r/SVG.pm";
  44. Slic3r::SVG::output(
  45. "output.svg",
  46. expolygons => [$expolygon],
  47. polylines => [ map $_->isa('Slic3r::ExtrusionPath') ? $_->polyline : $_->polygon->split_at_first_point, @$loops ],
  48. red_polylines => [ map $_->polyline, @$gap_fill ],
  49. );
  50. }
  51. is scalar(@single), $expected->{single} // 0, "expected number of single lines ($descr)";
  52. is scalar(@loops), $expected->{loops} // 0, "expected number of loops ($descr)";
  53. is scalar(@$gap_fill), $expected->{gaps} // 0, "expected number of gap fills ($descr)";
  54. if ($expected->{single}) {
  55. ok abs($loops->[0]->width - $expected->{width}) < epsilon, "single line covers the full width ($descr)";
  56. }
  57. if ($expected->{loops}) {
  58. my $loop_width = $loops[0][0]->width;
  59. my $gap_fill_width = @$gap_fill ? $gap_fill->[0]->width : 0;
  60. ok $loop_width * $expected->{loops} * 2 + $gap_fill_width > $expected->{width} - epsilon,
  61. "loop total width + gap fill covers the full width ($descr)";
  62. }
  63. };
  64. my $fw = 0.7;
  65. my $test_rect = sub {
  66. my ($width, $expected) = @_;
  67. my $e = Slic3r::ExPolygon->new([ scale_points [0,0], [100,0], [100,$width], [0,$width] ]);
  68. $expected->{width} = $width;
  69. $test->($e, $fw, $expected, $width);
  70. };
  71. $test_rect->($fw * 1, { single => 1, gaps => 0 });
  72. $test_rect->($fw * 1.3, { single => 1, gaps => 0 });
  73. $test_rect->($fw * 1.5, { single => 1, gaps => 0 });
  74. $test_rect->($fw * 2, { loops => 1, gaps => 0 });
  75. $test_rect->($fw * 2.5, { loops => 1, gaps => 1 });
  76. $test_rect->($fw * 3, { loops => 1, gaps => 1 });
  77. $test_rect->($fw * 3.5, { loops => 2, gaps => 0 });
  78. $test_rect->($fw * 4, { loops => 2, gaps => 1 });
  79. }
  80. __END__