fill.t 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. use Test::More;
  2. use strict;
  3. use warnings;
  4. plan tests => 95;
  5. BEGIN {
  6. use FindBin;
  7. use lib "$FindBin::Bin/../lib";
  8. use local::lib "$FindBin::Bin/../local-lib";
  9. }
  10. use List::Util qw(first sum max);
  11. use Slic3r;
  12. use Slic3r::Geometry qw(PI X Y scaled_epsilon scale unscale convex_hull);
  13. use Slic3r::Geometry::Clipper qw(union diff diff_ex offset offset2_ex diff_pl union_ex);
  14. use Slic3r::Surface qw(:types);
  15. use Slic3r::Test;
  16. sub scale_points (@) { map [scale $_->[X], scale $_->[Y]], @_ }
  17. {
  18. my $print = Slic3r::Print->new;
  19. my $surface_width = 250;
  20. my $distance = Slic3r::Flow::solid_spacing($surface_width, 47);
  21. is $distance, 50, 'adjusted solid distance';
  22. is $surface_width % $distance, 0, 'adjusted solid distance';
  23. }
  24. {
  25. my $filler = Slic3r::Filler->new_from_type('rectilinear');
  26. $filler->set_angle(-(PI)/2);
  27. $filler->set_min_spacing(5);
  28. $filler->set_dont_adjust(1);
  29. $filler->set_endpoints_overlap(0);
  30. my $test = sub {
  31. my ($expolygon) = @_;
  32. my $surface = Slic3r::Surface->new(
  33. surface_type => S_TYPE_TOP,
  34. expolygon => $expolygon,
  35. );
  36. return $filler->fill_surface($surface);
  37. };
  38. # square
  39. $filler->set_density($filler->min_spacing / 50);
  40. for my $i (0..3) {
  41. # check that it works regardless of the points order
  42. my @points = ([0,0], [100,0], [100,100], [0,100]);
  43. @points = (@points[$i..$#points], @points[0..($i-1)]);
  44. my $paths = $test->(my $e = Slic3r::ExPolygon->new([ scale_points @points ]));
  45. is(scalar @$paths, 1, 'one continuous path') or done_testing, exit;
  46. ok abs($paths->[0]->length - scale(3*100 + 2*50)) - scaled_epsilon, 'path has expected length';
  47. }
  48. # diamond with endpoints on grid
  49. {
  50. my $paths = $test->(my $e = Slic3r::ExPolygon->new([ scale_points [0,0], [100,0], [150,50], [100,100], [0,100], [-50,50] ]));
  51. is(scalar @$paths, 1, 'one continuous path') or done_testing, exit;
  52. }
  53. # square with hole
  54. for my $angle (-(PI/2), -(PI/4), -(PI), PI/2, PI) {
  55. for my $spacing (25, 5, 7.5, 8.5) {
  56. $filler->set_density($filler->min_spacing / $spacing);
  57. $filler->set_angle($angle);
  58. my $paths = $test->(my $e = Slic3r::ExPolygon->new(
  59. [ scale_points [0,0], [100,0], [100,100], [0,100] ],
  60. [ scale_points reverse [25,25], [75,25], [75,75], [25,75] ],
  61. ));
  62. if (0) {
  63. require "Slic3r/SVG.pm";
  64. Slic3r::SVG::output(
  65. "fill.svg",
  66. no_arrows => 1,
  67. expolygons => [$e],
  68. polylines => $paths,
  69. );
  70. }
  71. ok(@$paths >= 2 && @$paths <= 3, '2 or 3 continuous paths') or done_testing, exit;
  72. ok(!@{diff_pl($paths->arrayref, offset(\@$e, +scaled_epsilon*10))},
  73. 'paths don\'t cross hole') or done_testing, exit;
  74. }
  75. }
  76. # rotated square
  77. $filler->set_angle(PI/4);
  78. $filler->set_dont_adjust(0);
  79. $filler->set_min_spacing(0.654498);
  80. $filler->set_endpoints_overlap(unscale(359974));
  81. $filler->set_density(1);
  82. $filler->set_layer_id(66);
  83. $filler->set_z(20.15);
  84. {
  85. my $e = Slic3r::ExPolygon->new(
  86. Slic3r::Polygon->new([25771516,14142125],[14142138,25771515],[2512749,14142131],[14142125,2512749]),
  87. );
  88. my $paths = $test->($e);
  89. is(scalar @$paths, 1, 'one continuous path') or done_testing, exit;
  90. ok abs($paths->[0]->length - scale(3*100 + 2*50)) - scaled_epsilon, 'path has expected length';
  91. }
  92. }
  93. {
  94. my $expolygon = Slic3r::ExPolygon->new([ scale_points [0,0], [50,0], [50,50], [0,50] ]);
  95. my $filler = Slic3r::Filler->new_from_type('rectilinear');
  96. $filler->set_bounding_box($expolygon->bounding_box);
  97. $filler->set_angle(0);
  98. my $surface = Slic3r::Surface->new(
  99. surface_type => S_TYPE_TOP,
  100. expolygon => $expolygon,
  101. );
  102. my $flow = Slic3r::Flow->new(
  103. width => 0.69,
  104. height => 0.4,
  105. nozzle_diameter => 0.50,
  106. );
  107. $filler->set_min_spacing($flow->spacing);
  108. $filler->set_density(1);
  109. foreach my $angle (0, 45) {
  110. $surface->expolygon->rotate(Slic3r::Geometry::deg2rad($angle), [0,0]);
  111. my $paths = $filler->fill_surface($surface, layer_height => 0.4, density => 0.4);
  112. is scalar @$paths, 1, 'one continuous path';
  113. }
  114. }
  115. {
  116. my $test = sub {
  117. my ($expolygon, $flow_spacing, $angle, $density) = @_;
  118. my $filler = Slic3r::Filler->new_from_type('rectilinear');
  119. $filler->set_bounding_box($expolygon->bounding_box);
  120. $filler->set_angle($angle // 0);
  121. $filler->set_dont_adjust(0);
  122. my $surface = Slic3r::Surface->new(
  123. surface_type => S_TYPE_BOTTOM,
  124. expolygon => $expolygon,
  125. );
  126. my $flow = Slic3r::Flow->new(
  127. width => $flow_spacing,
  128. height => 0.4,
  129. nozzle_diameter => $flow_spacing,
  130. );
  131. $filler->set_min_spacing($flow->spacing);
  132. my $paths = $filler->fill_surface(
  133. $surface,
  134. layer_height => $flow->height,
  135. density => $density // 1,
  136. );
  137. # check whether any part was left uncovered
  138. my @grown_paths = map @{Slic3r::Polyline->new(@$_)->grow(scale $filler->spacing/2)}, @$paths;
  139. my $uncovered = diff_ex([ @$expolygon ], [ @grown_paths ], 1);
  140. # ignore very small dots
  141. @$uncovered = grep $_->area > (scale $flow_spacing)**2, @$uncovered;
  142. is scalar(@$uncovered), 0, 'solid surface is fully filled';
  143. if (0 && @$uncovered) {
  144. require "Slic3r/SVG.pm";
  145. Slic3r::SVG::output(
  146. "uncovered.svg",
  147. expolygons => [$expolygon],
  148. red_expolygons => $uncovered,
  149. polylines => $paths,
  150. );
  151. exit;
  152. }
  153. };
  154. my $expolygon = Slic3r::ExPolygon->new([
  155. [6883102, 9598327.01296997],
  156. [6883102, 20327272.01297],
  157. [3116896, 20327272.01297],
  158. [3116896, 9598327.01296997],
  159. ]);
  160. $test->($expolygon, 0.55);
  161. for (1..20) {
  162. $expolygon->scale(1.05);
  163. $test->($expolygon, 0.55);
  164. }
  165. $expolygon = Slic3r::ExPolygon->new(
  166. [[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]]
  167. );
  168. $test->($expolygon, 0.524341649025257, PI/2);
  169. $expolygon = Slic3r::ExPolygon->new([ scale_points [0,0], [98,0], [98,10], [0,10] ]);
  170. $test->($expolygon, 0.5, 45, 0.99); # non-solid infill
  171. }
  172. {
  173. my $collection = Slic3r::Polyline::Collection->new(
  174. Slic3r::Polyline->new([0,15], [0,18], [0,20]),
  175. Slic3r::Polyline->new([0,10], [0,8], [0,5]),
  176. );
  177. is_deeply
  178. [ map $_->[Y], map @$_, @{$collection->chained_path_from(Slic3r::Point->new(0,30), 0)} ],
  179. [20, 18, 15, 10, 8, 5],
  180. 'chained path';
  181. }
  182. {
  183. my $collection = Slic3r::Polyline::Collection->new(
  184. Slic3r::Polyline->new([4,0], [10,0], [15,0]),
  185. Slic3r::Polyline->new([10,5], [15,5], [20,5]),
  186. );
  187. is_deeply
  188. [ map $_->[X], map @$_, @{$collection->chained_path_from(Slic3r::Point->new(30,0), 0)} ],
  189. [reverse 4, 10, 15, 10, 15, 20],
  190. 'chained path';
  191. }
  192. {
  193. my $collection = Slic3r::ExtrusionPath::Collection->new(
  194. map Slic3r::ExtrusionPath->new(polyline => $_, role => 0, mm3_per_mm => 1),
  195. Slic3r::Polyline->new([0,15], [0,18], [0,20]),
  196. Slic3r::Polyline->new([0,10], [0,8], [0,5]),
  197. );
  198. is_deeply
  199. [ map $_->[Y], map @{$_->polyline}, @{$collection->chained_path_from(Slic3r::Point->new(0,30), 0)} ],
  200. [20, 18, 15, 10, 8, 5],
  201. 'chained path';
  202. }
  203. {
  204. my $collection = Slic3r::ExtrusionPath::Collection->new(
  205. map Slic3r::ExtrusionPath->new(polyline => $_, role => 0, mm3_per_mm => 1),
  206. Slic3r::Polyline->new([15,0], [10,0], [4,0]),
  207. Slic3r::Polyline->new([10,5], [15,5], [20,5]),
  208. );
  209. is_deeply
  210. [ map $_->[X], map @{$_->polyline}, @{$collection->chained_path_from(Slic3r::Point->new(30,0), 0)} ],
  211. [reverse 4, 10, 15, 10, 15, 20],
  212. 'chained path';
  213. }
  214. for my $pattern (qw(rectilinear honeycomb hilbertcurve concentric)) {
  215. my $config = Slic3r::Config->new_from_defaults;
  216. $config->set('fill_pattern', $pattern);
  217. $config->set('external_fill_pattern', $pattern);
  218. $config->set('perimeters', 1);
  219. $config->set('skirts', 0);
  220. $config->set('fill_density', 20);
  221. $config->set('layer_height', 0.05);
  222. $config->set('perimeter_extruder', 1);
  223. $config->set('infill_extruder', 2);
  224. my $print = Slic3r::Test::init_print('20mm_cube', config => $config, scale => 2);
  225. ok my $gcode = Slic3r::Test::gcode($print), "successful $pattern infill generation";
  226. my $tool = undef;
  227. my @perimeter_points = my @infill_points = ();
  228. Slic3r::GCode::Reader->new->parse($gcode, sub {
  229. my ($self, $cmd, $args, $info) = @_;
  230. if ($cmd =~ /^T(\d+)/) {
  231. $tool = $1;
  232. } elsif ($cmd eq 'G1' && $info->{extruding} && $info->{dist_XY} > 0) {
  233. if ($tool == $config->perimeter_extruder-1) {
  234. push @perimeter_points, Slic3r::Point->new_scale($args->{X}, $args->{Y});
  235. } elsif ($tool == $config->infill_extruder-1) {
  236. push @infill_points, Slic3r::Point->new_scale($args->{X}, $args->{Y});
  237. }
  238. }
  239. });
  240. my $convex_hull = convex_hull(\@perimeter_points);
  241. ok !(defined first { !$convex_hull->contains_point($_) } @infill_points), "infill does not exceed perimeters ($pattern)";
  242. }
  243. {
  244. my $config = Slic3r::Config->new_from_defaults;
  245. $config->set('infill_only_where_needed', 1);
  246. $config->set('bottom_solid_layers', 0);
  247. $config->set('infill_extruder', 2);
  248. $config->set('infill_extrusion_width', 0.5);
  249. $config->set('fill_density', 40);
  250. $config->set('cooling', 0); # for preventing speeds from being altered
  251. $config->set('first_layer_speed', '100%'); # for preventing speeds from being altered
  252. my $test = sub {
  253. my $print = Slic3r::Test::init_print('pyramid', config => $config);
  254. my $tool = undef;
  255. my @infill_extrusions = (); # array of polylines
  256. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  257. my ($self, $cmd, $args, $info) = @_;
  258. if ($cmd =~ /^T(\d+)/) {
  259. $tool = $1;
  260. } elsif ($cmd eq 'G1' && $info->{extruding} && $info->{dist_XY} > 0) {
  261. if ($tool == $config->infill_extruder-1) {
  262. push @infill_extrusions, Slic3r::Line->new_scale(
  263. [ $self->X, $self->Y ],
  264. [ $info->{new_X}, $info->{new_Y} ],
  265. );
  266. }
  267. }
  268. });
  269. return 0 if !@infill_extrusions; # prevent calling convex_hull() with no points
  270. my $convex_hull = convex_hull([ map $_->pp, map @$_, @infill_extrusions ]);
  271. return unscale unscale sum(map $_->area, @{offset([$convex_hull], scale(+$config->infill_extrusion_width/2))});
  272. };
  273. my $tolerance = 5; # mm^2
  274. $config->set('solid_infill_below_area', 0);
  275. ok $test->() < $tolerance,
  276. 'no infill is generated when using infill_only_where_needed on a pyramid';
  277. $config->set('solid_infill_below_area', 70);
  278. ok abs($test->() - $config->solid_infill_below_area) < $tolerance,
  279. 'infill is only generated under the forced solid shells';
  280. }
  281. {
  282. my $config = Slic3r::Config->new_from_defaults;
  283. $config->set('skirts', 0);
  284. $config->set('perimeters', 1);
  285. $config->set('fill_density', 0);
  286. $config->set('top_solid_layers', 0);
  287. $config->set('bottom_solid_layers', 0);
  288. $config->set('solid_infill_below_area', 20000000);
  289. $config->set('solid_infill_every_layers', 2);
  290. $config->set('perimeter_speed', 99);
  291. $config->set('external_perimeter_speed', 99);
  292. $config->set('cooling', 0);
  293. $config->set('first_layer_speed', '100%');
  294. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  295. my %layers_with_extrusion = ();
  296. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  297. my ($self, $cmd, $args, $info) = @_;
  298. if ($cmd eq 'G1' && $info->{dist_XY} > 0 && $info->{extruding}) {
  299. if (($args->{F} // $self->F) != $config->perimeter_speed*60) {
  300. $layers_with_extrusion{$self->Z} = ($args->{F} // $self->F);
  301. }
  302. }
  303. });
  304. ok !%layers_with_extrusion,
  305. "solid_infill_below_area and solid_infill_every_layers are ignored when fill_density is 0";
  306. }
  307. {
  308. my $config = Slic3r::Config->new_from_defaults;
  309. $config->set('skirts', 0);
  310. $config->set('perimeters', 3);
  311. $config->set('fill_density', 0);
  312. $config->set('layer_height', 0.2);
  313. $config->set('first_layer_height', 0.2);
  314. $config->set('nozzle_diameter', [0.35]);
  315. $config->set('infill_extruder', 2);
  316. $config->set('solid_infill_extruder', 2);
  317. $config->set('infill_extrusion_width', 0.52);
  318. $config->set('solid_infill_extrusion_width', 0.52);
  319. $config->set('first_layer_extrusion_width', 0);
  320. my $print = Slic3r::Test::init_print('A', config => $config);
  321. my %infill = (); # Z => [ Line, Line ... ]
  322. my $tool = undef;
  323. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  324. my ($self, $cmd, $args, $info) = @_;
  325. if ($cmd =~ /^T(\d+)/) {
  326. $tool = $1;
  327. } elsif ($cmd eq 'G1' && $info->{extruding} && $info->{dist_XY} > 0) {
  328. if ($tool == $config->infill_extruder-1) {
  329. my $z = 1 * $self->Z;
  330. $infill{$z} ||= [];
  331. push @{$infill{$z}}, Slic3r::Line->new_scale(
  332. [ $self->X, $self->Y ],
  333. [ $info->{new_X}, $info->{new_Y} ],
  334. );
  335. }
  336. }
  337. });
  338. my $grow_d = scale($config->infill_extrusion_width)/2;
  339. my $layer0_infill = union([ map @{$_->grow($grow_d)}, @{ $infill{0.2} } ]);
  340. my $layer1_infill = union([ map @{$_->grow($grow_d)}, @{ $infill{0.4} } ]);
  341. my $diff = diff($layer0_infill, $layer1_infill);
  342. $diff = offset2_ex($diff, -$grow_d, +$grow_d);
  343. $diff = [ grep { $_->area > 2*(($grow_d*2)**2) } @$diff ];
  344. is scalar(@$diff), 0, 'no missing parts in solid shell when fill_density is 0';
  345. }
  346. {
  347. # GH: #2697
  348. my $config = Slic3r::Config->new_from_defaults;
  349. $config->set('perimeter_extrusion_width', 0.72);
  350. $config->set('top_infill_extrusion_width', 0.1);
  351. $config->set('infill_extruder', 2); # in order to distinguish infill
  352. $config->set('solid_infill_extruder', 2); # in order to distinguish infill
  353. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  354. my %infill = (); # Z => [ Line, Line ... ]
  355. my %other = (); # Z => [ Line, Line ... ]
  356. my $tool = undef;
  357. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  358. my ($self, $cmd, $args, $info) = @_;
  359. if ($cmd =~ /^T(\d+)/) {
  360. $tool = $1;
  361. } elsif ($cmd eq 'G1' && $info->{extruding} && $info->{dist_XY} > 0) {
  362. my $z = 1 * $self->Z;
  363. my $line = Slic3r::Line->new_scale(
  364. [ $self->X, $self->Y ],
  365. [ $info->{new_X}, $info->{new_Y} ],
  366. );
  367. if ($tool == $config->infill_extruder-1) {
  368. $infill{$z} //= [];
  369. push @{$infill{$z}}, $line;
  370. } else {
  371. $other{$z} //= [];
  372. push @{$other{$z}}, $line;
  373. }
  374. }
  375. });
  376. my $top_z = max(keys %infill);
  377. my $top_infill_grow_d = scale($config->top_infill_extrusion_width)/2;
  378. my $top_infill = union([ map @{$_->grow($top_infill_grow_d)}, @{ $infill{$top_z} } ]);
  379. my $perimeters_grow_d = scale($config->perimeter_extrusion_width)/2;
  380. my $perimeters = union([ map @{$_->grow($perimeters_grow_d)}, @{ $other{$top_z} } ]);
  381. my $covered = union_ex([ @$top_infill, @$perimeters ]);
  382. my @holes = map @{$_->holes}, @$covered;
  383. ok sum(map unscale unscale $_->area*-1, @holes) < 1, 'no gaps between top solid infill and perimeters';
  384. }
  385. __END__