dihedral_angles.cpp 4.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 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 "dihedral_angles.h"
  9. #include <cassert>
  10. template <
  11. typename DerivedV,
  12. typename DerivedT,
  13. typename Derivedtheta,
  14. typename Derivedcos_theta>
  15. IGL_INLINE void igl::dihedral_angles(
  16. Eigen::PlainObjectBase<DerivedV>& V,
  17. Eigen::PlainObjectBase<DerivedT>& T,
  18. Eigen::PlainObjectBase<Derivedtheta>& theta,
  19. Eigen::PlainObjectBase<Derivedcos_theta>& cos_theta)
  20. {
  21. using namespace Eigen;
  22. assert(T.cols() == 4);
  23. Matrix<typename Derivedtheta::Scalar,Dynamic,6> l;
  24. edge_lengths(V,T,l);
  25. Matrix<typename Derivedtheta::Scalar,Dynamic,4> s;
  26. face_areas(l,s);
  27. return dihedral_angles_intrinsic(l,s,theta,cos_theta);
  28. }
  29. template <
  30. typename DerivedL,
  31. typename DerivedA,
  32. typename Derivedtheta,
  33. typename Derivedcos_theta>
  34. IGL_INLINE void igl::dihedral_angles_intrinsic(
  35. Eigen::PlainObjectBase<DerivedL>& L,
  36. Eigen::PlainObjectBase<DerivedA>& A,
  37. Eigen::PlainObjectBase<Derivedtheta>& theta,
  38. Eigen::PlainObjectBase<Derivedcos_theta>& cos_theta)
  39. {
  40. using namespace Eigen;
  41. const int m = L.rows();
  42. assert(m == A.rows());
  43. // Law of cosines
  44. // http://math.stackexchange.com/a/49340/35376
  45. Matrix<typename Derivedtheta::Scalar,Dynamic,6> H_sqr(m,6);
  46. H_sqr.col(0) = (1./16.) * (4. * L.col(3).array().square() * L.col(0).array().square() -
  47. ((L.col(1).array().square() + L.col(4).array().square()) -
  48. (L.col(2).array().square() + L.col(5).array().square())).square());
  49. H_sqr.col(1) = (1./16.) * (4. * L.col(4).array().square() * L.col(1).array().square() -
  50. ((L.col(2).array().square() + L.col(5).array().square()) -
  51. (L.col(3).array().square() + L.col(0).array().square())).square());
  52. H_sqr.col(2) = (1./16.) * (4. * L.col(5).array().square() * L.col(2).array().square() -
  53. ((L.col(3).array().square() + L.col(0).array().square()) -
  54. (L.col(4).array().square() + L.col(1).array().square())).square());
  55. H_sqr.col(3) = (1./16.) * (4. * L.col(0).array().square() * L.col(3).array().square() -
  56. ((L.col(4).array().square() + L.col(1).array().square()) -
  57. (L.col(5).array().square() + L.col(2).array().square())).square());
  58. H_sqr.col(4) = (1./16.) * (4. * L.col(1).array().square() * L.col(4).array().square() -
  59. ((L.col(5).array().square() + L.col(2).array().square()) -
  60. (L.col(0).array().square() + L.col(3).array().square())).square());
  61. H_sqr.col(5) = (1./16.) * (4. * L.col(2).array().square() * L.col(5).array().square() -
  62. ((L.col(0).array().square() + L.col(3).array().square()) -
  63. (L.col(1).array().square() + L.col(4).array().square())).square());
  64. cos_theta.resize(m,6);
  65. cos_theta.col(0) = (H_sqr.col(0).array() -
  66. A.col(1).array().square() - A.col(2).array().square()).array() /
  67. (-2.*A.col(1).array() * A.col(2).array());
  68. cos_theta.col(1) = (H_sqr.col(1).array() -
  69. A.col(2).array().square() - A.col(0).array().square()).array() /
  70. (-2.*A.col(2).array() * A.col(0).array());
  71. cos_theta.col(2) = (H_sqr.col(2).array() -
  72. A.col(0).array().square() - A.col(1).array().square()).array() /
  73. (-2.*A.col(0).array() * A.col(1).array());
  74. cos_theta.col(3) = (H_sqr.col(3).array() -
  75. A.col(3).array().square() - A.col(0).array().square()).array() /
  76. (-2.*A.col(3).array() * A.col(0).array());
  77. cos_theta.col(4) = (H_sqr.col(4).array() -
  78. A.col(3).array().square() - A.col(1).array().square()).array() /
  79. (-2.*A.col(3).array() * A.col(1).array());
  80. cos_theta.col(5) = (H_sqr.col(5).array() -
  81. A.col(3).array().square() - A.col(2).array().square()).array() /
  82. (-2.*A.col(3).array() * A.col(2).array());
  83. theta = cos_theta.array().acos();
  84. cos_theta.resize(m,6);
  85. }
  86. #ifdef IGL_STATIC_LIBRARY
  87. // Explicit template specialization
  88. template void igl::dihedral_angles_intrinsic<Eigen::Matrix<double, -1, 6, 0, -1, 6>, Eigen::Matrix<double, -1, 4, 0, -1, 4>, Eigen::Matrix<double, -1, 6, 0, -1, 6>, Eigen::Matrix<double, -1, 6, 0, -1, 6> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 6, 0, -1, 6> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 4, 0, -1, 4> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 6, 0, -1, 6> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 6, 0, -1, 6> >&);
  89. #endif