test_extrusion_entity.cpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include <catch2/catch.hpp>
  2. #include <cstdlib>
  3. #include "libslic3r/ExtrusionEntityCollection.hpp"
  4. #include "libslic3r/ExtrusionEntity.hpp"
  5. #include "libslic3r/Point.hpp"
  6. #include "libslic3r/libslic3r.h"
  7. #include "test_data.hpp"
  8. using namespace Slic3r;
  9. static inline Slic3r::Point random_point(float LO=-50, float HI=50)
  10. {
  11. Vec2f pt = Vec2f(LO, LO) + (Vec2d(rand(), rand()) * (HI-LO) / RAND_MAX).cast<float>();
  12. return pt.cast<coord_t>();
  13. }
  14. // build a sample extrusion entity collection with random start and end points.
  15. static Slic3r::ExtrusionPath random_path(size_t length = 20, float LO = -50, float HI = 50)
  16. {
  17. ExtrusionPath t {erPerimeter, 1.0, 1.0, 1.0};
  18. for (size_t j = 0; j < length; ++ j)
  19. t.polyline.append(random_point(LO, HI));
  20. return t;
  21. }
  22. static Slic3r::ExtrusionPaths random_paths(size_t count = 10, size_t length = 20, float LO = -50, float HI = 50)
  23. {
  24. Slic3r::ExtrusionPaths p;
  25. for (size_t i = 0; i < count; ++ i)
  26. p.push_back(random_path(length, LO, HI));
  27. return p;
  28. }
  29. SCENARIO("ExtrusionEntityCollection: Polygon flattening", "[ExtrusionEntity]") {
  30. srand(0xDEADBEEF); // consistent seed for test reproducibility.
  31. // Generate one specific random path set and save it for later comparison
  32. Slic3r::ExtrusionPaths nosort_path_set = random_paths();
  33. Slic3r::ExtrusionEntityCollection sub_nosort;
  34. sub_nosort.append(nosort_path_set);
  35. sub_nosort.no_sort = true;
  36. Slic3r::ExtrusionEntityCollection sub_sort;
  37. sub_sort.no_sort = false;
  38. sub_sort.append(random_paths());
  39. GIVEN("A Extrusion Entity Collection with a child that has one child that is marked as no-sort") {
  40. Slic3r::ExtrusionEntityCollection sample;
  41. Slic3r::ExtrusionEntityCollection output;
  42. sample.append(sub_sort);
  43. sample.append(sub_nosort);
  44. sample.append(sub_sort);
  45. WHEN("The EEC is flattened with default options (preserve_order=false)") {
  46. output = sample.flatten();
  47. THEN("The output EEC contains no Extrusion Entity Collections") {
  48. CHECK(std::count_if(output.entities.cbegin(), output.entities.cend(), [=](const ExtrusionEntity* e) {return e->is_collection();}) == 0);
  49. }
  50. }
  51. WHEN("The EEC is flattened with preservation (preserve_order=true)") {
  52. output = sample.flatten(true);
  53. THEN("The output EECs contains one EEC.") {
  54. CHECK(std::count_if(output.entities.cbegin(), output.entities.cend(), [=](const ExtrusionEntity* e) {return e->is_collection();}) == 1);
  55. }
  56. AND_THEN("The ordered EEC contains the same order of elements than the original") {
  57. // find the entity in the collection
  58. for (auto e : output.entities)
  59. if (e->is_collection()) {
  60. ExtrusionEntityCollection *temp = dynamic_cast<ExtrusionEntityCollection*>(e);
  61. // check each Extrusion path against nosort_path_set to see if the first and last match the same
  62. CHECK(nosort_path_set.size() == temp->entities.size());
  63. for (size_t i = 0; i < nosort_path_set.size(); ++ i) {
  64. CHECK(temp->entities[i]->first_point() == nosort_path_set[i].first_point());
  65. CHECK(temp->entities[i]->last_point() == nosort_path_set[i].last_point());
  66. }
  67. }
  68. }
  69. }
  70. }
  71. }