FileIO.cpp
Go to the documentation of this file.
00001 
00018 #include <baselib_binding/SharedPtr.h>
00019 
00020 #include <urdf2inventor/Helpers.h>
00021 #include <urdf2graspit/FileIO.h>
00022 
00023 #include <ros/ros.h>
00024 
00025 #include <map>
00026 #include <string>
00027 #include <vector>
00028   
00029 bool urdf2graspit::FileIO::initOutputDirImpl(const std::string& robotName) const
00030 {
00031     std::string robotDir;
00032     return initGraspItRobotDir(robotName,robotDir);
00033 }
00034 
00035 bool urdf2graspit::FileIO::initGraspItRobotDir(const std::string& robotName, std::string& robotDir) const
00036 {
00037     bool ret = urdf_traverser::helpers::makeDirectoryIfNeeded(getOutputDirectory().c_str());
00038 
00039     std::stringstream targetDir;
00040     targetDir << getOutputDirectory();
00041 
00042     std::vector<std::string> dirs;
00043     outStructure.getRobotDirPath(dirs);
00044     for (std::vector<std::string>::const_iterator it = dirs.begin(); it != dirs.end(); ++it)
00045     {
00046         targetDir << "/" << *it;
00047         ret = ret && urdf_traverser::helpers::makeDirectoryIfNeeded(targetDir.str().c_str());
00048     }
00049     if (!ret)
00050     {
00051         ROS_ERROR("Could not create directory structure in: %s", getOutputDirectory().c_str());
00052         return false;
00053     }
00054     robotDir = targetDir.str();
00055     return true;
00056 }
00057 
00058 
00059 std::string urdf2graspit::FileIO::getRobotDir(const std::string& robotName) const
00060 {
00061     std::stringstream targetDir;
00062     targetDir << getOutputDirectory() << "/" << outStructure.getRobotDirPath();
00063     return targetDir.str();
00064 }
00065 
00066 bool urdf2graspit::FileIO::writeGraspitMeshFiles(const std::map<std::string, std::string>& meshDescXML) const
00067 {
00068     std::string outputMeshDir =  getOutputDirectory() + "/" + outStructure.getMeshDirPath();
00069     if (!urdf_traverser::helpers::makeDirectoryIfNeeded(outputMeshDir.c_str()))
00070     {
00071         ROS_ERROR("Could not create directory %s", outputMeshDir.c_str());
00072         return false;
00073     }
00074 
00075     std::map<std::string, std::string>::const_iterator mdit;
00076     for (mdit = meshDescXML.begin(); mdit != meshDescXML.end(); ++mdit)
00077     {
00078         std::stringstream outXMLFile;
00079         std::string _outFilename=mdit->first;
00080         outXMLFile << outputMeshDir << "/" << _outFilename << ".xml";
00081         if (!urdf_traverser::helpers::writeToFile(mdit->second, outXMLFile.str()))
00082         {
00083             ROS_ERROR("Could not write file %s", outXMLFile.str().c_str());
00084             return false;
00085         }
00086     }
00087 
00088     return true;
00089 }
00090 
00091 bool urdf2graspit::FileIO::writeEigen(const std::string& robotName, const std::string& content) const
00092 {
00093     std::string eigenDir = getOutputDirectory() + "/" + outStructure.getEigenGraspDirPath();
00094     if (!urdf_traverser::helpers::makeDirectoryIfNeeded(eigenDir.c_str()))
00095     {
00096         ROS_ERROR("Could not make directory %s", eigenDir.c_str());
00097         return false;
00098     }
00099 
00100     std::string eigenFile = getOutputDirectory() + "/" + outStructure.getEigenGraspFilePath("");
00101     if (!urdf_traverser::helpers::writeToFile(content, eigenFile))
00102     {
00103         ROS_ERROR("Could not write eigengrasp file %s", eigenFile.c_str());
00104         return false;
00105     }
00106     return true;
00107 }
00108 
00109 
00110 bool urdf2graspit::FileIO::writeContacts(const std::string& robotName, const std::string& content,
00111     const std::string& filename) const
00112 {
00113     std::string contactDir = getOutputDirectory() + "/" + outStructure.getContactsDirPath();
00114     if (!urdf_traverser::helpers::makeDirectoryIfNeeded(contactDir.c_str()))
00115     {
00116         ROS_ERROR("Could not make directory %s", contactDir.c_str());
00117         return false;
00118     }
00119     
00120     std::string contactFilename = getOutputDirectory() + "/" + outStructure.getContactsFilePath(filename);
00121     ROS_INFO("Writing contacts to file %s",contactFilename.c_str());
00122     return urdf_traverser::helpers::writeToFile(content, contactFilename);
00123 }
00124 
00125 
00126 bool urdf2graspit::FileIO::writeRobotXML(const std::string& robotName, const std::string& content) const
00127 {
00128     // target dir is robot directory
00129     std::string outDir = getOutputDirectory() + "/" + outStructure.getRobotDirPath();
00130     if (!urdf_traverser::helpers::makeDirectoryIfNeeded(outDir.c_str()))
00131     {
00132         ROS_ERROR("Could not make directory %s", outDir.c_str());
00133         return false;
00134     }
00135 
00136     std::string robotFile = getOutputDirectory() + "/" + outStructure.getRobotFilePath();
00137     return urdf_traverser::helpers::writeToFile(content, robotFile);
00138 }
00139 
00140 
00141 bool urdf2graspit::FileIO::writeWorldFileTemplate(
00142     const std::string& robotName,
00143     const std::string& content) const
00144 {
00145     std::string outDir = getOutputDirectory() + "/" + outStructure.getWorldDirPath();
00146     if (!urdf_traverser::helpers::makeDirectoryIfNeeded(outDir.c_str()))
00147     {
00148         ROS_ERROR("Could not make directory %s", outDir.c_str());
00149         return false;
00150     }
00151     std::string outFilename = getOutputDirectory() + "/" + outStructure.getWorldFilePath();
00152     // ROS_INFO("Writing world file %s",outFilename.c_str());
00153     return urdf_traverser::helpers::writeToFile(content, outFilename);
00154 }
00155 
00156 bool urdf2graspit::FileIO::writeImpl(const ConversionResultPtr& data) const
00157 {
00158     GraspItConversionResultPtr graspitData = baselib_binding_ns::dynamic_pointer_cast<GraspItConversionResultT>(data);
00159 
00160     if (!graspitData.get())
00161     {
00162         ROS_ERROR("Conversion result is not of right type");
00163         return false;
00164     }    
00165 
00166     if (!writeRobotXML(graspitData->robotName, graspitData->robotXML))
00167     {
00168         ROS_ERROR("Could not write EigenGrasp file");
00169         return false;
00170     }
00171 
00172     if (!writeGraspitMeshFiles(graspitData->meshXMLDesc))
00173     {
00174         ROS_ERROR("Could not write mesh files");
00175         return false;
00176     }
00177 
00178     if (!writeEigen(graspitData->robotName, graspitData->eigenGraspXML))
00179     {
00180         ROS_ERROR("Could not write EigenGrasp file");
00181         return false;
00182     }
00183 
00184     /*if (!writeContacts(graspitData->robotName, graspitData->contacts))
00185     {
00186         ROS_ERROR("Could not write EigenGrasp file");
00187         return false;
00188     }*/
00189 
00190     if (!writeWorldFileTemplate(graspitData->robotName, graspitData->world))
00191     {
00192         ROS_ERROR("Could not write world file");
00193         return false;
00194     }
00195 
00196     return true;
00197 }


urdf2graspit
Author(s): Jennifer Buehler
autogenerated on Wed May 8 2019 02:53:45