test_flow.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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", "100%" }
  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 bridge_flow = 0.f;
  91. float layer_height = 0.5f;
  92. // Spacing for non-bridges is has some overlap
  93. THEN("External perimeter flow has spacing fixed to 1.125 * nozzle_diameter") {
  94. auto flow = Flow::new_from_config_width(frExternalPerimeter, ConfigOptionFloatOrPercent(0, false), nozzle_diameter, layer_height, bridge_flow);
  95. REQUIRE(flow.spacing() == Approx(1.125 * nozzle_diameter - layer_height * (1.0 - PI / 4.0)));
  96. }
  97. THEN("Internal perimeter flow has spacing fixed to 1.125 * nozzle_diameter") {
  98. auto flow = Flow::new_from_config_width(frPerimeter, ConfigOptionFloatOrPercent(0, false), nozzle_diameter, layer_height, bridge_flow);
  99. REQUIRE(flow.spacing() == Approx(1.125 *nozzle_diameter - layer_height * (1.0 - PI / 4.0)));
  100. }
  101. THEN("Spacing for supplied width is 0.8927f") {
  102. auto flow = Flow::new_from_config_width(frExternalPerimeter, width, nozzle_diameter, layer_height, bridge_flow);
  103. REQUIRE(flow.spacing() == Approx(width.value - layer_height * (1.0 - PI / 4.0)));
  104. flow = Flow::new_from_config_width(frPerimeter, width, nozzle_diameter, layer_height, bridge_flow);
  105. REQUIRE(flow.spacing() == Approx(width.value - layer_height * (1.0 - PI / 4.0)));
  106. }
  107. }
  108. /// Check the min/max
  109. GIVEN("Nozzle Diameter of 0.25") {
  110. float nozzle_diameter = 0.25f;
  111. float bridge_flow = 0.f;
  112. float layer_height = 0.5f;
  113. WHEN("layer height is set to 0.2") {
  114. layer_height = 0.15f;
  115. THEN("Max width is set.") {
  116. auto flow = Flow::new_from_config_width(frPerimeter, ConfigOptionFloatOrPercent(0, false), nozzle_diameter, layer_height, bridge_flow);
  117. REQUIRE(flow.width == Approx(1.125 * nozzle_diameter));
  118. }
  119. }
  120. WHEN("Layer height is set to 0.2") {
  121. layer_height = 0.3f;
  122. THEN("Min width is set.") {
  123. auto flow = Flow::new_from_config_width(frPerimeter, ConfigOptionFloatOrPercent(0, false), nozzle_diameter, layer_height, bridge_flow);
  124. REQUIRE(flow.width == Approx(1.125 * nozzle_diameter));
  125. }
  126. }
  127. }
  128. #if 0
  129. /// Check for an edge case in the maths where the spacing could be 0; original
  130. /// math is 0.99. Slic3r issue #4654
  131. GIVEN("Input spacing of 0.414159 and a total width of 2") {
  132. double in_spacing = 0.414159;
  133. double total_width = 2.0;
  134. auto flow = Flow::new_from_spacing(1.0, 0.4, 0.3, false);
  135. WHEN("solid_spacing() is called") {
  136. double result = flow.solid_spacing(total_width, in_spacing);
  137. THEN("Yielded spacing is greater than 0") {
  138. REQUIRE(result > 0);
  139. }
  140. }
  141. }
  142. #endif
  143. }
  144. /// Spacing, width calculation for bridge extrusions
  145. SCENARIO("Flow: Flow math for bridges", "[Flow]") {
  146. GIVEN("Nozzle Diameter of 0.4, a desired width of 1mm and layer height of 0.5") {
  147. auto width = ConfigOptionFloatOrPercent(1.0, false);
  148. float nozzle_diameter = 0.4f;
  149. float bridge_flow = 1.0f;
  150. float layer_height = 0.5f;
  151. WHEN("Flow role is frExternalPerimeter") {
  152. auto flow = Flow::new_from_config_width(frExternalPerimeter, width, nozzle_diameter, layer_height, bridge_flow);
  153. THEN("Bridge width is same as nozzle diameter") {
  154. REQUIRE(flow.width == Approx(nozzle_diameter));
  155. }
  156. THEN("Bridge spacing is same as nozzle diameter + BRIDGE_EXTRA_SPACING") {
  157. REQUIRE(flow.spacing() == Approx(nozzle_diameter + BRIDGE_EXTRA_SPACING));
  158. }
  159. }
  160. WHEN("Flow role is frInfill") {
  161. auto flow = Flow::new_from_config_width(frInfill, width, nozzle_diameter, layer_height, bridge_flow);
  162. THEN("Bridge width is same as nozzle diameter") {
  163. REQUIRE(flow.width == Approx(nozzle_diameter));
  164. }
  165. THEN("Bridge spacing is same as nozzle diameter + BRIDGE_EXTRA_SPACING") {
  166. REQUIRE(flow.spacing() == Approx(nozzle_diameter + BRIDGE_EXTRA_SPACING));
  167. }
  168. }
  169. WHEN("Flow role is frPerimeter") {
  170. auto flow = Flow::new_from_config_width(frPerimeter, width, nozzle_diameter, layer_height, bridge_flow);
  171. THEN("Bridge width is same as nozzle diameter") {
  172. REQUIRE(flow.width == Approx(nozzle_diameter));
  173. }
  174. THEN("Bridge spacing is same as nozzle diameter + BRIDGE_EXTRA_SPACING") {
  175. REQUIRE(flow.spacing() == Approx(nozzle_diameter + BRIDGE_EXTRA_SPACING));
  176. }
  177. }
  178. WHEN("Flow role is frSupportMaterial") {
  179. auto flow = Flow::new_from_config_width(frSupportMaterial, width, nozzle_diameter, layer_height, bridge_flow);
  180. THEN("Bridge width is same as nozzle diameter") {
  181. REQUIRE(flow.width == Approx(nozzle_diameter));
  182. }
  183. THEN("Bridge spacing is same as nozzle diameter + BRIDGE_EXTRA_SPACING") {
  184. REQUIRE(flow.spacing() == Approx(nozzle_diameter + BRIDGE_EXTRA_SPACING));
  185. }
  186. }
  187. }
  188. }