decimate.cpp 2.3 KB

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