collada_to_graphviz.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 * 
00004 *  Copyright (c) 2008, Willow Garage, Inc.
00005 *  All rights reserved.
00006 * 
00007 *  Redistribution and use in source and binary forms, with or without
00008 *  modification, are permitted provided that the following conditions
00009 *  are met:
00010 * 
00011 *   * Redstributions of source code must retain the above copyright
00012 *     notice, this list of conditions and the following disclaimer.
00013 *   * Redistributions in binary form must reproduce the above
00014 *     copyright notice, this list of conditions and the following
00015 *     disclaimer in the documentation and/or other materials provided
00016 *     with the distribution.
00017 *   * Neither the name of the Willow Garage nor the names of its
00018 *     contributors may be used to endorse or promote products derived
00019 *     from this software without specific prior written permission.
00020 * 
00021 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 *  POSSIBILITY OF SUCH DAMAGE.
00033 *********************************************************************/
00034 
00035 // this program was modified from urdf_to_graphviz.cpp
00036 #include <ros/ros.h>
00037 #include "collada_parser/collada_parser.h"
00038 #include "urdf/model.h"
00039 #include "urdf_parser/urdf_parser.h"
00040 #include <iostream>
00041 #include <fstream>
00042 
00043 using namespace urdf;
00044 using namespace std;
00045 
00046 #define PRINT_ORIGIN(os, origin) \
00047 os << "xyz: " << origin.position.x << " " << origin.position.y << " " << origin.position.z << " "; \
00048 { double r,p,y; origin.rotation.getRPY(r, p, y); \
00049   os << "rpy: " << r << " " << p << " " << y; }
00050 
00051 #define PRINT_ORIGIN_XML(os, origin) \
00052   os << "xyz=\"" << origin.position.x << " " << origin.position.y << " " << origin.position.z << "\""; \
00053   { double r,p,y; origin.rotation.getRPY(r, p, y);  os << " rpy=\"" << r << " " << p << " " << y << "\""; }
00054 
00055 #define PRINT_GEOM(os, geometry) \
00056   if ( geometry->type == urdf::Geometry::MESH ) { os << "geom: name: " << ((urdf::Mesh *)geometry.get())->filename; }
00057 
00058 void addChildLinkNames(boost::shared_ptr<const Link> link, ofstream& os)
00059 {
00060   double r, p, y;
00061   os << "\"" << link->name << "\" [label=\"" << link->name;
00062   if ( link->inertial != NULL ) {
00063     os << "\\n inertial xyz: "
00064        << link->inertial->origin.position.x << " "
00065        << link->inertial->origin.position.y << " "
00066        << link->inertial->origin.position.z << " ";
00067     link->inertial->origin.rotation.getRPY(r, p, y);
00068     os << "rpy: " << r << " " << p << " " << y << "\\n";
00069     os << "mass: " << link->inertial->mass << " ";
00070     os << "matrix: "
00071        << link->inertial->ixx << " "
00072        << link->inertial->ixy << " "
00073        << link->inertial->ixz << " "
00074        << link->inertial->iyy << " "
00075        << link->inertial->iyz << " "
00076        << link->inertial->izz << "\\n";
00077     //os << "\"];" << endl;
00078   } else {
00079     //os << "\"];" << endl;
00080   }
00081 
00082   if ( !!link->visual ) {
00083     os << "\\n";
00084     os << "visual: ";
00085     PRINT_ORIGIN ( os, link->visual->origin );
00086     os << "\\n";
00087     PRINT_GEOM ( os, link->visual->geometry );
00088     //os << "\\n";
00089   }
00090 
00091   if ( !!link->collision ) {
00092     os << "\\n";
00093     os << "collision: ";
00094     PRINT_ORIGIN ( os, link->collision->origin );
00095     os << "\\n";
00096     PRINT_GEOM ( os, link->collision->geometry );
00097     //os << "\\n";
00098   }
00099 
00100   os << "\"];" << endl;
00101   for (std::vector<boost::shared_ptr<Link> >::const_iterator child = link->child_links.begin(); child != link->child_links.end(); child++)
00102     addChildLinkNames(*child, os);
00103 }
00104 
00105 void addChildJointNames(boost::shared_ptr<const Link> link, ofstream& os)
00106 {
00107   double r, p, y;
00108   for (std::vector<boost::shared_ptr<Link> >::const_iterator child = link->child_links.begin(); child != link->child_links.end(); child++){
00109     (*child)->parent_joint->parent_to_joint_origin_transform.rotation.getRPY(r,p,y);
00110     os << "\"" << link->name << "\" -> \"" << (*child)->parent_joint->name 
00111        << "\" [label=\"xyz: "
00112        << (*child)->parent_joint->parent_to_joint_origin_transform.position.x << " " 
00113        << (*child)->parent_joint->parent_to_joint_origin_transform.position.y << " " 
00114        << (*child)->parent_joint->parent_to_joint_origin_transform.position.z << " " 
00115        << "\\nrpy: " << r << " " << p << " " << y << "\"]" << endl;
00116     os << "\"" << (*child)->parent_joint->name << "\" -> \"" << (*child)->name << "\"" << endl;
00117 
00118 
00119     addChildJointNames(*child, os);
00120   }
00121 }
00122 
00123 void printTree(boost::shared_ptr<const Link> link, string file)
00124 {
00125   std::ofstream os;
00126   os.open(file.c_str());
00127   os << "digraph G {" << endl;
00128 
00129   os << "node [shape=box];" << endl;
00130   addChildLinkNames(link, os);
00131 
00132   os << "node [shape=ellipse, color=blue, fontcolor=blue];" << endl;
00133   addChildJointNames(link, os);
00134 
00135   os << "}" << endl;
00136   os.close();
00137 }
00138 
00139 int main(int argc, char** argv)
00140 {
00141   if (argc != 2){
00142     std::cerr << "Usage: urdf_to_graphiz input.xml" << std::endl;
00143     return -1;
00144   }
00145 
00146   // get the entire file
00147   std::string xml_string;
00148   std::fstream xml_file(argv[1], std::fstream::in);
00149   while ( xml_file.good() )
00150   {
00151     std::string line;
00152     std::getline( xml_file, line);
00153     xml_string += (line + "\n");
00154   }
00155   xml_file.close();
00156 
00157   boost::shared_ptr<ModelInterface> robot;
00158   if( xml_string.find("<COLLADA") != std::string::npos )
00159   {
00160     ROS_DEBUG("Parsing robot collada xml string");
00161     robot = parseCollada(xml_string);
00162   }
00163   else
00164   {
00165     ROS_DEBUG("Parsing robot urdf xml string");
00166     robot = parseURDF(xml_string);
00167   }
00168 
00169   if (!robot){
00170     std::cerr << "ERROR: Model Parsing the xml failed" << std::endl;
00171     return -1;
00172   }
00173   string output = robot->getName();
00174 
00175   // print entire tree to file
00176   printTree(robot->getRoot(), output+".gv");
00177   cout << "Created file " << output << ".gv" << endl;
00178 
00179   string command = "dot -Tpdf "+output+".gv  -o "+output+".pdf";
00180   system(command.c_str());
00181   cout << "Created file " << output << ".pdf" << endl;
00182 
00183   return 0;
00184 }


collada_tools
Author(s): Yohei Kakiuchi (youhei@jsk.t.u-tokyo.ac.jp)
autogenerated on Mon Oct 6 2014 01:10:10