mesh_boolean.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include <test_common.h>
  2. #include <vector>
  3. #include <igl/copyleft/cgal/mesh_boolean.h>
  4. #include <igl/MeshBooleanType.h>
  5. #include <igl/exterior_edges.h>
  6. #include <igl/is_vertex_manifold.h>
  7. #include <igl/unique_edge_map.h>
  8. #include <igl/is_edge_manifold.h>
  9. namespace {
  10. template<typename DerivedF>
  11. void assert_no_exterior_edges(
  12. const Eigen::PlainObjectBase<DerivedF>& F) {
  13. Eigen::MatrixXi Eb;
  14. igl::exterior_edges(F, Eb);
  15. REQUIRE (Eb.rows() == 0);
  16. }
  17. template<typename DerivedV, typename DerivedF>
  18. void assert_is_manifold(
  19. const Eigen::PlainObjectBase<DerivedV>& V,
  20. const Eigen::PlainObjectBase<DerivedF>& F) {
  21. Eigen::MatrixXi B;
  22. REQUIRE (igl::is_vertex_manifold(F, B));
  23. REQUIRE (igl::is_edge_manifold(F));
  24. }
  25. template<typename DerivedV, typename DerivedF>
  26. void assert_genus_eq(
  27. const Eigen::PlainObjectBase<DerivedV>& V,
  28. const Eigen::PlainObjectBase<DerivedF>& F,
  29. const int genus) {
  30. const int num_vertices = V.rows();
  31. const int num_faces = F.rows();
  32. Eigen::Matrix<
  33. typename DerivedF::Scalar,
  34. Eigen::Dynamic,
  35. Eigen::Dynamic>
  36. E, uE, EMAP;
  37. std::vector<std::vector<size_t> > uE2E;
  38. igl::unique_edge_map(F, E, uE, EMAP, uE2E);
  39. const int num_edges = uE.rows();
  40. const int euler = num_vertices - num_edges + num_faces;
  41. REQUIRE (2 - 2 * genus == euler);
  42. }
  43. }
  44. TEST_CASE("MeshBoolean: TwoCubes", "[igl/copyleft/boolean]") {
  45. Eigen::MatrixXd V1;
  46. Eigen::MatrixXi F1;
  47. test_common::load_mesh("two-boxes-bad-self-union.ply", V1, F1);
  48. Eigen::MatrixXd V2(0, 3);
  49. Eigen::MatrixXi F2(0, 3);
  50. Eigen::MatrixXd Vo;
  51. Eigen::MatrixXi Fo;
  52. igl::copyleft::cgal::mesh_boolean(V1, F1, V2, F2,
  53. igl::MESH_BOOLEAN_TYPE_UNION,
  54. Vo, Fo);
  55. assert_no_exterior_edges(Fo);
  56. assert_is_manifold(Vo, Fo);
  57. assert_genus_eq(Vo, Fo, 0);
  58. }
  59. TEST_CASE("MeshBoolean: MinusTest", "[igl/copyleft/boolean]") {
  60. // Many thanks to Eric Yao for submitting this test case.
  61. Eigen::MatrixXd V1, V2, Vo;
  62. Eigen::MatrixXi F1, F2, Fo;
  63. test_common::load_mesh("boolean_minus_test_cube.obj", V1, F1);
  64. test_common::load_mesh("boolean_minus_test_green.obj", V2, F2);
  65. igl::copyleft::cgal::mesh_boolean(V1, F1, V2, F2,
  66. igl::MESH_BOOLEAN_TYPE_MINUS,
  67. Vo, Fo);
  68. assert_no_exterior_edges(Fo);
  69. assert_is_manifold(Vo, Fo);
  70. assert_genus_eq(Vo, Fo, 1);
  71. }
  72. TEST_CASE("MeshBoolean: IntersectWithSelf", "[igl/copyleft/boolean]") {
  73. Eigen::MatrixXd V1, Vo;
  74. Eigen::MatrixXi F1, Fo;
  75. test_common::load_mesh("cube.obj", V1, F1);
  76. igl::copyleft::cgal::mesh_boolean(V1, F1, V1, F1,
  77. igl::MESH_BOOLEAN_TYPE_INTERSECT,
  78. Vo, Fo);
  79. assert_no_exterior_edges(Fo);
  80. assert_is_manifold(Vo, Fo);
  81. assert_genus_eq(Vo, Fo, 0);
  82. }