test_extrusion_entity.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //#define CATCH_CONFIG_DISABLE
  2. #include <catch2/catch.hpp>
  3. #include <string>
  4. #include "test_data.hpp"
  5. #include <libslic3r/libslic3r.h>
  6. #include <libslic3r/ExtrusionEntityCollection.hpp>
  7. #include <libslic3r/ExtrusionEntity.hpp>
  8. #include <libslic3r/Point.hpp>
  9. #include <libslic3r/GCodeReader.hpp>
  10. #include <cstdlib>
  11. using namespace Slic3r;
  12. Slic3r::Point random_point(float LO=-50, float HI=50) {
  13. float x = LO + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(HI-LO)));
  14. float y = LO + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(HI-LO)));
  15. return Slic3r::Point(x, y);
  16. }
  17. // build a sample extrusion entity collection with random start and end points.
  18. Slic3r::ExtrusionPath random_path(size_t length = 20, float LO=-50, float HI=50) {
  19. Slic3r::ExtrusionPath t { Slic3r::ExtrusionRole::erPerimeter, 1.0, 1.0, 1.0};
  20. for (size_t j = 0; j < length; j++) {
  21. t.polyline.append(random_point(LO, HI));
  22. }
  23. return t;
  24. }
  25. Slic3r::ExtrusionPaths random_paths(size_t count=10, size_t length=20, float LO=-50, float HI=50) {
  26. Slic3r::ExtrusionPaths p;
  27. for (size_t i = 0; i < count; i++)
  28. p.push_back(random_path(length, LO, HI));
  29. return p;
  30. }
  31. using namespace Slic3r;
  32. SCENARIO("ExtrusionEntityCollection: Polygon flattening") {
  33. srand(0xDEADBEEF); // consistent seed for test reproducibility.
  34. // Generate one specific random path set and save it for later comparison
  35. Slic3r::ExtrusionPaths nosort_path_set {random_paths()};
  36. Slic3r::ExtrusionEntityCollection sub_nosort;
  37. sub_nosort.append(nosort_path_set);
  38. sub_nosort.no_sort = true;
  39. Slic3r::ExtrusionEntityCollection sub_sort;
  40. sub_sort.no_sort = false;
  41. sub_sort.append(random_paths());
  42. GIVEN("A Extrusion Entity Collection with a child that has one child that is marked as no-sort") {
  43. Slic3r::ExtrusionEntityCollection sample;
  44. sample.append(sub_sort);
  45. sample.append(sub_nosort);
  46. sample.append(sub_sort);
  47. WHEN("The EEC is flattened with default options (preserve_order=false)") {
  48. Slic3r::ExtrusionEntityCollection output = Slic3r::FlatenEntities(false).flatten(sample);
  49. THEN("The output EEC contains no Extrusion Entity Collections") {
  50. CHECK(std::count_if(output.entities.cbegin(), output.entities.cend(), [=](const ExtrusionEntity* e) {return e->is_collection();}) == 0);
  51. }
  52. }
  53. WHEN("The EEC is flattened with preservation (preserve_order=true)") {
  54. Slic3r::ExtrusionEntityCollection output = Slic3r::FlatenEntities(true).flatten(sample);
  55. THEN("The output EECs contains one EEC.") {
  56. CHECK(std::count_if(output.entities.cbegin(), output.entities.cend(), [=](const ExtrusionEntity* e) {return e->is_collection();}) == 1);
  57. }
  58. AND_THEN("The ordered EEC contains the same order of elements than the original") {
  59. // find the entity in the collection
  60. for (auto e : output.entities) {
  61. if (!e->is_collection()) continue;
  62. Slic3r::ExtrusionEntityCollection* temp = dynamic_cast<ExtrusionEntityCollection*>(e);
  63. // check each Extrusion path against nosort_path_set to see if the first and last match the same
  64. CHECK(nosort_path_set.size() == temp->entities.size());
  65. for (size_t i = 0; i < nosort_path_set.size(); i++) {
  66. CHECK(temp->entities[i]->first_point() == nosort_path_set[i].first_point());
  67. CHECK(temp->entities[i]->last_point() == nosort_path_set[i].last_point());
  68. }
  69. }
  70. }
  71. }
  72. }
  73. }
  74. SCENARIO("ExtrusionEntityCollection: no sort") {
  75. DynamicPrintConfig &config = Slic3r::DynamicPrintConfig::full_print_config();
  76. config.set_key_value("gcode_comments", new ConfigOptionBool(true));
  77. config.set_deserialize("skirts", "0");
  78. Model model{};
  79. Print print{};
  80. Slic3r::Test::init_print(print, { Slic3r::Test::TestMesh::cube_20x20x20}, model, &config);
  81. std::map<double, bool> layers_with_skirt;
  82. std::map<double, bool> layers_with_brim;
  83. std::string gcode_filepath{ "" };
  84. print.process();
  85. //replace extrusion from sliceing by manual ones
  86. print.objects()[0]->clear_layers();
  87. Layer* customL_layer = print.objects()[0]->add_layer(0, 0.2, 0.2, 0.1);
  88. LayerRegion* custom_region = customL_layer->add_region(print.regions()[0]);
  89. ExtrusionPath path_peri(ExtrusionRole::erPerimeter);
  90. path_peri.polyline.append(Point{ 0,0 });
  91. path_peri.polyline.append(Point{ scale_(1),scale_(0) });
  92. ExtrusionPath path_fill1(ExtrusionRole::erInternalInfill);
  93. path_fill1.polyline.append(Point{ scale_(1),scale_(0) });
  94. path_fill1.polyline.append(Point{ scale_(2),scale_(0) });
  95. ExtrusionPath path_fill2(ExtrusionRole::erInternalInfill);
  96. path_fill2.polyline.append(Point{ scale_(2),scale_(0) });
  97. path_fill2.polyline.append(Point{ scale_(3),scale_(0) });
  98. ExtrusionEntityCollection coll_fill;
  99. coll_fill.append(path_fill2);
  100. coll_fill.append(path_fill1);
  101. ExtrusionEntityCollection coll_peri;
  102. coll_peri.append(path_peri);
  103. WHEN("sort") {
  104. custom_region->fills.append(coll_fill);
  105. custom_region->perimeters.append(coll_peri);
  106. coll_fill.no_sort = false;
  107. Slic3r::Test::gcode(gcode_filepath, print);
  108. auto parser{ Slic3r::GCodeReader() };
  109. std::vector<float> extrude_x;
  110. parser.parse_file(gcode_filepath, [&extrude_x](Slic3r::GCodeReader& self, const Slic3r::GCodeReader::GCodeLine& line)
  111. {
  112. if (line.comment() == " infill" || line.comment() == " perimeter" || line.comment() == " move to first infill point") {
  113. extrude_x.push_back(line.x());
  114. }
  115. });
  116. Slic3r::Test::clean_file(gcode_filepath, "gcode");
  117. REQUIRE(extrude_x.size()==3);
  118. REQUIRE(extrude_x[0] == 91);
  119. REQUIRE(extrude_x[1] == 92);
  120. REQUIRE(extrude_x[2] == 93);
  121. }
  122. WHEN("no sort") {
  123. coll_fill.no_sort = true;
  124. custom_region->fills.append(coll_fill);
  125. custom_region->perimeters.append(coll_peri);
  126. Slic3r::Test::gcode(gcode_filepath, print);
  127. auto parser{ Slic3r::GCodeReader() };
  128. std::vector<float> extrude_x;
  129. parser.parse_file(gcode_filepath, [&extrude_x](Slic3r::GCodeReader& self, const Slic3r::GCodeReader::GCodeLine& line)
  130. {
  131. if (line.comment() == " infill" || line.comment() == " perimeter" || line.comment() == " move to first infill point") {
  132. extrude_x.push_back(line.x());
  133. }
  134. });
  135. Slic3r::Test::clean_file(gcode_filepath, "gcode");
  136. REQUIRE(extrude_x.size() == 5);
  137. REQUIRE(extrude_x[0] == 91);
  138. REQUIRE(extrude_x[1] == 92);
  139. REQUIRE(extrude_x[2] == 93);
  140. REQUIRE(extrude_x[3] == 91);
  141. REQUIRE(extrude_x[4] == 92);
  142. }
  143. }