cotmatrix_entries.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 "cotmatrix_entries.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::cotmatrix_entries(
  17. const Eigen::PlainObjectBase<DerivedV>& V,
  18. const Eigen::PlainObjectBase<DerivedF>& F,
  19. Eigen::PlainObjectBase<DerivedC>& C)
  20. {
  21. using namespace std;
  22. using namespace Eigen;
  23. // simplex size (3: triangles, 4: tetrahedra)
  24. int simplex_size = F.cols();
  25. // Number of elements
  26. int m = F.rows();
  27. // Law of cosines + law of sines
  28. switch(simplex_size)
  29. {
  30. case 3:
  31. {
  32. // Triangles
  33. //Matrix<typename DerivedC::Scalar,Dynamic,3> l;
  34. //edge_lengths(V,F,l);
  35. // edge lengths numbered same as opposite vertices
  36. Matrix<typename DerivedC::Scalar,Dynamic,3> l;
  37. igl::edge_lengths(V,F,l);
  38. // double area
  39. Matrix<typename DerivedC::Scalar,Dynamic,1> dblA;
  40. doublearea(l,dblA);
  41. // cotangents and diagonal entries for element matrices
  42. // correctly divided by 4 (alec 2010)
  43. C.resize(m,3);
  44. for(int i = 0;i<m;i++)
  45. {
  46. 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;
  47. 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;
  48. 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;
  49. }
  50. break;
  51. }
  52. case 4:
  53. {
  54. // edge lengths numbered same as opposite vertices
  55. Matrix<typename DerivedC::Scalar,Dynamic,6> l;
  56. edge_lengths(V,F,l);
  57. Matrix<typename DerivedC::Scalar,Dynamic,4> s;
  58. face_areas(l,s);
  59. Matrix<typename DerivedC::Scalar,Dynamic,6> cos_theta,theta;
  60. dihedral_angles_intrinsic(l,s,theta,cos_theta);
  61. // volume
  62. Matrix<typename DerivedC::Scalar,Dynamic,1> vol;
  63. volume(l,vol);
  64. // Law of sines
  65. // http://mathworld.wolfram.com/Tetrahedron.html
  66. Matrix<typename DerivedC::Scalar,Dynamic,6> sin_theta(m,6);
  67. sin_theta.col(0) = vol.array() / ((2./(3.*l.col(0).array())).array() * s.col(1).array() * s.col(2).array());
  68. sin_theta.col(1) = vol.array() / ((2./(3.*l.col(1).array())).array() * s.col(2).array() * s.col(0).array());
  69. sin_theta.col(2) = vol.array() / ((2./(3.*l.col(2).array())).array() * s.col(0).array() * s.col(1).array());
  70. sin_theta.col(3) = vol.array() / ((2./(3.*l.col(3).array())).array() * s.col(3).array() * s.col(0).array());
  71. sin_theta.col(4) = vol.array() / ((2./(3.*l.col(4).array())).array() * s.col(3).array() * s.col(1).array());
  72. sin_theta.col(5) = vol.array() / ((2./(3.*l.col(5).array())).array() * s.col(3).array() * s.col(2).array());
  73. // http://arxiv.org/pdf/1208.0354.pdf Page 18
  74. C = (1./6.) * l.array() * cos_theta.array() / sin_theta.array();
  75. break;
  76. }
  77. default:
  78. {
  79. fprintf(stderr,
  80. "cotmatrix_entries.h: Error: Simplex size (%d) not supported\n", simplex_size);
  81. assert(false);
  82. }
  83. }
  84. }
  85. #ifdef IGL_STATIC_LIBRARY
  86. // Explicit template specialization
  87. // generated by autoexplicit.sh
  88. 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::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 4, 0, -1, 4> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  89. // generated by autoexplicit.sh
  90. 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::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  91. 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::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> >&);
  92. #endif