forward_kinematics.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 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 "forward_kinematics.h"
  9. #include <functional>
  10. IGL_INLINE void igl::forward_kinematics(
  11. const Eigen::MatrixXd & C,
  12. const Eigen::MatrixXi & BE,
  13. const Eigen::VectorXi & P,
  14. const std::vector<
  15. Eigen::Quaterniond,Eigen::aligned_allocator<Eigen::Quaterniond> > & dQ,
  16. std::vector<
  17. Eigen::Quaterniond,Eigen::aligned_allocator<Eigen::Quaterniond> > & vQ,
  18. std::vector<Eigen::Vector3d> & vT)
  19. {
  20. using namespace std;
  21. using namespace Eigen;
  22. const int m = BE.rows();
  23. assert(m == P.rows());
  24. assert(m == (int)dQ.size());
  25. vector<bool> computed(m,false);
  26. vQ.resize(m);
  27. vT.resize(m);
  28. function<void (int) > fk_helper = [&] (int b)
  29. {
  30. if(!computed[b])
  31. {
  32. if(P(b) < 0)
  33. {
  34. // base case for roots
  35. vQ[b] = dQ[b];
  36. const Vector3d r = C.row(BE(b,0)).transpose();
  37. vT[b] = r-dQ[b]*r;
  38. }else
  39. {
  40. // Otherwise first compute parent's
  41. const int p = P(b);
  42. fk_helper(p);
  43. vQ[b] = vQ[p] * dQ[b];
  44. const Vector3d r = C.row(BE(b,0)).transpose();
  45. vT[b] = vT[p] - vQ[b]*r + vQ[p]*r;
  46. }
  47. computed[b] = true;
  48. }
  49. };
  50. for(int b = 0;b<m;b++)
  51. {
  52. fk_helper(b);
  53. }
  54. }
  55. #ifndef IGL_HEADER_ONLY
  56. // Explicit template instanciation
  57. #endif