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