fill.t 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. use Test::More;
  2. use strict;
  3. use warnings;
  4. #plan tests => 43;
  5. # Test of a 100% coverage is off.
  6. plan tests => 19;
  7. BEGIN {
  8. use FindBin;
  9. use lib "$FindBin::Bin/../lib";
  10. use local::lib "$FindBin::Bin/../local-lib";
  11. }
  12. use List::Util qw(first sum);
  13. use Slic3r;
  14. use Slic3r::Geometry qw(X Y scale unscale convex_hull);
  15. use Slic3r::Geometry::Clipper qw(union diff diff_ex offset offset2_ex);
  16. use Slic3r::Surface qw(:types);
  17. use Slic3r::Test;
  18. sub scale_points (@) { map [scale $_->[X], scale $_->[Y]], @_ }
  19. {
  20. my $expolygon = Slic3r::ExPolygon->new([ scale_points [0,0], [50,0], [50,50], [0,50] ]);
  21. my $filler = Slic3r::Filler->new_from_type('rectilinear');
  22. $filler->set_bounding_box($expolygon->bounding_box);
  23. $filler->set_angle(0);
  24. my $surface = Slic3r::Surface->new(
  25. surface_type => S_TYPE_TOP,
  26. expolygon => $expolygon,
  27. );
  28. my $flow = Slic3r::Flow->new(
  29. width => 0.69,
  30. height => 0.4,
  31. nozzle_diameter => 0.50,
  32. );
  33. $filler->set_spacing($flow->spacing);
  34. foreach my $angle (0, 45) {
  35. $surface->expolygon->rotate(Slic3r::Geometry::deg2rad($angle), [0,0]);
  36. my $paths = $filler->fill_surface($surface, layer_height => 0.4, density => 0.4);
  37. is scalar @$paths, 1, 'one continuous path';
  38. }
  39. }
  40. SKIP:
  41. {
  42. skip "The FillRectilinear2 does not fill the surface completely", 1;
  43. my $test = sub {
  44. my ($expolygon, $flow_spacing, $angle, $density) = @_;
  45. my $filler = Slic3r::Filler->new_from_type('rectilinear');
  46. $filler->set_bounding_box($expolygon->bounding_box);
  47. $filler->set_angle($angle // 0);
  48. # Adjust line spacing to fill the region.
  49. $filler->set_dont_adjust(0);
  50. $filler->set_link_max_length(scale(1.2*$flow_spacing));
  51. my $surface = Slic3r::Surface->new(
  52. surface_type => S_TYPE_BOTTOM,
  53. expolygon => $expolygon,
  54. );
  55. my $flow = Slic3r::Flow->new(
  56. width => $flow_spacing,
  57. height => 0.4,
  58. nozzle_diameter => $flow_spacing,
  59. );
  60. $filler->set_spacing($flow->spacing);
  61. my $paths = $filler->fill_surface(
  62. $surface,
  63. layer_height => $flow->height,
  64. density => $density // 1,
  65. );
  66. # check whether any part was left uncovered
  67. my @grown_paths = map @{Slic3r::Polyline->new(@$_)->grow(scale $filler->spacing/2)}, @$paths;
  68. my $uncovered = diff_ex([ @$expolygon ], [ @grown_paths ], 1);
  69. # ignore very small dots
  70. my $uncovered_filtered = [ grep $_->area > (scale $flow_spacing)**2, @$uncovered ];
  71. is scalar(@$uncovered_filtered), 0, 'solid surface is fully filled';
  72. if (0 && @$uncovered_filtered) {
  73. require "Slic3r/SVG.pm";
  74. Slic3r::SVG::output("uncovered.svg",
  75. no_arrows => 1,
  76. expolygons => [ $expolygon ],
  77. blue_expolygons => [ @$uncovered ],
  78. red_expolygons => [ @$uncovered_filtered ],
  79. polylines => [ @$paths ],
  80. );
  81. exit;
  82. }
  83. };
  84. my $expolygon = Slic3r::ExPolygon->new([
  85. [6883102, 9598327.01296997],
  86. [6883102, 20327272.01297],
  87. [3116896, 20327272.01297],
  88. [3116896, 9598327.01296997],
  89. ]);
  90. $test->($expolygon, 0.55);
  91. for (1..20) {
  92. $expolygon->scale(1.05);
  93. $test->($expolygon, 0.55);
  94. }
  95. $expolygon = Slic3r::ExPolygon->new(
  96. [[59515297,5422499],[59531249,5578697],[59695801,6123186],[59965713,6630228],[60328214,7070685],[60773285,7434379],[61274561,7702115],[61819378,7866770],[62390306,7924789],[62958700,7866744],[63503012,7702244],[64007365,7434357],[64449960,7070398],[64809327,6634999],[65082143,6123325],[65245005,5584454],[65266967,5422499],[66267307,5422499],[66269190,8310081],[66275379,17810072],[66277259,20697500],[65267237,20697500],[65245004,20533538],[65082082,19994444],[64811462,19488579],[64450624,19048208],[64012101,18686514],[63503122,18415781],[62959151,18251378],[62453416,18198442],[62390147,18197355],[62200087,18200576],[61813519,18252990],[61274433,18415918],[60768598,18686517],[60327567,19047892],[59963609,19493297],[59695865,19994587],[59531222,20539379],[59515153,20697500],[58502480,20697500],[58502480,5422499]]
  97. );
  98. $test->($expolygon, 0.524341649025257);
  99. $expolygon = Slic3r::ExPolygon->new([ scale_points [0,0], [98,0], [98,10], [0,10] ]);
  100. $test->($expolygon, 0.5, 45, 0.99); # non-solid infill
  101. }
  102. {
  103. my $collection = Slic3r::Polyline::Collection->new(
  104. Slic3r::Polyline->new([0,15], [0,18], [0,20]),
  105. Slic3r::Polyline->new([0,10], [0,8], [0,5]),
  106. );
  107. is_deeply
  108. [ map $_->[Y], map @$_, @{$collection->chained_path_from(Slic3r::Point->new(0,30), 0)} ],
  109. [20, 18, 15, 10, 8, 5],
  110. 'chained path';
  111. }
  112. {
  113. my $collection = Slic3r::Polyline::Collection->new(
  114. Slic3r::Polyline->new([4,0], [10,0], [15,0]),
  115. Slic3r::Polyline->new([10,5], [15,5], [20,5]),
  116. );
  117. is_deeply
  118. [ map $_->[X], map @$_, @{$collection->chained_path_from(Slic3r::Point->new(30,0), 0)} ],
  119. [reverse 4, 10, 15, 10, 15, 20],
  120. 'chained path';
  121. }
  122. {
  123. my $collection = Slic3r::ExtrusionPath::Collection->new(
  124. map Slic3r::ExtrusionPath->new(polyline => $_, role => 0, mm3_per_mm => 1),
  125. Slic3r::Polyline->new([0,15], [0,18], [0,20]),
  126. Slic3r::Polyline->new([0,10], [0,8], [0,5]),
  127. );
  128. is_deeply
  129. [ map $_->[Y], map @{$_->polyline}, @{$collection->chained_path_from(Slic3r::Point->new(0,30), 0)} ],
  130. [20, 18, 15, 10, 8, 5],
  131. 'chained path';
  132. }
  133. {
  134. my $collection = Slic3r::ExtrusionPath::Collection->new(
  135. map Slic3r::ExtrusionPath->new(polyline => $_, role => 0, mm3_per_mm => 1),
  136. Slic3r::Polyline->new([15,0], [10,0], [4,0]),
  137. Slic3r::Polyline->new([10,5], [15,5], [20,5]),
  138. );
  139. is_deeply
  140. [ map $_->[X], map @{$_->polyline}, @{$collection->chained_path_from(Slic3r::Point->new(30,0), 0)} ],
  141. [reverse 4, 10, 15, 10, 15, 20],
  142. 'chained path';
  143. }
  144. for my $pattern (qw(rectilinear honeycomb hilbertcurve concentric)) {
  145. my $config = Slic3r::Config::new_from_defaults;
  146. $config->set('nozzle_diameter', [0.4,0.4,0.4,0.4]);
  147. $config->set('fill_pattern', $pattern);
  148. $config->set('top_fill_pattern', $pattern);
  149. $config->set('bottom_fill_pattern', $pattern);
  150. $config->set('perimeters', 1);
  151. $config->set('skirts', 0);
  152. $config->set('fill_density', 20);
  153. $config->set('layer_height', 0.05);
  154. $config->set('perimeter_extruder', 1);
  155. $config->set('infill_extruder', 2);
  156. my $print = Slic3r::Test::init_print('20mm_cube', config => $config, scale => 2);
  157. ok my $gcode = Slic3r::Test::gcode($print), "successful $pattern infill generation";
  158. my $tool = undef;
  159. my @perimeter_points = my @infill_points = ();
  160. Slic3r::GCode::Reader->new->parse($gcode, sub {
  161. my ($self, $cmd, $args, $info) = @_;
  162. if ($cmd =~ /^T(\d+)/) {
  163. $tool = $1;
  164. } elsif ($cmd eq 'G1' && $info->{extruding} && $info->{dist_XY} > 0) {
  165. if ($tool == $config->perimeter_extruder-1) {
  166. push @perimeter_points, Slic3r::Point->new_scale($args->{X}, $args->{Y});
  167. } elsif ($tool == $config->infill_extruder-1) {
  168. push @infill_points, Slic3r::Point->new_scale($args->{X}, $args->{Y});
  169. }
  170. }
  171. });
  172. my $convex_hull = convex_hull(\@perimeter_points);
  173. ok !(defined first { !$convex_hull->contains_point($_) } @infill_points), "infill does not exceed perimeters ($pattern)";
  174. }
  175. {
  176. my $config = Slic3r::Config::new_from_defaults;
  177. $config->set('nozzle_diameter', [0.4,0.4,0.4,0.4]);
  178. $config->set('infill_only_where_needed', 1);
  179. $config->set('bottom_solid_layers', 0);
  180. $config->set('infill_extruder', 2);
  181. $config->set('infill_extrusion_width', 0.5);
  182. $config->set('wipe_into_infill', 0);
  183. $config->set('fill_density', 40);
  184. $config->set('cooling', [ 0 ]); # for preventing speeds from being altered
  185. $config->set('first_layer_speed', '100%'); # for preventing speeds from being altered
  186. my $test = sub {
  187. my $print = Slic3r::Test::init_print('pyramid', config => $config);
  188. my $tool = undef;
  189. my @infill_extrusions = (); # array of polylines
  190. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  191. my ($self, $cmd, $args, $info) = @_;
  192. if ($cmd =~ /^T(\d+)/) {
  193. $tool = $1;
  194. } elsif ($cmd eq 'G1' && $info->{extruding} && $info->{dist_XY} > 0) {
  195. if ($tool == $config->infill_extruder-1) {
  196. push @infill_extrusions, Slic3r::Line->new_scale(
  197. [ $self->X, $self->Y ],
  198. [ $info->{new_X}, $info->{new_Y} ],
  199. );
  200. }
  201. }
  202. });
  203. return 0 if !@infill_extrusions; # prevent calling convex_hull() with no points
  204. my $convex_hull = convex_hull([ map $_->pp, map @$_, @infill_extrusions ]);
  205. return unscale unscale sum(map $_->area, @{offset([$convex_hull], scale(+$config->infill_extrusion_width/2))});
  206. };
  207. my $tolerance = 5; # mm^2
  208. $config->set('solid_infill_below_area', 0);
  209. ok $test->() < $tolerance,
  210. 'no infill is generated when using infill_only_where_needed on a pyramid';
  211. $config->set('solid_infill_below_area', 70);
  212. ok abs($test->() - $config->solid_infill_below_area) < $tolerance,
  213. 'infill is only generated under the forced solid shells';
  214. }
  215. {
  216. my $config = Slic3r::Config::new_from_defaults;
  217. $config->set('skirts', 0);
  218. $config->set('perimeters', 1);
  219. $config->set('fill_density', 0);
  220. $config->set('top_solid_layers', 0);
  221. $config->set('bottom_solid_layers', 0);
  222. $config->set('solid_infill_below_area', 20000000);
  223. $config->set('solid_infill_every_layers', 2);
  224. $config->set('perimeter_speed', 99);
  225. $config->set('external_perimeter_speed', 99);
  226. $config->set('cooling', [ 0 ]);
  227. $config->set('first_layer_speed', '100%');
  228. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  229. my %layers_with_extrusion = ();
  230. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  231. my ($self, $cmd, $args, $info) = @_;
  232. if ($cmd eq 'G1' && $info->{dist_XY} > 0 && $info->{extruding}) {
  233. if (($args->{F} // $self->F) != $config->perimeter_speed*60) {
  234. $layers_with_extrusion{$self->Z} = ($args->{F} // $self->F);
  235. }
  236. }
  237. });
  238. ok !%layers_with_extrusion,
  239. "solid_infill_below_area and solid_infill_every_layers are ignored when fill_density is 0";
  240. }
  241. {
  242. my $config = Slic3r::Config::new_from_defaults;
  243. $config->set('skirts', 0);
  244. $config->set('perimeters', 3);
  245. $config->set('fill_density', 0);
  246. $config->set('layer_height', 0.2);
  247. $config->set('first_layer_height', 0.2);
  248. $config->set('nozzle_diameter', [0.35,0.35,0.35,0.35]);
  249. $config->set('infill_extruder', 2);
  250. $config->set('solid_infill_extruder', 2);
  251. $config->set('infill_extrusion_width', 0.52);
  252. $config->set('solid_infill_extrusion_width', 0.52);
  253. $config->set('first_layer_extrusion_width', 0);
  254. my $print = Slic3r::Test::init_print('A', config => $config);
  255. my %infill = (); # Z => [ Line, Line ... ]
  256. my $tool = undef;
  257. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  258. my ($self, $cmd, $args, $info) = @_;
  259. if ($cmd =~ /^T(\d+)/) {
  260. $tool = $1;
  261. } elsif ($cmd eq 'G1' && $info->{extruding} && $info->{dist_XY} > 0) {
  262. if ($tool == $config->infill_extruder-1) {
  263. my $z = 1 * $self->Z;
  264. $infill{$z} ||= [];
  265. push @{$infill{$z}}, Slic3r::Line->new_scale(
  266. [ $self->X, $self->Y ],
  267. [ $info->{new_X}, $info->{new_Y} ],
  268. );
  269. }
  270. }
  271. });
  272. my $grow_d = scale($config->infill_extrusion_width)/2;
  273. my $layer0_infill = union([ map @{$_->grow($grow_d)}, @{ $infill{0.2} } ]);
  274. my $layer1_infill = union([ map @{$_->grow($grow_d)}, @{ $infill{0.4} } ]);
  275. my $diff = diff($layer0_infill, $layer1_infill);
  276. $diff = offset2_ex($diff, -$grow_d, +$grow_d);
  277. $diff = [ grep { $_->area > 2*(($grow_d*2)**2) } @$diff ];
  278. is scalar(@$diff), 0, 'no missing parts in solid shell when fill_density is 0';
  279. }
  280. __END__