dqs.cpp 2.5 KB

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