test_3mf.cpp 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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[0];
  29. // apply generic transformation to the 1st volume
  30. Geometry::Transformation src_volume_transform;
  31. src_volume_transform.set_offset(Vec3d(10.0, 20.0, 0.0));
  32. src_volume_transform.set_rotation(Vec3d(Geometry::deg2rad(25.0), Geometry::deg2rad(35.0), Geometry::deg2rad(45.0)));
  33. src_volume_transform.set_scaling_factor(Vec3d(1.1, 1.2, 1.3));
  34. src_volume_transform.set_mirror(Vec3d(-1.0, 1.0, -1.0));
  35. src_object->volumes[0]->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(Vec3d(5.0, 10.0, 0.0));
  39. src_instance_transform.set_rotation(Vec3d(Geometry::deg2rad(12.0), Geometry::deg2rad(13.0), Geometry::deg2rad(14.0)));
  40. src_instance_transform.set_scaling_factor(Vec3d(0.9, 0.8, 0.7));
  41. src_instance_transform.set_mirror(Vec3d(1.0, -1.0, -1.0));
  42. src_object->instances[0]->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. {
  63. for (size_t i = 0; i < dst_mesh.its.vertices.size(); ++i)
  64. {
  65. res &= dst_mesh.its.vertices[i].isApprox(src_mesh.its.vertices[i]);
  66. if (!res)
  67. {
  68. Vec3f diff = dst_mesh.its.vertices[i] - src_mesh.its.vertices[i];
  69. std::cout << i << ": diff " << to_string((Vec3d)diff.cast<double>()) << "\n";
  70. }
  71. }
  72. }
  73. THEN("world vertices coordinates after load match") {
  74. REQUIRE(res);
  75. }
  76. }
  77. }
  78. }