test_extrusion_entity.cpp 7.0 KB

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