cotangent.cpp 5.8 KB

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