MultiBodyTreeInitCache.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "MultiBodyTreeInitCache.hpp"
  2. namespace btInverseDynamics {
  3. MultiBodyTree::InitCache::InitCache() {
  4. m_inertias.resize(0);
  5. m_joints.resize(0);
  6. m_num_dofs = 0;
  7. m_root_index=-1;
  8. }
  9. int MultiBodyTree::InitCache::addBody(const int body_index, const int parent_index,
  10. const JointType joint_type,
  11. const vec3& parent_r_parent_body_ref,
  12. const mat33& body_T_parent_ref,
  13. const vec3& body_axis_of_motion, const idScalar mass,
  14. const vec3& body_r_body_com, const mat33& body_I_body,
  15. const int user_int, void* user_ptr) {
  16. switch (joint_type) {
  17. case REVOLUTE:
  18. case PRISMATIC:
  19. m_num_dofs += 1;
  20. break;
  21. case FIXED:
  22. // does not add a degree of freedom
  23. // m_num_dofs+=0;
  24. break;
  25. case FLOATING:
  26. m_num_dofs += 6;
  27. break;
  28. default:
  29. bt_id_error_message("unknown joint type %d\n", joint_type);
  30. return -1;
  31. }
  32. if(-1 == parent_index) {
  33. if(m_root_index>=0) {
  34. bt_id_error_message("trying to add body %d as root, but already added %d as root body\n",
  35. body_index, m_root_index);
  36. return -1;
  37. }
  38. m_root_index=body_index;
  39. }
  40. JointData joint;
  41. joint.m_child = body_index;
  42. joint.m_parent = parent_index;
  43. joint.m_type = joint_type;
  44. joint.m_parent_pos_parent_child_ref = parent_r_parent_body_ref;
  45. joint.m_child_T_parent_ref = body_T_parent_ref;
  46. joint.m_child_axis_of_motion = body_axis_of_motion;
  47. InertiaData body;
  48. body.m_mass = mass;
  49. body.m_body_pos_body_com = body_r_body_com;
  50. body.m_body_I_body = body_I_body;
  51. m_inertias.push_back(body);
  52. m_joints.push_back(joint);
  53. m_user_int.push_back(user_int);
  54. m_user_ptr.push_back(user_ptr);
  55. return 0;
  56. }
  57. int MultiBodyTree::InitCache::getInertiaData(const int index, InertiaData* inertia) const {
  58. if (index < 0 || index > static_cast<int>(m_inertias.size())) {
  59. bt_id_error_message("index out of range\n");
  60. return -1;
  61. }
  62. *inertia = m_inertias[index];
  63. return 0;
  64. }
  65. int MultiBodyTree::InitCache::getUserInt(const int index, int* user_int) const {
  66. if (index < 0 || index > static_cast<int>(m_user_int.size())) {
  67. bt_id_error_message("index out of range\n");
  68. return -1;
  69. }
  70. *user_int = m_user_int[index];
  71. return 0;
  72. }
  73. int MultiBodyTree::InitCache::getUserPtr(const int index, void** user_ptr) const {
  74. if (index < 0 || index > static_cast<int>(m_user_ptr.size())) {
  75. bt_id_error_message("index out of range\n");
  76. return -1;
  77. }
  78. *user_ptr = m_user_ptr[index];
  79. return 0;
  80. }
  81. int MultiBodyTree::InitCache::getJointData(const int index, JointData* joint) const {
  82. if (index < 0 || index > static_cast<int>(m_joints.size())) {
  83. bt_id_error_message("index out of range\n");
  84. return -1;
  85. }
  86. *joint = m_joints[index];
  87. return 0;
  88. }
  89. int MultiBodyTree::InitCache::buildIndexSets() {
  90. // NOTE: This function assumes that proper indices were provided
  91. // User2InternalIndex from utils can be used to facilitate this.
  92. m_parent_index.resize(numBodies());
  93. for (idArrayIdx j = 0; j < m_joints.size(); j++) {
  94. const JointData& joint = m_joints[j];
  95. m_parent_index[joint.m_child] = joint.m_parent;
  96. }
  97. return 0;
  98. }
  99. }