cotangent.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 "doublearea.h"
  10. #include "edge_lengths.h"
  11. #include "face_areas.h"
  12. #include "volume.h"
  13. #include "dihedral_angles.h"
  14. #include "verbose.h"
  15. template <typename DerivedV, typename DerivedF, typename DerivedC>
  16. IGL_INLINE void igl::cotangent(
  17. const Eigen::PlainObjectBase<DerivedV>& V,
  18. const Eigen::PlainObjectBase<DerivedF>& F,
  19. Eigen::PlainObjectBase<DerivedC>& C)
  20. {
  21. using namespace igl;
  22. using namespace std;
  23. using namespace Eigen;
  24. // simplex size (3: triangles, 4: tetrahedra)
  25. int simplex_size = F.cols();
  26. // Number of elements
  27. int m = F.rows();
  28. // Law of cosines + law of sines
  29. switch(simplex_size)
  30. {
  31. case 3:
  32. {
  33. // Triangles
  34. //Matrix<typename DerivedC::Scalar,Dynamic,3> l;
  35. //edge_lengths(V,F,l);
  36. // edge lengths numbered same as opposite vertices
  37. Matrix<typename DerivedC::Scalar,Dynamic,3> l;
  38. igl::edge_lengths(V,F,l);
  39. // double area
  40. Matrix<typename DerivedC::Scalar,Dynamic,1> dblA;
  41. doublearea(l,dblA);
  42. // cotangents and diagonal entries for element matrices
  43. // correctly divided by 4 (alec 2010)
  44. C.resize(m,3);
  45. for(int i = 0;i<m;i++)
  46. {
  47. 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;
  48. 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;
  49. 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;
  50. }
  51. break;
  52. }
  53. case 4:
  54. {
  55. // edge lengths numbered same as opposite vertices
  56. Matrix<typename DerivedC::Scalar,Dynamic,6> l;
  57. edge_lengths(V,F,l);
  58. Matrix<typename DerivedC::Scalar,Dynamic,4> s;
  59. face_areas(l,s);
  60. Matrix<typename DerivedC::Scalar,Dynamic,6> cos_theta,theta;
  61. dihedral_angles_intrinsic(l,s,theta,cos_theta);
  62. // volume
  63. Matrix<typename DerivedC::Scalar,Dynamic,1> vol;
  64. volume(l,vol);
  65. // Law of sines
  66. // http://mathworld.wolfram.com/Tetrahedron.html
  67. Matrix<typename DerivedC::Scalar,Dynamic,6> sin_theta(m,6);
  68. sin_theta.col(0) = vol.array() / ((2./(3.*l.col(0).array())).array() * s.col(1).array() * s.col(2).array());
  69. sin_theta.col(1) = vol.array() / ((2./(3.*l.col(1).array())).array() * s.col(2).array() * s.col(0).array());
  70. sin_theta.col(2) = vol.array() / ((2./(3.*l.col(2).array())).array() * s.col(0).array() * s.col(1).array());
  71. sin_theta.col(3) = vol.array() / ((2./(3.*l.col(3).array())).array() * s.col(3).array() * s.col(0).array());
  72. sin_theta.col(4) = vol.array() / ((2./(3.*l.col(4).array())).array() * s.col(3).array() * s.col(1).array());
  73. sin_theta.col(5) = vol.array() / ((2./(3.*l.col(5).array())).array() * s.col(3).array() * s.col(2).array());
  74. // http://arxiv.org/pdf/1208.0354.pdf Page 18
  75. C = (1./6.) * l.array() * cos_theta.array() / sin_theta.array();
  76. // LEGACY
  77. // // Tetrahedra
  78. // typedef Matrix<typename MatV::Scalar,3,1> Vec3;
  79. // typedef Matrix<typename MatV::Scalar,3,3> Mat3;
  80. // typedef Matrix<typename MatV::Scalar,3,4> Mat3x4;
  81. // typedef Matrix<typename MatV::Scalar,4,4> Mat4x4;
  82. //
  83. // // preassemble right hand side
  84. // // COLUMN-MAJOR ORDER FOR LAPACK
  85. // Mat3x4 rhs;
  86. // rhs <<
  87. // 1,0,0,-1,
  88. // 0,1,0,-1,
  89. // 0,0,1,-1;
  90. //
  91. // bool diag_all_pos = true;
  92. // C.resize(m,6);
  93. //
  94. // // loop over tetrahedra
  95. // for(int j = 0;j<F.rows();j++)
  96. // {
  97. // // points a,b,c,d make up the tetrahedra
  98. // size_t a = F(j,0);
  99. // size_t b = F(j,1);
  100. // size_t c = F(j,2);
  101. // size_t d = F(j,3);
  102. // //const std::vector<double> & pa = vertices[a];
  103. // //const std::vector<double> & pb = vertices[b];
  104. // //const std::vector<double> & pc = vertices[c];
  105. // //const std::vector<double> & pd = vertices[d];
  106. // Vec3 pa = V.row(a);
  107. // Vec3 pb = V.row(b);
  108. // Vec3 pc = V.row(c);
  109. // Vec3 pd = V.row(d);
  110. //
  111. // // Following definition that appears in the appendix of: ``Interactive
  112. // // Topology-aware Surface Reconstruction,'' by Sharf, A. et al
  113. // // http://www.cs.bgu.ac.il/~asharf/Projects/InSuRe/Insure_siggraph_final.pdf
  114. //
  115. // // compute transpose of jacobian Jj
  116. // Mat3 JTj;
  117. // JTj.row(0) = pa-pd;
  118. // JTj.row(1) = pb-pd;
  119. // JTj.row(2) = pc-pd;
  120. //
  121. // // compute abs(determinant of JTj)/6 (volume of tet)
  122. // // determinant of transpose of A equals determinant of A
  123. // double volume = fabs(JTj.determinant())/6.0;
  124. // //printf("volume[%d] = %g\n",j+1,volume);
  125. //
  126. // // solve Jj' * Ej = [-I -1], for Ej
  127. // // in other words solve JTj * Ej = [-I -1], for Ej
  128. // Mat3x4 Ej = JTj.inverse() * rhs;
  129. // // compute Ej'*Ej
  130. // Mat4x4 EjTEj = Ej.transpose() * Ej;
  131. //
  132. // // Kj = det(JTj)/6 * Ej'Ej
  133. // Mat4x4 Kj = EjTEj*volume;
  134. // diag_all_pos &= ((Kj(0,0)>0) & (Kj(1,1)>0)) & ((Kj(2,2)>0) & (Kj(3,3)>0));
  135. // C(j,0) = Kj(1,2);
  136. // C(j,1) = Kj(2,0);
  137. // C(j,2) = Kj(0,1);
  138. // C(j,3) = Kj(3,0);
  139. // C(j,4) = Kj(3,1);
  140. // C(j,5) = Kj(3,2);
  141. // }
  142. // if(diag_all_pos)
  143. // {
  144. //#ifdef VERBOSE
  145. // verbose("cotangent.h: Flipping sign of cotangent, so that cots are positive\n");
  146. //#endif
  147. // C *= -1.0;
  148. // }
  149. break;
  150. }
  151. default:
  152. {
  153. fprintf(stderr,
  154. "cotangent.h: Error: Simplex size (%d) not supported\n", simplex_size);
  155. assert(false);
  156. }
  157. }
  158. }
  159. #ifndef IGL_HEADER_ONLY
  160. // Explicit template specialization
  161. 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::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  162. #endif