test_flow.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #include <catch2/catch.hpp>
  2. #include <numeric>
  3. #include <sstream>
  4. #include "test_data.hpp" // get access to init_print, etc
  5. #include "libslic3r/Config.hpp"
  6. #include "libslic3r/Model.hpp"
  7. #include "libslic3r/Config.hpp"
  8. #include "libslic3r/GCodeReader.hpp"
  9. #include "libslic3r/Flow.hpp"
  10. #include "libslic3r/libslic3r.h"
  11. using namespace Slic3r::Test;
  12. using namespace Slic3r;
  13. SCENARIO("Extrusion width specifics", "[Flow]") {
  14. GIVEN("A config with a skirt, brim, some fill density, 3 perimeters, and 1 bottom solid layer and a 20mm cube mesh") {
  15. // this is a sharedptr
  16. DynamicPrintConfig config = Slic3r::DynamicPrintConfig::full_print_config();
  17. config.set_deserialize({
  18. { "brim_width", 2 },
  19. { "skirts", 1 },
  20. { "perimeters", 3 },
  21. { "fill_density", "40%" },
  22. { "first_layer_height", 0.3 }
  23. });
  24. WHEN("first layer width set to 2mm") {
  25. Slic3r::Model model;
  26. config.set("first_layer_extrusion_width", 2);
  27. Slic3r::Print print;
  28. Slic3r::Test::init_print({TestMesh::cube_20x20x20}, print, model, config);
  29. std::vector<double> E_per_mm_bottom;
  30. std::string gcode = Test::gcode(print);
  31. Slic3r::GCodeReader parser;
  32. const double layer_height = config.opt_float("layer_height");
  33. parser.parse_buffer(gcode, [&E_per_mm_bottom, layer_height] (Slic3r::GCodeReader& self, const Slic3r::GCodeReader::GCodeLine& line)
  34. {
  35. if (self.z() == Approx(layer_height).margin(0.01)) { // only consider first layer
  36. if (line.extruding(self) && line.dist_XY(self) > 0) {
  37. E_per_mm_bottom.emplace_back(line.dist_E(self) / line.dist_XY(self));
  38. }
  39. }
  40. });
  41. THEN(" First layer width applies to everything on first layer.") {
  42. bool pass = false;
  43. double avg_E = std::accumulate(E_per_mm_bottom.cbegin(), E_per_mm_bottom.cend(), 0.0) / static_cast<double>(E_per_mm_bottom.size());
  44. pass = (std::count_if(E_per_mm_bottom.cbegin(), E_per_mm_bottom.cend(), [avg_E] (const double& v) { return v == Approx(avg_E); }) == 0);
  45. REQUIRE(pass == true);
  46. REQUIRE(E_per_mm_bottom.size() > 0); // make sure it actually passed because of extrusion
  47. }
  48. THEN(" First layer width does not apply to upper layer.") {
  49. }
  50. }
  51. }
  52. }
  53. // needs gcode export
  54. SCENARIO(" Bridge flow specifics.", "[Flow]") {
  55. GIVEN("A default config with no cooling and a fixed bridge speed, flow ratio and an overhang mesh.") {
  56. WHEN("bridge_flow_ratio is set to 1.0") {
  57. THEN("Output flow is as expected.") {
  58. }
  59. }
  60. WHEN("bridge_flow_ratio is set to 0.5") {
  61. THEN("Output flow is as expected.") {
  62. }
  63. }
  64. WHEN("bridge_flow_ratio is set to 2.0") {
  65. THEN("Output flow is as expected.") {
  66. }
  67. }
  68. }
  69. GIVEN("A default config with no cooling and a fixed bridge speed, flow ratio, fixed extrusion width of 0.4mm and an overhang mesh.") {
  70. WHEN("bridge_flow_ratio is set to 1.0") {
  71. THEN("Output flow is as expected.") {
  72. }
  73. }
  74. WHEN("bridge_flow_ratio is set to 0.5") {
  75. THEN("Output flow is as expected.") {
  76. }
  77. }
  78. WHEN("bridge_flow_ratio is set to 2.0") {
  79. THEN("Output flow is as expected.") {
  80. }
  81. }
  82. }
  83. }
  84. /// Test the expected behavior for auto-width,
  85. /// spacing, etc
  86. SCENARIO("Flow: Flow math for non-bridges", "[Flow]") {
  87. GIVEN("Nozzle Diameter of 0.4, a desired width of 1mm and layer height of 0.5") {
  88. ConfigOptionFloatOrPercent width(1.0, false);
  89. float nozzle_diameter = 0.4f;
  90. float layer_height = 0.4f;
  91. // Spacing for non-bridges is has some overlap
  92. THEN("External perimeter flow has spacing fixed to 1.125 * nozzle_diameter") {
  93. auto flow = Flow::new_from_config_width(frExternalPerimeter, ConfigOptionFloatOrPercent(0, false), nozzle_diameter, layer_height);
  94. REQUIRE(flow.spacing() == Approx(1.125 * nozzle_diameter - layer_height * (1.0 - PI / 4.0)));
  95. }
  96. THEN("Internal perimeter flow has spacing fixed to 1.125 * nozzle_diameter") {
  97. auto flow = Flow::new_from_config_width(frPerimeter, ConfigOptionFloatOrPercent(0, false), nozzle_diameter, layer_height);
  98. REQUIRE(flow.spacing() == Approx(1.125 *nozzle_diameter - layer_height * (1.0 - PI / 4.0)));
  99. }
  100. THEN("Spacing for supplied width is 0.8927f") {
  101. auto flow = Flow::new_from_config_width(frExternalPerimeter, width, nozzle_diameter, layer_height);
  102. REQUIRE(flow.spacing() == Approx(width.value - layer_height * (1.0 - PI / 4.0)));
  103. flow = Flow::new_from_config_width(frPerimeter, width, nozzle_diameter, layer_height);
  104. REQUIRE(flow.spacing() == Approx(width.value - layer_height * (1.0 - PI / 4.0)));
  105. }
  106. }
  107. /// Check the min/max
  108. GIVEN("Nozzle Diameter of 0.25") {
  109. float nozzle_diameter = 0.25f;
  110. float layer_height = 0.5f;
  111. WHEN("layer height is set to 0.2") {
  112. layer_height = 0.15f;
  113. THEN("Max width is set.") {
  114. auto flow = Flow::new_from_config_width(frPerimeter, ConfigOptionFloatOrPercent(0, false), nozzle_diameter, layer_height);
  115. REQUIRE(flow.width() == Approx(1.125 * nozzle_diameter));
  116. }
  117. }
  118. WHEN("Layer height is set to 0.25") {
  119. layer_height = 0.25f;
  120. THEN("Min width is set.") {
  121. auto flow = Flow::new_from_config_width(frPerimeter, ConfigOptionFloatOrPercent(0, false), nozzle_diameter, layer_height);
  122. REQUIRE(flow.width() == Approx(1.125 * nozzle_diameter));
  123. }
  124. }
  125. }
  126. #if 0
  127. /// Check for an edge case in the maths where the spacing could be 0; original
  128. /// math is 0.99. Slic3r issue #4654
  129. GIVEN("Input spacing of 0.414159 and a total width of 2") {
  130. double in_spacing = 0.414159;
  131. double total_width = 2.0;
  132. auto flow = Flow::new_from_spacing(1.0, 0.4, 0.3);
  133. WHEN("solid_spacing() is called") {
  134. double result = flow.solid_spacing(total_width, in_spacing);
  135. THEN("Yielded spacing is greater than 0") {
  136. REQUIRE(result > 0);
  137. }
  138. }
  139. }
  140. #endif
  141. }
  142. /// Spacing, width calculation for bridge extrusions
  143. SCENARIO("Flow: Flow math for bridges", "[Flow]") {
  144. GIVEN("Nozzle Diameter of 0.4, a desired width of 1mm and layer height of 0.5") {
  145. float nozzle_diameter = 0.4f;
  146. float bridge_flow = 1.0f;
  147. WHEN("Flow role is frExternalPerimeter") {
  148. auto flow = Flow::bridging_flow(nozzle_diameter * sqrt(bridge_flow), nozzle_diameter);
  149. THEN("Bridge width is same as nozzle diameter") {
  150. REQUIRE(flow.width() == Approx(nozzle_diameter));
  151. }
  152. THEN("Bridge spacing is same as nozzle diameter + BRIDGE_EXTRA_SPACING") {
  153. REQUIRE(flow.spacing() == Approx(nozzle_diameter + BRIDGE_EXTRA_SPACING));
  154. }
  155. }
  156. }
  157. }