test_shells.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. #include <catch2/catch.hpp>
  2. #include "libslic3r/GCodeReader.hpp"
  3. #include "test_data.hpp" // get access to init_print, etc
  4. using namespace Slic3r::Test;
  5. using namespace Slic3r;
  6. SCENARIO("Shells", "[Shells]") {
  7. GIVEN("20mm box") {
  8. auto test = [](const DynamicPrintConfig &config){
  9. std::vector<coord_t> zs;
  10. std::set<coord_t> layers_with_solid_infill;
  11. std::set<coord_t> layers_with_bridge_infill;
  12. const double solid_infill_speed = config.opt_float("solid_infill_speed") * 60;
  13. const double bridge_speed = config.opt_float("bridge_speed") * 60;
  14. GCodeReader parser;
  15. parser.parse_buffer(Slic3r::Test::slice({ Slic3r::Test::TestMesh::cube_20x20x20 }, config),
  16. [&zs, &layers_with_solid_infill, &layers_with_bridge_infill, solid_infill_speed, bridge_speed]
  17. (Slic3r::GCodeReader &self, const Slic3r::GCodeReader::GCodeLine &line)
  18. {
  19. double z = line.new_Z(self);
  20. REQUIRE(z >= 0);
  21. if (z > 0) {
  22. coord_t scaled_z = scaled<float>(z);
  23. zs.emplace_back(scaled_z);
  24. if (line.extruding(self) && line.dist_XY(self) > 0) {
  25. double f = line.new_F(self);
  26. if (std::abs(f - solid_infill_speed) < EPSILON)
  27. layers_with_solid_infill.insert(scaled_z);
  28. if (std::abs(f - bridge_speed) < EPSILON)
  29. layers_with_bridge_infill.insert(scaled_z);
  30. }
  31. }
  32. });
  33. sort_remove_duplicates(zs);
  34. auto has_solid_infill = [&layers_with_solid_infill](coord_t z) { return layers_with_solid_infill.find(z) != layers_with_solid_infill.end(); };
  35. auto has_bridge_infill = [&layers_with_bridge_infill](coord_t z) { return layers_with_bridge_infill.find(z) != layers_with_bridge_infill.end(); };
  36. auto has_shells = [&has_solid_infill, &has_bridge_infill, &zs](int layer_idx) { coord_t z = zs[layer_idx]; return has_solid_infill(z) || has_bridge_infill(z); };
  37. const int bottom_solid_layers = config.opt_int("bottom_solid_layers");
  38. const int top_solid_layers = config.opt_int("top_solid_layers");
  39. THEN("correct number of bottom solid layers") {
  40. for (int i = 0; i < bottom_solid_layers; ++ i)
  41. REQUIRE(has_shells(i));
  42. for (int i = bottom_solid_layers; i < int(zs.size() / 2); ++ i)
  43. REQUIRE(! has_shells(i));
  44. }
  45. THEN("correct number of top solid layers") {
  46. // NOTE: there is one additional layer with enusring line under the bridge layer, bridges would be otherwise anchored weakly to the perimeter.
  47. size_t additional_ensuring_anchors = top_solid_layers > 0 ? 1 : 0;
  48. for (int i = 0; i < top_solid_layers + additional_ensuring_anchors; ++ i)
  49. REQUIRE(has_shells(int(zs.size()) - i - 1));
  50. for (int i = top_solid_layers + additional_ensuring_anchors; i < int(zs.size() / 2); ++ i)
  51. REQUIRE(! has_shells(int(zs.size()) - i - 1));
  52. }
  53. if (top_solid_layers > 0) {
  54. THEN("solid infill speed is used on solid infill") {
  55. for (int i = 0; i < top_solid_layers - 1; ++ i) {
  56. auto z = zs[int(zs.size()) - i - 1];
  57. REQUIRE(has_solid_infill(z));
  58. REQUIRE(! has_bridge_infill(z));
  59. }
  60. }
  61. THEN("bridge used in first solid layer over sparse infill") {
  62. auto z = zs[int(zs.size()) - top_solid_layers];
  63. REQUIRE(! has_solid_infill(z));
  64. REQUIRE(has_bridge_infill(z));
  65. }
  66. }
  67. };
  68. auto config = Slic3r::DynamicPrintConfig::full_print_config_with({
  69. { "skirts", 0 },
  70. { "perimeters", 0 },
  71. { "solid_infill_speed", 99 },
  72. { "top_solid_infill_speed", 99 },
  73. { "bridge_speed", 72 },
  74. { "first_layer_speed", "100%" },
  75. { "cooling", "0" }
  76. });
  77. WHEN("three top and bottom layers") {
  78. // proper number of shells is applied
  79. config.set_deserialize_strict({
  80. { "top_solid_layers", 3 },
  81. { "bottom_solid_layers", 3 }
  82. });
  83. test(config);
  84. }
  85. WHEN("zero top and bottom layers") {
  86. // no shells are applied when both top and bottom are set to zero
  87. config.set_deserialize_strict({
  88. { "top_solid_layers", 0 },
  89. { "bottom_solid_layers", 0 }
  90. });
  91. test(config);
  92. }
  93. WHEN("three top and bottom layers, zero infill") {
  94. // proper number of shells is applied even when fill density is none
  95. config.set_deserialize_strict({
  96. { "perimeters", 1 },
  97. { "top_solid_layers", 3 },
  98. { "bottom_solid_layers", 3 }
  99. });
  100. test(config);
  101. }
  102. }
  103. }
  104. static std::set<double> layers_with_speed(const std::string &gcode, int speed)
  105. {
  106. std::set<double> out;
  107. GCodeReader parser;
  108. parser.parse_buffer(gcode, [&out, speed](Slic3r::GCodeReader &self, const Slic3r::GCodeReader::GCodeLine &line) {
  109. if (line.extruding(self) && is_approx<double>(line.new_F(self), speed * 60.))
  110. out.insert(self.z());
  111. });
  112. return out;
  113. }
  114. SCENARIO("Shells (from Perl)", "[Shells]") {
  115. GIVEN("V shape, Slic3r GH #1161") {
  116. int solid_speed = 99;
  117. auto config = Slic3r::DynamicPrintConfig::full_print_config_with({
  118. { "layer_height", 0.3 },
  119. { "first_layer_height", 0.3 },
  120. { "bottom_solid_layers", 0 },
  121. { "top_solid_layers", 3 },
  122. // to prevent speeds from being altered
  123. { "cooling", "0" },
  124. { "bridge_speed", solid_speed },
  125. { "solid_infill_speed", solid_speed },
  126. { "top_solid_infill_speed", solid_speed },
  127. // to prevent speeds from being altered
  128. { "first_layer_speed", "100%" },
  129. // prevent speed alteration
  130. { "enable_dynamic_overhang_speeds", 0 }
  131. });
  132. THEN("correct number of top solid shells is generated in V-shaped object") {
  133. size_t n = 0;
  134. for (auto z : layers_with_speed(Slic3r::Test::slice({TestMesh::V}, config), solid_speed))
  135. if (z <= 7.2)
  136. ++ n;
  137. REQUIRE(n == 3 + 1/*one additional layer with ensuring for bridge anchors*/);
  138. }
  139. }
  140. //TODO CHECK AFTER REMOVAL OF "ensure_vertical_wall_thickness"
  141. // GIVEN("V shape") {
  142. // // we need to check against one perimeter because this test is calibrated
  143. // // (shape, extrusion_width) so that perimeters cover the bottom surfaces of
  144. // // their lower layer - the test checks that shells are not generated on the
  145. // // above layers (thus 'across' the shadow perimeter)
  146. // // the test is actually calibrated to leave a narrow bottom region for each
  147. // // layer - we test that in case of fill_density = 0 such narrow shells are
  148. // // discarded instead of grown
  149. // int bottom_solid_layers = 3;
  150. // auto config = Slic3r::DynamicPrintConfig::full_print_config_with({
  151. // { "perimeters", 1 },
  152. // { "fill_density", 0 },
  153. // // to prevent speeds from being altered
  154. // { "cooling", "0" },
  155. // // to prevent speeds from being altered
  156. // { "first_layer_speed", "100%" },
  157. // // prevent speed alteration
  158. // { "enable_dynamic_overhang_speeds", 0 },
  159. // { "layer_height", 0.4 },
  160. // { "first_layer_height", 0.4 },
  161. // { "extrusion_width", 0.55 },
  162. // { "bottom_solid_layers", bottom_solid_layers },
  163. // { "top_solid_layers", 0 },
  164. // { "solid_infill_speed", 99 }
  165. // });
  166. // THEN("shells are not propagated across perimeters of the neighbor layer") {
  167. // std::string gcode = Slic3r::Test::slice({TestMesh::V}, config);
  168. // REQUIRE(layers_with_speed(gcode, 99).size() == bottom_solid_layers);
  169. // }
  170. // }
  171. // GIVEN("sloping_hole") {
  172. // int bottom_solid_layers = 3;
  173. // int top_solid_layers = 3;
  174. // int solid_speed = 99;
  175. // auto config = Slic3r::DynamicPrintConfig::full_print_config_with({
  176. // { "perimeters", 3 },
  177. // // to prevent speeds from being altered
  178. // { "cooling", "0" },
  179. // // to prevent speeds from being altered
  180. // { "first_layer_speed", "100%" },
  181. // // prevent speed alteration
  182. // { "enable_dynamic_overhang_speeds", 0 },
  183. // { "layer_height", 0.4 },
  184. // { "first_layer_height", 0.4 },
  185. // { "bottom_solid_layers", bottom_solid_layers },
  186. // { "top_solid_layers", top_solid_layers },
  187. // { "solid_infill_speed", solid_speed },
  188. // { "top_solid_infill_speed", solid_speed },
  189. // { "bridge_speed", solid_speed },
  190. // { "filament_diameter", 3. },
  191. // { "nozzle_diameter", 0.5 }
  192. // });
  193. // THEN("no superfluous shells are generated") {
  194. // std::string gcode = Slic3r::Test::slice({TestMesh::sloping_hole}, config);
  195. // REQUIRE(layers_with_speed(gcode, solid_speed).size() == bottom_solid_layers + top_solid_layers);
  196. // }
  197. // }
  198. GIVEN("20mm_cube, spiral vase") {
  199. double layer_height = 0.3;
  200. auto config = Slic3r::DynamicPrintConfig::full_print_config_with({
  201. { "perimeters", 1 },
  202. { "fill_density", 0 },
  203. { "layer_height", layer_height },
  204. { "first_layer_height", layer_height },
  205. { "top_solid_layers", 0 },
  206. { "spiral_vase", 1 },
  207. { "bottom_solid_layers", 0 },
  208. { "skirts", 0 },
  209. { "start_gcode", "" },
  210. { "temperature", 200 },
  211. { "first_layer_temperature", 205}
  212. });
  213. // TODO: this needs to be tested with a model with sloping edges, where starting
  214. // points of each layer are not aligned - in that case we would test that no
  215. // travel moves are left to move to the new starting point - in a cube, end
  216. // points coincide with next layer starting points (provided there's no clipping)
  217. auto test = [layer_height](const DynamicPrintConfig &config) {
  218. size_t travel_moves_after_first_extrusion = 0;
  219. bool started_extruding = false;
  220. bool first_layer_temperature_set = false;
  221. bool temperature_set = false;
  222. std::vector<double> z_steps;
  223. GCodeReader parser;
  224. parser.parse_buffer(Slic3r::Test::slice({TestMesh::cube_20x20x20}, config),
  225. [&](Slic3r::GCodeReader &self, const Slic3r::GCodeReader::GCodeLine &line) {
  226. if (line.cmd_is("G1")) {
  227. if (line.extruding(self))
  228. started_extruding = true;
  229. if (started_extruding) {
  230. if (double dz = line.dist_Z(self); dz > 0)
  231. z_steps.emplace_back(dz);
  232. if (line.travel() && line.dist_XY(self) > 0 && ! line.has(Z))
  233. ++ travel_moves_after_first_extrusion;
  234. }
  235. } else if (line.cmd_is("M104")) {
  236. int s;
  237. if (line.has_value('S', s)) {
  238. if (s == 205)
  239. first_layer_temperature_set = true;
  240. else if (s == 200)
  241. temperature_set = true;
  242. }
  243. }
  244. });
  245. THEN("first layer temperature is set") {
  246. REQUIRE(first_layer_temperature_set);
  247. }
  248. THEN("temperature is set") {
  249. REQUIRE(temperature_set);
  250. }
  251. // we allow one travel move after first extrusion: i.e. when moving to the first
  252. // spiral point after moving to second layer (bottom layer had loop clipping, so
  253. // we're slightly distant from the starting point of the loop)
  254. THEN("no gaps in spiral vase") {
  255. REQUIRE(travel_moves_after_first_extrusion <= 1);
  256. }
  257. THEN("no gaps in Z") {
  258. REQUIRE(std::count_if(z_steps.begin(), z_steps.end(),
  259. [&layer_height](auto z_step) { return z_step > layer_height + EPSILON; }) == 0);
  260. }
  261. };
  262. WHEN("solid model") {
  263. test(config);
  264. }
  265. WHEN("solid model with negative z-offset") {
  266. config.set_deserialize_strict("z_offset", "-10");
  267. test(config);
  268. }
  269. // Disabled because the current unreliable medial axis code doesn't always produce valid loops.
  270. // $test->('40x10', 'hollow model with negative z-offset');
  271. }
  272. GIVEN("20mm_cube, spiral vase") {
  273. double layer_height = 0.4;
  274. auto config = Slic3r::DynamicPrintConfig::full_print_config_with({
  275. { "spiral_vase", 1 },
  276. { "perimeters", 1 },
  277. { "fill_density", 0 },
  278. { "top_solid_layers", 0 },
  279. { "bottom_solid_layers", 0 },
  280. { "retract_layer_change", 0 },
  281. { "skirts", 0 },
  282. { "layer_height", layer_height },
  283. { "first_layer_height", layer_height },
  284. { "start_gcode", "" },
  285. // { "use_relative_e_distances", 1}
  286. });
  287. config.validate();
  288. std::vector<std::pair<double, double>> this_layer; // [ dist_Z, dist_XY ], ...
  289. int z_moves = 0;
  290. bool bottom_layer_not_flat = false;
  291. bool null_z_moves_not_layer_changes = false;
  292. bool null_z_moves_not_multiples_of_layer_height = false;
  293. bool sum_of_partial_z_equals_to_layer_height = false;
  294. bool all_layer_segments_have_same_slope = false;
  295. bool horizontal_extrusions = false;
  296. GCodeReader parser;
  297. parser.parse_buffer(Slic3r::Test::slice({TestMesh::cube_20x20x20}, config),
  298. [&](Slic3r::GCodeReader &self, const Slic3r::GCodeReader::GCodeLine &line) {
  299. if (line.cmd_is("G1")) {
  300. if (z_moves < 2) {
  301. // skip everything up to the second Z move
  302. // (i.e. start of second layer)
  303. if (line.has(Z)) {
  304. ++ z_moves;
  305. if (double dz = line.dist_Z(self); dz > 0 && ! is_approx<double>(dz, layer_height))
  306. bottom_layer_not_flat = true;
  307. }
  308. } else if (line.dist_Z(self) == 0 && line.has(Z)) {
  309. if (line.dist_XY(self) != 0)
  310. null_z_moves_not_layer_changes = true;
  311. double z = line.new_Z(self);
  312. if (fmod(z + EPSILON, layer_height) > 2 * EPSILON)
  313. null_z_moves_not_multiples_of_layer_height = true;
  314. double total_dist_XY = 0;
  315. double total_dist_Z = 0;
  316. for (auto &seg : this_layer) {
  317. total_dist_Z += seg.first;
  318. total_dist_XY += seg.second;
  319. }
  320. if (std::abs(total_dist_Z - layer_height) >
  321. // The first segment on the 2nd layer has extrusion interpolated from zero
  322. // and the 1st segment has such a low extrusion assigned, that it is effectively zero, thus the move
  323. // is considered non-extruding and a higher epsilon is required.
  324. (z_moves == 2 ? 0.0021 : EPSILON))
  325. sum_of_partial_z_equals_to_layer_height = true;
  326. //printf("Total height: %f, layer height: %f, good: %d\n", sum(map $_->[0], @this_layer), $config->layer_height, $sum_of_partial_z_equals_to_layer_height);
  327. for (auto &seg : this_layer)
  328. // check that segment's dist_Z is proportioned to its dist_XY
  329. if (std::abs(seg.first * total_dist_XY / layer_height - seg.second) > 0.2)
  330. all_layer_segments_have_same_slope = true;
  331. this_layer.clear();
  332. } else if (line.extruding(self) && line.dist_XY(self) > 0) {
  333. if (line.dist_Z(self) == 0)
  334. horizontal_extrusions = true;
  335. //printf("Pushing dist_z: %f, dist_xy: %f\n", $info->{dist_Z}, $info->{dist_XY});
  336. this_layer.emplace_back(line.dist_Z(self), line.dist_XY(self));
  337. }
  338. }
  339. });
  340. THEN("bottom layer is flat when using spiral vase") {
  341. REQUIRE(! bottom_layer_not_flat);
  342. }
  343. THEN("null Z moves are layer changes") {
  344. REQUIRE(! null_z_moves_not_layer_changes);
  345. }
  346. THEN("null Z moves are multiples of layer height") {
  347. REQUIRE(! null_z_moves_not_multiples_of_layer_height);
  348. }
  349. THEN("sum of partial Z increments equals to a full layer height") {
  350. REQUIRE(! sum_of_partial_z_equals_to_layer_height);
  351. }
  352. THEN("all layer segments have the same slope") {
  353. REQUIRE(! all_layer_segments_have_same_slope);
  354. }
  355. THEN("no horizontal extrusions") {
  356. REQUIRE(! horizontal_extrusions);
  357. }
  358. }
  359. }
  360. #if 0
  361. // The current Spiral Vase slicing code removes the holes and all but the largest contours from each slice,
  362. // therefore the following test is no more valid.
  363. {
  364. my $config = Slic3r::Config::new_from_defaults;
  365. $config->set('perimeters', 1);
  366. $config->set('fill_density', 0);
  367. $config->set('top_solid_layers', 0);
  368. $config->set('spiral_vase', 1);
  369. $config->set('bottom_solid_layers', 0);
  370. $config->set('skirts', 0);
  371. $config->set('first_layer_height', $config->layer_height);
  372. $config->set('start_gcode', '');
  373. my $print = Slic3r::Test::init_print('two_hollow_squares', config => $config);
  374. my $diagonal_moves = 0;
  375. Slic3r::GCode::Reader->new->parse(Slic3r::Test::gcode($print), sub {
  376. my ($self, $cmd, $args, $info) = @_;
  377. if ($cmd eq 'G1') {
  378. if ($info->{extruding} && $info->{dist_XY} > 0) {
  379. if ($info->{dist_Z} > 0) {
  380. $diagonal_moves++;
  381. }
  382. }
  383. }
  384. });
  385. is $diagonal_moves, 0, 'no spiral moves on two-island object';
  386. }
  387. #endif