test_fill.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. #include <catch2/catch.hpp>
  2. #include <numeric>
  3. #include <sstream>
  4. #include "libslic3r/libslic3r.h"
  5. #include "libslic3r/ClipperUtils.hpp"
  6. #include "libslic3r/Fill/Fill.hpp"
  7. #include "libslic3r/Flow.hpp"
  8. #include "libslic3r/Layer.hpp"
  9. #include "libslic3r/Geometry.hpp"
  10. #include "libslic3r/Geometry/ConvexHull.hpp"
  11. #include "libslic3r/Point.hpp"
  12. #include "libslic3r/Print.hpp"
  13. #include "libslic3r/SVG.hpp"
  14. #include "test_data.hpp"
  15. using namespace Slic3r;
  16. using namespace std::literals;
  17. bool test_if_solid_surface_filled(const ExPolygon& expolygon, double flow_spacing, double angle = 0, double density = 1.0);
  18. #if 0
  19. TEST_CASE("Fill: adjusted solid distance") {
  20. int surface_width = 250;
  21. int distance = Slic3r::Flow::solid_spacing(surface_width, 47);
  22. REQUIRE(distance == Approx(50));
  23. REQUIRE(surface_width % distance == 0);
  24. }
  25. #endif
  26. TEST_CASE("Fill: Pattern Path Length", "[Fill]") {
  27. std::unique_ptr<Slic3r::Fill> filler(Slic3r::Fill::new_from_type("rectilinear"));
  28. filler->angle = float(-(PI)/2.0);
  29. FillParams fill_params;
  30. filler->spacing = 5;
  31. fill_params.dont_adjust = true;
  32. //fill_params.endpoints_overlap = false;
  33. fill_params.density = float(filler->spacing / 50.0);
  34. auto test = [&filler, &fill_params] (const ExPolygon& poly) -> Slic3r::Polylines {
  35. Slic3r::Surface surface(stTop, poly);
  36. return filler->fill_surface(&surface, fill_params);
  37. };
  38. SECTION("Square") {
  39. Slic3r::Points test_set;
  40. test_set.reserve(4);
  41. std::vector<Vec2d> points { {0,0}, {100,0}, {100,100}, {0,100} };
  42. for (size_t i = 0; i < 4; ++i) {
  43. std::transform(points.cbegin()+i, points.cend(), std::back_inserter(test_set), [] (const Vec2d& a) -> Point { return Point::new_scale(a.x(), a.y()); } );
  44. std::transform(points.cbegin(), points.cbegin()+i, std::back_inserter(test_set), [] (const Vec2d& a) -> Point { return Point::new_scale(a.x(), a.y()); } );
  45. Slic3r::Polylines paths = test(Slic3r::ExPolygon(test_set));
  46. REQUIRE(paths.size() == 1); // one continuous path
  47. // TODO: determine what the "Expected length" should be for rectilinear fill of a 100x100 polygon.
  48. // This check only checks that it's above scale(3*100 + 2*50) + scaled_epsilon.
  49. // ok abs($paths->[0]->length - scale(3*100 + 2*50)) - scaled_epsilon, 'path has expected length';
  50. REQUIRE(std::abs(paths[0].length() - static_cast<double>(scale_(3*100 + 2*50))) - SCALED_EPSILON > 0); // path has expected length
  51. test_set.clear();
  52. }
  53. }
  54. SECTION("Diamond with endpoints on grid") {
  55. std::vector<Vec2d> points {Vec2d(0,0), Vec2d(100,0), Vec2d(150,50), Vec2d(100,100), Vec2d(0,100), Vec2d(-50,50)};
  56. Slic3r::Points test_set;
  57. test_set.reserve(6);
  58. std::transform(points.cbegin(), points.cend(), std::back_inserter(test_set), [] (const Vec2d& a) -> Point { return Point::new_scale(a.x(), a.y()); } );
  59. Slic3r::Polylines paths = test(Slic3r::ExPolygon(test_set));
  60. REQUIRE(paths.size() == 1); // one continuous path
  61. }
  62. SECTION("Square with hole") {
  63. std::vector<Vec2d> square {Vec2d(0,0), Vec2d(100,0), Vec2d(100,100), Vec2d(0,100)};
  64. std::vector<Vec2d> hole {Vec2d(25,25), Vec2d(75,25), Vec2d(75,75), Vec2d(25,75) };
  65. std::reverse(hole.begin(), hole.end());
  66. Slic3r::Points test_hole;
  67. Slic3r::Points test_square;
  68. std::transform(square.cbegin(), square.cend(), std::back_inserter(test_square), [] (const Vec2d& a) -> Point { return Point::new_scale(a.x(), a.y()); } );
  69. std::transform(hole.cbegin(), hole.cend(), std::back_inserter(test_hole), [] (const Vec2d& a) -> Point { return Point::new_scale(a.x(), a.y()); } );
  70. for (double angle : {-(PI/2.0), -(PI/4.0), -(PI), PI/2.0, PI}) {
  71. for (double spacing : {25.0, 5.0, 7.5, 8.5}) {
  72. fill_params.density = float(filler->spacing / spacing);
  73. filler->angle = float(angle);
  74. ExPolygon e(test_square, test_hole);
  75. Slic3r::Polylines paths = test(e);
  76. #if 0
  77. {
  78. BoundingBox bbox = get_extents(e);
  79. SVG svg("c:\\data\\temp\\square_with_holes.svg", bbox);
  80. svg.draw(e);
  81. svg.draw(paths);
  82. svg.Close();
  83. }
  84. #endif
  85. REQUIRE((paths.size() >= 1 && paths.size() <= 3));
  86. // paths don't cross hole
  87. REQUIRE(diff_pl(paths, offset(e, float(SCALED_EPSILON*10))).size() == 0);
  88. }
  89. }
  90. }
  91. SECTION("Regression: Missing infill segments in some rare circumstances") {
  92. filler->angle = float(PI/4.0);
  93. fill_params.dont_adjust = false;
  94. filler->spacing = 0.654498;
  95. //filler->endpoints_overlap = unscale(359974);
  96. fill_params.density = 1;
  97. filler->layer_id = 66;
  98. filler->z = 20.15;
  99. Slic3r::Points points {Point(25771516,14142125),Point(14142138,25771515),Point(2512749,14142131),Point(14142125,2512749)};
  100. Slic3r::Polylines paths = test(Slic3r::ExPolygon(points));
  101. REQUIRE(paths.size() == 1); // one continuous path
  102. // TODO: determine what the "Expected length" should be for rectilinear fill of a 100x100 polygon.
  103. // This check only checks that it's above scale(3*100 + 2*50) + scaled_epsilon.
  104. // ok abs($paths->[0]->length - scale(3*100 + 2*50)) - scaled_epsilon, 'path has expected length';
  105. REQUIRE(std::abs(paths[0].length() - static_cast<double>(scale_(3*100 + 2*50))) - SCALED_EPSILON > 0); // path has expected length
  106. }
  107. SECTION("Rotated Square produces one continuous path") {
  108. Slic3r::ExPolygon expolygon(Polygon::new_scale({ {0, 0}, {50, 0}, {50, 50}, {0, 50} }));
  109. std::unique_ptr<Slic3r::Fill> filler(Slic3r::Fill::new_from_type("rectilinear"));
  110. filler->bounding_box = get_extents(expolygon);
  111. filler->angle = 0;
  112. Surface surface(stTop, expolygon);
  113. // width, height, nozzle_dmr
  114. auto flow = Slic3r::Flow(0.69f, 0.4f, 0.5f);
  115. FillParams fill_params;
  116. for (auto density : { 0.4, 1.0 }) {
  117. fill_params.density = density;
  118. filler->spacing = flow.spacing();
  119. REQUIRE(!fill_params.use_arachne); // Make this test fail when Arachne is used because this test is not ready for it.
  120. for (auto angle : { 0.0, 45.0}) {
  121. surface.expolygon.rotate(angle, Point(0,0));
  122. Polylines paths = filler->fill_surface(&surface, fill_params);
  123. // one continuous path
  124. REQUIRE(paths.size() == 1);
  125. }
  126. }
  127. }
  128. #if 0 // Disabled temporarily due to precission issues on the Mac VM
  129. SECTION("Solid surface fill") {
  130. Slic3r::Points points {
  131. Point::new_scale(6883102, 9598327.01296997),
  132. Point::new_scale(6883102, 20327272.01297),
  133. Point::new_scale(3116896, 20327272.01297),
  134. Point::new_scale(3116896, 9598327.01296997)
  135. };
  136. Slic3r::ExPolygon expolygon(points);
  137. REQUIRE(test_if_solid_surface_filled(expolygon, 0.55) == true);
  138. for (size_t i = 0; i <= 20; ++i)
  139. {
  140. expolygon.scale(1.05);
  141. REQUIRE(test_if_solid_surface_filled(expolygon, 0.55) == true);
  142. }
  143. }
  144. #endif
  145. SECTION("Solid surface fill") {
  146. Slic3r::Points points {
  147. Slic3r::Point(59515297,5422499),Slic3r::Point(59531249,5578697),Slic3r::Point(59695801,6123186),
  148. Slic3r::Point(59965713,6630228),Slic3r::Point(60328214,7070685),Slic3r::Point(60773285,7434379),
  149. Slic3r::Point(61274561,7702115),Slic3r::Point(61819378,7866770),Slic3r::Point(62390306,7924789),
  150. Slic3r::Point(62958700,7866744),Slic3r::Point(63503012,7702244),Slic3r::Point(64007365,7434357),
  151. Slic3r::Point(64449960,7070398),Slic3r::Point(64809327,6634999),Slic3r::Point(65082143,6123325),
  152. Slic3r::Point(65245005,5584454),Slic3r::Point(65266967,5422499),Slic3r::Point(66267307,5422499),
  153. Slic3r::Point(66269190,8310081),Slic3r::Point(66275379,17810072),Slic3r::Point(66277259,20697500),
  154. Slic3r::Point(65267237,20697500),Slic3r::Point(65245004,20533538),Slic3r::Point(65082082,19994444),
  155. Slic3r::Point(64811462,19488579),Slic3r::Point(64450624,19048208),Slic3r::Point(64012101,18686514),
  156. Slic3r::Point(63503122,18415781),Slic3r::Point(62959151,18251378),Slic3r::Point(62453416,18198442),
  157. Slic3r::Point(62390147,18197355),Slic3r::Point(62200087,18200576),Slic3r::Point(61813519,18252990),
  158. Slic3r::Point(61274433,18415918),Slic3r::Point(60768598,18686517),Slic3r::Point(60327567,19047892),
  159. Slic3r::Point(59963609,19493297),Slic3r::Point(59695865,19994587),Slic3r::Point(59531222,20539379),
  160. Slic3r::Point(59515153,20697500),Slic3r::Point(58502480,20697500),Slic3r::Point(58502480,5422499)
  161. };
  162. Slic3r::ExPolygon expolygon(points);
  163. REQUIRE(test_if_solid_surface_filled(expolygon, 0.55) == true);
  164. REQUIRE(test_if_solid_surface_filled(expolygon, 0.55, PI/2.0) == true);
  165. }
  166. SECTION("Solid surface fill") {
  167. Slic3r::Points points {
  168. Point::new_scale(0,0),Point::new_scale(98,0),Point::new_scale(98,10), Point::new_scale(0,10)
  169. };
  170. Slic3r::ExPolygon expolygon(points);
  171. REQUIRE(test_if_solid_surface_filled(expolygon, 0.5, 45.0, 0.99) == true);
  172. }
  173. }
  174. SCENARIO("Infill does not exceed perimeters", "[Fill]")
  175. {
  176. auto test = [](const std::string_view pattern) {
  177. auto config = Slic3r::DynamicPrintConfig::full_print_config_with({
  178. { "nozzle_diameter", "0.4, 0.4, 0.4, 0.4" },
  179. { "fill_pattern", pattern },
  180. { "top_fill_pattern", pattern },
  181. { "bottom_fill_pattern", pattern },
  182. { "perimeters", 1 },
  183. { "skirts", 0 },
  184. { "fill_density", 0.2 },
  185. { "layer_height", 0.05 },
  186. { "perimeter_extruder", 1 },
  187. { "infill_extruder", 2 }
  188. });
  189. WHEN("40mm cube sliced") {
  190. std::string gcode = Slic3r::Test::slice({ mesh(Slic3r::Test::TestMesh::cube_20x20x20, Vec3d::Zero(), 2.0) }, config);
  191. THEN("gcode not empty") {
  192. REQUIRE(! gcode.empty());
  193. }
  194. THEN("infill does not exceed perimeters") {
  195. GCodeReader parser;
  196. const int perimeter_extruder = config.opt_int("perimeter_extruder");
  197. const int infill_extruder = config.opt_int("infill_extruder");
  198. int tool = -1;
  199. Points perimeter_points;
  200. Points infill_points;
  201. parser.parse_buffer(gcode, [&tool, &perimeter_points, &infill_points, perimeter_extruder, infill_extruder]
  202. (Slic3r::GCodeReader &self, const Slic3r::GCodeReader::GCodeLine &line)
  203. {
  204. // if the command is a T command, set the the current tool
  205. if (boost::starts_with(line.cmd(), "T")) {
  206. tool = atoi(line.cmd().data() + 1) + 1;
  207. } else if (line.cmd() == "G1" && line.extruding(self) && line.dist_XY(self) > 0) {
  208. if (tool == perimeter_extruder)
  209. perimeter_points.emplace_back(line.new_XY_scaled(self));
  210. else if (tool == infill_extruder)
  211. infill_points.emplace_back(line.new_XY_scaled(self));
  212. }
  213. });
  214. auto convex_hull = Geometry::convex_hull(perimeter_points);
  215. int num_inside = std::count_if(infill_points.begin(), infill_points.end(), [&convex_hull](const Point &pt){ return convex_hull.contains(pt); });
  216. REQUIRE(num_inside == infill_points.size());
  217. }
  218. }
  219. };
  220. GIVEN("Rectilinear") { test("rectilinear"sv); }
  221. GIVEN("Honeycomb") { test("honeycomb"sv); }
  222. GIVEN("HilbertCurve") { test("hilbertcurve"sv); }
  223. GIVEN("Concentric") { test("concentric"sv); }
  224. }
  225. // SCENARIO("Infill only where needed", "[Fill]")
  226. // {
  227. // DynamicPrintConfig config = Slic3r::DynamicPrintConfig::full_print_config();
  228. // config.set_deserialize_strict({
  229. // { "nozzle_diameter", "0.4, 0.4, 0.4, 0.4" },
  230. // { "infill_only_where_needed", true },
  231. // { "bottom_solid_layers", 0 },
  232. // { "infill_extruder", 2 },
  233. // { "infill_extrusion_width", 0.5 },
  234. // { "wipe_into_infill", false },
  235. // { "fill_density", 0.4 },
  236. // // for preventing speeds from being altered
  237. // { "cooling", "0, 0, 0, 0" },
  238. // // for preventing speeds from being altered
  239. // { "first_layer_speed", "100%" }
  240. // });
  241. // auto test = [&config]() -> double {
  242. // TriangleMesh pyramid = Test::mesh(Slic3r::Test::TestMesh::pyramid);
  243. // // Arachne doesn't use "Detect thin walls," and because of this, it filters out tiny infill areas differently.
  244. // // So, for Arachne, we cut the pyramid model to achieve similar results.
  245. // if (config.opt_enum<PerimeterGeneratorType>("perimeter_generator") == Slic3r::PerimeterGeneratorType::Arachne) {
  246. // indexed_triangle_set lower{};
  247. // cut_mesh(pyramid.its, 35, nullptr, &lower);
  248. // pyramid = TriangleMesh(lower);
  249. // }
  250. // std::string gcode = Slic3r::Test::slice({ pyramid }, config);
  251. // THEN("gcode not empty") {
  252. // REQUIRE(! gcode.empty());
  253. // }
  254. // GCodeReader parser;
  255. // int tool = -1;
  256. // const int infill_extruder = config.opt_int("infill_extruder");
  257. // Points infill_points;
  258. // parser.parse_buffer(gcode, [&tool, &infill_points, infill_extruder](Slic3r::GCodeReader &self, const Slic3r::GCodeReader::GCodeLine &line)
  259. // {
  260. // // if the command is a T command, set the the current tool
  261. // if (boost::starts_with(line.cmd(), "T")) {
  262. // tool = atoi(line.cmd().data() + 1) + 1;
  263. // } else if (line.cmd() == "G1" && line.extruding(self) && line.dist_XY(self) > 0) {
  264. // if (tool == infill_extruder) {
  265. // infill_points.emplace_back(self.xy_scaled());
  266. // infill_points.emplace_back(line.new_XY_scaled(self));
  267. // }
  268. // }
  269. // });
  270. // // prevent calling convex_hull() with no points
  271. // THEN("infill not empty") {
  272. // REQUIRE(! infill_points.empty());
  273. // }
  274. // auto opt_width = config.opt<ConfigOptionFloatOrPercent>("infill_extrusion_width");
  275. // REQUIRE(! opt_width->percent);
  276. // Polygons convex_hull = expand(Geometry::convex_hull(infill_points), scaled<float>(opt_width->value / 2));
  277. // return SCALING_FACTOR * SCALING_FACTOR * std::accumulate(convex_hull.begin(), convex_hull.end(), 0., [](double acc, const Polygon &poly){ return acc + poly.area(); });
  278. // };
  279. // double tolerance = 5; // mm^2
  280. // // GIVEN("solid_infill_below_area == 0") {
  281. // // config.opt_float("solid_infill_below_area") = 0;
  282. // // WHEN("pyramid is sliced ") {
  283. // // auto area = test();
  284. // // THEN("no infill is generated when using infill_only_where_needed on a pyramid") {
  285. // // REQUIRE(area < tolerance);
  286. // // }
  287. // // }
  288. // // }
  289. // // GIVEN("solid_infill_below_area == 70") {
  290. // // config.opt_float("solid_infill_below_area") = 70;
  291. // // WHEN("pyramid is sliced ") {
  292. // // auto area = test();
  293. // // THEN("infill is only generated under the forced solid shells") {
  294. // // REQUIRE(std::abs(area - 70) < tolerance);
  295. // // }
  296. // // }
  297. // // }
  298. // }
  299. SCENARIO("Combine infill", "[Fill]")
  300. {
  301. {
  302. auto test = [](const DynamicPrintConfig &config) {
  303. std::string gcode = Test::slice({ Test::TestMesh::cube_20x20x20 }, config);
  304. THEN("infill_every_layers does not crash") {
  305. REQUIRE(! gcode.empty());
  306. }
  307. Slic3r::GCodeReader parser;
  308. int tool = -1;
  309. std::set<coord_t> layers; // layer_z => 1
  310. std::map<coord_t, bool> layer_infill; // layer_z => has_infill
  311. const int infill_extruder = config.opt_int("infill_extruder");
  312. const int support_material_extruder = config.opt_int("support_material_extruder");
  313. parser.parse_buffer(gcode,
  314. [&tool, &layers, &layer_infill, infill_extruder, support_material_extruder](Slic3r::GCodeReader &self, const Slic3r::GCodeReader::GCodeLine &line)
  315. {
  316. coord_t z = line.new_Z(self) / SCALING_FACTOR;
  317. if (boost::starts_with(line.cmd(), "T")) {
  318. tool = atoi(line.cmd().data() + 1);
  319. } else if (line.cmd_is("G1") && line.extruding(self) && line.dist_XY(self) > 0 && tool + 1 != support_material_extruder) {
  320. if (tool + 1 == infill_extruder)
  321. layer_infill[z] = true;
  322. else if (auto it = layer_infill.find(z); it == layer_infill.end())
  323. layer_infill.insert(it, std::make_pair(z, false));
  324. }
  325. // Previously, all G-code commands had a fixed number of decimal points with means with redundant zeros after decimal points.
  326. // We changed this behavior and got rid of these redundant padding zeros, which caused this test to fail
  327. // because the position in Z-axis is compared as a string, and previously, G-code contained the following two commands:
  328. // "G1 Z5 F5000 ; lift nozzle"
  329. // "G1 Z5.000 F7800.000"
  330. // That has a different Z-axis position from the view of string comparisons of floating-point numbers.
  331. // To correct the computation of the number of printed layers, even in the case of string comparisons of floating-point numbers,
  332. // we filtered out the G-code command with the commend 'lift nozzle'.
  333. if (line.cmd_is("G1") && line.dist_Z(self) != 0 && line.comment().find("lift nozzle") == std::string::npos)
  334. layers.insert(z);
  335. });
  336. auto layers_with_perimeters = int(layer_infill.size());
  337. auto layers_with_infill = int(std::count_if(layer_infill.begin(), layer_infill.end(), [](auto &v){ return v.second; }));
  338. THEN("expected number of layers") {
  339. REQUIRE(layers.size() == layers_with_perimeters + config.opt_int("raft_layers"));
  340. }
  341. if (config.opt_int("raft_layers") == 0) {
  342. // first infill layer printed directly on print bed is not combined, so we don't consider it.
  343. -- layers_with_infill;
  344. -- layers_with_perimeters;
  345. }
  346. // we expect that infill is generated for half the number of combined layers
  347. // plus for each single layer that was not combined (remainder)
  348. THEN("infill is only present in correct number of layers") {
  349. int infill_every = config.opt_int("infill_every_layers");
  350. REQUIRE(layers_with_infill == int(layers_with_perimeters / infill_every) + (layers_with_perimeters % infill_every));
  351. }
  352. };
  353. auto config = Slic3r::DynamicPrintConfig::full_print_config_with({
  354. { "nozzle_diameter", "0.5, 0.5, 0.5, 0.5" },
  355. { "layer_height", 0.2 },
  356. { "first_layer_height", 0.2 },
  357. { "infill_every_layers", 2 },
  358. { "perimeter_extruder", 1 },
  359. { "infill_extruder", 2 },
  360. { "wipe_into_infill", false },
  361. { "support_material_extruder", 3 },
  362. { "support_material_interface_extruder", 3 },
  363. { "top_solid_layers", 0 },
  364. { "bottom_solid_layers", 0 }
  365. });
  366. test(config);
  367. // Reuse the config above
  368. config.set_deserialize_strict({
  369. { "skirts", 0 }, // prevent usage of perimeter_extruder in raft layers
  370. { "raft_layers", 5 }
  371. });
  372. test(config);
  373. }
  374. WHEN("infill_every_layers == 2") {
  375. Slic3r::Print print;
  376. Slic3r::Test::init_and_process_print({ Test::TestMesh::cube_20x20x20 }, print, {
  377. { "nozzle_diameter", "0.5" },
  378. { "layer_height", 0.2 },
  379. { "first_layer_height", 0.2 },
  380. { "infill_every_layers", 2 }
  381. });
  382. THEN("infill combination produces internal void surfaces") {
  383. bool has_void = false;
  384. for (const Layer *layer : print.get_object(0)->layers())
  385. if (layer->get_region(0)->fill_surfaces().filter_by_type(stInternalVoid).size() > 0) {
  386. has_void = true;
  387. break;
  388. }
  389. REQUIRE(has_void);
  390. }
  391. }
  392. WHEN("infill_every_layers disabled") {
  393. // we disable combination after infill has been generated
  394. Slic3r::Print print;
  395. Slic3r::Test::init_and_process_print({ Test::TestMesh::cube_20x20x20 }, print, {
  396. { "nozzle_diameter", "0.5" },
  397. { "layer_height", 0.2 },
  398. { "first_layer_height", 0.2 },
  399. { "infill_every_layers", 1 }
  400. });
  401. THEN("infill combination is idempotent") {
  402. bool has_infill_on_each_layer = true;
  403. for (const Layer *layer : print.get_object(0)->layers())
  404. if (layer->get_region(0)->fill_surfaces().empty()) {
  405. has_infill_on_each_layer = false;
  406. break;
  407. }
  408. REQUIRE(has_infill_on_each_layer);
  409. }
  410. }
  411. }
  412. SCENARIO("Infill density zero", "[Fill]")
  413. {
  414. WHEN("20mm cube is sliced") {
  415. DynamicPrintConfig config = Slic3r::DynamicPrintConfig::full_print_config();
  416. config.set_deserialize_strict({
  417. { "skirts", 0 },
  418. { "perimeters", 1 },
  419. { "fill_density", 0 },
  420. { "top_solid_layers", 0 },
  421. { "bottom_solid_layers", 0 },
  422. { "solid_infill_below_area", 20000000 },
  423. { "solid_infill_every_layers", 2 },
  424. { "perimeter_speed", 99 },
  425. { "external_perimeter_speed", 99 },
  426. { "cooling", "0" },
  427. { "first_layer_speed", "100%" }
  428. });
  429. std::string gcode = Slic3r::Test::slice({ Slic3r::Test::TestMesh::cube_20x20x20 }, config);
  430. THEN("gcode not empty") {
  431. REQUIRE(! gcode.empty());
  432. }
  433. THEN("solid_infill_below_area and solid_infill_every_layers are ignored when fill_density is 0") {
  434. GCodeReader parser;
  435. const double perimeter_speed = config.opt_float("perimeter_speed");
  436. std::map<double, double> layers_with_extrusion;
  437. parser.parse_buffer(gcode, [&layers_with_extrusion, perimeter_speed](Slic3r::GCodeReader &self, const Slic3r::GCodeReader::GCodeLine &line) {
  438. if (line.cmd() == "G1" && line.extruding(self) && line.dist_XY(self) > 0) {
  439. double f = line.new_F(self);
  440. if (std::abs(f - perimeter_speed * 60.) > 0.01)
  441. // It is a perimeter.
  442. layers_with_extrusion[self.z()] = f;
  443. }
  444. });
  445. REQUIRE(layers_with_extrusion.empty());
  446. }
  447. }
  448. WHEN("A is sliced") {
  449. DynamicPrintConfig config = Slic3r::DynamicPrintConfig::full_print_config();
  450. config.set_deserialize_strict({
  451. { "skirts", 0 },
  452. { "perimeters", 3 },
  453. { "fill_density", 0 },
  454. { "layer_height", 0.2 },
  455. { "first_layer_height", 0.2 },
  456. { "nozzle_diameter", "0.35,0.35,0.35,0.35" },
  457. { "infill_extruder", 2 },
  458. { "solid_infill_extruder", 2 },
  459. { "infill_extrusion_width", 0.52 },
  460. { "solid_infill_extrusion_width", 0.52 },
  461. { "first_layer_extrusion_width", 0 }
  462. });
  463. std::string gcode = Slic3r::Test::slice({ Slic3r::Test::TestMesh::A }, config);
  464. THEN("gcode not empty") {
  465. REQUIRE(! gcode.empty());
  466. }
  467. THEN("no missing parts in solid shell when fill_density is 0") {
  468. GCodeReader parser;
  469. int tool = -1;
  470. const int infill_extruder = config.opt_int("infill_extruder");
  471. std::map<coord_t, Lines> infill;
  472. parser.parse_buffer(gcode, [&tool, &infill, infill_extruder](Slic3r::GCodeReader &self, const Slic3r::GCodeReader::GCodeLine &line) {
  473. if (boost::starts_with(line.cmd(), "T")) {
  474. tool = atoi(line.cmd().data() + 1) + 1;
  475. } else if (line.cmd() == "G1" && line.extruding(self) && line.dist_XY(self) > 0) {
  476. if (tool == infill_extruder)
  477. infill[scaled<coord_t>(self.z())].emplace_back(self.xy_scaled(), line.new_XY_scaled(self));
  478. }
  479. });
  480. auto opt_width = config.opt<ConfigOptionFloatOrPercent>("infill_extrusion_width");
  481. REQUIRE(! opt_width->percent);
  482. auto grow_d = scaled<float>(opt_width->value / 2);
  483. auto inflate_lines = [grow_d](const Lines &lines) {
  484. Polygons out;
  485. for (const Line &line : lines)
  486. append(out, offset(Polyline{ line.a, line.b }, grow_d, Slic3r::ClipperLib::jtSquare, 3.));
  487. return union_(out);
  488. };
  489. Polygons layer0_infill = inflate_lines(infill[scaled<coord_t>(0.2)]);
  490. Polygons layer1_infill = inflate_lines(infill[scaled<coord_t>(0.4)]);
  491. ExPolygons poly = opening_ex(diff_ex(layer0_infill, layer1_infill), grow_d);
  492. const double threshold = 2. * sqr(grow_d * 2.);
  493. int missing_parts = std::count_if(poly.begin(), poly.end(), [threshold](const ExPolygon &poly){ return poly.area() > threshold; });
  494. REQUIRE(missing_parts == 0);
  495. }
  496. }
  497. }
  498. /*
  499. {
  500. # GH: #2697
  501. my $config = Slic3r::Config->new_from_defaults;
  502. $config->set('perimeter_extrusion_width', 0.72);
  503. $config->set('top_infill_extrusion_width', 0.1);
  504. $config->set('infill_extruder', 2); # in order to distinguish infill
  505. $config->set('solid_infill_extruder', 2); # in order to distinguish infill
  506. my $print = Slic3r::Test::init_print('20mm_cube', config => $config);
  507. my %infill = (); # Z => [ Line, Line ... ]
  508. my %other = (); # Z => [ Line, Line ... ]
  509. my $tool = undef;
  510. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  511. my ($self, $cmd, $args, $info) = @_;
  512. if ($cmd =~ /^T(\d+)/) {
  513. $tool = $1;
  514. } elsif ($cmd eq 'G1' && $info->{extruding} && $info->{dist_XY} > 0) {
  515. my $z = 1 * $self->Z;
  516. my $line = Slic3r::Line->new_scale(
  517. [ $self->X, $self->Y ],
  518. [ $info->{new_X}, $info->{new_Y} ],
  519. );
  520. if ($tool == $config->infill_extruder-1) {
  521. $infill{$z} //= [];
  522. push @{$infill{$z}}, $line;
  523. } else {
  524. $other{$z} //= [];
  525. push @{$other{$z}}, $line;
  526. }
  527. }
  528. });
  529. my $top_z = max(keys %infill);
  530. my $top_infill_grow_d = scale($config->top_infill_extrusion_width)/2;
  531. my $top_infill = union([ map @{$_->grow($top_infill_grow_d)}, @{ $infill{$top_z} } ]);
  532. my $perimeters_grow_d = scale($config->perimeter_extrusion_width)/2;
  533. my $perimeters = union([ map @{$_->grow($perimeters_grow_d)}, @{ $other{$top_z} } ]);
  534. my $covered = union_ex([ @$top_infill, @$perimeters ]);
  535. my @holes = map @{$_->holes}, @$covered;
  536. ok sum(map unscale unscale $_->area*-1, @holes) < 1, 'no gaps between top solid infill and perimeters';
  537. }
  538. {
  539. skip "The FillRectilinear2 does not fill the surface completely", 1;
  540. my $test = sub {
  541. my ($expolygon, $flow_spacing, $angle, $density) = @_;
  542. my $filler = Slic3r::Filler->new_from_type('rectilinear');
  543. $filler->set_bounding_box($expolygon->bounding_box);
  544. $filler->set_angle($angle // 0);
  545. # Adjust line spacing to fill the region.
  546. $filler->set_dont_adjust(0);
  547. $filler->set_link_max_length(scale(1.2*$flow_spacing));
  548. my $surface = Slic3r::Surface->new(
  549. surface_type => S_TYPE_BOTTOM,
  550. expolygon => $expolygon,
  551. );
  552. my $flow = Slic3r::Flow->new(
  553. width => $flow_spacing,
  554. height => 0.4,
  555. nozzle_diameter => $flow_spacing,
  556. );
  557. $filler->set_spacing($flow->spacing);
  558. my $paths = $filler->fill_surface(
  559. $surface,
  560. layer_height => $flow->height,
  561. density => $density // 1,
  562. );
  563. # check whether any part was left uncovered
  564. my @grown_paths = map @{Slic3r::Polyline->new(@$_)->grow(scale $filler->spacing/2)}, @$paths;
  565. my $uncovered = diff_ex([ @$expolygon ], [ @grown_paths ], 1);
  566. # ignore very small dots
  567. my $uncovered_filtered = [ grep $_->area > (scale $flow_spacing)**2, @$uncovered ];
  568. is scalar(@$uncovered_filtered), 0, 'solid surface is fully filled';
  569. if (0 && @$uncovered_filtered) {
  570. require "Slic3r/SVG.pm";
  571. Slic3r::SVG::output("uncovered.svg",
  572. no_arrows => 1,
  573. expolygons => [ $expolygon ],
  574. blue_expolygons => [ @$uncovered ],
  575. red_expolygons => [ @$uncovered_filtered ],
  576. polylines => [ @$paths ],
  577. );
  578. exit;
  579. }
  580. };
  581. my $expolygon = Slic3r::ExPolygon->new([
  582. [6883102, 9598327.01296997],
  583. [6883102, 20327272.01297],
  584. [3116896, 20327272.01297],
  585. [3116896, 9598327.01296997],
  586. ]);
  587. $test->($expolygon, 0.55);
  588. for (1..20) {
  589. $expolygon->scale(1.05);
  590. $test->($expolygon, 0.55);
  591. }
  592. $expolygon = Slic3r::ExPolygon->new(
  593. [[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]]
  594. );
  595. $test->($expolygon, 0.524341649025257);
  596. $expolygon = Slic3r::ExPolygon->new([ scale_points [0,0], [98,0], [98,10], [0,10] ]);
  597. $test->($expolygon, 0.5, 45, 0.99); # non-solid infill
  598. }
  599. */
  600. bool test_if_solid_surface_filled(const ExPolygon& expolygon, double flow_spacing, double angle, double density)
  601. {
  602. std::unique_ptr<Slic3r::Fill> filler(Slic3r::Fill::new_from_type("rectilinear"));
  603. filler->bounding_box = get_extents(expolygon.contour);
  604. filler->angle = float(angle);
  605. Flow flow(float(flow_spacing), 0.4f, float(flow_spacing));
  606. filler->spacing = flow.spacing();
  607. FillParams fill_params;
  608. fill_params.density = float(density);
  609. fill_params.dont_adjust = false;
  610. Surface surface(stBottom, expolygon);
  611. if (fill_params.use_arachne) // Make this test fail when Arachne is used because this test is not ready for it.
  612. return false;
  613. Slic3r::Polylines paths = filler->fill_surface(&surface, fill_params);
  614. // check whether any part was left uncovered
  615. Polygons grown_paths;
  616. grown_paths.reserve(paths.size());
  617. // figure out what is actually going on here re: data types
  618. float line_offset = float(scale_(filler->spacing / 2.0 + EPSILON));
  619. std::for_each(paths.begin(), paths.end(), [line_offset, &grown_paths] (const Slic3r::Polyline& p) {
  620. polygons_append(grown_paths, offset(p, line_offset));
  621. });
  622. // Shrink the initial expolygon a bit, this simulates the infill / perimeter overlap that we usually apply.
  623. ExPolygons uncovered = diff_ex(offset(expolygon, - float(0.2 * scale_(flow_spacing))), grown_paths, ApplySafetyOffset::Yes);
  624. // ignore very small dots
  625. const double scaled_flow_spacing = std::pow(scale_(flow_spacing), 2);
  626. uncovered.erase(std::remove_if(uncovered.begin(), uncovered.end(), [scaled_flow_spacing](const ExPolygon& poly) { return poly.area() < scaled_flow_spacing; }), uncovered.end());
  627. #if 0
  628. if (! uncovered.empty()) {
  629. BoundingBox bbox = get_extents(expolygon.contour);
  630. bbox.merge(get_extents(uncovered));
  631. bbox.merge(get_extents(grown_paths));
  632. SVG svg("c:\\data\\temp\\test_if_solid_surface_filled.svg", bbox);
  633. svg.draw(expolygon);
  634. svg.draw(uncovered, "red");
  635. svg.Close();
  636. }
  637. #endif
  638. return uncovered.empty(); // solid surface is fully filled
  639. }