cotangent.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. break;
  77. }
  78. default:
  79. {
  80. fprintf(stderr,
  81. "cotangent.h: Error: Simplex size (%d) not supported\n", simplex_size);
  82. assert(false);
  83. }
  84. }
  85. }
  86. #ifndef IGL_HEADER_ONLY
  87. // Explicit template specialization
  88. 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> >&);
  89. #endif