2
0

MultiBodyTreeDebugGraph.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include "MultiBodyTreeDebugGraph.hpp"
  2. #include <cstdio>
  3. namespace btInverseDynamics
  4. {
  5. int writeGraphvizDotFile(const MultiBodyTree* tree, const MultiBodyNameMap* map,
  6. const char* filename)
  7. {
  8. if (0x0 == tree)
  9. {
  10. bt_id_error_message("tree pointer is null\n");
  11. return -1;
  12. }
  13. if (0x0 == filename)
  14. {
  15. bt_id_error_message("filename is null\n");
  16. return -1;
  17. }
  18. FILE* fp = fopen(filename, "w");
  19. if (NULL == fp)
  20. {
  21. bt_id_error_message("cannot open file %s for writing\n", filename);
  22. return -1;
  23. }
  24. fprintf(fp,
  25. "// to generate postscript file, run dot -Tps %s -o %s.ps\n"
  26. "// details see graphviz documentation at http://graphviz.org\n"
  27. "digraph tree {\n",
  28. filename, filename);
  29. for (int body = 0; body < tree->numBodies(); body++)
  30. {
  31. std::string name;
  32. if (0x0 != map)
  33. {
  34. if (-1 == map->getBodyName(body, &name))
  35. {
  36. bt_id_error_message("can't get name of body %d\n", body);
  37. return -1;
  38. }
  39. fprintf(fp, " %d [label=\"%d/%s\"];\n", body, body, name.c_str());
  40. }
  41. }
  42. for (int body = 0; body < tree->numBodies(); body++)
  43. {
  44. int parent;
  45. const char* joint_type;
  46. int qi;
  47. if (-1 == tree->getParentIndex(body, &parent))
  48. {
  49. bt_id_error_message("indexing error\n");
  50. return -1;
  51. }
  52. if (-1 == tree->getJointTypeStr(body, &joint_type))
  53. {
  54. bt_id_error_message("indexing error\n");
  55. return -1;
  56. }
  57. if (-1 == tree->getDoFOffset(body, &qi))
  58. {
  59. bt_id_error_message("indexing error\n");
  60. return -1;
  61. }
  62. if (-1 != parent)
  63. {
  64. fprintf(fp, " %d -> %d [label= \"type:%s, q=%d\"];\n", parent, body,
  65. joint_type, qi);
  66. }
  67. }
  68. fprintf(fp, "}\n");
  69. fclose(fp);
  70. return 0;
  71. }
  72. } // namespace btInverseDynamics