cotmatrix_entries.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
  4. // Copyright (C) 2018 Alec Jacobson <alecjacobson@gmail.com>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla Public License
  7. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  8. // obtain one at http://mozilla.org/MPL/2.0/.
  9. #include "cotmatrix_entries.h"
  10. #include "doublearea.h"
  11. #include "squared_edge_lengths.h"
  12. #include "edge_lengths.h"
  13. #include "face_areas.h"
  14. #include "volume.h"
  15. #include "dihedral_angles.h"
  16. #include "verbose.h"
  17. template <typename DerivedV, typename DerivedF, typename DerivedC>
  18. IGL_INLINE void igl::cotmatrix_entries(
  19. const Eigen::MatrixBase<DerivedV>& V,
  20. const Eigen::MatrixBase<DerivedF>& F,
  21. Eigen::PlainObjectBase<DerivedC>& C)
  22. {
  23. using namespace std;
  24. using namespace Eigen;
  25. // simplex size (3: triangles, 4: tetrahedra)
  26. int simplex_size = F.cols();
  27. // Number of elements
  28. int m = F.rows();
  29. // Law of cosines + law of sines
  30. switch(simplex_size)
  31. {
  32. case 3:
  33. {
  34. // Triangles
  35. //Compute Squared Edge lengths
  36. Matrix<typename DerivedC::Scalar,Dynamic,3> l2;
  37. igl::squared_edge_lengths(V,F,l2);
  38. //Compute Edge lengths
  39. Matrix<typename DerivedC::Scalar,Dynamic,3> l;
  40. l = l2.array().sqrt();
  41. // double area
  42. Matrix<typename DerivedC::Scalar,Dynamic,1> dblA;
  43. doublearea(l,0.,dblA);
  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. // Alec: I'm doubtful that using l2 here is actually improving numerics.
  50. C(i,0) = (l2(i,1) + l2(i,2) - l2(i,0))/dblA(i)/4.0;
  51. C(i,1) = (l2(i,2) + l2(i,0) - l2(i,1))/dblA(i)/4.0;
  52. C(i,2) = (l2(i,0) + l2(i,1) - l2(i,2))/dblA(i)/4.0;
  53. }
  54. break;
  55. }
  56. case 4:
  57. {
  58. // edge lengths numbered same as opposite vertices
  59. Matrix<typename DerivedC::Scalar,Dynamic,6> l;
  60. edge_lengths(V,F,l);
  61. Matrix<typename DerivedC::Scalar,Dynamic,4> s;
  62. face_areas(l,s);
  63. Matrix<typename DerivedC::Scalar,Dynamic,6> cos_theta,theta;
  64. dihedral_angles_intrinsic(l,s,theta,cos_theta);
  65. // volume
  66. Matrix<typename DerivedC::Scalar,Dynamic,1> vol;
  67. volume(l,vol);
  68. // Law of sines
  69. // http://mathworld.wolfram.com/Tetrahedron.html
  70. Matrix<typename DerivedC::Scalar,Dynamic,6> sin_theta(m,6);
  71. sin_theta.col(0) = vol.array() / ((2./(3.*l.col(0).array())).array() * s.col(1).array() * s.col(2).array());
  72. sin_theta.col(1) = vol.array() / ((2./(3.*l.col(1).array())).array() * s.col(2).array() * s.col(0).array());
  73. sin_theta.col(2) = vol.array() / ((2./(3.*l.col(2).array())).array() * s.col(0).array() * s.col(1).array());
  74. sin_theta.col(3) = vol.array() / ((2./(3.*l.col(3).array())).array() * s.col(3).array() * s.col(0).array());
  75. sin_theta.col(4) = vol.array() / ((2./(3.*l.col(4).array())).array() * s.col(3).array() * s.col(1).array());
  76. sin_theta.col(5) = vol.array() / ((2./(3.*l.col(5).array())).array() * s.col(3).array() * s.col(2).array());
  77. // http://arxiv.org/pdf/1208.0354.pdf Page 18
  78. C = (1./6.) * l.array() * cos_theta.array() / sin_theta.array();
  79. break;
  80. }
  81. default:
  82. {
  83. fprintf(stderr,
  84. "cotmatrix_entries.h: Error: Simplex size (%d) not supported\n", simplex_size);
  85. assert(false);
  86. }
  87. }
  88. }
  89. template <typename Derivedl, typename DerivedC>
  90. IGL_INLINE void igl::cotmatrix_entries(
  91. const Eigen::MatrixBase<Derivedl>& l,
  92. Eigen::PlainObjectBase<DerivedC>& C)
  93. {
  94. using namespace Eigen;
  95. const int m = l.rows();
  96. assert(l.cols() == 3 && "Only triangles accepted");
  97. //Compute squared Edge lengths
  98. Matrix<typename DerivedC::Scalar,Dynamic,3> l2;
  99. l2 = l.array().square();
  100. // Alec: It's a little annoying that there's duplicate code here. The
  101. // "extrinic" version above is first computing squared edge lengths, taking
  102. // the square root and calling this. We can't have a cotmatrix_entries(l,l2,C)
  103. // overload because it will confuse Eigen with the cotmatrix_entries(V,F,C)
  104. // overload. In the end, I'd like to be convinced that using l2 directly above
  105. // is actually better numerically (or significantly faster) than just calling
  106. // edge_lengths and this cotmatrix_entries(l,C);
  107. //
  108. // double area
  109. Matrix<typename DerivedC::Scalar,Dynamic,1> dblA;
  110. doublearea(l,0.,dblA);
  111. // cotangents and diagonal entries for element matrices
  112. // correctly divided by 4 (alec 2010)
  113. C.resize(m,3);
  114. for(int i = 0;i<m;i++)
  115. {
  116. // Alec: I'm doubtful that using l2 here is actually improving numerics.
  117. C(i,0) = (l2(i,1) + l2(i,2) - l2(i,0))/dblA(i)/4.0;
  118. C(i,1) = (l2(i,2) + l2(i,0) - l2(i,1))/dblA(i)/4.0;
  119. C(i,2) = (l2(i,0) + l2(i,1) - l2(i,2))/dblA(i)/4.0;
  120. }
  121. }
  122. #ifdef IGL_STATIC_LIBRARY
  123. // Explicit template instantiation
  124. // generated by autoexplicit.sh
  125. template void igl::cotmatrix_entries<Eigen::Matrix<double, -1, -1, 1, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 1, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  126. // generated by autoexplicit.sh
  127. template void igl::cotmatrix_entries<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  128. template void igl::cotmatrix_entries<Eigen::Matrix<double, -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::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  129. template void igl::cotmatrix_entries<Eigen::Matrix<double, -1, -1, 1, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 1, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  130. template void igl::cotmatrix_entries<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::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  131. template void igl::cotmatrix_entries<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 4, 0, -1, 4>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 4, 0, -1, 4> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  132. template void igl::cotmatrix_entries<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  133. #endif