thin.t 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. use Test::More tests => 28;
  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 @lines = @{$res->[0]->lines};
  94. my @angles = map { $lines[$_-1]->ccw($lines[$_]->b) } 1..$#lines;
  95. ok !!(none { $_ < 0 } @angles) || (none { $_ > 0 } @angles),
  96. 'all medial axis segments of a semicircumference have the same orientation';
  97. }
  98. {
  99. my $expolygon = Slic3r::ExPolygon->new(Slic3r::Polygon->new_scale(
  100. [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],
  101. [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],
  102. ));
  103. my $res = $expolygon->medial_axis(scale 0.55, scale 0.25);
  104. is scalar(@$res), 2, 'medial axis of a (bit too narrow) french cross is two lines';
  105. ok unscale($res->[0]->length) >= (9.9) - epsilon, 'medial axis has reasonable length';
  106. ok unscale($res->[1]->length) >= (9.9) - epsilon, 'medial axis has reasonable length';
  107. }
  108. {
  109. my $expolygon = Slic3r::ExPolygon->new(Slic3r::Polygon->new_scale(
  110. [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],
  111. ));
  112. my $res = $expolygon->medial_axis(scale 1, scale 0.25);
  113. is scalar(@$res), 1, 'medial axis of a (bit too narrow) french cross is two lines';
  114. ok unscale($res->[0]->length) >= (1.4) - epsilon, 'medial axis has reasonable length';
  115. # 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
  116. }
  117. {
  118. my $expolygon = Slic3r::ExPolygon->new(Slic3r::Polygon->new_scale(
  119. [100, 100],
  120. [120, 100],
  121. [112, 200],
  122. [108, 200],
  123. ));
  124. my $res = $expolygon->medial_axis(scale 20, scale 0.5);
  125. is scalar(@$res), 1, 'medial axis of a narrow trapezoid is a single line';
  126. ok unscale($res->[0]->length) >= (200-100 - (120-100)) - epsilon, 'medial axis has reasonable length';
  127. }
  128. {
  129. my $expolygon = Slic3r::ExPolygon->new(Slic3r::Polygon->new_scale(
  130. [100, 100],
  131. [120, 100],
  132. [120, 180],
  133. [200, 180],
  134. [200, 200],
  135. [100, 200],
  136. ));
  137. my $res = $expolygon->medial_axis(scale 20, scale 0.5);
  138. is scalar(@$res), 1, 'medial axis of a L shape is a single polyline';
  139. my $len = unscale($res->[0]->length) + 20; # 20 is the thickness of the expolygon, which is subtracted from the ends
  140. ok $len > 80*2 && $len < 100*2, 'medial axis has reasonable length';
  141. }
  142. {
  143. my $expolygon = Slic3r::ExPolygon->new(Slic3r::Polygon->new(
  144. [-203064906,-51459966],[-219312231,-51459966],[-219335477,-51459962],[-219376095,-51459962],[-219412047,-51459966],
  145. [-219572094,-51459966],[-219624814,-51459962],[-219642183,-51459962],[-219656665,-51459966],[-220815482,-51459966],
  146. [-220815482,-37738966],[-221117540,-37738966],[-221117540,-51762024],[-203064906,-51762024],
  147. ));
  148. my $polylines = $expolygon->medial_axis(819998, 102499.75);
  149. my $perimeter = $expolygon->contour->split_at_first_point->length;
  150. ok sum(map $_->length, @$polylines) > $perimeter/2/4*3, 'medial axis has a reasonable length';
  151. }
  152. {
  153. my $expolygon = Slic3r::ExPolygon->new(Slic3r::Polygon->new_scale(
  154. [50, 100],
  155. [1000, 102],
  156. [50, 104],
  157. ));
  158. my $res = $expolygon->medial_axis(scale 4, scale 0.5);
  159. is scalar(@$res), 1, 'medial axis of a narrow triangle is a single line';
  160. ok unscale($res->[0]->length) >= (200-100 - (120-100)) - epsilon, 'medial axis has reasonable length';
  161. }
  162. {
  163. # GH #2474
  164. my $expolygon = Slic3r::ExPolygon->new(Slic3r::Polygon->new(
  165. [91294454,31032190],[11294481,31032190],[11294481,29967810],[44969182,29967810],[89909960,29967808],[91294454,29967808]
  166. ));
  167. my $polylines = $expolygon->medial_axis(1871238, 500000);
  168. is scalar(@$polylines), 1, 'medial axis is a single polyline';
  169. my $polyline = $polylines->[0];
  170. my $expected_y = $expolygon->bounding_box->center->y; #;;
  171. ok abs(sum(map $_->y, @$polyline) / @$polyline - $expected_y) < scaled_epsilon, #,,
  172. 'medial axis is horizontal and is centered';
  173. # order polyline from left to right
  174. $polyline->reverse if $polyline->first_point->x > $polyline->last_point->x;
  175. my $polyline_bb = $polyline->bounding_box;
  176. is $polyline->first_point->x, $polyline_bb->x_min, 'expected x_min';
  177. is $polyline->last_point->x, $polyline_bb->x_max, 'expected x_max';
  178. is_deeply [ map $_->x, @$polyline ], [ sort map $_->x, @$polyline ],
  179. 'medial axis is not self-overlapping';
  180. }
  181. __END__