test_print.cpp 10 KB

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