thin.t 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. use Test::More tests => 29;
  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 Slic3r;
  10. use List::Util qw(first sum none);
  11. use Slic3r::Geometry qw(epsilon scale unscale scaled_epsilon Y);
  12. use Slic3r::Test;
  13. # Disable this until a more robust implementation is provided. It currently
  14. # fails on Linux 32bit because some spurious extrudates are generated.
  15. if (0) {
  16. my $config = Slic3r::Config->new_from_defaults;
  17. $config->set('layer_height', 0.2);
  18. $config->set('first_layer_height', '100%');
  19. $config->set('extrusion_width', 0.5);
  20. $config->set('first_layer_extrusion_width', '200%'); # check this one too
  21. $config->set('skirts', 0);
  22. $config->set('thin_walls', 1);
  23. my $print = Slic3r::Test::init_print('gt2_teeth', config => $config);
  24. my %extrusion_paths = (); # Z => count of continuous extrusions
  25. my $extruding = 0;
  26. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  27. my ($self, $cmd, $args, $info) = @_;
  28. if ($cmd eq 'G1') {
  29. if ($info->{extruding} && $info->{dist_XY}) {
  30. if (!$extruding) {
  31. $extrusion_paths{$self->Z} //= 0;
  32. $extrusion_paths{$self->Z}++;
  33. }
  34. $extruding = 1;
  35. } else {
  36. $extruding = 0;
  37. }
  38. }
  39. });
  40. ok !(first { $_ != 3 } values %extrusion_paths),
  41. 'no superfluous thin walls are generated for toothed profile';
  42. }
  43. {
  44. my $square = Slic3r::Polygon->new_scale( # ccw
  45. [100, 100],
  46. [200, 100],
  47. [200, 200],
  48. [100, 200],
  49. );
  50. my $hole_in_square = Slic3r::Polygon->new_scale( # cw
  51. [140, 140],
  52. [140, 160],
  53. [160, 160],
  54. [160, 140],
  55. );
  56. my $expolygon = Slic3r::ExPolygon->new($square, $hole_in_square);
  57. my $res = $expolygon->medial_axis(scale 40, scale 0.5);
  58. is scalar(@$res), 1, 'medial axis of a square shape is a single path';
  59. isa_ok $res->[0], 'Slic3r::Polyline', 'medial axis result is a polyline';
  60. ok $res->[0]->first_point->coincides_with($res->[0]->last_point), 'polyline forms a closed loop';
  61. ok $res->[0]->length > $hole_in_square->length && $res->[0]->length < $square->length,
  62. 'medial axis loop has reasonable length';
  63. }
  64. {
  65. my $expolygon = Slic3r::ExPolygon->new(Slic3r::Polygon->new_scale(
  66. [100, 100],
  67. [120, 100],
  68. [120, 200],
  69. [100, 200],
  70. ));
  71. my $res = $expolygon->medial_axis(scale 20, scale 0.5);
  72. is scalar(@$res), 1, 'medial axis of a narrow rectangle is a single line';
  73. ok unscale($res->[0]->length) >= (200-100 - (120-100)) - epsilon, 'medial axis has reasonable length';
  74. $expolygon = Slic3r::ExPolygon->new(Slic3r::Polygon->new_scale(
  75. [100, 100],
  76. [120, 100],
  77. [120, 200],
  78. [105, 200], # extra point in the short side
  79. [100, 200],
  80. ));
  81. my $res2 = $expolygon->medial_axis(scale 1, scale 0.5);
  82. is scalar(@$res), 1, 'medial axis of a narrow rectangle with an extra vertex is still a single line';
  83. ok unscale($res->[0]->length) >= (200-100 - (120-100)) - epsilon, 'medial axis has still a reasonable length';
  84. ok !(grep { abs($_ - scale 150) < scaled_epsilon } map $_->[Y], map @$_, @$res2), "extra vertices don't influence medial axis";
  85. }
  86. {
  87. my $expolygon = Slic3r::ExPolygon->new(
  88. Slic3r::Polygon->new([1185881,829367],[1421988,1578184],[1722442,2303558],[2084981,2999998],[2506843,3662186],[2984809,4285086],[3515250,4863959],[4094122,5394400],[4717018,5872368],[5379210,6294226],[6075653,6656769],[6801033,6957229],[7549842,7193328],[8316383,7363266],[9094809,7465751],[9879211,7500000],[10663611,7465750],[11442038,7363265],[12208580,7193327],[12957389,6957228],[13682769,6656768],[14379209,6294227],[15041405,5872366],[15664297,5394401],[16243171,4863960],[16758641,4301424],[17251579,3662185],[17673439,3000000],[18035980,2303556],[18336441,1578177],[18572539,829368],[18750748,0],[19758422,0],[19727293,236479],[19538467,1088188],[19276136,1920196],[18942292,2726179],[18539460,3499999],[18070731,4235755],[17539650,4927877],[16950279,5571067],[16307090,6160437],[15614974,6691519],[14879209,7160248],[14105392,7563079],[13299407,7896927],[12467399,8159255],[11615691,8348082],[10750769,8461952],[9879211,8500000],[9007652,8461952],[8142729,8348082],[7291022,8159255],[6459015,7896927],[5653029,7563079],[4879210,7160247],[4143447,6691519],[3451331,6160437],[2808141,5571066],[2218773,4927878],[1687689,4235755],[1218962,3499999],[827499,2748020],[482284,1920196],[219954,1088186],[31126,236479],[0,0],[1005754,0]),
  89. );
  90. my $res = $expolygon->medial_axis(scale 1.324888, scale 0.25);
  91. is scalar(@$res), 1, 'medial axis of a semicircumference is a single line';
  92. # check whether turns are all CCW or all CW
  93. my @all_lines = @{$res->[0]->lines};
  94. # remove lines that are near the end.
  95. my @lines = grep($_->a->y >= 1578184 || $_->b->y >= 1578184, @all_lines);
  96. my @angles = map { $lines[$_-1]->ccw($lines[$_]->b) } 1..$#lines;
  97. ok !!(none { $_ < 0 } @angles) || (none { $_ > 0 } @angles),
  98. 'all medial axis segments of a semicircumference have the same orientation (but the 2 end points)';
  99. }
  100. {
  101. my $expolygon = Slic3r::ExPolygon->new(Slic3r::Polygon->new_scale(
  102. [4.3, 4], [4.3, 0], [4,0], [4,4], [0,4], [0,4.5], [4,4.5], [4,10], [4.3,10], [4.3, 4.5],
  103. [6, 4.5], [6,10], [6.2,10], [6.2,4.5], [10,4.5], [10,4], [6.2,4], [6.2,0], [6, 0], [6, 4],
  104. ));
  105. $expolygon->contour->make_counter_clockwise();
  106. my $res = $expolygon->medial_axis(scale 0.55, scale 0.25);
  107. is scalar(@$res), 2, 'medial axis of a (bit too narrow) french cross is two lines';
  108. ok unscale($res->[0]->length) >= (9.9) - epsilon, 'medial axis has reasonable length';
  109. ok unscale($res->[1]->length) >= (9.9) - epsilon, 'medial axis has reasonable length';
  110. my @lines1 = @{$res->[0]->lines};
  111. my @angles1 = map { $lines1[$_-1]->ccw($lines1[$_]->b) } 1..$#lines1;
  112. my @lines2 = @{$res->[1]->lines};
  113. my @angles2 = map { $lines2[$_-1]->ccw($lines2[$_]->b) } 1..$#lines2;
  114. my @angles = (@angles1, @angles2);
  115. ok !!(none { $_ != 0 } @angles),
  116. 'medial axis of a (bit too narrow) french cross is two lines has only strait lines';
  117. }
  118. {
  119. my $expolygon = Slic3r::ExPolygon->new(Slic3r::Polygon->new_scale(
  120. [0.86526705,1.4509841], [0.57696039,1.8637021], [0.4502297,2.5569978], [0.45626199,3.2965596], [1.1218851,3.3049455], [0.96681072,2.8243202], [0.86328971,2.2056997], [0.85367905,1.7790778],
  121. ));
  122. $expolygon->contour->make_counter_clockwise();
  123. my $res = $expolygon->medial_axis(scale 1, scale 0.25);
  124. is scalar(@$res), 1, 'medial axis of a (bit too narrow) french cross is two lines';
  125. ok unscale($res->[0]->length) >= (1.4) - epsilon, 'medial axis has reasonable length';
  126. # TODO: check if min width is < 0.3 and max width is > 0.6 (min($res->[0]->width.front, $res->[0]->width.back) # problem: can't have access to width
  127. }
  128. {
  129. my $expolygon = Slic3r::ExPolygon->new(Slic3r::Polygon->new_scale(
  130. [100, 100],
  131. [120, 100],
  132. [112, 200],
  133. [108, 200],
  134. ));
  135. my $res = $expolygon->medial_axis(scale 20, scale 0.5);
  136. is scalar(@$res), 1, 'medial axis of a narrow trapezoid is a single line';
  137. ok unscale($res->[0]->length) >= (200-100 - (120-100)) - epsilon, 'medial axis has reasonable length';
  138. }
  139. {
  140. my $expolygon = Slic3r::ExPolygon->new(Slic3r::Polygon->new_scale(
  141. [100, 100],
  142. [120, 100],
  143. [120, 180],
  144. [200, 180],
  145. [200, 200],
  146. [100, 200],
  147. ));
  148. my $res = $expolygon->medial_axis(scale 20, scale 0.5);
  149. is scalar(@$res), 1, 'medial axis of a L shape is a single polyline';
  150. my $len = unscale($res->[0]->length) + 20; # 20 is the thickness of the expolygon, which is subtracted from the ends
  151. ok $len > 80*2 && $len < 100*2, 'medial axis has reasonable length';
  152. }
  153. {
  154. my $expolygon = Slic3r::ExPolygon->new(Slic3r::Polygon->new(
  155. [-203064906,-51459966],[-219312231,-51459966],[-219335477,-51459962],[-219376095,-51459962],[-219412047,-51459966],
  156. [-219572094,-51459966],[-219624814,-51459962],[-219642183,-51459962],[-219656665,-51459966],[-220815482,-51459966],
  157. [-220815482,-37738966],[-221117540,-37738966],[-221117540,-51762024],[-203064906,-51762024],
  158. ));
  159. my $polylines = $expolygon->medial_axis(819998, 102499.75);
  160. my $perimeter = $expolygon->contour->split_at_first_point->length;
  161. ok sum(map $_->length, @$polylines) > $perimeter/2/4*3, 'medial axis has a reasonable length';
  162. }
  163. {
  164. my $expolygon = Slic3r::ExPolygon->new(Slic3r::Polygon->new_scale(
  165. [50, 100],
  166. [1000, 102],
  167. [50, 104],
  168. ));
  169. my $res = $expolygon->medial_axis(scale 4, scale 0.5);
  170. is scalar(@$res), 1, 'medial axis of a narrow triangle is a single line';
  171. ok unscale($res->[0]->length) >= (200-100 - (120-100)) - epsilon, 'medial axis has reasonable length';
  172. }
  173. {
  174. # GH #2474
  175. my $expolygon = Slic3r::ExPolygon->new(Slic3r::Polygon->new(
  176. [91294454,31032190],[11294481,31032190],[11294481,29967810],[44969182,29967810],[89909960,29967808],[91294454,29967808]
  177. ));
  178. my $polylines = $expolygon->medial_axis(1871238, 500000);
  179. is scalar(@$polylines), 1, 'medial axis is a single polyline';
  180. my $polyline = $polylines->[0];
  181. my $expected_y = $expolygon->bounding_box->center->y; #;;
  182. ok abs(sum(map $_->y, @$polyline) / @$polyline - $expected_y) < scaled_epsilon, #,,
  183. 'medial axis is horizontal and is centered';
  184. # order polyline from left to right
  185. $polyline->reverse if $polyline->first_point->x > $polyline->last_point->x;
  186. my $polyline_bb = $polyline->bounding_box;
  187. is $polyline->first_point->x, $polyline_bb->x_min, 'expected x_min';
  188. is $polyline->last_point->x, $polyline_bb->x_max, 'expected x_max';
  189. is_deeply [ map $_->x, @$polyline ], [ sort map $_->x, @$polyline ],
  190. 'medial axis is not self-overlapping';
  191. }
  192. __END__