cotmatrix.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #include <test_common.h>
  2. #include <igl/PI.h>
  3. #include <igl/cotmatrix.h>
  4. TEST_CASE("cotmatrix: constant_in_null_space", "[igl]")
  5. {
  6. const auto test_case = [](const std::string &param)
  7. {
  8. Eigen::MatrixXd V;
  9. Eigen::MatrixXi F;
  10. Eigen::SparseMatrix<double> L;
  11. // Load example mesh: GetParam() will be name of mesh file
  12. test_common::load_mesh(param, V, F, TC);
  13. igl::cotmatrix(V,F,L);
  14. REQUIRE (L.rows() == V.rows());
  15. REQUIRE (L.cols() == L.rows());
  16. Eigen::VectorXd C = Eigen::VectorXd::Ones(L.rows());
  17. Eigen::VectorXd Z = Eigen::VectorXd::Zero(L.rows());
  18. // REQUIRE (b == a);
  19. // REQUIRE (a==b);
  20. // ASSERT_NEAR(a,b,1e-15)
  21. REQUIRE (1e-12 > ((L*C)-(Z)).norm());
  22. };
  23. test_common::run_test_cases(test_common::all_meshes(), test_case);
  24. }
  25. TEST_CASE("cotmatrix: cube", "[igl]")
  26. {
  27. //The allowed error for this test
  28. const double epsilon = 1e-15;
  29. Eigen::MatrixXd V, TC;
  30. Eigen::MatrixXi F;
  31. //This is a cube of dimensions 1.0x1.0x1.0
  32. test_common::load_mesh("cube.obj", V, F, TC);
  33. //Scale the cube to have huge sides
  34. Eigen::MatrixXd V_huge = V * 1.0e8;
  35. //Scale the cube to have tiny sides
  36. Eigen::MatrixXd V_tiny = V * 1.0e-8;
  37. //Check cotmatrix (Laplacian)
  38. //The laplacian for the cube is quite singular.
  39. //Each edge in a diagonal has two opposite angles of 90, with cotangent 0.0 each
  40. //Each edge in a side has two opposite angle of 45, with (half)cotangen 0.5 each
  41. //So the cotangent matrix always are (0+0) or (0.5+0.5)
  42. Eigen::SparseMatrix<double> L1;
  43. igl::cotmatrix(V,F,L1);
  44. REQUIRE (L1.rows() == V.rows());
  45. REQUIRE (L1.cols() == V.rows());
  46. //// This is hitting an Eigen bug. https://github.com/libigl/libigl/pull/1064
  47. // for(int f = 0;f<L1.rows();f++)
  48. // {
  49. //#ifdef IGL_EDGE_LENGTHS_SQUARED_H
  50. // //Hard assert if we have edge_lenght_squared
  51. // REQUIRE (L1.coeff(f,f) == -3.0);
  52. // REQUIRE (L1.row(f).sum() == 0.0);
  53. // REQUIRE (L1.col(f).sum() == 0.0);
  54. //#else
  55. // //Soft assert if we have not edge_lenght_squared
  56. // REQUIRE (L1.coeff(f,f) == Approx (-3.0).margin( epsilon));
  57. // REQUIRE (L1.row(f).sum() == Approx (0.0).margin( epsilon));
  58. // REQUIRE (L1.col(f).sum() == Approx (0.0).margin( epsilon));
  59. //#endif
  60. // }
  61. Eigen::VectorXd row_sum = L1 * Eigen::VectorXd::Constant(L1.rows(),1,1);
  62. Eigen::RowVectorXd col_sum = Eigen::RowVectorXd::Constant(1,L1.rows(),1) * L1;
  63. Eigen::VectorXd diag = L1.diagonal();
  64. #ifdef IGL_EDGE_LENGTHS_SQUARED_H
  65. test_common::assert_eq( row_sum, Eigen::VectorXd::Zero(L1.rows()) );
  66. test_common::assert_eq( col_sum, Eigen::RowVectorXd::Zero(L1.rows()) );
  67. test_common::assert_eq( diag, Eigen::VectorXd::Constant(L1.rows(),1,-3) );
  68. #else
  69. test_common::assert_near( row_sum, Eigen::VectorXd::Zero(L1.rows()) , epsilon);
  70. test_common::assert_near( col_sum, Eigen::RowVectorXd::Zero(L1.rows()) , epsilon);
  71. test_common::assert_near( diag, Eigen::VectorXd::Constant(L1.rows(),1,-3) , epsilon);
  72. #endif
  73. }
  74. //Same for huge cube.
  75. igl::cotmatrix(V_huge,F,L1);
  76. REQUIRE (L1.rows() == V.rows());
  77. REQUIRE (L1.cols() == V.rows());
  78. for(int f = 0;f<L1.rows();f++)
  79. {
  80. REQUIRE (L1.coeff(f,f) == Approx (-3.0).margin( epsilon));
  81. REQUIRE (L1.row(f).sum() == Approx (0.0).margin( epsilon));
  82. REQUIRE (L1.col(f).sum() == Approx (0.0).margin( epsilon));
  83. }
  84. //Same for tiny cube. we need to use a tolerance this time...
  85. igl::cotmatrix(V_tiny,F,L1);
  86. REQUIRE (L1.rows() == V.rows());
  87. REQUIRE (L1.cols() == V.rows());
  88. for(int f = 0;f<L1.rows();f++)
  89. {
  90. REQUIRE (L1.coeff(f,f) == Approx (-3.0).margin( epsilon));
  91. REQUIRE (L1.row(f).sum() == Approx (0.0).margin( epsilon));
  92. REQUIRE (L1.col(f).sum() == Approx (0.0).margin( epsilon));
  93. }
  94. }
  95. TEST_CASE("cotmatrix: tetrahedron", "[igl]")
  96. {
  97. //The allowed error for this test
  98. const double epsilon = 1e-15;
  99. Eigen::MatrixXd V, TC;
  100. Eigen::MatrixXi F;
  101. //This is a cube of dimensions 1.0x1.0x1.0
  102. test_common::load_mesh("cube.obj", V, F, TC);
  103. //Prepare another mesh with triangles along side diagonals of the cube
  104. //These triangles are form a regular tetrahedron of side sqrt(2)
  105. Eigen::MatrixXi F_equi(4,3);
  106. F_equi << 4,6,1,
  107. 6,4,3,
  108. 4,1,3,
  109. 1,6,3;
  110. //Scale the cube to have huge sides
  111. Eigen::MatrixXd V_huge = V * 1.0e8;
  112. //Scale the cube to have tiny sides
  113. Eigen::MatrixXd V_tiny = V * 1.0e-8;
  114. //Check cotmatrix (Laplacian)
  115. //The laplacian for the cube is quite singular.
  116. //Each edge in a diagonal has two opposite angles of 90, with cotangent 0.0 each
  117. //Each edge in a side has two opposite angle of 45, with (half)cotangen 0.5 each
  118. //So the cotangent matrix always are (0+0) or (0.5+0.5)
  119. Eigen::SparseMatrix<double> L1;
  120. //Check the regular tetrahedron of side sqrt(2)
  121. igl::cotmatrix(V,F_equi,L1);
  122. REQUIRE (L1.rows() == V.rows());
  123. REQUIRE (L1.cols() == V.rows());
  124. for(int f = 0;f<L1.rows();f++)
  125. {
  126. //Check the diagonal. Only can value 0.0 for unused vertex or -3 / tan(60)
  127. if (L1.coeff(f,f) < -0.1)
  128. REQUIRE (L1.coeff(f,f) == Approx (-3 / tan(igl::PI / 3.0)).margin( epsilon));
  129. else
  130. REQUIRE (L1.coeff(f,f) == Approx (0.0).margin( epsilon));
  131. #ifdef IGL_EDGE_LENGTHS_SQUARED_H
  132. //Hard assert if we have edge_lenght_squared
  133. REQUIRE (L1.row(f).sum() == 0.0);
  134. REQUIRE (L1.col(f).sum() == 0.0);
  135. #else
  136. //Soft assert if we have not edge_lenght_squared
  137. REQUIRE (L1.row(f).sum() == Approx (0.0).margin( epsilon));
  138. REQUIRE (L1.col(f).sum() == Approx (0.0).margin( epsilon));
  139. #endif
  140. }
  141. //Check the huge regular tetrahedron
  142. igl::cotmatrix(V_huge,F_equi,L1);
  143. REQUIRE (L1.rows() == V.rows());
  144. REQUIRE (L1.cols() == V.rows());
  145. for(int f = 0;f<L1.rows();f++)
  146. {
  147. //Check the diagonal. Only can value 0.0 for unused vertex or -3 / tan(60)
  148. if (L1.coeff(f,f) < -0.1)
  149. REQUIRE (L1.coeff(f,f) == Approx (-3 / tan(igl::PI / 3.0)).margin( epsilon));
  150. else
  151. REQUIRE (L1.coeff(f,f) == Approx (0.0).margin( epsilon));
  152. REQUIRE (L1.row(f).sum() == Approx (0.0).margin( epsilon));
  153. REQUIRE (L1.col(f).sum() == Approx (0.0).margin( epsilon));
  154. }
  155. //Check the tiny regular tetrahedron
  156. igl::cotmatrix(V_tiny,F_equi,L1);
  157. REQUIRE (L1.rows() == V.rows());
  158. REQUIRE (L1.cols() == V.rows());
  159. for(int f = 0;f<L1.rows();f++)
  160. {
  161. //Check the diagonal. Only can value 0.0 for unused vertex or -3 / tan(60)
  162. if (L1.coeff(f,f) < -0.1)
  163. REQUIRE (L1.coeff(f,f) == Approx (-3 / tan(igl::PI / 3.0)).margin( epsilon));
  164. else
  165. REQUIRE (L1.coeff(f,f) == Approx (0.0).margin( epsilon));
  166. REQUIRE (L1.row(f).sum() == Approx (0.0).margin( epsilon));
  167. REQUIRE (L1.col(f).sum() == Approx (0.0).margin( epsilon));
  168. }
  169. }