cotangent.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #ifndef IGL_COTANGENT_H
  2. #define IGL_COTANGENT_H
  3. namespace igl
  4. {
  5. // COTANGENT compute the cotangents of each angle in mesh (V,F)
  6. //
  7. // Templates:
  8. // MatV vertex position matrix, e.g. Eigen::MatrixXd
  9. // MatF face index matrix, e.g. Eigen::MatrixXd
  10. // MatC cotangent weights matrix, e.g. Eigen::MatrixXd
  11. // Inputs:
  12. // V #V by dim list of rest domain positions
  13. // F #F by {3|4} list of {triangle|tetrahedra} indices into V
  14. // Outputs:
  15. // C #F by {3|6} list of cotangents corresponding angles
  16. // for triangles, columns correspond to edges 23,31,12
  17. // for tets, columns correspond to edges 23,31,12,41,42,43
  18. template <class MatV, class MatF, class MatC>
  19. inline void cotangent(const MatV & V, const MatF & F, MatC & C);
  20. }
  21. // Implementation
  22. #include <verbose.h>
  23. #include <Eigen/Dense>
  24. template <class MatV, class MatF, class MatC>
  25. inline void igl::cotangent(const MatV & V, const MatF & F, MatC & C)
  26. {
  27. using namespace igl;
  28. using namespace std;
  29. using namespace Eigen;
  30. // simplex size (3: triangles, 4: tetrahedra)
  31. int simplex_size = F.cols();
  32. // Number of elements
  33. int m = F.rows();
  34. if(simplex_size == 3)
  35. {
  36. // Triangles
  37. // edge lengths numbered same as opposite vertices
  38. Matrix<typename MatC::Scalar,Dynamic,3> l(m,3);
  39. // loop over faces
  40. for(int i = 0;i<m;i++)
  41. {
  42. l(i,0) = sqrt((V.row(F(i,1))-V.row(F(i,2))).array().pow(2).sum());
  43. l(i,1) = sqrt((V.row(F(i,2))-V.row(F(i,0))).array().pow(2).sum());
  44. l(i,2) = sqrt((V.row(F(i,0))-V.row(F(i,1))).array().pow(2).sum());
  45. }
  46. // semiperimeters
  47. Matrix<typename MatC::Scalar,Dynamic,1> s = l.rowwise().sum()*0.5;
  48. assert(s.rows() == m);
  49. // Heron's forumal for area
  50. Matrix<typename MatC::Scalar,Dynamic,1> dblA(m);
  51. for(int i = 0;i<m;i++)
  52. {
  53. dblA(i) = 2.0*sqrt(s(i)*(s(i)-l(i,0))*(s(i)-l(i,1))*(s(i)-l(i,2)));
  54. }
  55. // cotangents and diagonal entries for element matrices
  56. // correctly divided by 4 (alec 2010)
  57. C.resize(m,3);
  58. for(int i = 0;i<m;i++)
  59. {
  60. C(i,0) = (l(i,1)*l(i,1) + l(i,2)*l(i,2) - l(i,0)*l(i,0))/dblA(i)/4.0;
  61. C(i,1) = (l(i,2)*l(i,2) + l(i,0)*l(i,0) - l(i,1)*l(i,1))/dblA(i)/4.0;
  62. C(i,2) = (l(i,0)*l(i,0) + l(i,1)*l(i,1) - l(i,2)*l(i,2))/dblA(i)/4.0;
  63. }
  64. }else if(simplex_size == 4)
  65. {
  66. // Tetrahedra
  67. typedef Matrix<typename MatC::Scalar,3,1> Vec3;
  68. typedef Matrix<typename MatC::Scalar,3,3> Mat3;
  69. typedef Matrix<typename MatC::Scalar,3,4> Mat3x4;
  70. typedef Matrix<typename MatC::Scalar,4,4> Mat4x4;
  71. // preassemble right hand side
  72. // COLUMN-MAJOR ORDER FOR LAPACK
  73. Mat3x4 rhs;
  74. rhs <<
  75. 1,0,0,-1,
  76. 0,1,0,-1,
  77. 0,0,1,-1;
  78. bool diag_all_pos = true;
  79. C.resize(m,6);
  80. // loop over tetrahedra
  81. for(int j = 0;j<F.rows();j++)
  82. {
  83. // points a,b,c,d make up the tetrahedra
  84. size_t a = F(j,0);
  85. size_t b = F(j,1);
  86. size_t c = F(j,2);
  87. size_t d = F(j,3);
  88. //const std::vector<double> & pa = vertices[a];
  89. //const std::vector<double> & pb = vertices[b];
  90. //const std::vector<double> & pc = vertices[c];
  91. //const std::vector<double> & pd = vertices[d];
  92. Vec3 pa = V.row(a);
  93. Vec3 pb = V.row(b);
  94. Vec3 pc = V.row(c);
  95. Vec3 pd = V.row(d);
  96. // Following definition that appears in the appendix of: ``Interactive
  97. // Topology-aware Surface Reconstruction,'' by Sharf, A. et al
  98. // http://www.cs.bgu.ac.il/~asharf/Projects/InSuRe/Insure_siggraph_final.pdf
  99. // compute transpose of jacobian Jj
  100. Mat3 JTj;
  101. JTj.row(0) = pa-pd;
  102. JTj.row(1) = pb-pd;
  103. JTj.row(2) = pc-pd;
  104. // compute abs(determinant of JTj)/6 (volume of tet)
  105. // determinant of transpose of A equals determinant of A
  106. double volume = fabs(JTj.determinant())/6.0;
  107. //printf("volume[%d] = %g\n",j+1,volume);
  108. // solve Jj' * Ej = [-I -1], for Ej
  109. // in other words solve JTj * Ej = [-I -1], for Ej
  110. Mat3x4 Ej = JTj.inverse() * rhs;
  111. // compute Ej'*Ej
  112. Mat4x4 EjTEj = Ej.transpose() * Ej;
  113. // Kj = det(JTj)/6 * Ej'Ej
  114. Mat4x4 Kj = EjTEj*volume;
  115. diag_all_pos &= Kj(0,0)>0 & Kj(1,1)>0 & Kj(2,2)>0 & Kj(3,3)>0;
  116. C(j,0) = Kj(1,2);
  117. C(j,1) = Kj(2,0);
  118. C(j,2) = Kj(0,1);
  119. C(j,3) = Kj(3,0);
  120. C(j,4) = Kj(3,1);
  121. C(j,5) = Kj(3,2);
  122. }
  123. if(diag_all_pos)
  124. {
  125. verbose("cotangent.h: Flipping sign of cotangent, so that cots are positive\n");
  126. C *= -1.0;
  127. }
  128. }else
  129. {
  130. fprintf(stderr,
  131. "cotangent.h: Error: Simplex size (%d) not supported\n", simplex_size);
  132. assert(false);
  133. }
  134. }
  135. #endif