00001 #include "ndescs2disk.h"
00002 #include <dirent.h>
00003
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
00024
00025
00026
00027
00028
00029
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;
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)
00074 {
00075
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
00081
00082
00083
00084
00085
00086 this->points_subscriber_ = this->node_handle_.subscribe("ndesc", 1, &Ndescs2Disk::points_callback, this);
00087
00088
00089
00090
00091
00092
00093
00094
00095 ROS_INFO("Init ndescs2disk node. Saving to %s",pref_out);
00096 }
00097
00098 void Ndescs2Disk::points_callback(const iri_perception_msgs::DescriptorSet::ConstPtr& descriptor_set_msg)
00099 {
00100 ROS_INFO("Received ndesc pointset");
00101
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
00124
00125
00126
00127
00128
00129
00130
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
00135
00136 }
00137
00138
00139
00140
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
00149
00150
00151
00152 ros::init(argc, argv, "ndescs2disk");
00153
00154
00155
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);
00165 ros::spin();
00166 }
00167
00168
00169