thin.t 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. use Test::More tests => 9;
  2. use strict;
  3. use warnings;
  4. BEGIN {
  5. use FindBin;
  6. use lib "$FindBin::Bin/../lib";
  7. }
  8. use Slic3r;
  9. use List::Util qw(first);
  10. use Slic3r::Geometry qw(epsilon scale unscale);
  11. use Slic3r::Test;
  12. goto TTT;
  13. {
  14. my $config = Slic3r::Config->new_from_defaults;
  15. $config->set('layer_height', 0.2);
  16. $config->set('first_layer_height', '100%');
  17. $config->set('extrusion_width', 0.5);
  18. $config->set('first_layer_extrusion_width', '200%'); # check this one too
  19. $config->set('skirts', 0);
  20. $config->set('thin_walls', 1);
  21. my $print = Slic3r::Test::init_print('gt2_teeth', config => $config);
  22. my %extrusion_paths = (); # Z => count of continuous extrusions
  23. my $extruding = 0;
  24. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  25. my ($self, $cmd, $args, $info) = @_;
  26. if ($cmd eq 'G1') {
  27. if ($info->{extruding} && $info->{dist_XY}) {
  28. if (!$extruding) {
  29. $extrusion_paths{$self->Z} //= 0;
  30. $extrusion_paths{$self->Z}++;
  31. }
  32. $extruding = 1;
  33. } else {
  34. $extruding = 0;
  35. }
  36. }
  37. });
  38. ok !(first { $_ != 3 } values %extrusion_paths),
  39. 'no superfluous thin walls are generated for toothed profile';
  40. }
  41. {
  42. my $square = Slic3r::Polygon->new_scale( # ccw
  43. [100, 100],
  44. [200, 100],
  45. [200, 200],
  46. [100, 200],
  47. );
  48. my $hole_in_square = Slic3r::Polygon->new_scale( # cw
  49. [140, 140],
  50. [140, 160],
  51. [160, 160],
  52. [160, 140],
  53. );
  54. my $expolygon = Slic3r::ExPolygon->new($square, $hole_in_square);
  55. my $res = $expolygon->medial_axis(scale 10);
  56. is scalar(@$res), 1, 'medial axis of a square shape is a single closed loop';
  57. ok $res->[0]->length > $hole_in_square->length && $res->[0]->length < $square->length,
  58. 'medial axis loop has reasonable length';
  59. }
  60. TTT: {
  61. my $expolygon = Slic3r::ExPolygon->new(Slic3r::Polygon->new_scale(
  62. [100, 100],
  63. [120, 100],
  64. [120, 200],
  65. [100, 200],
  66. ));
  67. my $res = $expolygon->medial_axis(scale 10);
  68. is scalar(@$res), 1, 'medial axis of a narrow rectangle is a single line';
  69. ok unscale($res->[0]->length) >= (200-100 - (120-100)) - epsilon, 'medial axis has reasonable length';
  70. $expolygon = Slic3r::ExPolygon->new(Slic3r::Polygon->new_scale(
  71. [100, 100],
  72. [120, 100],
  73. [120, 200],
  74. [105, 200], # extra point in the short side
  75. [100, 200],
  76. ));
  77. my $res2 = $expolygon->medial_axis(scale 10);
  78. use Slic3r::SVG;
  79. Slic3r::SVG::output(
  80. "thin.svg",
  81. expolygons => [$expolygon],
  82. polylines => $res2,
  83. );
  84. }
  85. {
  86. my $expolygon = Slic3r::ExPolygon->new(Slic3r::Polygon->new_scale(
  87. [100, 100],
  88. [120, 100],
  89. [112, 200],
  90. [108, 200],
  91. ));
  92. my $res = $expolygon->medial_axis(scale 10);
  93. is scalar(@$res), 1, 'medial axis of a narrow trapezoid is a single line';
  94. ok unscale($res->[0]->length) >= (200-100 - (120-100)) - epsilon, 'medial axis has reasonable length';
  95. }
  96. {
  97. my $expolygon = Slic3r::ExPolygon->new(Slic3r::Polygon->new_scale(
  98. [100, 100],
  99. [120, 100],
  100. [120, 180],
  101. [200, 180],
  102. [200, 200],
  103. [100, 200],
  104. ));
  105. my $res = $expolygon->medial_axis(scale 20);
  106. is scalar(@$res), 1, 'medial axis of a L shape is a single polyline';
  107. my $len = unscale($res->[0]->length) + 20; # 20 is the thickness of the expolygon, which is subtracted from the ends
  108. ok $len > 80*2 && $len < 100*2, 'medial axis has reasonable length';
  109. if (0) {
  110. require "Slic3r/SVG.pm";
  111. Slic3r::SVG::output(
  112. "thin.svg",
  113. expolygons => [$expolygon],
  114. polylines => $res,
  115. );
  116. }
  117. }
  118. __END__