test_flow.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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/GCodeReader.hpp"
  7. #include "libslic3r/Flow.hpp"
  8. #include "libslic3r/libslic3r.h"
  9. using namespace Slic3r::Test;
  10. using namespace Slic3r;
  11. SCENARIO("Extrusion width specifics", "[Flow]") {
  12. auto test = [](const DynamicPrintConfig &config) {
  13. Slic3r::GCodeReader parser;
  14. const double layer_height = config.opt_float("layer_height");
  15. std::vector<double> E_per_mm_bottom;
  16. parser.parse_buffer(Slic3r::Test::slice({ Slic3r::Test::TestMesh::cube_20x20x20 }, config),
  17. [&E_per_mm_bottom, layer_height] (Slic3r::GCodeReader& self, const Slic3r::GCodeReader::GCodeLine& line)
  18. {
  19. if (self.z() == Approx(layer_height).margin(0.01)) { // only consider first layer
  20. if (line.extruding(self) && line.dist_XY(self) > 0)
  21. E_per_mm_bottom.emplace_back(line.dist_E(self) / line.dist_XY(self));
  22. }
  23. });
  24. THEN("First layer width applies to everything on first layer.") {
  25. REQUIRE(E_per_mm_bottom.size() > 0);
  26. const double E_per_mm_avg = std::accumulate(E_per_mm_bottom.cbegin(), E_per_mm_bottom.cend(), 0.0) / static_cast<double>(E_per_mm_bottom.size());
  27. bool pass = (std::count_if(E_per_mm_bottom.cbegin(), E_per_mm_bottom.cend(), [E_per_mm_avg] (const double& v) { return v == Approx(E_per_mm_avg); }) == 0);
  28. REQUIRE(pass);
  29. }
  30. THEN("First layer width does not apply to upper layer.") {
  31. }
  32. };
  33. GIVEN("A config with a skirt, brim, some fill density, 3 perimeters, and 1 bottom solid layer") {
  34. auto config = Slic3r::DynamicPrintConfig::full_print_config_with({
  35. { "skirts", 1 },
  36. { "brim_width", 2 },
  37. { "perimeters", 3 },
  38. { "fill_density", "40%" },
  39. { "first_layer_height", 0.3 },
  40. { "first_layer_extrusion_width", "2" },
  41. });
  42. WHEN("Slicing a 20mm cube") {
  43. test(config);
  44. }
  45. }
  46. GIVEN("A config with more options and a 20mm cube ") {
  47. auto config = Slic3r::DynamicPrintConfig::full_print_config_with({
  48. { "skirts", 1 },
  49. { "brim_width", 2 },
  50. { "perimeters", 3 },
  51. { "fill_density", "40%" },
  52. { "layer_height", "0.35" },
  53. { "first_layer_height", "0.35" },
  54. { "bottom_solid_layers", 1 },
  55. { "first_layer_extrusion_width", "2" },
  56. { "filament_diameter", "3" },
  57. { "nozzle_diameter", "0.5" }
  58. });
  59. WHEN("Slicing a 20mm cube") {
  60. test(config);
  61. }
  62. }
  63. }
  64. SCENARIO(" Bridge flow specifics.", "[Flow]") {
  65. auto config = DynamicPrintConfig::full_print_config_with({
  66. { "bridge_speed", 99 },
  67. { "bridge_flow_ratio", 1 },
  68. // to prevent speeds from being altered
  69. { "cooling", "0" },
  70. // to prevent speeds from being altered
  71. { "first_layer_speed", "100%" }
  72. });
  73. auto test = [](const DynamicPrintConfig &config) {
  74. GCodeReader parser;
  75. const double bridge_speed = config.opt_float("bridge_speed") * 60.;
  76. std::vector<double> E_per_mm;
  77. parser.parse_buffer(Slic3r::Test::slice({ Slic3r::Test::TestMesh::overhang }, config),
  78. [&E_per_mm, bridge_speed](Slic3r::GCodeReader &self, const Slic3r::GCodeReader::GCodeLine &line) {
  79. if (line.extruding(self) && line.dist_XY(self) > 0) {
  80. if (is_approx<double>(line.new_F(self), bridge_speed))
  81. E_per_mm.emplace_back(line.dist_E(self) / line.dist_XY(self));
  82. }
  83. });
  84. const double nozzle_dmr = config.opt<ConfigOptionFloats>("nozzle_diameter")->get_at(0);
  85. const double filament_dmr = config.opt<ConfigOptionFloats>("filament_diameter")->get_at(0);
  86. const double bridge_mm_per_mm = sqr(nozzle_dmr / filament_dmr) * config.opt_float("bridge_flow_ratio");
  87. size_t num_errors = std::count_if(E_per_mm.begin(), E_per_mm.end(),
  88. [bridge_mm_per_mm](double v){ return std::abs(v - bridge_mm_per_mm) > 0.01; });
  89. return num_errors == 0;
  90. };
  91. GIVEN("A default config with no cooling and a fixed bridge speed, flow ratio and an overhang mesh.") {
  92. WHEN("bridge_flow_ratio is set to 0.5 and extrusion width to default") {
  93. config.set_deserialize_strict({ { "bridge_flow_ratio", 0.5}, { "extrusion_width", "0" } });
  94. THEN("Output flow is as expected.") {
  95. REQUIRE(test(config));
  96. }
  97. }
  98. WHEN("bridge_flow_ratio is set to 2.0 and extrusion width to default") {
  99. config.set_deserialize_strict({ { "bridge_flow_ratio", 2.0}, { "extrusion_width", "0" } });
  100. THEN("Output flow is as expected.") {
  101. REQUIRE(test(config));
  102. }
  103. }
  104. WHEN("bridge_flow_ratio is set to 0.5 and extrusion_width to 0.4") {
  105. config.set_deserialize_strict({ { "bridge_flow_ratio", 0.5}, { "extrusion_width", 0.4 } });
  106. THEN("Output flow is as expected.") {
  107. REQUIRE(test(config));
  108. }
  109. }
  110. WHEN("bridge_flow_ratio is set to 1.0 and extrusion_width to 0.4") {
  111. config.set_deserialize_strict({ { "bridge_flow_ratio", 1.0}, { "extrusion_width", 0.4 } });
  112. THEN("Output flow is as expected.") {
  113. REQUIRE(test(config));
  114. }
  115. }
  116. WHEN("bridge_flow_ratio is set to 2 and extrusion_width to 0.4") {
  117. config.set_deserialize_strict({ { "bridge_flow_ratio", 2.}, { "extrusion_width", 0.4 } });
  118. THEN("Output flow is as expected.") {
  119. REQUIRE(test(config));
  120. }
  121. }
  122. }
  123. GIVEN("A default config with no cooling and a fixed bridge speed, flow ratio, fixed extrusion width of 0.4mm and an overhang mesh.") {
  124. WHEN("bridge_flow_ratio is set to 1.0") {
  125. THEN("Output flow is as expected.") {
  126. }
  127. }
  128. WHEN("bridge_flow_ratio is set to 0.5") {
  129. THEN("Output flow is as expected.") {
  130. }
  131. }
  132. WHEN("bridge_flow_ratio is set to 2.0") {
  133. THEN("Output flow is as expected.") {
  134. }
  135. }
  136. }
  137. }
  138. /// Test the expected behavior for auto-width,
  139. /// spacing, etc
  140. SCENARIO("Flow: Flow math for non-bridges", "[Flow]") {
  141. GIVEN("Nozzle Diameter of 0.4, a desired width of 1mm and layer height of 0.5") {
  142. ConfigOptionFloatOrPercent width(1.0, false);
  143. float nozzle_diameter = 0.4f;
  144. float layer_height = 0.4f;
  145. // Spacing for non-bridges is has some overlap
  146. THEN("External perimeter flow has spacing fixed to 1.125 * nozzle_diameter") {
  147. auto flow = Flow::new_from_config_width(frExternalPerimeter, ConfigOptionFloatOrPercent(0, false), nozzle_diameter, layer_height);
  148. REQUIRE(flow.spacing() == Approx(1.125 * nozzle_diameter - layer_height * (1.0 - PI / 4.0)));
  149. }
  150. THEN("Internal perimeter flow has spacing fixed to 1.125 * nozzle_diameter") {
  151. auto flow = Flow::new_from_config_width(frPerimeter, ConfigOptionFloatOrPercent(0, false), nozzle_diameter, layer_height);
  152. REQUIRE(flow.spacing() == Approx(1.125 *nozzle_diameter - layer_height * (1.0 - PI / 4.0)));
  153. }
  154. THEN("Spacing for supplied width is 0.8927f") {
  155. auto flow = Flow::new_from_config_width(frExternalPerimeter, width, nozzle_diameter, layer_height);
  156. REQUIRE(flow.spacing() == Approx(width.value - layer_height * (1.0 - PI / 4.0)));
  157. flow = Flow::new_from_config_width(frPerimeter, width, nozzle_diameter, layer_height);
  158. REQUIRE(flow.spacing() == Approx(width.value - layer_height * (1.0 - PI / 4.0)));
  159. }
  160. }
  161. /// Check the min/max
  162. GIVEN("Nozzle Diameter of 0.25") {
  163. float nozzle_diameter = 0.25f;
  164. float layer_height = 0.5f;
  165. WHEN("layer height is set to 0.2") {
  166. layer_height = 0.15f;
  167. THEN("Max width is set.") {
  168. auto flow = Flow::new_from_config_width(frPerimeter, ConfigOptionFloatOrPercent(0, false), nozzle_diameter, layer_height);
  169. REQUIRE(flow.width() == Approx(1.125 * nozzle_diameter));
  170. }
  171. }
  172. WHEN("Layer height is set to 0.25") {
  173. layer_height = 0.25f;
  174. THEN("Min width is set.") {
  175. auto flow = Flow::new_from_config_width(frPerimeter, ConfigOptionFloatOrPercent(0, false), nozzle_diameter, layer_height);
  176. REQUIRE(flow.width() == Approx(1.125 * nozzle_diameter));
  177. }
  178. }
  179. }
  180. #if 0
  181. /// Check for an edge case in the maths where the spacing could be 0; original
  182. /// math is 0.99. Slic3r issue #4654
  183. GIVEN("Input spacing of 0.414159 and a total width of 2") {
  184. double in_spacing = 0.414159;
  185. double total_width = 2.0;
  186. auto flow = Flow::new_from_spacing(1.0, 0.4, 0.3);
  187. WHEN("solid_spacing() is called") {
  188. double result = flow.solid_spacing(total_width, in_spacing);
  189. THEN("Yielded spacing is greater than 0") {
  190. REQUIRE(result > 0);
  191. }
  192. }
  193. }
  194. #endif
  195. }
  196. /// Spacing, width calculation for bridge extrusions
  197. SCENARIO("Flow: Flow math for bridges", "[Flow]") {
  198. GIVEN("Nozzle Diameter of 0.4, a desired width of 1mm and layer height of 0.5") {
  199. float nozzle_diameter = 0.4f;
  200. float bridge_flow = 1.0f;
  201. WHEN("Flow role is frExternalPerimeter") {
  202. auto flow = Flow::bridging_flow(nozzle_diameter * sqrt(bridge_flow), nozzle_diameter);
  203. THEN("Bridge width is same as nozzle diameter") {
  204. REQUIRE(flow.width() == Approx(nozzle_diameter));
  205. }
  206. THEN("Bridge spacing is same as nozzle diameter + BRIDGE_EXTRA_SPACING_MULT * nozzle_diameter") {
  207. REQUIRE(flow.spacing() == Approx(nozzle_diameter + BRIDGE_EXTRA_SPACING_MULT * nozzle_diameter));
  208. }
  209. }
  210. WHEN("Flow role is frInfill") {
  211. auto flow = Flow::new_from_config_width(frInfill, width, nozzle_diameter, layer_height, bridge_flow);
  212. THEN("Bridge width is same as nozzle diameter") {
  213. REQUIRE(flow.width == Approx(nozzle_diameter));
  214. }
  215. THEN("Bridge spacing is same as nozzle diameter + BRIDGE_EXTRA_SPACING_MULT * nozzle_diameter") {
  216. REQUIRE(flow.spacing() == Approx(nozzle_diameter + BRIDGE_EXTRA_SPACING_MULT * nozzle_diameter));
  217. }
  218. }
  219. WHEN("Flow role is frPerimeter") {
  220. auto flow = Flow::new_from_config_width(frPerimeter, width, nozzle_diameter, layer_height, bridge_flow);
  221. THEN("Bridge width is same as nozzle diameter") {
  222. REQUIRE(flow.width == Approx(nozzle_diameter));
  223. }
  224. THEN("Bridge spacing is same as nozzle diameter + BRIDGE_EXTRA_SPACING_MULT * nozzle_diameter") {
  225. REQUIRE(flow.spacing() == Approx(nozzle_diameter + BRIDGE_EXTRA_SPACING_MULT * nozzle_diameter));
  226. }
  227. }
  228. WHEN("Flow role is frSupportMaterial") {
  229. auto flow = Flow::new_from_config_width(frSupportMaterial, width, nozzle_diameter, layer_height, bridge_flow);
  230. THEN("Bridge width is same as nozzle diameter") {
  231. REQUIRE(flow.width == Approx(nozzle_diameter));
  232. }
  233. THEN("Bridge spacing is same as nozzle diameter + BRIDGE_EXTRA_SPACING_MULT * nozzle_diameter") {
  234. REQUIRE(flow.spacing() == Approx(nozzle_diameter + BRIDGE_EXTRA_SPACING_MULT * nozzle_diameter));
  235. }
  236. }
  237. }
  238. }