MultiBodyTreeCreator.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include "MultiBodyTreeCreator.hpp"
  2. namespace btInverseDynamics {
  3. MultiBodyTree* CreateMultiBodyTree(const MultiBodyTreeCreator& creator) {
  4. int num_bodies;
  5. int parent_index;
  6. JointType joint_type;
  7. vec3 body_r_parent_body_ref;
  8. mat33 body_R_parent_ref;
  9. vec3 body_axis_of_motion;
  10. idScalar mass;
  11. vec3 body_r_body_com;
  12. mat33 body_I_body;
  13. int user_int;
  14. void* user_ptr;
  15. MultiBodyTree* tree = new MultiBodyTree();
  16. if (0x0 == tree) {
  17. error_message("cannot allocate tree\n");
  18. return 0x0;
  19. }
  20. // TODO: move to some policy argument
  21. tree->setAcceptInvalidMassParameters(false);
  22. // get number of bodies in the system
  23. if (-1 == creator.getNumBodies(&num_bodies)) {
  24. error_message("getting body indices\n");
  25. delete tree;
  26. return 0x0;
  27. }
  28. // get data for all bodies
  29. for (int index = 0; index < num_bodies; index++) {
  30. // get body parameters from user callbacks
  31. if (-1 ==
  32. creator.getBody(index, &parent_index, &joint_type, &body_r_parent_body_ref,
  33. &body_R_parent_ref, &body_axis_of_motion, &mass, &body_r_body_com,
  34. &body_I_body, &user_int, &user_ptr)) {
  35. error_message("getting data for body %d\n", index);
  36. delete tree;
  37. return 0x0;
  38. }
  39. // add body to system
  40. if (-1 ==
  41. tree->addBody(index, parent_index, joint_type, body_r_parent_body_ref,
  42. body_R_parent_ref, body_axis_of_motion, mass, body_r_body_com,
  43. body_I_body, user_int, user_ptr)) {
  44. error_message("adding body %d\n", index);
  45. delete tree;
  46. return 0x0;
  47. }
  48. }
  49. // finalize initialization
  50. if (-1 == tree->finalize()) {
  51. error_message("building system\n");
  52. delete tree;
  53. return 0x0;
  54. }
  55. return tree;
  56. }
  57. }