decimate.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include <test_common.h>
  2. #include <igl/decimate.h>
  3. #include <igl/sort.h>
  4. #include <igl/sortrows.h>
  5. #include <igl/slice.h>
  6. #include <igl/matlab_format.h>
  7. #include <iostream>
  8. // class decimate : public ::testing::TestWithParam<std::string> {};
  9. TEST_CASE("decimate: hemisphere", "[igl]")
  10. {
  11. // Load a hemisphere centered at the origin. For each original vertex compute
  12. // its "perfect normal" (i.e., its position treated as unit vectors).
  13. // Decimate the model and using the birth indices of the output vertices grab
  14. // their original "perfect normals" and compare them to their current
  15. // positions treated as unit vectors. If vertices have not moved much, then
  16. // these should be similar (mostly this is checking if the birth indices are
  17. // sane).
  18. Eigen::MatrixXd V,U;
  19. Eigen::MatrixXi F,G;
  20. Eigen::VectorXi J,I;
  21. // Load example mesh: GetParam() will be name of mesh file
  22. igl::read_triangle_mesh(test_common::data_path("hemisphere.obj"), V, F);
  23. // Perfect normals from positions
  24. Eigen::MatrixXd NV = V.rowwise().normalized();
  25. // Remove half of the faces
  26. igl::decimate(V,F,F.rows()/2,U,G,J,I);
  27. // Expect that all normals still point in same direction as original
  28. Eigen::MatrixXd NU = U.rowwise().normalized();
  29. Eigen::MatrixXd NVI;
  30. igl::slice(NV,I,1,NVI);
  31. REQUIRE (NU.rows() == NVI.rows());
  32. REQUIRE (NU.cols() == NVI.cols());
  33. // Dot product
  34. Eigen::VectorXd D = (NU.array()*NVI.array()).rowwise().sum();
  35. Eigen::VectorXd O = Eigen::VectorXd::Ones(D.rows());
  36. // 0.2 chosen to succeed on 256 face hemisphere.obj reduced to 128 faces
  37. test_common::assert_near(D,O,0.02);
  38. }
  39. TEST_CASE("decimate: closed", "[igl]")
  40. {
  41. const auto test_case = [](const std::string &param)
  42. {
  43. Eigen::MatrixXd V,U;
  44. Eigen::MatrixXi F,G;
  45. Eigen::VectorXi J;
  46. // Load example mesh: GetParam() will be name of mesh file
  47. igl::read_triangle_mesh(test_common::data_path(param), V, F);
  48. igl::decimate(V,F,0,U,G,J);
  49. REQUIRE (4 == U.rows());
  50. REQUIRE (4 == G.rows());
  51. {
  52. Eigen::MatrixXi I;
  53. igl::sort(Eigen::MatrixXi(G),2,true,G,I);
  54. }
  55. {
  56. Eigen::VectorXi I;
  57. igl::sortrows(Eigen::MatrixXi(G),true,G,I);
  58. }
  59. // Tet with sorted faces
  60. Eigen::MatrixXi T(4,3);
  61. T<<
  62. 0,1,2,
  63. 0,1,3,
  64. 0,2,3,
  65. 1,2,3;
  66. test_common::assert_eq(G,T);
  67. };
  68. test_common::run_test_cases(test_common::closed_genus_0_meshes(), test_case);
  69. }