dqs.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 "dqs.h"
  9. #include <Eigen/Geometry>
  10. template <
  11. typename DerivedV,
  12. typename DerivedW,
  13. typename Q,
  14. typename QAlloc,
  15. typename T,
  16. typename DerivedU>
  17. IGL_INLINE void igl::dqs(
  18. const Eigen::PlainObjectBase<DerivedV> & V,
  19. const Eigen::PlainObjectBase<DerivedW> & W,
  20. const std::vector<Q,QAlloc> & vQ,
  21. const std::vector<T> & vT,
  22. Eigen::PlainObjectBase<DerivedU> & U)
  23. {
  24. using namespace std;
  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.resize(V.rows(),V.cols());
  30. // Convert quats + trans into dual parts
  31. 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. #pragma omp parallel for if (nv>10000)
  43. for(int i = 0;i<nv;i++)
  44. {
  45. Q b0(0,0,0,0);
  46. Q be(0,0,0,0);
  47. // Loop over handles
  48. for(int c = 0;c<W.cols();c++)
  49. {
  50. b0.coeffs() += W(i,c) * vQ[c].coeffs();
  51. be.coeffs() += W(i,c) * vD[c].coeffs();
  52. }
  53. Q ce = be;
  54. ce.coeffs() /= b0.norm();
  55. Q c0 = b0;
  56. c0.coeffs() /= b0.norm();
  57. // See algorithm 1 in "Geometric skinning with approximate dual quaternion
  58. // blending" by Kavan et al
  59. T v = V.row(i);
  60. T d0 = c0.vec();
  61. T de = ce.vec();
  62. typename Q::Scalar a0 = c0.w();
  63. typename Q::Scalar ae = ce.w();
  64. U.row(i) = v + 2*d0.cross(d0.cross(v) + a0*v) + 2*(a0*de - ae*d0 + d0.cross(de));
  65. }
  66. }
  67. #ifdef IGL_STATIC_LIBRARY
  68. // Explicit template specialization
  69. 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> >&);
  70. #endif