User2InternalIndex.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "User2InternalIndex.hpp"
  2. namespace btInverseDynamics {
  3. User2InternalIndex::User2InternalIndex() : m_map_built(false) {}
  4. void User2InternalIndex::addBody(const int body, const int parent) {
  5. m_user_parent_index_map[body] = parent;
  6. }
  7. int User2InternalIndex::findRoot(int index) {
  8. if (0 == m_user_parent_index_map.count(index)) {
  9. return index;
  10. }
  11. return findRoot(m_user_parent_index_map[index]);
  12. }
  13. // modelled after URDF2Bullet.cpp:void ComputeParentIndices(const
  14. // URDFImporterInterface& u2b, URDF2BulletCachedData& cache, int urdfLinkIndex,
  15. // int urdfParentIndex)
  16. void User2InternalIndex::recurseIndexSets(const int user_body_index) {
  17. m_user_to_internal[user_body_index] = m_current_index;
  18. m_current_index++;
  19. for (size_t i = 0; i < m_user_child_indices[user_body_index].size(); i++) {
  20. recurseIndexSets(m_user_child_indices[user_body_index][i]);
  21. }
  22. }
  23. int User2InternalIndex::buildMapping() {
  24. // find root index
  25. int user_root_index = -1;
  26. for (std::map<int, int>::iterator it = m_user_parent_index_map.begin();
  27. it != m_user_parent_index_map.end(); it++) {
  28. int current_root_index = findRoot(it->second);
  29. if (it == m_user_parent_index_map.begin()) {
  30. user_root_index = current_root_index;
  31. } else {
  32. if (user_root_index != current_root_index) {
  33. error_message("multiple roots (at least) %d and %d\n", user_root_index,
  34. current_root_index);
  35. return -1;
  36. }
  37. }
  38. }
  39. // build child index map
  40. for (std::map<int, int>::iterator it = m_user_parent_index_map.begin();
  41. it != m_user_parent_index_map.end(); it++) {
  42. m_user_child_indices[it->second].push_back(it->first);
  43. }
  44. m_current_index = -1;
  45. // build internal index set
  46. m_user_to_internal[user_root_index] = -1; // add map for root link
  47. recurseIndexSets(user_root_index);
  48. // reverse mapping
  49. for (std::map<int, int>::iterator it = m_user_to_internal.begin();
  50. it != m_user_to_internal.end(); it++) {
  51. m_internal_to_user[it->second] = it->first;
  52. }
  53. m_map_built = true;
  54. return 0;
  55. }
  56. int User2InternalIndex::user2internal(const int user, int *internal) const {
  57. if (!m_map_built) {
  58. return -1;
  59. }
  60. std::map<int, int>::const_iterator it;
  61. it = m_user_to_internal.find(user);
  62. if (it != m_user_to_internal.end()) {
  63. *internal = it->second;
  64. return 0;
  65. } else {
  66. error_message("no user index %d\n", user);
  67. return -1;
  68. }
  69. }
  70. int User2InternalIndex::internal2user(const int internal, int *user) const {
  71. if (!m_map_built) {
  72. return -1;
  73. }
  74. std::map<int, int>::const_iterator it;
  75. it = m_internal_to_user.find(internal);
  76. if (it != m_internal_to_user.end()) {
  77. *user = it->second;
  78. return 0;
  79. } else {
  80. error_message("no internal index %d\n", internal);
  81. return -1;
  82. }
  83. }
  84. }