test_flow.cpp 9.0 KB

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