cotangent.cpp 4.6 KB

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