MultiBodyTreeDebugGraph.cpp 1.9 KB

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