00001 #include <cerrno> 00002 #include <cstdlib> 00003 #include <iostream> 00004 #include <string> 00005 00006 #include <boost/format.hpp> 00007 #include <boost/filesystem/path.hpp> 00008 00009 #include <ros/ros.h> 00010 00011 #include "file.hh" 00012 #include "names.hh" 00013 00014 00015 boost::filesystem::path 00016 getInitFileFromModelName (const std::string& modelName, 00017 const std::string& defaultPath) 00018 { 00019 boost::filesystem::path res(defaultPath); 00020 res /= modelName; 00021 res /= modelName + ".init"; 00022 return res; 00023 } 00024 00025 boost::filesystem::path 00026 getModelFileFromModelName (const std::string& modelName, 00027 const std::string& defaultPath) 00028 { 00029 boost::filesystem::path res(defaultPath); 00030 res /= modelName; 00031 res /= modelName + ".wrl"; 00032 return res; 00033 } 00034 00035 boost::filesystem::path 00036 getConfigurationFileFromModelName (const std::string& modelName, 00037 const std::string& defaultPath) 00038 { 00039 boost::filesystem::path res(defaultPath); 00040 res /= modelName; 00041 res /= modelName + ".xml"; 00042 return res; 00043 } 00044 00045 boost::filesystem::path 00046 getInitialPoseFileFromModelName (const std::string& modelName, 00047 const std::string& defaultPath) 00048 { 00049 boost::filesystem::path res(defaultPath); 00050 res /= modelName; 00051 res /= modelName + ".0.pos"; 00052 return res; 00053 } 00054 00055 bool 00056 makeModelFile(boost::filesystem::ofstream& modelStream, 00057 std::string& fullModelPath) 00058 { 00059 std::string modelDescription; 00060 if (!ros::param::has(visp_tracker::model_description_param)) 00061 { 00062 ROS_ERROR_STREAM("Failed to initialize: no model is provided."); 00063 return false; 00064 } 00065 ROS_DEBUG_STREAM("Trying to load the model from the parameter server."); 00066 00067 ros::param::get(visp_tracker::model_description_param, modelDescription); 00068 00069 char* tmpname = strdup("/tmp/tmpXXXXXX"); 00070 if (mkdtemp(tmpname) == NULL) 00071 { 00072 ROS_ERROR_STREAM 00073 ("Failed to create the temporary directory: " << strerror(errno)); 00074 return false; 00075 } 00076 boost::filesystem::path path(tmpname); 00077 path /= "model.wrl"; 00078 free(tmpname); 00079 00080 fullModelPath = path.external_file_string(); 00081 00082 modelStream.open(path); 00083 if (!modelStream.good()) 00084 { 00085 ROS_ERROR_STREAM 00086 ("Failed to create the temporary file: " << path); 00087 return false; 00088 } 00089 modelStream << modelDescription; 00090 modelStream.flush(); 00091 return true; 00092 }