forward_kinematics.cpp 1.5 KB

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