test_print.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //#define CATCH_CONFIG_DISABLE
  2. //#include <catch_main.hpp>
  3. #include <catch2/catch.hpp>
  4. #include "test_data.hpp"
  5. #include <libslic3r/libslic3r.h>
  6. #include <libslic3r/Layer.hpp>
  7. #include <libslic3r/SVG.hpp>
  8. //#include <libslic3r/config.hpp>
  9. #include <string>
  10. using namespace Slic3r;
  11. using namespace Slic3r::Test;
  12. using namespace std::literals;
  13. SCENARIO("PrintObject: Perimeter generation") {
  14. GIVEN("20mm cube and default config & 0.3 layer height") {
  15. DynamicPrintConfig &config = Slic3r::DynamicPrintConfig::full_print_config();
  16. TestMesh m = TestMesh::cube_20x20x20;
  17. Model model{};
  18. config.set_key_value("fill_density", new ConfigOptionPercent(0));
  19. config.set_deserialize("nozzle_diameter", "0.4");
  20. config.set_deserialize("layer_height", "0.3");
  21. WHEN("make_perimeters() is called") {
  22. Print print{};
  23. Slic3r::Test::init_print(print, { m }, model, &config);
  24. PrintObject& object = *(print.objects().at(0));
  25. print.process();
  26. // there are 66.66666.... layers for 0.3mm in 20mm
  27. //slic3r is rounded (slice at half-layer), slic3rPE is less?
  28. //TODO: check the slic32r why it's not cut at half-layer
  29. THEN("67 layers exist in the model") {
  30. REQUIRE(object.layers().size() == 67);
  31. }
  32. THEN("Every layer in region 0 has 1 island of perimeters") {
  33. for(Layer* layer : object.layers()) {
  34. REQUIRE(layer->regions()[0]->perimeters.entities.size() == 1);
  35. }
  36. }
  37. THEN("Every layer (but top) in region 0 has 3 paths in its perimeters list.") {
  38. LayerPtrs layers = object.layers();
  39. for (auto it_layer = layers.begin(); it_layer != layers.end() - 1; ++it_layer) {
  40. REQUIRE((*it_layer)->regions()[0]->perimeters.items_count() == 3);
  41. }
  42. }
  43. THEN("Top layer in region 0 has 1 path in its perimeters list (only 1 perimeter on top).") {
  44. REQUIRE(object.layers().back()->regions()[0]->perimeters.items_count() == 1);
  45. }
  46. }
  47. }
  48. }
  49. SCENARIO("Print: Skirt generation") {
  50. GIVEN("20mm cube and default config") {
  51. DynamicPrintConfig &config = Slic3r::DynamicPrintConfig::full_print_config();
  52. TestMesh m = TestMesh::cube_20x20x20;
  53. Slic3r::Model model{};
  54. config.set_key_value("skirt_height", new ConfigOptionInt(1));
  55. config.set_key_value("skirt_distance", new ConfigOptionFloat(1));
  56. WHEN("Skirts is set to 2 loops") {
  57. config.set_key_value("skirts", new ConfigOptionInt(2));
  58. Print print{};
  59. Slic3r::Test::init_print(print, { m }, model, &config);
  60. print.process();
  61. THEN("Skirt Extrusion collection has 2 loops in it") {
  62. REQUIRE(print.skirt().items_count() == 2);
  63. REQUIRE(print.skirt().flatten().entities.size() == 2);
  64. }
  65. }
  66. }
  67. }
  68. void test_is_solid_infill(Print &p, size_t obj_id, size_t layer_id ) {
  69. const PrintObject& obj { *(p.objects().at(obj_id)) };
  70. const Layer& layer { *(obj.get_layer((int)layer_id)) };
  71. // iterate over all of the regions in the layer
  72. for (const LayerRegion* reg : layer.regions()) {
  73. // for each region, iterate over the fill surfaces
  74. for (const Surface& su : reg->fill_surfaces.surfaces) {
  75. CHECK(su.has_fill_solid());
  76. }
  77. }
  78. }
  79. SCENARIO("Print: Changing number of solid surfaces does not cause all surfaces to become internal.") {
  80. GIVEN("sliced 20mm cube and config with top_solid_surfaces = 2 and bottom_solid_surfaces = 1") {
  81. DynamicPrintConfig &config = Slic3r::DynamicPrintConfig::full_print_config();
  82. TestMesh m { TestMesh::cube_20x20x20 };
  83. config.set_key_value("top_solid_layers", new ConfigOptionInt(2));
  84. config.set_key_value("bottom_solid_layers", new ConfigOptionInt(1));
  85. config.set_key_value("layer_height", new ConfigOptionFloat(0.5)); // get a known number of layers
  86. config.set_key_value("first_layer_height", new ConfigOptionFloatOrPercent(0.5, false));
  87. config.set_key_value("enforce_full_fill_volume", new ConfigOptionBool(true));
  88. Slic3r::Model model;
  89. auto event_counter {0U};
  90. std::string stage;
  91. Print print{};
  92. Slic3r::Test::init_print(print, { m }, model, &config);
  93. print.process();
  94. // Precondition: Ensure that the model has 2 solid top layers (39, 38)
  95. // and one solid bottom layer (0).
  96. test_is_solid_infill(print, 0, 0); // should be solid
  97. test_is_solid_infill(print, 0, 39); // should be solid
  98. test_is_solid_infill(print, 0, 38); // should be solid
  99. WHEN("Model is re-sliced with top_solid_layers == 3") {
  100. ((ConfigOptionInt&)(print.regions()[0]->config().top_solid_layers)).value = 3;
  101. print.invalidate_state_by_config_options(std::vector<Slic3r::t_config_option_key>{ "posPrepareInfill" });
  102. print.process();
  103. THEN("Print object does not have 0 solid bottom layers.") {
  104. test_is_solid_infill(print, 0, 0);
  105. }
  106. AND_THEN("Print object has 3 top solid layers") {
  107. test_is_solid_infill(print, 0, 39);
  108. test_is_solid_infill(print, 0, 38);
  109. test_is_solid_infill(print, 0, 37);
  110. }
  111. }
  112. }
  113. }
  114. SCENARIO("Print: Brim generation") {
  115. GIVEN("20mm cube and default config, 1mm first layer width") {
  116. DynamicPrintConfig &config = Slic3r::DynamicPrintConfig::full_print_config();
  117. TestMesh m{ TestMesh::cube_20x20x20 };
  118. Slic3r::Model model{};
  119. config.set_key_value("first_layer_extrusion_width", new ConfigOptionFloatOrPercent(1, false));
  120. WHEN("Brim is set to 3mm") {
  121. config.set_key_value("brim_width", new ConfigOptionFloat(3));
  122. Print print{};
  123. Slic3r::Test::init_print(print, { m }, model, &config);
  124. print.process();
  125. //{
  126. // std::stringstream stri;
  127. // stri << "20mm_cube_brim_test_" << ".svg";
  128. // SVG svg(stri.str());
  129. // svg.draw(print.brim().as_polylines(), "red");
  130. // svg.Close();
  131. //}
  132. THEN("Brim Extrusion collection has 3 loops in it") {
  133. REQUIRE(print.brim().items_count() == 3);
  134. }
  135. }
  136. WHEN("Brim is set to 6mm") {
  137. config.set_key_value("brim_width", new ConfigOptionFloat(6));
  138. Print print{};
  139. Slic3r::Test::init_print(print, { m }, model, &config);
  140. print.process();
  141. THEN("Brim Extrusion collection has 6 loops in it") {
  142. REQUIRE(print.brim().items_count() == 6);
  143. }
  144. }
  145. WHEN("Brim is set to 6mm with 1mm offset") {
  146. config.set_key_value("brim_width", new ConfigOptionFloat(6));
  147. config.set_key_value("brim_offset", new ConfigOptionFloat(1));
  148. Print print{};
  149. Slic3r::Test::init_print(print, { m }, model, &config);
  150. print.process();
  151. THEN("Brim Extrusion collection has 5 loops in it") {
  152. REQUIRE(print.brim().items_count() == 5);
  153. }
  154. }
  155. WHEN("Brim without first layer compensation") {
  156. config.set_key_value("brim_width", new ConfigOptionFloat(1));
  157. config.set_key_value("brim_offset", new ConfigOptionFloat(0));
  158. Print print{};
  159. Slic3r::Test::init_print(print, { m }, model, &config);
  160. print.process();
  161. THEN("First Brim Extrusion has a length of ~88") {
  162. REQUIRE(print.brim().entities.size() > 0);
  163. double dist = unscaled(ExtrusionLength{}.length(*print.brim().entities.front()));
  164. REQUIRE(dist > 22*4);
  165. REQUIRE(dist < 22*4+1);
  166. }
  167. }
  168. WHEN("Brim with 1mm first layer compensation") {
  169. config.set_key_value("brim_width", new ConfigOptionFloat(1));
  170. config.set_key_value("brim_offset", new ConfigOptionFloat(0));
  171. config.set_key_value("first_layer_size_compensation", new ConfigOptionFloat(-1));
  172. Print print{};
  173. Slic3r::Test::init_print(print, { m }, model, &config);
  174. print.process();
  175. THEN("First Brim Extrusion has a length of ~80") {
  176. REQUIRE(print.brim().entities.size() > 0);
  177. double dist = unscaled(ExtrusionLength{}.length(*print.brim().entities.front()));
  178. REQUIRE(dist > 20 * 4);
  179. REQUIRE(dist < 20 * 4 + 1);
  180. }
  181. }
  182. WHEN("Brim is set to 6mm, extrusion width 0.5mm") {
  183. config.set_key_value("brim_width", new ConfigOptionFloat(6));
  184. config.set_key_value("first_layer_extrusion_width", new ConfigOptionFloatOrPercent(0.5, false));
  185. Print print{};
  186. Slic3r::Test::init_print(print, { m }, model, &config);
  187. print.process();
  188. double nbLoops = 6.0 / print.brim_flow(print.extruders().front()).spacing();
  189. THEN("Brim Extrusion collection has " + std::to_string(nbLoops) + " loops in it (flow="+ std::to_string(print.brim_flow(print.extruders().front()).spacing())+")") {
  190. REQUIRE(print.brim().items_count() == floor(nbLoops));
  191. }
  192. }
  193. WHEN("Brim ears activated, 3mm") {
  194. config.set_key_value("brim_width", new ConfigOptionFloat(3));
  195. config.set_key_value("brim_ears", new ConfigOptionBool(true));
  196. Print print{};
  197. Slic3r::Test::init_print(print, { m }, model, &config);
  198. print.process();
  199. THEN("Brim ears Extrusion collection has 4 extrusions in it") {
  200. REQUIRE(print.brim().items_count() == 4);
  201. }
  202. }
  203. }
  204. }
  205. SCENARIO("Print: perimeter generation") {
  206. GIVEN("cube with hole, just enough space for two loops at a point") {
  207. DynamicPrintConfig& config = Slic3r::DynamicPrintConfig::full_print_config();
  208. Slic3r::Model model{};
  209. config.set_key_value("first_layer_extrusion_width", new ConfigOptionFloatOrPercent(0.42, false));
  210. config.set_deserialize("nozzle_diameter", "0.4");
  211. config.set_deserialize("layer_height", "0.2");
  212. config.set_deserialize("first_layer_height", "0.2");
  213. config.set_key_value("only_one_perimeter_top", new ConfigOptionBool(false));
  214. Print print{};
  215. auto facets = std::vector<Vec3i32>{
  216. Vec3i32(1,4,3),Vec3i32(4,1,2),Vec3i32(16,12,14),Vec3i32(16,10,12),Vec3i32(10,4,6),Vec3i32(4,10,16),Vec3i32(8,14,12),Vec3i32(8,2,14),
  217. Vec3i32(6,2,8),Vec3i32(2,6,4),Vec3i32(14,15,16),Vec3i32(15,14,13),Vec3i32(15,4,16),Vec3i32(4,15,3),Vec3i32(13,11,15),Vec3i32(13,7,11),
  218. Vec3i32(7,1,5),Vec3i32(1,7,13),Vec3i32(9,15,11),Vec3i32(9,3,15),Vec3i32(5,3,9),Vec3i32(3,5,1),Vec3i32(1,14,2),Vec3i32(14,1,13),
  219. Vec3i32(9,12,10),Vec3i32(12,9,11),Vec3i32(6,9,10),Vec3i32(9,6,5),Vec3i32(8,5,6),Vec3i32(5,8,7),Vec3i32(7,12,11),Vec3i32(12,7,8)
  220. };
  221. for (Vec3i32& vec : facets)
  222. vec -= Vec3i32(1, 1, 1);
  223. TriangleMesh tm = TriangleMesh{ std::vector<Vec3d>{Vec3d(-5,-5,-0.1),Vec3d(-5,-5,0.1),Vec3d(-5,5,-0.1),Vec3d(-5,5,0.1),
  224. Vec3d(-1.328430,0,-0.1),Vec3d(-1.328430,0,0.1),Vec3d(1.5,-2.828430,-0.1),Vec3d(1.5,-2.828430,0.1),
  225. Vec3d(1.5,2.828430,-0.1),Vec3d(1.5,2.828430,0.1),Vec3d(4.328430,0,-0.1),Vec3d(4.328430,0,0.1),
  226. Vec3d(5,-5,-0.1),Vec3d(5,-5,0.1),Vec3d(5,5,-0.1),Vec3d(5,5,0.1)},
  227. facets };
  228. Slic3r::Test::init_print(print, {tm}, model, &config);
  229. print.process();
  230. THEN("hole perimeter should not be printed first") {
  231. ExtrusionEntity* loop = print.objects()[0]->layers()[0]->regions()[0]->perimeters.entities[0];
  232. REQUIRE(loop->is_collection());
  233. loop = ((ExtrusionEntityCollection*)loop)->entities.front();
  234. REQUIRE(loop->is_loop());
  235. // what we don't want in first is something that is (((ExtrusionLoop*)loop)->loop_role() & ExtrusionLoopRole::elrHole) != 0 && loop->role() == elrExternalPerimeter
  236. REQUIRE( (((ExtrusionLoop*)loop)->loop_role() & ExtrusionLoopRole::elrHole) == 0);
  237. }
  238. }
  239. }