dqs.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "dqs.h"
  2. #include <Eigen/Geometry>
  3. template <
  4. typename DerivedV,
  5. typename DerivedW,
  6. typename Q,
  7. typename T,
  8. typename DerivedU>
  9. IGL_INLINE void igl::dqs(
  10. const Eigen::PlainObjectBase<DerivedV> & V,
  11. const Eigen::PlainObjectBase<DerivedW> & W,
  12. const std::vector<Q> & vQ,
  13. const std::vector<T> & vT,
  14. Eigen::PlainObjectBase<DerivedU> & U)
  15. {
  16. using namespace std;
  17. using namespace igl;
  18. assert(V.rows() <= W.rows());
  19. assert(W.cols() == (int)vQ.size());
  20. assert(W.cols() == (int)vT.size());
  21. // resize output
  22. U.resize(V.rows(),V.cols());
  23. // Convert quats + trans into dual parts
  24. vector<Q> vD(vQ.size());
  25. for(int c = 0;c<W.cols();c++)
  26. {
  27. const Q & q = vQ[c];
  28. vD[c].w() = -0.5*( vT[c](0)*q.x() + vT[c](1)*q.y() + vT[c](2)*q.z());
  29. vD[c].x() = 0.5*( vT[c](0)*q.w() + vT[c](1)*q.z() - vT[c](2)*q.y());
  30. vD[c].y() = 0.5*(-vT[c](0)*q.z() + vT[c](1)*q.w() + vT[c](2)*q.x());
  31. vD[c].z() = 0.5*( vT[c](0)*q.y() - vT[c](1)*q.x() + vT[c](2)*q.w());
  32. }
  33. // Loop over vertices
  34. const int nv = V.rows();
  35. #pragma omp parallel for if (nv>10000)
  36. for(int i = 0;i<nv;i++)
  37. {
  38. Q b0(0,0,0,0);
  39. Q be(0,0,0,0);
  40. // Loop over handles
  41. for(int c = 0;c<W.cols();c++)
  42. {
  43. b0.coeffs() += W(i,c) * vQ[c].coeffs();
  44. be.coeffs() += W(i,c) * vD[c].coeffs();
  45. }
  46. Q ce = be;
  47. ce.coeffs() /= b0.norm();
  48. Q c0 = b0;
  49. c0.coeffs() /= b0.norm();
  50. // See algorithm 1 in "Geometric skinning with approximate dual quaternion
  51. // blending" by Kavan et al
  52. T v = V.row(i);
  53. T d0 = c0.vec();
  54. T de = ce.vec();
  55. typename Q::Scalar a0 = c0.w();
  56. typename Q::Scalar ae = ce.w();
  57. U.row(i) = v + 2*d0.cross(d0.cross(v) + a0*v) + 2*(a0*de - ae*d0 + d0.cross(de));
  58. }
  59. }
  60. #ifndef IGL_HEADER_ONLY
  61. // Explicit template instanciation
  62. template void igl::dqs<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Quaternion<double, 0>, Eigen::Matrix<double, 3, 1, 0, 3, 1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, std::vector<Eigen::Quaternion<double, 0>, std::allocator<Eigen::Quaternion<double, 0> > > const&, std::vector<Eigen::Matrix<double, 3, 1, 0, 3, 1>, std::allocator<Eigen::Matrix<double, 3, 1, 0, 3, 1> > > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  63. #endif