decimate.cpp 2.4 KB

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