test_3mf.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. bool ret = load_3mf(path.c_str(), &config, &model, false);
  14. THEN("load should succeed") {
  15. REQUIRE(ret);
  16. }
  17. }
  18. }
  19. }
  20. SCENARIO("Export+Import geometry to/from 3mf file cycle", "[3mf]") {
  21. GIVEN("world vertices coordinates before save") {
  22. // load a model from stl file
  23. Model src_model;
  24. std::string src_file = std::string(TEST_DATA_DIR) + "/test_3mf/Prusa.stl";
  25. load_stl(src_file.c_str(), &src_model);
  26. src_model.add_default_instances();
  27. ModelObject* src_object = src_model.objects[0];
  28. // apply generic transformation to the 1st volume
  29. Geometry::Transformation src_volume_transform;
  30. src_volume_transform.set_offset(Vec3d(10.0, 20.0, 0.0));
  31. src_volume_transform.set_rotation(Vec3d(Geometry::deg2rad(25.0), Geometry::deg2rad(35.0), Geometry::deg2rad(45.0)));
  32. src_volume_transform.set_scaling_factor(Vec3d(1.1, 1.2, 1.3));
  33. src_volume_transform.set_mirror(Vec3d(-1.0, 1.0, -1.0));
  34. src_object->volumes[0]->set_transformation(src_volume_transform);
  35. // apply generic transformation to the 1st instance
  36. Geometry::Transformation src_instance_transform;
  37. src_instance_transform.set_offset(Vec3d(5.0, 10.0, 0.0));
  38. src_instance_transform.set_rotation(Vec3d(Geometry::deg2rad(12.0), Geometry::deg2rad(13.0), Geometry::deg2rad(14.0)));
  39. src_instance_transform.set_scaling_factor(Vec3d(0.9, 0.8, 0.7));
  40. src_instance_transform.set_mirror(Vec3d(1.0, -1.0, -1.0));
  41. src_object->instances[0]->set_transformation(src_instance_transform);
  42. WHEN("model is saved+loaded to/from 3mf file") {
  43. // save the model to 3mf file
  44. std::string test_file = std::string(TEST_DATA_DIR) + "/test_3mf/prusa.3mf";
  45. store_3mf(test_file.c_str(), &src_model, nullptr, false);
  46. // load back the model from the 3mf file
  47. Model dst_model;
  48. DynamicPrintConfig dst_config;
  49. load_3mf(test_file.c_str(), &dst_config, &dst_model, false);
  50. boost::filesystem::remove(test_file);
  51. // compare meshes
  52. TriangleMesh src_mesh = src_model.mesh();
  53. src_mesh.repair();
  54. TriangleMesh dst_mesh = dst_model.mesh();
  55. dst_mesh.repair();
  56. bool res = src_mesh.its.vertices.size() == dst_mesh.its.vertices.size();
  57. if (res)
  58. {
  59. for (size_t i = 0; i < dst_mesh.its.vertices.size(); ++i)
  60. {
  61. res &= dst_mesh.its.vertices[i].isApprox(src_mesh.its.vertices[i]);
  62. if (!res)
  63. {
  64. Vec3f diff = dst_mesh.its.vertices[i] - src_mesh.its.vertices[i];
  65. std::cout << i << ": diff " << to_string((Vec3d)diff.cast<double>()) << "\n";
  66. }
  67. }
  68. }
  69. THEN("world vertices coordinates after load match") {
  70. REQUIRE(res);
  71. }
  72. }
  73. }
  74. }