MultiBodyTreeInitCache.cpp 3.1 KB

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