fill.t 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. use Test::More;
  2. use strict;
  3. use warnings;
  4. plan tests => 43;
  5. BEGIN {
  6. use FindBin;
  7. use lib "$FindBin::Bin/../lib";
  8. }
  9. use List::Util qw(first sum);
  10. use Slic3r;
  11. use Slic3r::Geometry qw(X Y scale unscale convex_hull);
  12. use Slic3r::Geometry::Clipper qw(union diff_ex offset);
  13. use Slic3r::Surface qw(:types);
  14. use Slic3r::Test;
  15. sub scale_points (@) { map [scale $_->[X], scale $_->[Y]], @_ }
  16. {
  17. my $print = Slic3r::Print->new;
  18. $print->init_extruders;
  19. my $filler = Slic3r::Fill::Rectilinear->new(
  20. print => $print,
  21. bounding_box => Slic3r::Geometry::BoundingBox->new_from_points([ Slic3r::Point->new(0, 0), Slic3r::Point->new(10, 10) ]),
  22. );
  23. my $surface_width = 250;
  24. my $distance = $filler->adjust_solid_spacing(
  25. width => $surface_width,
  26. distance => 100,
  27. );
  28. is $distance, 125, 'adjusted solid distance';
  29. is $surface_width % $distance, 0, 'adjusted solid distance';
  30. }
  31. {
  32. my $expolygon = Slic3r::ExPolygon->new([ scale_points [0,0], [50,0], [50,50], [0,50] ]);
  33. my $filler = Slic3r::Fill::Rectilinear->new(
  34. bounding_box => $expolygon->bounding_box,
  35. angle => 0,
  36. );
  37. my $surface = Slic3r::Surface->new(
  38. surface_type => S_TYPE_TOP,
  39. expolygon => $expolygon,
  40. );
  41. my $flow = Slic3r::Flow->new(
  42. width => 0.69,
  43. spacing => 0.69,
  44. nozzle_diameter => 0.50,
  45. );
  46. foreach my $angle (0, 45) {
  47. $surface->expolygon->rotate(Slic3r::Geometry::deg2rad($angle), [0,0]);
  48. my ($params, @paths) = $filler->fill_surface($surface, flow => $flow, layer_height => 0.4, density => 0.4);
  49. is scalar @paths, 1, 'one continuous path';
  50. }
  51. }
  52. {
  53. my $test = sub {
  54. my ($expolygon, $flow_spacing, $angle, $density) = @_;
  55. my $filler = Slic3r::Fill::Rectilinear->new(
  56. bounding_box => $expolygon->bounding_box,
  57. angle => $angle // 0,
  58. );
  59. my $surface = Slic3r::Surface->new(
  60. surface_type => S_TYPE_BOTTOM,
  61. expolygon => $expolygon,
  62. );
  63. my $flow = Slic3r::Flow->new(
  64. width => $flow_spacing,
  65. spacing => $flow_spacing,
  66. nozzle_diameter => $flow_spacing,
  67. );
  68. my ($params, @paths) = $filler->fill_surface(
  69. $surface,
  70. flow => $flow,
  71. layer_height => 0.4,
  72. density => $density // 1,
  73. );
  74. # check whether any part was left uncovered
  75. my @grown_paths = map @{Slic3r::Polyline->new(@$_)->grow(scale $params->{flow}->spacing/2)}, @paths;
  76. my $uncovered = diff_ex([ @$expolygon ], [ @grown_paths ], 1);
  77. # ignore very small dots
  78. @$uncovered = grep $_->area > (scale $flow_spacing)**2, @$uncovered;
  79. is scalar(@$uncovered), 0, 'solid surface is fully filled';
  80. if (0 && @$uncovered) {
  81. require "Slic3r/SVG.pm";
  82. Slic3r::SVG::output(
  83. "uncovered.svg",
  84. expolygons => [$expolygon],
  85. red_expolygons => $uncovered,
  86. );
  87. exit;
  88. }
  89. };
  90. my $expolygon = Slic3r::ExPolygon->new([
  91. [6883102, 9598327.01296997],
  92. [6883102, 20327272.01297],
  93. [3116896, 20327272.01297],
  94. [3116896, 9598327.01296997],
  95. ]);
  96. $test->($expolygon, 0.55);
  97. for (1..20) {
  98. $expolygon->scale(1.05);
  99. $test->($expolygon, 0.55);
  100. }
  101. $expolygon = Slic3r::ExPolygon->new(
  102. [[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]]
  103. );
  104. $test->($expolygon, 0.524341649025257);
  105. $expolygon = Slic3r::ExPolygon->new([ scale_points [0,0], [98,0], [98,10], [0,10] ]);
  106. $test->($expolygon, 0.5, 45, 0.99); # non-solid infill
  107. }
  108. {
  109. my $collection = Slic3r::Polyline::Collection->new(
  110. Slic3r::Polyline->new([0,15], [0,18], [0,20]),
  111. Slic3r::Polyline->new([0,10], [0,8], [0,5]),
  112. );
  113. is_deeply
  114. [ map $_->[Y], map @$_, @{$collection->chained_path_from(Slic3r::Point->new(0,30), 0)} ],
  115. [20, 18, 15, 10, 8, 5],
  116. 'chained path';
  117. }
  118. {
  119. my $collection = Slic3r::Polyline::Collection->new(
  120. Slic3r::Polyline->new([4,0], [10,0], [15,0]),
  121. Slic3r::Polyline->new([10,5], [15,5], [20,5]),
  122. );
  123. is_deeply
  124. [ map $_->[X], map @$_, @{$collection->chained_path_from(Slic3r::Point->new(30,0), 0)} ],
  125. [reverse 4, 10, 15, 10, 15, 20],
  126. 'chained path';
  127. }
  128. {
  129. my $collection = Slic3r::ExtrusionPath::Collection->new(
  130. map Slic3r::ExtrusionPath->new(polyline => $_, role => 0, mm3_per_mm => 1),
  131. Slic3r::Polyline->new([0,15], [0,18], [0,20]),
  132. Slic3r::Polyline->new([0,10], [0,8], [0,5]),
  133. );
  134. is_deeply
  135. [ map $_->[Y], map @{$_->polyline}, @{$collection->chained_path_from(Slic3r::Point->new(0,30), 0)} ],
  136. [20, 18, 15, 10, 8, 5],
  137. 'chained path';
  138. }
  139. {
  140. my $collection = Slic3r::ExtrusionPath::Collection->new(
  141. map Slic3r::ExtrusionPath->new(polyline => $_, role => 0, mm3_per_mm => 1),
  142. Slic3r::Polyline->new([15,0], [10,0], [4,0]),
  143. Slic3r::Polyline->new([10,5], [15,5], [20,5]),
  144. );
  145. is_deeply
  146. [ map $_->[X], map @{$_->polyline}, @{$collection->chained_path_from(Slic3r::Point->new(30,0), 0)} ],
  147. [reverse 4, 10, 15, 10, 15, 20],
  148. 'chained path';
  149. }
  150. for my $pattern (qw(rectilinear honeycomb hilbertcurve concentric)) {
  151. my $config = Slic3r::Config->new_from_defaults;
  152. $config->set('fill_pattern', $pattern);
  153. $config->set('solid_fill_pattern', $pattern);
  154. $config->set('perimeters', 1);
  155. $config->set('skirts', 0);
  156. $config->set('fill_density', 20);
  157. $config->set('layer_height', 0.05);
  158. $config->set('perimeter_extruder', 1);
  159. $config->set('infill_extruder', 2);
  160. my $print = Slic3r::Test::init_print('20mm_cube', config => $config, scale => 2);
  161. ok my $gcode = Slic3r::Test::gcode($print), "successful $pattern infill generation";
  162. my $tool = undef;
  163. my @perimeter_points = my @infill_points = ();
  164. Slic3r::GCode::Reader->new->parse($gcode, sub {
  165. my ($self, $cmd, $args, $info) = @_;
  166. if ($cmd =~ /^T(\d+)/) {
  167. $tool = $1;
  168. } elsif ($cmd eq 'G1' && $info->{extruding} && $info->{dist_XY} > 0) {
  169. if ($tool == $config->perimeter_extruder-1) {
  170. push @perimeter_points, Slic3r::Point->new_scale($args->{X}, $args->{Y});
  171. } elsif ($tool == $config->infill_extruder-1) {
  172. push @infill_points, Slic3r::Point->new_scale($args->{X}, $args->{Y});
  173. }
  174. }
  175. });
  176. my $convex_hull = convex_hull(\@perimeter_points);
  177. ok !(defined first { !$convex_hull->contains_point($_) } @infill_points), "infill does not exceed perimeters ($pattern)";
  178. }
  179. {
  180. my $config = Slic3r::Config->new_from_defaults;
  181. $config->set('infill_only_where_needed', 1);
  182. $config->set('bottom_solid_layers', 0);
  183. $config->set('infill_extruder', 2);
  184. $config->set('infill_extrusion_width', 0.5);
  185. $config->set('cooling', 0); # for preventing speeds from being altered
  186. $config->set('first_layer_speed', '100%'); # for preventing speeds from being altered
  187. my $test = sub {
  188. my $print = Slic3r::Test::init_print('pyramid', config => $config);
  189. my $tool = undef;
  190. my @infill_extrusions = (); # array of polylines
  191. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  192. my ($self, $cmd, $args, $info) = @_;
  193. if ($cmd =~ /^T(\d+)/) {
  194. $tool = $1;
  195. } elsif ($cmd eq 'G1' && $info->{extruding} && $info->{dist_XY} > 0) {
  196. if ($tool == $config->infill_extruder-1) {
  197. push @infill_extrusions, Slic3r::Line->new_scale(
  198. [ $self->X, $self->Y ],
  199. [ $info->{new_X}, $info->{new_Y} ],
  200. );
  201. }
  202. }
  203. });
  204. return 0 if !@infill_extrusions; # prevent calling convex_hull() with no points
  205. my $convex_hull = convex_hull([ map $_->pp, map @$_, @infill_extrusions ]);
  206. return unscale unscale sum(map $_->area, @{offset([$convex_hull], scale(+$config->infill_extrusion_width/2))});
  207. };
  208. my $tolerance = 5; # mm^2
  209. $config->set('solid_infill_below_area', 0);
  210. ok $test->() < $tolerance,
  211. 'no infill is generated when using infill_only_where_needed on a pyramid';
  212. $config->set('solid_infill_below_area', 70);
  213. ok abs($test->() - $config->solid_infill_below_area) < $tolerance,
  214. 'infill is only generated under the forced solid shells';
  215. }
  216. {
  217. my $config = Slic3r::Config->new_from_defaults;
  218. $config->set('skirts', 0);
  219. $config->set('perimeters', 0);
  220. $config->set('fill_density', 0);
  221. $config->set('top_solid_layers', 0);
  222. $config->set('bottom_solid_layers', 0);
  223. $config->set('solid_infill_below_area', 20000000);
  224. $config->set('solid_infill_every_layers', 2);
  225. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  226. my %layers_with_extrusion = ();
  227. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  228. my ($self, $cmd, $args, $info) = @_;
  229. $layers_with_extrusion{$self->Z} = 1 if $info->{extruding};
  230. });
  231. ok !%layers_with_extrusion,
  232. "solid_infill_below_area and solid_infill_every_layers are ignored when fill_density is 0";
  233. }
  234. {
  235. my $config = Slic3r::Config->new_from_defaults;
  236. $config->set('skirts', 0);
  237. $config->set('perimeters', 3);
  238. $config->set('fill_density', 0);
  239. $config->set('layer_height', 0.2);
  240. $config->set('first_layer_height', 0.2);
  241. $config->set('nozzle_diameter', [0.35]);
  242. $config->set('infill_extruder', 2);
  243. $config->set('infill_extrusion_width', 0.52);
  244. $config->set('first_layer_extrusion_width', 0);
  245. my $print = Slic3r::Test::init_print('A', config => $config);
  246. my %infill = (); # Z => [ Line, Line ... ]
  247. my $tool = undef;
  248. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  249. my ($self, $cmd, $args, $info) = @_;
  250. if ($cmd =~ /^T(\d+)/) {
  251. $tool = $1;
  252. } elsif ($cmd eq 'G1' && $info->{extruding} && $info->{dist_XY} > 0) {
  253. if ($tool == $config->infill_extruder-1) {
  254. my $z = 1 * $self->Z;
  255. $infill{$z} ||= [];
  256. push @{$infill{$z}}, Slic3r::Line->new_scale(
  257. [ $self->X, $self->Y ],
  258. [ $info->{new_X}, $info->{new_Y} ],
  259. );
  260. }
  261. }
  262. });
  263. my $grow_d = scale($config->infill_extrusion_width)/2;
  264. my $layer0_infill = union([ map @{$_->grow($grow_d)}, @{ $infill{0.2} } ]);
  265. my $layer1_infill = union([ map @{$_->grow($grow_d)}, @{ $infill{0.4} } ]);
  266. my $diff = [ grep $_->area >= 4*($grow_d**2), @{diff_ex($layer0_infill, $layer1_infill)} ];
  267. is scalar(@$diff), 0, 'no missing parts in solid shell when fill_density is 0';
  268. }
  269. __END__