test_3mf.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include <catch2/catch.hpp>
  2. #include "libslic3r/Model.hpp"
  3. #include "libslic3r/Format/3mf.hpp"
  4. #include "libslic3r/Format/STL.hpp"
  5. #include <boost/filesystem/operations.hpp>
  6. using namespace Slic3r;
  7. SCENARIO("Reading 3mf file", "[3mf]") {
  8. GIVEN("umlauts in the path of the file") {
  9. Model model;
  10. WHEN("3mf model is read") {
  11. std::string path = std::string(TEST_DATA_DIR) + "/test_3mf/Geräte/Büchse.3mf";
  12. DynamicPrintConfig config;
  13. ConfigSubstitutionContext ctxt{ ForwardCompatibilitySubstitutionRule::Disable };
  14. bool ret = load_3mf(path.c_str(), config, ctxt, &model, false);
  15. THEN("load should succeed") {
  16. REQUIRE(ret);
  17. }
  18. }
  19. }
  20. }
  21. SCENARIO("Export+Import geometry to/from 3mf file cycle", "[3mf]") {
  22. GIVEN("world vertices coordinates before save") {
  23. // load a model from stl file
  24. Model src_model;
  25. std::string src_file = std::string(TEST_DATA_DIR) + "/test_3mf/Prusa.stl";
  26. load_stl(src_file.c_str(), &src_model);
  27. src_model.add_default_instances();
  28. ModelObject* src_object = src_model.objects.front();
  29. // apply generic transformation to the 1st volume
  30. Geometry::Transformation src_volume_transform;
  31. src_volume_transform.set_offset({ 10.0, 20.0, 0.0 });
  32. src_volume_transform.set_rotation({ Geometry::deg2rad(25.0), Geometry::deg2rad(35.0), Geometry::deg2rad(45.0) });
  33. src_volume_transform.set_scaling_factor({ 1.1, 1.2, 1.3 });
  34. src_volume_transform.set_mirror({ -1.0, 1.0, -1.0 });
  35. src_object->volumes.front()->set_transformation(src_volume_transform);
  36. // apply generic transformation to the 1st instance
  37. Geometry::Transformation src_instance_transform;
  38. src_instance_transform.set_offset({ 5.0, 10.0, 0.0 });
  39. src_instance_transform.set_rotation({ Geometry::deg2rad(12.0), Geometry::deg2rad(13.0), Geometry::deg2rad(14.0) });
  40. src_instance_transform.set_scaling_factor({ 0.9, 0.8, 0.7 });
  41. src_instance_transform.set_mirror({ 1.0, -1.0, -1.0 });
  42. src_object->instances.front()->set_transformation(src_instance_transform);
  43. WHEN("model is saved+loaded to/from 3mf file") {
  44. // save the model to 3mf file
  45. std::string test_file = std::string(TEST_DATA_DIR) + "/test_3mf/prusa.3mf";
  46. store_3mf(test_file.c_str(), &src_model, nullptr, false);
  47. // load back the model from the 3mf file
  48. Model dst_model;
  49. DynamicPrintConfig dst_config;
  50. {
  51. ConfigSubstitutionContext ctxt{ ForwardCompatibilitySubstitutionRule::Disable };
  52. load_3mf(test_file.c_str(), dst_config, ctxt, &dst_model, false);
  53. }
  54. boost::filesystem::remove(test_file);
  55. // compare meshes
  56. TriangleMesh src_mesh = src_model.mesh();
  57. src_mesh.repair();
  58. TriangleMesh dst_mesh = dst_model.mesh();
  59. dst_mesh.repair();
  60. bool res = src_mesh.its.vertices.size() == dst_mesh.its.vertices.size();
  61. if (res) {
  62. for (size_t i = 0; i < dst_mesh.its.vertices.size(); ++i) {
  63. res &= dst_mesh.its.vertices[i].isApprox(src_mesh.its.vertices[i]);
  64. }
  65. }
  66. THEN("world vertices coordinates after load match") {
  67. REQUIRE(res);
  68. }
  69. }
  70. }
  71. }
  72. SCENARIO("2D convex hull of sinking object", "[3mf]") {
  73. GIVEN("model") {
  74. // load a model
  75. Model model;
  76. std::string src_file = std::string(TEST_DATA_DIR) + "/test_3mf/Prusa.stl";
  77. load_stl(src_file.c_str(), &model);
  78. model.add_default_instances();
  79. WHEN("model is rotated, scaled and set as sinking") {
  80. ModelObject* object = model.objects.front();
  81. object->center_around_origin(false);
  82. // set instance's attitude so that it is rotated, scaled and sinking
  83. ModelInstance* instance = object->instances.front();
  84. instance->set_rotation(X, -M_PI / 4.0);
  85. instance->set_offset(Vec3d::Zero());
  86. instance->set_scaling_factor({ 2.0, 2.0, 2.0 });
  87. // calculate 2D convex hull
  88. Polygon hull_2d = object->convex_hull_2d(instance->get_transformation().get_matrix());
  89. // verify result
  90. Points result = {
  91. { -91501496, -15914144 },
  92. { 91501496, -15914144 },
  93. { 91501496, 4243 },
  94. { 78229680, 4246883 },
  95. { 56898100, 4246883 },
  96. { -85501496, 4242641 },
  97. { -91501496, 4243 }
  98. };
  99. bool res = hull_2d.points == result;
  100. THEN("2D convex hull should match with reference") {
  101. REQUIRE(res);
  102. }
  103. }
  104. }
  105. }