bridges.t 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. use Test::More tests => 16;
  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);
  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],[30,30],[0,30],[0,0]),
  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. sub check_angle {
  72. my ($lower, $bridge, $expected, $tolerance, $expected_coverage) = @_;
  73. if (ref($lower) eq 'ARRAY') {
  74. $lower = Slic3r::ExPolygon::Collection->new(@$lower);
  75. }
  76. $expected_coverage //= -1;
  77. $expected_coverage = $bridge->area if $expected_coverage == -1;
  78. my $bd = Slic3r::BridgeDetector->new($bridge, $lower, scale 0.5);
  79. $tolerance //= rad2deg($bd->resolution) + epsilon;
  80. $bd->detect_angle;
  81. my $result = $bd->angle;
  82. my $coverage = $bd->coverage;
  83. is sum(map $_->area, @$coverage), $expected_coverage, 'correct coverage area';
  84. # our epsilon is equal to the steps used by the bridge detection algorithm
  85. ###use XXX; YYY [ rad2deg($result), $expected ];
  86. # returned value must be non-negative, check for that too
  87. my $delta=rad2deg($result) - $expected;
  88. $delta-=180 if $delta>=180 - epsilon;
  89. return defined $result && $result>=0 && abs($delta) < $tolerance;
  90. }
  91. {
  92. my $config = Slic3r::Config::new_from_defaults;
  93. $config->set('top_solid_layers', 0); # to prevent bridging on sparse infill
  94. $config->set('bridge_speed', 99);
  95. my $print = Slic3r::Test::init_print('bridge', config => $config);
  96. my %extrusions = (); # angle => length
  97. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  98. my ($self, $cmd, $args, $info) = @_;
  99. if ($cmd eq 'G1' && ($args->{F} // $self->F)/60 == $config->bridge_speed) {
  100. my $line = Slic3r::Line->new_scale(
  101. [ $self->X, $self->Y ],
  102. [ $info->{new_X}, $info->{new_Y} ],
  103. );
  104. my $angle = $line->direction;
  105. $extrusions{$angle} //= 0;
  106. $extrusions{$angle} += $line->length;
  107. }
  108. });
  109. ok !!%extrusions, "bridge is generated";
  110. my ($main_angle) = sort { $extrusions{$b} <=> $extrusions{$a} } keys %extrusions;
  111. is $main_angle, 0, "bridge has the expected direction";
  112. }
  113. __END__