mesh_boolean.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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;
  37. Eigen::Matrix<
  38. typename DerivedF::Scalar,
  39. Eigen::Dynamic,
  40. 1>
  41. EMAP;
  42. std::vector<std::vector<size_t> > uE2E;
  43. igl::unique_edge_map(F, E, uE, EMAP, uE2E);
  44. const int num_edges = uE.rows();
  45. const int euler = num_vertices - num_edges + num_faces;
  46. REQUIRE (2 - 2 * genus == euler);
  47. }
  48. }
  49. TEST_CASE("MeshBoolean: TwoCubes", "[igl/copyleft/boolean]") {
  50. Eigen::MatrixXd V1;
  51. Eigen::MatrixXi F1;
  52. igl::read_triangle_mesh(test_common::data_path("two-boxes-bad-self-union.ply"), V1, F1);
  53. Eigen::MatrixXd V2(0, 3);
  54. Eigen::MatrixXi F2(0, 3);
  55. Eigen::MatrixXd Vo;
  56. Eigen::MatrixXi Fo;
  57. igl::copyleft::cgal::mesh_boolean(V1, F1, V2, F2,
  58. igl::MESH_BOOLEAN_TYPE_UNION,
  59. Vo, Fo);
  60. assert_no_exterior_edges(Fo);
  61. assert_is_manifold(Vo, Fo);
  62. assert_genus_eq(Vo, Fo, 0);
  63. }
  64. TEST_CASE("MeshBoolean: MinusTest", "[igl/copyleft/boolean]") {
  65. // Many thanks to Eric Yao for submitting this test case.
  66. Eigen::MatrixXd V1, V2, Vo;
  67. Eigen::MatrixXi F1, F2, Fo;
  68. igl::read_triangle_mesh(test_common::data_path("boolean_minus_test_cube.obj"), V1, F1);
  69. igl::read_triangle_mesh(test_common::data_path("boolean_minus_test_green.obj"), V2, F2);
  70. igl::copyleft::cgal::mesh_boolean(V1, F1, V2, F2,
  71. igl::MESH_BOOLEAN_TYPE_MINUS,
  72. Vo, Fo);
  73. assert_no_exterior_edges(Fo);
  74. assert_is_manifold(Vo, Fo);
  75. assert_genus_eq(Vo, Fo, 1);
  76. }
  77. TEST_CASE("MeshBoolean: IntersectWithSelf", "[igl/copyleft/boolean]") {
  78. Eigen::MatrixXd V1, Vo;
  79. Eigen::MatrixXi F1, Fo;
  80. igl::read_triangle_mesh(test_common::data_path("cube.obj"), V1, F1);
  81. igl::copyleft::cgal::mesh_boolean(V1, F1, V1, F1,
  82. igl::MESH_BOOLEAN_TYPE_INTERSECT,
  83. Vo, Fo);
  84. assert_no_exterior_edges(Fo);
  85. assert_is_manifold(Vo, Fo);
  86. assert_genus_eq(Vo, Fo, 0);
  87. }