User2InternalIndex.cpp 2.7 KB

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