test_fill.cpp 34 KB

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