RandomTreeCreator.cpp 2.4 KB

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