decimate.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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_CASE("decimate: hemisphere", "[igl]")
  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, TC;
  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, TC);
  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. REQUIRE (NU.rows() == NVI.rows());
  33. REQUIRE (NU.cols() == NVI.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_CASE("decimate: closed", "[igl]")
  41. {
  42. const auto test_case = [](const std::string &param)
  43. {
  44. Eigen::MatrixXd V,U, TC;
  45. Eigen::MatrixXi F,G;
  46. Eigen::VectorXi J;
  47. // Load example mesh: GetParam() will be name of mesh file
  48. test_common::load_mesh(param, V, F, TC);
  49. igl::decimate(V,F,0,U,G,J);
  50. REQUIRE (4 == U.rows());
  51. REQUIRE (4 == G.rows());
  52. {
  53. Eigen::MatrixXi I;
  54. igl::sort(Eigen::MatrixXi(G),2,true,G,I);
  55. }
  56. {
  57. Eigen::VectorXi I;
  58. igl::sortrows(Eigen::MatrixXi(G),true,G,I);
  59. }
  60. // Tet with sorted faces
  61. Eigen::MatrixXi T(4,3);
  62. T<<
  63. 0,1,2,
  64. 0,1,3,
  65. 0,2,3,
  66. 1,2,3;
  67. test_common::assert_eq(G,T);
  68. };
  69. test_common::run_test_cases(test_common::closed_genus_0_meshes(), test_case);
  70. }
  71. INSTANTIATE_TEST_CASE_P
  72. (
  73. closed_genus_0_meshes,
  74. decimate,
  75. ::testing::ValuesIn(test_common::closed_genus_0_meshes()),
  76. test_common::string_test_name
  77. );