cotangent.cpp 4.6 KB

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