test_3mf.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. TriangleMesh dst_mesh = dst_model.mesh();
  58. bool res = src_mesh.its.vertices.size() == dst_mesh.its.vertices.size();
  59. if (res) {
  60. for (size_t i = 0; i < dst_mesh.its.vertices.size(); ++i) {
  61. res &= dst_mesh.its.vertices[i].isApprox(src_mesh.its.vertices[i]);
  62. }
  63. }
  64. THEN("world vertices coordinates after load match") {
  65. REQUIRE(res);
  66. }
  67. }
  68. }
  69. }
  70. SCENARIO("2D convex hull of sinking object", "[3mf]") {
  71. GIVEN("model") {
  72. // load a model
  73. Model model;
  74. std::string src_file = std::string(TEST_DATA_DIR) + "/test_3mf/Prusa.stl";
  75. load_stl(src_file.c_str(), &model);
  76. model.add_default_instances();
  77. WHEN("model is rotated, scaled and set as sinking") {
  78. ModelObject* object = model.objects.front();
  79. object->center_around_origin(false);
  80. // set instance's attitude so that it is rotated, scaled and sinking
  81. ModelInstance* instance = object->instances.front();
  82. instance->set_rotation(X, -M_PI / 4.0);
  83. instance->set_offset(Vec3d::Zero());
  84. instance->set_scaling_factor({ 2.0, 2.0, 2.0 });
  85. // calculate 2D convex hull
  86. Polygon hull_2d = object->convex_hull_2d(instance->get_transformation().get_matrix());
  87. // verify result
  88. Points result = {
  89. { -91501496, -15914144 },
  90. { 91501496, -15914144 },
  91. { 91501496, 4243 },
  92. { 78229680, 4246883 },
  93. { 56898100, 4246883 },
  94. { -85501496, 4242641 },
  95. { -91501496, 4243 }
  96. };
  97. // Allow 1um error due to floating point rounding.
  98. bool res = hull_2d.points.size() == result.size();
  99. if (res)
  100. for (size_t i = 0; i < result.size(); ++ i) {
  101. const Point &p1 = result[i];
  102. const Point &p2 = hull_2d.points[i];
  103. if (std::abs(p1.x() - p2.x()) > 1 || std::abs(p1.y() - p2.y()) > 1) {
  104. res = false;
  105. break;
  106. }
  107. }
  108. THEN("2D convex hull should match with reference") {
  109. REQUIRE(res);
  110. }
  111. }
  112. }
  113. }