cotmatrix.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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);
  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;
  30. Eigen::MatrixXi F;
  31. //This is a cube of dimensions 1.0x1.0x1.0
  32. test_common::load_mesh("cube.obj", V, F);
  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. //Same for huge cube.
  74. igl::cotmatrix(V_huge,F,L1);
  75. REQUIRE (L1.rows() == V.rows());
  76. REQUIRE (L1.cols() == V.rows());
  77. for(int f = 0;f<L1.rows();f++)
  78. {
  79. REQUIRE (L1.coeff(f,f) == Approx (-3.0).margin( epsilon));
  80. REQUIRE (L1.row(f).sum() == Approx (0.0).margin( epsilon));
  81. REQUIRE (L1.col(f).sum() == Approx (0.0).margin( epsilon));
  82. }
  83. //Same for tiny cube. we need to use a tolerance this time...
  84. igl::cotmatrix(V_tiny,F,L1);
  85. REQUIRE (L1.rows() == V.rows());
  86. REQUIRE (L1.cols() == V.rows());
  87. for(int f = 0;f<L1.rows();f++)
  88. {
  89. REQUIRE (L1.coeff(f,f) == Approx (-3.0).margin( epsilon));
  90. REQUIRE (L1.row(f).sum() == Approx (0.0).margin( epsilon));
  91. REQUIRE (L1.col(f).sum() == Approx (0.0).margin( epsilon));
  92. }
  93. }
  94. TEST_CASE("cotmatrix: tetrahedron", "[igl]")
  95. {
  96. //The allowed error for this test
  97. const double epsilon = 1e-15;
  98. Eigen::MatrixXd V;
  99. Eigen::MatrixXi F;
  100. //This is a cube of dimensions 1.0x1.0x1.0
  101. test_common::load_mesh("cube.obj", V, F);
  102. //Prepare another mesh with triangles along side diagonals of the cube
  103. //These triangles are form a regular tetrahedron of side sqrt(2)
  104. Eigen::MatrixXi F_equi(4,3);
  105. F_equi << 4,6,1,
  106. 6,4,3,
  107. 4,1,3,
  108. 1,6,3;
  109. //Scale the cube to have huge sides
  110. Eigen::MatrixXd V_huge = V * 1.0e8;
  111. //Scale the cube to have tiny sides
  112. Eigen::MatrixXd V_tiny = V * 1.0e-8;
  113. //Check cotmatrix (Laplacian)
  114. //The laplacian for the cube is quite singular.
  115. //Each edge in a diagonal has two opposite angles of 90, with cotangent 0.0 each
  116. //Each edge in a side has two opposite angle of 45, with (half)cotangen 0.5 each
  117. //So the cotangent matrix always are (0+0) or (0.5+0.5)
  118. Eigen::SparseMatrix<double> L1;
  119. //Check the regular tetrahedron of side sqrt(2)
  120. igl::cotmatrix(V,F_equi,L1);
  121. REQUIRE (L1.rows() == V.rows());
  122. REQUIRE (L1.cols() == V.rows());
  123. for(int f = 0;f<L1.rows();f++)
  124. {
  125. //Check the diagonal. Only can value 0.0 for unused vertex or -3 / tan(60)
  126. if (L1.coeff(f,f) < -0.1)
  127. REQUIRE (L1.coeff(f,f) == Approx (-3 / tan(igl::PI / 3.0)).margin( epsilon));
  128. else
  129. REQUIRE (L1.coeff(f,f) == Approx (0.0).margin( epsilon));
  130. #ifdef IGL_EDGE_LENGTHS_SQUARED_H
  131. //Hard assert if we have edge_lenght_squared
  132. REQUIRE (L1.row(f).sum() == 0.0);
  133. REQUIRE (L1.col(f).sum() == 0.0);
  134. #else
  135. //Soft assert if we have not edge_lenght_squared
  136. REQUIRE (L1.row(f).sum() == Approx (0.0).margin( epsilon));
  137. REQUIRE (L1.col(f).sum() == Approx (0.0).margin( epsilon));
  138. #endif
  139. }
  140. //Check the huge regular tetrahedron
  141. igl::cotmatrix(V_huge,F_equi,L1);
  142. REQUIRE (L1.rows() == V.rows());
  143. REQUIRE (L1.cols() == V.rows());
  144. for(int f = 0;f<L1.rows();f++)
  145. {
  146. //Check the diagonal. Only can value 0.0 for unused vertex or -3 / tan(60)
  147. if (L1.coeff(f,f) < -0.1)
  148. REQUIRE (L1.coeff(f,f) == Approx (-3 / tan(igl::PI / 3.0)).margin( epsilon));
  149. else
  150. REQUIRE (L1.coeff(f,f) == Approx (0.0).margin( epsilon));
  151. REQUIRE (L1.row(f).sum() == Approx (0.0).margin( epsilon));
  152. REQUIRE (L1.col(f).sum() == Approx (0.0).margin( epsilon));
  153. }
  154. //Check the tiny regular tetrahedron
  155. igl::cotmatrix(V_tiny,F_equi,L1);
  156. REQUIRE (L1.rows() == V.rows());
  157. REQUIRE (L1.cols() == V.rows());
  158. for(int f = 0;f<L1.rows();f++)
  159. {
  160. //Check the diagonal. Only can value 0.0 for unused vertex or -3 / tan(60)
  161. if (L1.coeff(f,f) < -0.1)
  162. REQUIRE (L1.coeff(f,f) == Approx (-3 / tan(igl::PI / 3.0)).margin( epsilon));
  163. else
  164. REQUIRE (L1.coeff(f,f) == Approx (0.0).margin( epsilon));
  165. REQUIRE (L1.row(f).sum() == Approx (0.0).margin( epsilon));
  166. REQUIRE (L1.col(f).sum() == Approx (0.0).margin( epsilon));
  167. }
  168. }