test_flow.cpp 8.9 KB

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