bridges.t 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. use Test::More tests => 20;
  2. use strict;
  3. use warnings;
  4. BEGIN {
  5. use FindBin;
  6. use lib "$FindBin::Bin/../lib";
  7. use local::lib "$FindBin::Bin/../local-lib";
  8. }
  9. use List::Util qw(first sum);
  10. use Slic3r;
  11. use Slic3r::Geometry qw(scale epsilon deg2rad rad2deg PI);
  12. use Slic3r::Test;
  13. {
  14. my $test = sub {
  15. my ($bridge_size, $rotate, $expected_angle, $tolerance) = @_;
  16. my ($x, $y) = @$bridge_size;
  17. my $lower = Slic3r::ExPolygon->new(
  18. Slic3r::Polygon->new_scale([-2,-2], [$x+2,-2], [$x+2,$y+2], [-2,$y+2]),
  19. Slic3r::Polygon->new_scale([0,0], [0,$y], [$x,$y], [$x,0]),
  20. );
  21. $lower->translate(scale 20, scale 20); # avoid negative coordinates for easier SVG preview
  22. $lower->rotate(deg2rad($rotate), [$x/2,$y/2]);
  23. my $bridge = $lower->[1]->clone;
  24. $bridge->reverse;
  25. $bridge = Slic3r::ExPolygon->new($bridge);
  26. ok check_angle([$lower], $bridge, $expected_angle, $tolerance), 'correct bridge angle for O-shaped overhang';
  27. };
  28. $test->([20,10], 0, 90);
  29. $test->([10,20], 0, 0);
  30. $test->([20,10], 45, 135, 20);
  31. $test->([20,10], 135, 45, 20);
  32. }
  33. {
  34. my $bridge = Slic3r::ExPolygon->new(
  35. Slic3r::Polygon->new_scale([0,0], [20,0], [20,10], [0,10]),
  36. );
  37. my $lower = [
  38. Slic3r::ExPolygon->new(
  39. Slic3r::Polygon->new_scale([-2,0], [0,0], [0,10], [-2,10]),
  40. ),
  41. ];
  42. $_->translate(scale 20, scale 20) for $bridge, @$lower; # avoid negative coordinates for easier SVG preview
  43. $lower->[1] = $lower->[0]->clone;
  44. $lower->[1]->translate(scale 22, 0);
  45. ok check_angle($lower, $bridge, 0), 'correct bridge angle for two-sided bridge';
  46. }
  47. {
  48. my $bridge = Slic3r::ExPolygon->new(
  49. Slic3r::Polygon->new_scale([0,0], [20,0], [10,10], [0,10]),
  50. );
  51. my $lower = [
  52. Slic3r::ExPolygon->new(
  53. Slic3r::Polygon->new_scale([0,0], [0,10], [10,10], [10,12], [-2,12], [-2,-2], [22,-2], [22,0]),
  54. ),
  55. ];
  56. $_->translate(scale 20, scale 20) for $bridge, @$lower; # avoid negative coordinates for easier SVG preview
  57. ok check_angle($lower, $bridge, 135), 'correct bridge angle for C-shaped overhang';
  58. }
  59. {
  60. my $bridge = Slic3r::ExPolygon->new(
  61. Slic3r::Polygon->new_scale([10,10],[20,10],[20,20], [10,20]),
  62. );
  63. my $lower = [
  64. Slic3r::ExPolygon->new(
  65. Slic3r::Polygon->new_scale([10,10],[10,20],[20,20],[20,30],[0,30],[0,10]),
  66. ),
  67. ];
  68. $_->translate(scale 20, scale 20) for $bridge, @$lower; # avoid negative coordinates for easier SVG preview
  69. ok check_angle($lower, $bridge, 45, undef, $bridge->area/2), 'correct bridge angle for square overhang with L-shaped anchors';
  70. }
  71. {
  72. # GH #2477: This test case failed when we computed coverage by summing length of centerlines
  73. # instead of summing their covered area.
  74. my $bridge = Slic3r::ExPolygon->new(
  75. Slic3r::Polygon->new([30299990,14299990],[1500010,14299990],[1500010,1500010],[30299990,1500010]),
  76. );
  77. my $lower = [
  78. Slic3r::ExPolygon->new(
  79. Slic3r::Polygon->new([31800000,15800000],[0,15800000],[0,0],[31800000,0]),
  80. Slic3r::Polygon->new([1499999,1500000],[1499999,14300000],[30300000,14300000],[30300000,1500000]),
  81. ),
  82. ];
  83. ok check_angle($lower, $bridge, 90, undef, $bridge->area, 500000),
  84. 'correct bridge angle for rectangle';
  85. }
  86. {
  87. # GH #3929: This test case checks that narrow gaps in lower slices don't prevent correct
  88. # direction detection.
  89. my $bridge = Slic3r::ExPolygon->new(
  90. Slic3r::Polygon->new([10099996,45867519],[3762370,45867519],[3762370,2132479],[10099996,2132479]),
  91. );
  92. my $lower = [
  93. Slic3r::ExPolygon->new(
  94. Slic3r::Polygon->new([13534103,210089],[13629884,235753],[14249999,401901],[14269611,421510],[14272931,424830],[14287518,439411],[14484206,636101],[15348099,1500000],[15360812,1547449],[15365467,1564815],[15388623,1651235],[15391897,1663454],[15393088,1667906],[15399044,1690134],[15457593,1908648],[15750000,2999999],[15750000,45000000],[15742825,45026783],[15741540,45031580],[15735900,45052628],[15663980,45321047],[15348099,46500000],[15151410,46696691],[14287518,47560587],[14267907,47580196],[14264587,47583515],[14249999,47598100],[14211041,47608539],[14204785,47610215],[14176024,47617916],[14105602,47636784],[14097768,47638884],[14048000,47652220],[13871472,47699515],[12750000,48000000],[10446106,48000000],[10446124,47990347],[10446124,9652],[10446106,0],[12750000,0]),
  95. Slic3r::Polygon->new([10251886,5013],[10251886,47994988],[10251907,48000000],[10100006,48000000],[10100006,0],[10251907,0]),
  96. Slic3r::Polygon->new([3762360,17017],[3762360,47982984],[3762397,48000000],[1249999,48000000],[536029,47808700],[456599,47787419],[73471,47684764],[0,47665076],[0,23124327],[119299,22907322],[159278,22834601],[196290,22690451],[239412,22522516],[303787,22271780],[639274,20965103],[639274,19034896],[616959,18947983],[607651,18911729],[559146,18722807],[494769,18472073],[159278,17165397],[38931,16946491],[0,16875676],[0,334922],[128529,300484],[1250000,0],[3762397,0]),
  97. ),
  98. ];
  99. ok check_angle($lower, $bridge, 0, undef, $bridge->area, 500000),
  100. 'correct bridge angle when lower slices have narrow gap';
  101. }
  102. sub check_angle {
  103. my ($lower, $bridge, $expected, $tolerance, $expected_coverage, $extrusion_width) = @_;
  104. if (ref($lower) eq 'ARRAY') {
  105. $lower = Slic3r::ExPolygon::Collection->new(@$lower);
  106. }
  107. $expected_coverage //= -1;
  108. $expected_coverage = $bridge->area if $expected_coverage == -1;
  109. $extrusion_width //= scale 0.5;
  110. my $bd = Slic3r::BridgeDetector->new($bridge, $lower, $extrusion_width);
  111. $tolerance //= rad2deg($bd->resolution) + epsilon;
  112. $bd->detect_angle;
  113. my $result = $bd->angle;
  114. my $coverage = $bd->coverage;
  115. is sum(map $_->area, @$coverage), $expected_coverage, 'correct coverage area';
  116. # our epsilon is equal to the steps used by the bridge detection algorithm
  117. ###use XXX; YYY [ rad2deg($result), $expected ];
  118. # returned value must be non-negative, check for that too
  119. my $delta = rad2deg($result) - $expected;
  120. $delta -= 180 if $delta >= 180 - epsilon;
  121. return defined $result && $result >= 0 && abs($delta) < $tolerance;
  122. }
  123. {
  124. my $config = Slic3r::Config->new_from_defaults;
  125. $config->set('top_solid_layers', 0); # to prevent bridging on sparse infill
  126. $config->set('bridge_speed', 99);
  127. my $print = Slic3r::Test::init_print('bridge', config => $config);
  128. my %extrusions = (); # angle => length
  129. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  130. my ($self, $cmd, $args, $info) = @_;
  131. if ($cmd eq 'G1' && ($args->{F} // $self->F)/60 == $config->bridge_speed) {
  132. my $line = Slic3r::Line->new_scale(
  133. [ $self->X, $self->Y ],
  134. [ $info->{new_X}, $info->{new_Y} ],
  135. );
  136. my $angle = $line->direction;
  137. $extrusions{$angle} //= 0;
  138. $extrusions{$angle} += $line->length;
  139. }
  140. });
  141. ok !!%extrusions, "bridge is generated";
  142. my ($main_angle) = sort { $extrusions{$b} <=> $extrusions{$a} } keys %extrusions;
  143. is $main_angle, 0, "bridge has the expected direction";
  144. }
  145. __END__