dihedral_angles_intrinsic.cpp 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Alec Jacobson <[email protected]>
  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_intrinsic.h"
  9. #include "edge_lengths.h"
  10. #include "face_areas.h"
  11. #include <cassert>
  12. template <
  13. typename DerivedL,
  14. typename DerivedA,
  15. typename Derivedtheta,
  16. typename Derivedcos_theta>
  17. IGL_INLINE void igl::dihedral_angles_intrinsic(
  18. const Eigen::MatrixBase<DerivedL>& L,
  19. const Eigen::MatrixBase<DerivedA>& A,
  20. Eigen::PlainObjectBase<Derivedtheta>& theta,
  21. Eigen::PlainObjectBase<Derivedcos_theta>& cos_theta)
  22. {
  23. const int m = L.rows();
  24. assert(m == A.rows());
  25. // Law of cosines
  26. // http://math.stackexchange.com/a/49340/35376
  27. Eigen::Matrix<typename Derivedtheta::Scalar ,Eigen::Dynamic,6> H_sqr(m,6);
  28. H_sqr.col(0) = (1./16.) * (4. * L.col(3).array().square() * L.col(0).array().square() -
  29. ((L.col(1).array().square() + L.col(4).array().square()) -
  30. (L.col(2).array().square() + L.col(5).array().square())).square());
  31. H_sqr.col(1) = (1./16.) * (4. * L.col(4).array().square() * L.col(1).array().square() -
  32. ((L.col(2).array().square() + L.col(5).array().square()) -
  33. (L.col(3).array().square() + L.col(0).array().square())).square());
  34. H_sqr.col(2) = (1./16.) * (4. * L.col(5).array().square() * L.col(2).array().square() -
  35. ((L.col(3).array().square() + L.col(0).array().square()) -
  36. (L.col(4).array().square() + L.col(1).array().square())).square());
  37. H_sqr.col(3) = (1./16.) * (4. * L.col(0).array().square() * L.col(3).array().square() -
  38. ((L.col(4).array().square() + L.col(1).array().square()) -
  39. (L.col(5).array().square() + L.col(2).array().square())).square());
  40. H_sqr.col(4) = (1./16.) * (4. * L.col(1).array().square() * L.col(4).array().square() -
  41. ((L.col(5).array().square() + L.col(2).array().square()) -
  42. (L.col(0).array().square() + L.col(3).array().square())).square());
  43. H_sqr.col(5) = (1./16.) * (4. * L.col(2).array().square() * L.col(5).array().square() -
  44. ((L.col(0).array().square() + L.col(3).array().square()) -
  45. (L.col(1).array().square() + L.col(4).array().square())).square());
  46. cos_theta.resize(m,6);
  47. cos_theta.col(0) = (H_sqr.col(0).array() -
  48. A.col(1).array().square() - A.col(2).array().square()).array() /
  49. (-2.*A.col(1).array() * A.col(2).array());
  50. cos_theta.col(1) = (H_sqr.col(1).array() -
  51. A.col(2).array().square() - A.col(0).array().square()).array() /
  52. (-2.*A.col(2).array() * A.col(0).array());
  53. cos_theta.col(2) = (H_sqr.col(2).array() -
  54. A.col(0).array().square() - A.col(1).array().square()).array() /
  55. (-2.*A.col(0).array() * A.col(1).array());
  56. cos_theta.col(3) = (H_sqr.col(3).array() -
  57. A.col(3).array().square() - A.col(0).array().square()).array() /
  58. (-2.*A.col(3).array() * A.col(0).array());
  59. cos_theta.col(4) = (H_sqr.col(4).array() -
  60. A.col(3).array().square() - A.col(1).array().square()).array() /
  61. (-2.*A.col(3).array() * A.col(1).array());
  62. cos_theta.col(5) = (H_sqr.col(5).array() -
  63. A.col(3).array().square() - A.col(2).array().square()).array() /
  64. (-2.*A.col(3).array() * A.col(2).array());
  65. theta = cos_theta.array().acos();
  66. cos_theta.resize(m,6);
  67. }
  68. #ifdef IGL_STATIC_LIBRARY
  69. // Explicit template instantiation
  70. 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> >(const Eigen::MatrixBase<Eigen::Matrix<double, -1, 6, 0, -1, 6> >&, const Eigen::MatrixBase<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> >&);
  71. #endif