dqs.cpp 2.8 KB

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