#ifndef IGL_COTANGENT_H #define IGL_COTANGENT_H namespace igl { // COTANGENT compute the cotangents of each angle in mesh (V,F) // // Templates: // MatV vertex position matrix, e.g. Eigen::MatrixXd // MatF face index matrix, e.g. Eigen::MatrixXd // MatC cotangent weights matrix, e.g. Eigen::MatrixXd // Inputs: // V #V by dim list of rest domain positions // F #F by {3|4} list of {triangle|tetrahedra} indices into V // Outputs: // C #F by {3|6} list of cotangents corresponding angles // for triangles, columns correspond to edges 23,31,12 // for tets, columns correspond to edges 23,31,12,41,42,43 template inline void cotangent(const MatV & V, const MatF & F, MatC & C); } // Implementation #include #include template inline void igl::cotangent(const MatV & V, const MatF & F, MatC & C) { using namespace igl; using namespace std; using namespace Eigen; // simplex size (3: triangles, 4: tetrahedra) int simplex_size = F.cols(); // Number of elements int m = F.rows(); if(simplex_size == 3) { // Triangles // edge lengths numbered same as opposite vertices Matrix l(m,3); // loop over faces for(int i = 0;i s = l.rowwise().sum()*0.5; assert(s.rows() == m); // Heron's forumal for area Matrix dblA(m); for(int i = 0;i Vec3; typedef Matrix Mat3; typedef Matrix Mat3x4; typedef Matrix Mat4x4; // preassemble right hand side // COLUMN-MAJOR ORDER FOR LAPACK Mat3x4 rhs; rhs << 1,0,0,-1, 0,1,0,-1, 0,0,1,-1; bool diag_all_pos = true; C.resize(m,6); // loop over tetrahedra for(int j = 0;j & pa = vertices[a]; //const std::vector & pb = vertices[b]; //const std::vector & pc = vertices[c]; //const std::vector & pd = vertices[d]; Vec3 pa = V.row(a); Vec3 pb = V.row(b); Vec3 pc = V.row(c); Vec3 pd = V.row(d); // Following definition that appears in the appendix of: ``Interactive // Topology-aware Surface Reconstruction,'' by Sharf, A. et al // http://www.cs.bgu.ac.il/~asharf/Projects/InSuRe/Insure_siggraph_final.pdf // compute transpose of jacobian Jj Mat3 JTj; JTj.row(0) = pa-pd; JTj.row(1) = pb-pd; JTj.row(2) = pc-pd; // compute abs(determinant of JTj)/6 (volume of tet) // determinant of transpose of A equals determinant of A double volume = fabs(JTj.determinant())/6.0; //printf("volume[%d] = %g\n",j+1,volume); // solve Jj' * Ej = [-I -1], for Ej // in other words solve JTj * Ej = [-I -1], for Ej Mat3x4 Ej = JTj.inverse() * rhs; // compute Ej'*Ej Mat4x4 EjTEj = Ej.transpose() * Ej; // Kj = det(JTj)/6 * Ej'Ej Mat4x4 Kj = EjTEj*volume; diag_all_pos &= Kj(0,0)>0 & Kj(1,1)>0 & Kj(2,2)>0 & Kj(3,3)>0; C(j,0) = Kj(1,2); C(j,1) = Kj(2,0); C(j,2) = Kj(0,1); C(j,3) = Kj(3,0); C(j,4) = Kj(3,1); C(j,5) = Kj(3,2); } if(diag_all_pos) { verbose("cotangent.h: Flipping sign of cotangent, so that cots are positive\n"); C *= -1.0; } }else { fprintf(stderr, "cotangent.h: Error: Simplex size (%d) not supported\n", simplex_size); assert(false); } } #endif