test_meshboolean.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include <catch2/catch.hpp>
  2. #include <test_utils.hpp>
  3. #include <libslic3r/TriangleMesh.hpp>
  4. #include <libslic3r/MeshBoolean.hpp>
  5. using namespace Slic3r;
  6. TEST_CASE("CGAL and TriangleMesh conversions", "[MeshBoolean]") {
  7. TriangleMesh sphere = make_sphere(1.);
  8. auto cgalmesh_ptr = MeshBoolean::cgal::triangle_mesh_to_cgal(sphere);
  9. REQUIRE(cgalmesh_ptr);
  10. REQUIRE(! MeshBoolean::cgal::does_self_intersect(*cgalmesh_ptr));
  11. TriangleMesh M = MeshBoolean::cgal::cgal_to_triangle_mesh(*cgalmesh_ptr);
  12. REQUIRE(M.its.vertices.size() == sphere.its.vertices.size());
  13. REQUIRE(M.its.indices.size() == sphere.its.indices.size());
  14. REQUIRE(M.volume() == Approx(sphere.volume()));
  15. REQUIRE(! MeshBoolean::cgal::does_self_intersect(M));
  16. }
  17. Vec3d calc_normal(const Vec3i &triangle, const std::vector<Vec3f> &vertices)
  18. {
  19. Vec3d v0 = vertices[triangle[0]].cast<double>();
  20. Vec3d v1 = vertices[triangle[1]].cast<double>();
  21. Vec3d v2 = vertices[triangle[2]].cast<double>();
  22. // n = triangle normal
  23. Vec3d n = (v1 - v0).cross(v2 - v0);
  24. n.normalize();
  25. return n;
  26. }
  27. TEST_CASE("Add TriangleMeshes", "[MeshBoolean]")
  28. {
  29. TriangleMesh tm1 = make_sphere(1.6, 1.6);
  30. size_t init_size = tm1.its.indices.size();
  31. Vec3f move(5, -3, 7);
  32. move.normalize();
  33. tm1.translate(0.3 * move);
  34. //its_write_obj(tm1.its, "tm1.obj");
  35. TriangleMesh tm2 = make_cube(1., 1., 1.);
  36. //its_write_obj(tm2.its, "tm2.obj");
  37. MeshBoolean::cgal::plus(tm1, tm2);
  38. //its_write_obj(tm1.its, "test_add.obj");
  39. CHECK(tm1.its.indices.size() > init_size);
  40. }