RandomTreeCreator.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "RandomTreeCreator.hpp"
  2. #include <cstdio>
  3. #include "IDRandomUtil.hpp"
  4. namespace btInverseDynamics
  5. {
  6. RandomTreeCreator::RandomTreeCreator(const int max_bodies, bool random_seed)
  7. {
  8. // seed generator
  9. if (random_seed)
  10. {
  11. randomInit(); // seeds with time()
  12. }
  13. else
  14. {
  15. randomInit(1); // seeds with 1
  16. }
  17. m_num_bodies = randomInt(1, max_bodies);
  18. }
  19. RandomTreeCreator::~RandomTreeCreator() {}
  20. int RandomTreeCreator::getNumBodies(int* num_bodies) const
  21. {
  22. *num_bodies = m_num_bodies;
  23. return 0;
  24. }
  25. int RandomTreeCreator::getBody(const int body_index, int* parent_index, JointType* joint_type,
  26. vec3* parent_r_parent_body_ref, mat33* body_T_parent_ref,
  27. vec3* body_axis_of_motion, idScalar* mass, vec3* body_r_body_com,
  28. mat33* body_I_body, int* user_int, void** user_ptr) const
  29. {
  30. if (0 == body_index)
  31. { //root body
  32. *parent_index = -1;
  33. }
  34. else
  35. {
  36. *parent_index = randomInt(0, body_index - 1);
  37. }
  38. switch (randomInt(0, 3))
  39. {
  40. case 0:
  41. *joint_type = FIXED;
  42. break;
  43. case 1:
  44. *joint_type = REVOLUTE;
  45. break;
  46. case 2:
  47. *joint_type = PRISMATIC;
  48. break;
  49. case 3:
  50. *joint_type = FLOATING;
  51. break;
  52. default:
  53. bt_id_error_message("randomInt() result out of range\n");
  54. return -1;
  55. }
  56. (*parent_r_parent_body_ref)(0) = randomFloat(-1.0, 1.0);
  57. (*parent_r_parent_body_ref)(1) = randomFloat(-1.0, 1.0);
  58. (*parent_r_parent_body_ref)(2) = randomFloat(-1.0, 1.0);
  59. bodyTParentFromAxisAngle(randomAxis(), randomFloat(-BT_ID_PI, BT_ID_PI), body_T_parent_ref);
  60. *body_axis_of_motion = randomAxis();
  61. *mass = randomMass();
  62. (*body_r_body_com)(0) = randomFloat(-1.0, 1.0);
  63. (*body_r_body_com)(1) = randomFloat(-1.0, 1.0);
  64. (*body_r_body_com)(2) = randomFloat(-1.0, 1.0);
  65. const double a = randomFloat(-BT_ID_PI, BT_ID_PI);
  66. const double b = randomFloat(-BT_ID_PI, BT_ID_PI);
  67. const double c = randomFloat(-BT_ID_PI, BT_ID_PI);
  68. vec3 ii = randomInertiaPrincipal();
  69. mat33 ii_diag;
  70. setZero(ii_diag);
  71. ii_diag(0, 0) = ii(0);
  72. ii_diag(1, 1) = ii(1);
  73. ii_diag(2, 2) = ii(2);
  74. *body_I_body = transformX(a) * transformY(b) * transformZ(c) * ii_diag *
  75. transformZ(-c) * transformY(-b) * transformX(-a);
  76. *user_int = 0;
  77. *user_ptr = 0;
  78. return 0;
  79. }
  80. } // namespace btInverseDynamics