1 #include <catch2/catch.hpp>
2
3 #include <numeric>
4 #include <sstream>
5
6 #include "libslic3r/ClipperUtils.hpp"
7 #include "libslic3r/Fill/Fill.hpp"
8 #include "libslic3r/Flow.hpp"
9 #include "libslic3r/Geometry.hpp"
10 #include "libslic3r/Print.hpp"
11 #include "libslic3r/SVG.hpp"
12 #include "libslic3r/libslic3r.h"
13
14 #include "test_data.hpp"
15
16 using namespace Slic3r;
17
18 bool test_if_solid_surface_filled(const ExPolygon& expolygon, double flow_spacing, double angle = 0, double density = 1.0);
19
20 #if 0
21 TEST_CASE("Fill: adjusted solid distance") {
22 int surface_width = 250;
23 int distance = Slic3r::Flow::solid_spacing(surface_width, 47);
24 REQUIRE(distance == Approx(50));
25 REQUIRE(surface_width % distance == 0);
26 }
27 #endif
28
29 TEST_CASE("Fill: Pattern Path Length", "[Fill]") {
30 std::unique_ptr<Slic3r::Fill> filler(Slic3r::Fill::new_from_type("rectilinear"));
31 filler->angle = float(-(PI)/2.0);
32 FillParams fill_params;
33 filler->spacing = 5;
34 fill_params.dont_adjust = true;
35 //fill_params.endpoints_overlap = false;
36 fill_params.density = float(filler->spacing / 50.0);
37
__anona10724e50102(const ExPolygon& poly) 38 auto test = [&filler, &fill_params] (const ExPolygon& poly) -> Slic3r::Polylines {
39 Slic3r::Surface surface(stTop, poly);
40 return filler->fill_surface(&surface, fill_params);
41 };
42
43 SECTION("Square") {
44 Slic3r::Points test_set;
45 test_set.reserve(4);
46 std::vector<Vec2d> points {Vec2d(0,0), Vec2d(100,0), Vec2d(100,100), Vec2d(0,100)};
47 for (size_t i = 0; i < 4; ++i) {
__anona10724e50202(const Vec2d& a) 48 std::transform(points.cbegin()+i, points.cend(), std::back_inserter(test_set), [] (const Vec2d& a) -> Point { return Point::new_scale(a.x(), a.y()); } );
__anona10724e50302(const Vec2d& a) 49 std::transform(points.cbegin(), points.cbegin()+i, std::back_inserter(test_set), [] (const Vec2d& a) -> Point { return Point::new_scale(a.x(), a.y()); } );
50 Slic3r::Polylines paths = test(Slic3r::ExPolygon(test_set));
51 REQUIRE(paths.size() == 1); // one continuous path
52
53 // TODO: determine what the "Expected length" should be for rectilinear fill of a 100x100 polygon.
54 // This check only checks that it's above scale(3*100 + 2*50) + scaled_epsilon.
55 // ok abs($paths->[0]->length - scale(3*100 + 2*50)) - scaled_epsilon, 'path has expected length';
56 REQUIRE(std::abs(paths[0].length() - static_cast<double>(scale_(3*100 + 2*50))) - SCALED_EPSILON > 0); // path has expected length
57
58 test_set.clear();
59 }
60 }
61 SECTION("Diamond with endpoints on grid") {
62 std::vector<Vec2d> points {Vec2d(0,0), Vec2d(100,0), Vec2d(150,50), Vec2d(100,100), Vec2d(0,100), Vec2d(-50,50)};
63 Slic3r::Points test_set;
64 test_set.reserve(6);
__anona10724e50402(const Vec2d& a) 65 std::transform(points.cbegin(), points.cend(), std::back_inserter(test_set), [] (const Vec2d& a) -> Point { return Point::new_scale(a.x(), a.y()); } );
66 Slic3r::Polylines paths = test(Slic3r::ExPolygon(test_set));
67 REQUIRE(paths.size() == 1); // one continuous path
68 }
69
70 SECTION("Square with hole") {
71 std::vector<Vec2d> square {Vec2d(0,0), Vec2d(100,0), Vec2d(100,100), Vec2d(0,100)};
72 std::vector<Vec2d> hole {Vec2d(25,25), Vec2d(75,25), Vec2d(75,75), Vec2d(25,75) };
73 std::reverse(hole.begin(), hole.end());
74
75 Slic3r::Points test_hole;
76 Slic3r::Points test_square;
77
__anona10724e50502(const Vec2d& a) 78 std::transform(square.cbegin(), square.cend(), std::back_inserter(test_square), [] (const Vec2d& a) -> Point { return Point::new_scale(a.x(), a.y()); } );
__anona10724e50602(const Vec2d& a) 79 std::transform(hole.cbegin(), hole.cend(), std::back_inserter(test_hole), [] (const Vec2d& a) -> Point { return Point::new_scale(a.x(), a.y()); } );
80
81 for (double angle : {-(PI/2.0), -(PI/4.0), -(PI), PI/2.0, PI}) {
82 for (double spacing : {25.0, 5.0, 7.5, 8.5}) {
83 fill_params.density = float(filler->spacing / spacing);
84 filler->angle = float(angle);
85 ExPolygon e(test_square, test_hole);
86 Slic3r::Polylines paths = test(e);
87 #if 0
88 {
89 BoundingBox bbox = get_extents(e);
90 SVG svg("c:\\data\\temp\\square_with_holes.svg", bbox);
91 svg.draw(e);
92 svg.draw(paths);
93 svg.Close();
94 }
95 #endif
96 REQUIRE((paths.size() >= 1 && paths.size() <= 3));
97 // paths don't cross hole
98 REQUIRE(diff_pl(paths, offset(e, float(SCALED_EPSILON*10))).size() == 0);
99 }
100 }
101 }
102 SECTION("Regression: Missing infill segments in some rare circumstances") {
103 filler->angle = float(PI/4.0);
104 fill_params.dont_adjust = false;
105 filler->spacing = 0.654498;
106 //filler->endpoints_overlap = unscale(359974);
107 fill_params.density = 1;
108 filler->layer_id = 66;
109 filler->z = 20.15;
110
111 Slic3r::Points points {Point(25771516,14142125),Point(14142138,25771515),Point(2512749,14142131),Point(14142125,2512749)};
112 Slic3r::Polylines paths = test(Slic3r::ExPolygon(points));
113 REQUIRE(paths.size() == 1); // one continuous path
114
115 // TODO: determine what the "Expected length" should be for rectilinear fill of a 100x100 polygon.
116 // This check only checks that it's above scale(3*100 + 2*50) + scaled_epsilon.
117 // ok abs($paths->[0]->length - scale(3*100 + 2*50)) - scaled_epsilon, 'path has expected length';
118 REQUIRE(std::abs(paths[0].length() - static_cast<double>(scale_(3*100 + 2*50))) - SCALED_EPSILON > 0); // path has expected length
119 }
120
121 SECTION("Rotated Square") {
122 Slic3r::Points square { Point::new_scale(0,0), Point::new_scale(50,0), Point::new_scale(50,50), Point::new_scale(0,50)};
123 Slic3r::ExPolygon expolygon(square);
124 std::unique_ptr<Slic3r::Fill> filler(Slic3r::Fill::new_from_type("rectilinear"));
125 filler->bounding_box = get_extents(expolygon.contour);
126 filler->angle = 0;
127
128 Surface surface(stTop, expolygon);
129 auto flow = Slic3r::Flow(0.69, 0.4, 0.50);
130
131 FillParams fill_params;
132 fill_params.density = 1.0;
133 filler->spacing = flow.spacing();
134
135 for (auto angle : { 0.0, 45.0}) {
136 surface.expolygon.rotate(angle, Point(0,0));
137 Polylines paths = filler->fill_surface(&surface, fill_params);
138 REQUIRE(paths.size() == 1);
139 }
140 }
141
142 #if 0 // Disabled temporarily due to precission issues on the Mac VM
143 SECTION("Solid surface fill") {
144 Slic3r::Points points {
145 Point::new_scale(6883102, 9598327.01296997),
146 Point::new_scale(6883102, 20327272.01297),
147 Point::new_scale(3116896, 20327272.01297),
148 Point::new_scale(3116896, 9598327.01296997)
149 };
150 Slic3r::ExPolygon expolygon(points);
151
152 REQUIRE(test_if_solid_surface_filled(expolygon, 0.55) == true);
153 for (size_t i = 0; i <= 20; ++i)
154 {
155 expolygon.scale(1.05);
156 REQUIRE(test_if_solid_surface_filled(expolygon, 0.55) == true);
157 }
158 }
159 #endif
160
161 SECTION("Solid surface fill") {
162 Slic3r::Points points {
163 Slic3r::Point(59515297,5422499),Slic3r::Point(59531249,5578697),Slic3r::Point(59695801,6123186),
164 Slic3r::Point(59965713,6630228),Slic3r::Point(60328214,7070685),Slic3r::Point(60773285,7434379),
165 Slic3r::Point(61274561,7702115),Slic3r::Point(61819378,7866770),Slic3r::Point(62390306,7924789),
166 Slic3r::Point(62958700,7866744),Slic3r::Point(63503012,7702244),Slic3r::Point(64007365,7434357),
167 Slic3r::Point(64449960,7070398),Slic3r::Point(64809327,6634999),Slic3r::Point(65082143,6123325),
168 Slic3r::Point(65245005,5584454),Slic3r::Point(65266967,5422499),Slic3r::Point(66267307,5422499),
169 Slic3r::Point(66269190,8310081),Slic3r::Point(66275379,17810072),Slic3r::Point(66277259,20697500),
170 Slic3r::Point(65267237,20697500),Slic3r::Point(65245004,20533538),Slic3r::Point(65082082,19994444),
171 Slic3r::Point(64811462,19488579),Slic3r::Point(64450624,19048208),Slic3r::Point(64012101,18686514),
172 Slic3r::Point(63503122,18415781),Slic3r::Point(62959151,18251378),Slic3r::Point(62453416,18198442),
173 Slic3r::Point(62390147,18197355),Slic3r::Point(62200087,18200576),Slic3r::Point(61813519,18252990),
174 Slic3r::Point(61274433,18415918),Slic3r::Point(60768598,18686517),Slic3r::Point(60327567,19047892),
175 Slic3r::Point(59963609,19493297),Slic3r::Point(59695865,19994587),Slic3r::Point(59531222,20539379),
176 Slic3r::Point(59515153,20697500),Slic3r::Point(58502480,20697500),Slic3r::Point(58502480,5422499)
177 };
178 Slic3r::ExPolygon expolygon(points);
179
180 REQUIRE(test_if_solid_surface_filled(expolygon, 0.55) == true);
181 REQUIRE(test_if_solid_surface_filled(expolygon, 0.55, PI/2.0) == true);
182 }
183 SECTION("Solid surface fill") {
184 Slic3r::Points points {
185 Point::new_scale(0,0),Point::new_scale(98,0),Point::new_scale(98,10), Point::new_scale(0,10)
186 };
187 Slic3r::ExPolygon expolygon(points);
188
189 REQUIRE(test_if_solid_surface_filled(expolygon, 0.5, 45.0, 0.99) == true);
190 }
191 }
192
193 /*
194 {
195 my $collection = Slic3r::Polyline::Collection->new(
196 Slic3r::Polyline->new([0,15], [0,18], [0,20]),
197 Slic3r::Polyline->new([0,10], [0,8], [0,5]),
198 );
199 is_deeply
200 [ map $_->[Y], map @$_, @{$collection->chained_path_from(Slic3r::Point->new(0,30), 0)} ],
201 [20, 18, 15, 10, 8, 5],
202 'chained path';
203 }
204
205 {
206 my $collection = Slic3r::Polyline::Collection->new(
207 Slic3r::Polyline->new([4,0], [10,0], [15,0]),
208 Slic3r::Polyline->new([10,5], [15,5], [20,5]),
209 );
210 is_deeply
211 [ map $_->[X], map @$_, @{$collection->chained_path_from(Slic3r::Point->new(30,0), 0)} ],
212 [reverse 4, 10, 15, 10, 15, 20],
213 'chained path';
214 }
215
216 {
217 my $collection = Slic3r::ExtrusionPath::Collection->new(
218 map Slic3r::ExtrusionPath->new(polyline => $_, role => 0, mm3_per_mm => 1),
219 Slic3r::Polyline->new([0,15], [0,18], [0,20]),
220 Slic3r::Polyline->new([0,10], [0,8], [0,5]),
221 );
222 is_deeply
223 [ map $_->[Y], map @{$_->polyline}, @{$collection->chained_path_from(Slic3r::Point->new(0,30), 0)} ],
224 [20, 18, 15, 10, 8, 5],
225 'chained path';
226 }
227
228 {
229 my $collection = Slic3r::ExtrusionPath::Collection->new(
230 map Slic3r::ExtrusionPath->new(polyline => $_, role => 0, mm3_per_mm => 1),
231 Slic3r::Polyline->new([15,0], [10,0], [4,0]),
232 Slic3r::Polyline->new([10,5], [15,5], [20,5]),
233 );
234 is_deeply
235 [ map $_->[X], map @{$_->polyline}, @{$collection->chained_path_from(Slic3r::Point->new(30,0), 0)} ],
236 [reverse 4, 10, 15, 10, 15, 20],
237 'chained path';
238 }
239
240 for my $pattern (qw(rectilinear honeycomb hilbertcurve concentric)) {
241 my $config = Slic3r::Config->new_from_defaults;
242 $config->set('fill_pattern', $pattern);
243 $config->set('external_fill_pattern', $pattern);
244 $config->set('perimeters', 1);
245 $config->set('skirts', 0);
246 $config->set('fill_density', 20);
247 $config->set('layer_height', 0.05);
248 $config->set('perimeter_extruder', 1);
249 $config->set('infill_extruder', 2);
250 my $print = Slic3r::Test::init_print('20mm_cube', config => $config, scale => 2);
251 ok my $gcode = Slic3r::Test::gcode($print), "successful $pattern infill generation";
252 my $tool = undef;
253 my @perimeter_points = my @infill_points = ();
254 Slic3r::GCode::Reader->new->parse($gcode, sub {
255 my ($self, $cmd, $args, $info) = @_;
256
257 if ($cmd =~ /^T(\d+)/) {
258 $tool = $1;
259 } elsif ($cmd eq 'G1' && $info->{extruding} && $info->{dist_XY} > 0) {
260 if ($tool == $config->perimeter_extruder-1) {
261 push @perimeter_points, Slic3r::Point->new_scale($args->{X}, $args->{Y});
262 } elsif ($tool == $config->infill_extruder-1) {
263 push @infill_points, Slic3r::Point->new_scale($args->{X}, $args->{Y});
264 }
265 }
266 });
267 my $convex_hull = convex_hull(\@perimeter_points);
268 ok !(defined first { !$convex_hull->contains_point($_) } @infill_points), "infill does not exceed perimeters ($pattern)";
269 }
270
271 {
272 my $config = Slic3r::Config->new_from_defaults;
273 $config->set('infill_only_where_needed', 1);
274 $config->set('bottom_solid_layers', 0);
275 $config->set('infill_extruder', 2);
276 $config->set('infill_extrusion_width', 0.5);
277 $config->set('fill_density', 40);
278 $config->set('cooling', 0); # for preventing speeds from being altered
279 $config->set('first_layer_speed', '100%'); # for preventing speeds from being altered
280
281 my $test = sub {
282 my $print = Slic3r::Test::init_print('pyramid', config => $config);
283
284 my $tool = undef;
285 my @infill_extrusions = (); # array of polylines
286 Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
287 my ($self, $cmd, $args, $info) = @_;
288
289 if ($cmd =~ /^T(\d+)/) {
290 $tool = $1;
291 } elsif ($cmd eq 'G1' && $info->{extruding} && $info->{dist_XY} > 0) {
292 if ($tool == $config->infill_extruder-1) {
293 push @infill_extrusions, Slic3r::Line->new_scale(
294 [ $self->X, $self->Y ],
295 [ $info->{new_X}, $info->{new_Y} ],
296 );
297 }
298 }
299 });
300 return 0 if !@infill_extrusions; # prevent calling convex_hull() with no points
301
302 my $convex_hull = convex_hull([ map $_->pp, map @$_, @infill_extrusions ]);
303 return unscale unscale sum(map $_->area, @{offset([$convex_hull], scale(+$config->infill_extrusion_width/2))});
304 };
305
306 my $tolerance = 5; # mm^2
307
308 $config->set('solid_infill_below_area', 0);
309 ok $test->() < $tolerance,
310 'no infill is generated when using infill_only_where_needed on a pyramid';
311
312 $config->set('solid_infill_below_area', 70);
313 ok abs($test->() - $config->solid_infill_below_area) < $tolerance,
314 'infill is only generated under the forced solid shells';
315 }
316
317 {
318 my $config = Slic3r::Config->new_from_defaults;
319 $config->set('skirts', 0);
320 $config->set('perimeters', 1);
321 $config->set('fill_density', 0);
322 $config->set('top_solid_layers', 0);
323 $config->set('bottom_solid_layers', 0);
324 $config->set('solid_infill_below_area', 20000000);
325 $config->set('solid_infill_every_layers', 2);
326 $config->set('perimeter_speed', 99);
327 $config->set('external_perimeter_speed', 99);
328 $config->set('cooling', 0);
329 $config->set('first_layer_speed', '100%');
330
331 my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
332 my %layers_with_extrusion = ();
333 Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
334 my ($self, $cmd, $args, $info) = @_;
335
336 if ($cmd eq 'G1' && $info->{dist_XY} > 0 && $info->{extruding}) {
337 if (($args->{F} // $self->F) != $config->perimeter_speed*60) {
338 $layers_with_extrusion{$self->Z} = ($args->{F} // $self->F);
339 }
340 }
341 });
342
343 ok !%layers_with_extrusion,
344 "solid_infill_below_area and solid_infill_every_layers are ignored when fill_density is 0";
345 }
346
347 {
348 my $config = Slic3r::Config->new_from_defaults;
349 $config->set('skirts', 0);
350 $config->set('perimeters', 3);
351 $config->set('fill_density', 0);
352 $config->set('layer_height', 0.2);
353 $config->set('first_layer_height', 0.2);
354 $config->set('nozzle_diameter', [0.35]);
355 $config->set('infill_extruder', 2);
356 $config->set('solid_infill_extruder', 2);
357 $config->set('infill_extrusion_width', 0.52);
358 $config->set('solid_infill_extrusion_width', 0.52);
359 $config->set('first_layer_extrusion_width', 0);
360
361 my $print = Slic3r::Test::init_print('A', config => $config);
362 my %infill = (); # Z => [ Line, Line ... ]
363 my $tool = undef;
364 Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
365 my ($self, $cmd, $args, $info) = @_;
366
367 if ($cmd =~ /^T(\d+)/) {
368 $tool = $1;
369 } elsif ($cmd eq 'G1' && $info->{extruding} && $info->{dist_XY} > 0) {
370 if ($tool == $config->infill_extruder-1) {
371 my $z = 1 * $self->Z;
372 $infill{$z} ||= [];
373 push @{$infill{$z}}, Slic3r::Line->new_scale(
374 [ $self->X, $self->Y ],
375 [ $info->{new_X}, $info->{new_Y} ],
376 );
377 }
378 }
379 });
380 my $grow_d = scale($config->infill_extrusion_width)/2;
381 my $layer0_infill = union([ map @{$_->grow($grow_d)}, @{ $infill{0.2} } ]);
382 my $layer1_infill = union([ map @{$_->grow($grow_d)}, @{ $infill{0.4} } ]);
383 my $diff = diff($layer0_infill, $layer1_infill);
384 $diff = offset2_ex($diff, -$grow_d, +$grow_d);
385 $diff = [ grep { $_->area > 2*(($grow_d*2)**2) } @$diff ];
386 is scalar(@$diff), 0, 'no missing parts in solid shell when fill_density is 0';
387 }
388
389 {
390 # GH: #2697
391 my $config = Slic3r::Config->new_from_defaults;
392 $config->set('perimeter_extrusion_width', 0.72);
393 $config->set('top_infill_extrusion_width', 0.1);
394 $config->set('infill_extruder', 2); # in order to distinguish infill
395 $config->set('solid_infill_extruder', 2); # in order to distinguish infill
396
397 my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
398 my %infill = (); # Z => [ Line, Line ... ]
399 my %other = (); # Z => [ Line, Line ... ]
400 my $tool = undef;
401 Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
402 my ($self, $cmd, $args, $info) = @_;
403
404 if ($cmd =~ /^T(\d+)/) {
405 $tool = $1;
406 } elsif ($cmd eq 'G1' && $info->{extruding} && $info->{dist_XY} > 0) {
407 my $z = 1 * $self->Z;
408 my $line = Slic3r::Line->new_scale(
409 [ $self->X, $self->Y ],
410 [ $info->{new_X}, $info->{new_Y} ],
411 );
412 if ($tool == $config->infill_extruder-1) {
413 $infill{$z} //= [];
414 push @{$infill{$z}}, $line;
415 } else {
416 $other{$z} //= [];
417 push @{$other{$z}}, $line;
418 }
419 }
420 });
421 my $top_z = max(keys %infill);
422 my $top_infill_grow_d = scale($config->top_infill_extrusion_width)/2;
423 my $top_infill = union([ map @{$_->grow($top_infill_grow_d)}, @{ $infill{$top_z} } ]);
424 my $perimeters_grow_d = scale($config->perimeter_extrusion_width)/2;
425 my $perimeters = union([ map @{$_->grow($perimeters_grow_d)}, @{ $other{$top_z} } ]);
426 my $covered = union_ex([ @$top_infill, @$perimeters ]);
427 my @holes = map @{$_->holes}, @$covered;
428 ok sum(map unscale unscale $_->area*-1, @holes) < 1, 'no gaps between top solid infill and perimeters';
429 }
430 */
431
test_if_solid_surface_filled(const ExPolygon & expolygon,double flow_spacing,double angle,double density)432 bool test_if_solid_surface_filled(const ExPolygon& expolygon, double flow_spacing, double angle, double density)
433 {
434 std::unique_ptr<Slic3r::Fill> filler(Slic3r::Fill::new_from_type("rectilinear"));
435 filler->bounding_box = get_extents(expolygon.contour);
436 filler->angle = float(angle);
437
438 Flow flow(flow_spacing, 0.4, flow_spacing);
439 filler->spacing = flow.spacing();
440
441 FillParams fill_params;
442 fill_params.density = float(density);
443 fill_params.dont_adjust = false;
444
445 Surface surface(stBottom, expolygon);
446 Slic3r::Polylines paths = filler->fill_surface(&surface, fill_params);
447
448 // check whether any part was left uncovered
449 Polygons grown_paths;
450 grown_paths.reserve(paths.size());
451
452 // figure out what is actually going on here re: data types
453 float line_offset = float(scale_(filler->spacing / 2.0 + EPSILON));
454 std::for_each(paths.begin(), paths.end(), [line_offset, &grown_paths] (const Slic3r::Polyline& p) {
455 polygons_append(grown_paths, offset(p, line_offset));
456 });
457
458 // Shrink the initial expolygon a bit, this simulates the infill / perimeter overlap that we usually apply.
459 ExPolygons uncovered = diff_ex(offset(expolygon, - float(0.2 * scale_(flow_spacing))), grown_paths, true);
460
461 // ignore very small dots
462 const double scaled_flow_spacing = std::pow(scale_(flow_spacing), 2);
463 uncovered.erase(std::remove_if(uncovered.begin(), uncovered.end(), [scaled_flow_spacing](const ExPolygon& poly) { return poly.area() < scaled_flow_spacing; }), uncovered.end());
464
465 #if 0
466 if (! uncovered.empty()) {
467 BoundingBox bbox = get_extents(expolygon.contour);
468 bbox.merge(get_extents(uncovered));
469 bbox.merge(get_extents(grown_paths));
470 SVG svg("c:\\data\\temp\\test_if_solid_surface_filled.svg", bbox);
471 svg.draw(expolygon);
472 svg.draw(uncovered, "red");
473 svg.Close();
474 }
475 #endif
476
477 return uncovered.empty(); // solid surface is fully filled
478 }
479