ndescs2disk.cpp
Go to the documentation of this file.
00001 #include "ndescs2disk.h"
00002 #include <dirent.h>
00003 //#include <sstream>
00004 
00005 bool DirectoryExists( const char* pzPath )
00006 {
00007   if ( pzPath == NULL) return false;
00008  
00009   DIR *pDir;
00010   bool bExists = false;
00011  
00012   pDir = opendir (pzPath);
00013  
00014   if (pDir != NULL)
00015   {
00016     bExists = true;    
00017     (void) closedir (pDir);
00018   }
00019  
00020   return bExists;
00021 }
00022 
00023 //Already defined in normaldescnode
00024 //template <class T>
00025 //inline std::string to_string (const T& t)
00026 //{
00027 //  std::stringstream ss;
00028 //  ss << t;
00029 //  return ss.str();
00030 //}
00031 
00032 void Ndescs2Disk::write_point_fvec(FILE *pf, const iri_perception_msgs::Descriptor &point)
00033 {
00034   int dim=point.descriptor.size();
00035   float f;
00036   dim+=2; //adding x,y coord
00037   if (fwrite (&(dim), sizeof (int), 1, pf) != 1){
00038     fprintf(stderr, "Error writting to file\n"); exit(-1);}
00039   f=point.u;
00040   if (fwrite (&f, sizeof (float), 1, pf) != 1){
00041     fprintf(stderr, "Error writting to file\n"); exit(-1);}
00042   f=point.v;
00043   if (fwrite (&f, sizeof (float), 1, pf) != 1){
00044     fprintf(stderr, "Error writting to file\n"); exit(-1);}
00045   for(unsigned int j=0;j<dim-2;j++)
00046     if (fwrite (&(point.descriptor[j]), sizeof (float), 1, pf) != 1){
00047       fprintf(stderr, "Error writting to file\n"); exit(-1);}
00048 }
00049 
00050 
00051 void Ndescs2Disk::write_point(FILE *pf, const iri_perception_msgs::Descriptor &point)
00052 {
00053   int dim=point.descriptor.size();
00054   if (fwrite (&(point.u), sizeof (int), 1, pf) != 1){
00055     fprintf(stderr, "Error writting to file\n"); exit(-1);}
00056   if (fwrite (&(point.v), sizeof (int), 1, pf) != 1){
00057     fprintf(stderr, "Error writting to file\n"); exit(-1);}
00058   if (fwrite (&(point.orientation), sizeof (float), 1, pf) != 1){
00059     fprintf(stderr, "Error writting to file\n"); exit(-1);}
00060   if (fwrite (&(point.point3d.x), sizeof (float), 1, pf) != 1){
00061     fprintf(stderr, "Error writting to file\n"); exit(-1);}
00062   if (fwrite (&(point.point3d.y), sizeof (float), 1, pf) != 1){
00063     fprintf(stderr, "Error writting to file\n"); exit(-1);}
00064   if (fwrite (&(point.point3d.z), sizeof (float), 1, pf) != 1){
00065     fprintf(stderr, "Error writting to file\n"); exit(-1);}
00066   if (fwrite (&dim, sizeof (int), 1, pf) != 1){
00067     fprintf(stderr, "Error writting to file\n"); exit(-1);}
00068   for(unsigned int j=0;j<dim;j++)
00069     if (fwrite (&(point.descriptor[j]), sizeof (float), 1, pf) != 1){
00070       fprintf(stderr, "Error writting to file\n"); exit(-1);}
00071 }
00072 
00073 Ndescs2Disk::Ndescs2Disk(char *pref_out, int use_fvec) //, int max_frames)
00074 {
00075   //init class attributes if necessary
00076   this->prefix_out_=std::string(pref_out);
00077   this->counter_=0;
00078   this->keep_nan_points_=false;
00079   this->use_fvec_=use_fvec;
00080   //this->max_frames_=max_frames;
00081   //string for port names
00082 
00083   // [init publishers]
00084 
00085   // [init subscribers]
00086   this->points_subscriber_ = this->node_handle_.subscribe("ndesc", 1, &Ndescs2Disk::points_callback, this);
00087 
00088   // [init services]
00089 
00090   // [init clients]
00091 
00092   // [init action servers]
00093 
00094   // [init action clients]
00095   ROS_INFO("Init ndescs2disk node. Saving to %s",pref_out);  //, maxframes=%d\n",pref_out,max_frames);
00096 }
00097 
00098 void Ndescs2Disk::points_callback(const iri_perception_msgs::DescriptorSet::ConstPtr& descriptor_set_msg)
00099 {
00100   ROS_INFO("Received ndesc pointset");
00101   //TODO: think of a better name generation tech.
00102   std::string file_name;
00103   if(!this->use_fvec_)
00104     file_name=this->prefix_out_+to_string(this->counter_)+".ndesc";
00105   else
00106     file_name=this->prefix_out_+".geofvecs";
00107   this->counter_++;
00108   int saved_points=0;
00109   FILE* pf=fopen(file_name.c_str(),"wb");
00110   for(int ii=0;ii<descriptor_set_msg->num;ii++)
00111   {
00112     if( (!isnan(descriptor_set_msg->descriptors[ii].point3d.x) &&
00113          !isnan(descriptor_set_msg->descriptors[ii].point3d.y) &&
00114          !isnan(descriptor_set_msg->descriptors[ii].point3d.z)) ||
00115         this->keep_nan_points_)
00116     {
00117       if(this->use_fvec_)
00118         write_point_fvec(pf, descriptor_set_msg->descriptors[ii]);
00119       else
00120         write_point(pf, descriptor_set_msg->descriptors[ii]);
00121       saved_points++;
00122     }
00123     // fprintf(pf,"%d %d %f %f %f ",descriptor_set_msg->descriptors[ii].u,
00124     //      descriptor_set_msg->descriptors[ii].v,
00125     //      descriptor_set_msg->descriptors[ii].point3d.x,
00126     //      descriptor_set_msg->descriptors[ii].point3d.y,
00127     //      descriptor_set_msg->descriptors[ii].point3d.z);
00128     // for(unsigned int j=0;j<descriptor_set_msg->descriptors[ii].descriptor.size();j++)
00129     //   fprintf(pf,"%f ",descriptor_set_msg->descriptors[ii].descriptor[j]);
00130     // fprintf(pf,"\n");
00131   }
00132   ROS_INFO("%d descriptors (out of %d) of size %d written to disk.\n",saved_points, descriptor_set_msg->num, descriptor_set_msg->descriptors[0].descriptor.size());
00133   fclose(pf);
00134   //if(this->max_frames_<=this->counter_ && this->max_frames_!=0)
00135   //  exit(0);
00136 }
00137 
00138 
00139 
00140 /* main function */
00141 int main(int argc,char *argv[])
00142 {
00143   if(argc<2){
00144     ROS_ERROR("Not enough arguments!\nUsage: ndesc2disk PREFIX_SAVE [-f MAX_FRAMES]");
00145     return -1;
00146   }
00147 
00148   //if(!DirectoryExists(argv[1]))
00149   //{
00150   //  ROS_ERROR("Error: %s does not exist",argv[1]);
00151   //}
00152   ros::init(argc, argv, "ndescs2disk");
00153   //int max_frames=0;
00154   //if (std::string(argv[2])==std::string("-f"))
00155   //  std::stringstream(std::string(argv[3])) >> max_frames;
00156   int use_fvecs=0;
00157   if(argc==3)
00158     if(std::string(argv[2])==std::string("geofvec"))
00159     {
00160       use_fvecs=1;
00161       ROS_INFO("Writing in geofvec format.");
00162     }
00163 
00164   Ndescs2Disk ndescs2disk(argv[1], use_fvecs);//, max_frames);
00165   ros::spin();
00166 }
00167 
00168 
00169 


iri_finddd
Author(s): Arnau Ramisa
autogenerated on Fri Dec 6 2013 21:09:52