rangeimageio.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2013, Fraunhofer FKIE
00003  *
00004  * Authors: Bastian Gaspers
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are met:
00008  *
00009  * * Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * * Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * * Neither the name of the Fraunhofer FKIE nor the names of its
00015  *   contributors may be used to endorse or promote products derived from
00016  *   this software without specific prior written permission.
00017  *
00018  * This file is part of the StructureColoring ROS package.
00019  *
00020  * The StructureColoring ROS package is free software:
00021  * you can redistribute it and/or modify it under the terms of the
00022  * GNU Lesser General Public License as published by the Free
00023  * Software Foundation, either version 3 of the License, or
00024  * (at your option) any later version.
00025  *
00026  * The StructureColoring ROS package is distributed in the hope that it will be useful,
00027  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00028  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00029  * GNU Lesser General Public License for more details.
00030  *
00031  * You should have received a copy of the GNU Lesser General Public License
00032  * along with The StructureColoring ROS package.
00033  * If not, see <http://www.gnu.org/licenses/>.
00034  */
00035 
00036 #include <structureColoring/segcomp/rangeimageio.h>
00037 #include <stdexcept>
00038 #include <cstdio>
00039 
00040 int getInt(FILE* file){
00041         bool isNumber = false;
00042         char item;
00043         do{
00044                 item = fgetc(file);
00045             if (item =='#') {   // comment
00046               while (item != '\n' && item != EOF) item=fgetc(file);
00047             }
00048 
00049             if (item == EOF) return 0;
00050             if (item >='0' && item <='9'){
00051                 isNumber = true;
00052 //              long int pos_byte = ftell(file);
00053 //              fseek(file, pos_byte, SEEK_SET);
00054                 break;
00055             }
00056 
00057             // illegal values
00058             if ( item !='\t' && item !='\r'
00059                 && item !='\n' && item !=',' && item !=' ') return(-1);
00060         } while (!isNumber);
00061         if (isNumber)
00062         {
00063                 ungetc(item, file);
00064                 int i=0;
00065                 int k = fscanf(file, "%d", &i);
00066                 if ((k == EOF) || (k == 0)) return -1;
00067                 item = fgetc(file);
00068                 return i;
00069         }
00070         return -1;
00071 }
00072 
00073 /*****************************************************************************/
00074 
00075 /*
00076 **      A Perceptron image contains both 12-bit range and 12-bit reflectance,
00077 **      packed together in one file, along with some other control bit info.
00078 **      The following code excerpt illustrates how to unpack the 12-bit
00079 **      range and convert it to Cartesian X,Y,Z.
00080 */
00081 
00082 void readPerceptronFile(FILE* fp, short int* buffer, unsigned int& col, unsigned int& row){
00083         int *LoadingImage;
00084         char header[5][40];
00085 
00086         row = 512;
00087         col = 512;
00088     LoadingImage=(int *)calloc(col * row,sizeof(int));
00089     if (!fscanf(fp,"%s %s",header[0],header[1])) exit(1);
00090     if (!fscanf(fp,"%s %s",header[0],header[1])) exit(1);
00091     if (!fscanf(fp,"%s %s %s",header[0],header[1],header[2])) exit(1);
00092     if (!fscanf(fp,"%s %s %s",header[0],header[1],header[2])) exit(1);
00093     if (!fscanf(fp,"%s %s %s",header[0],header[1],header[2])) exit(1);
00094     if (!fscanf(fp,"%s %s %s",header[0],header[1],header[2])) exit(1);
00095     if (!fscanf(fp,"%s %s %s %s",header[0],header[1],header[2],header[3])) exit(1);
00096     if (!fscanf(fp,"%s %s %s %s",header[0],header[1],header[2],header[3])) exit(1);
00097     if (!fscanf(fp,"%s %s %s",header[0],header[1],header[2])) exit(1);
00098     if (!fscanf(fp,"%s %s %s %s %s",header[0],header[1],header[2],
00099         header[3],header[4])) exit(1);
00100     char crap[2];
00101     if(!fscanf(fp,"%c%c",crap,crap+1)) exit(1); /* extra new-line bytes */
00102     if(!fread(LoadingImage,sizeof(int),col * row,fp)) exit(1);
00103     for (unsigned int i=0; i < col * row; i++)
00104     {
00105       buffer[i]=(short int)(htonl(LoadingImage[i]) & 4095);
00106     }
00107 }
00108 
00109 /*****************************************************************************/
00110 
00111 void writePGM(const std::string& filename, const unsigned int& width, const unsigned int& height, const std::vector<unsigned char>& data){
00112         FILE* file = NULL;
00113         if( (file = fopen(filename.c_str(), "w")) == NULL){printf("cannot open pgm file for writing"); return;}
00114         fprintf(file, "P5\n");
00115         fprintf(file, "%d %d\n", width, height);
00116         fprintf(file, "255\n");
00117         for(unsigned int i=0; i<width*height; i++){
00118                 assert(data[i] < 255);
00119                 fputc(data[i], file);
00120         }
00121         fclose(file);
00122 }
00123 
00124 /*****************************************************************************/
00125 
00126 void writeRasterfile(const std::string& filename, const unsigned int& width, const unsigned int& height, const std::vector<unsigned char>& data){
00127         FILE* file = NULL;
00128         if( (file = fopen(filename.c_str(), "w")) == NULL){printf("cannot open rasterfile file for writing"); return;}
00129         int rasterHeader[8];
00130         rasterHeader[0] = RAS_MAGIC;
00131         rasterHeader[1] = width;
00132         rasterHeader[2] = height;
00133         rasterHeader[3] = 8;
00134         rasterHeader[4] = width * height;
00135         rasterHeader[5] = 1;
00136         rasterHeader[6] = 0;
00137         rasterHeader[7] = 0;
00138         for (unsigned int i=0; i < 8; ++i){
00139                 rasterHeader[i] = ntohl(rasterHeader[i]);
00140         }
00141 //      ROS_INFO("ras_magic = %u", rasterHeader[0]);
00142 //      ROS_INFO("ras_width = %u", rasterHeader[1]);
00143 //      ROS_INFO("ras_height = %u", rasterHeader[2]);
00144 //      ROS_INFO("ras_depth = %u", rasterHeader[3]);
00145 //      ROS_INFO("ras_length = %u", rasterHeader[4]);
00146 //      ROS_INFO("ras_type = %u", rasterHeader[5]);
00147 //      ROS_INFO("ras_maptype = %u", rasterHeader[6]);
00148 //      ROS_INFO("ras_maplength = %u", rasterHeader[7]);
00149         fwrite(rasterHeader, sizeof(int), 8, file);
00150         for(unsigned int i=0; i<width*height; i++){
00151                 fputc(data[i], file);
00152         }
00153         fclose(file);
00154 }
00155 
00156 /*****************************************************************************/
00157 
00158 void writeMSNormals(const std::string& filename, const PlanePatch::PlanePatches& planes){
00159         FILE* file = NULL;
00160         if ( (file = fopen(filename.c_str(), "w")) == NULL){printf("cannot open normals file for writing"); return;}
00161         fprintf(file, "%d\n", (int)planes.size());
00162         for(PlanePatch::PlanePatches::const_iterator planes_it = planes.begin(); planes_it != planes.end(); ++planes_it){
00163                 fprintf(file, "%d ", (int)(*planes_it)->getId());
00164                 const Eigen::Vector3f& planeNormal((*planes_it)->getPlane3D().getPlaneNormal());
00165                 fprintf(file, "%lf %lf %lf\n", planeNormal.x(), planeNormal.y(), planeNormal.z());
00166         }
00167         fclose(file);
00168 }
00169 
00170 /*****************************************************************************/
00171 
00172 void writeTimeToFile(const std::string& filename, const double timeInSec){
00173         FILE* fp;
00174         if((fp = fopen(filename.c_str(), "a"))==NULL) return;
00175         fprintf(fp, "%g\n", timeInSec);
00176         fclose(fp);
00177 }
00178 
00179 /*****************************************************************************/
00180 
00181 void readPointCloudFromABWFile(PointCloud& pointCloud, const std::string& filename, unsigned int& width, unsigned int& height, std::vector<unsigned int>& undefPoints, bool pgm){
00182         FILE * file = NULL;
00183         if ( (file = fopen(filename.c_str(),"r")) == NULL) {printf("cannot read file: %s", filename.c_str()); exit (2);}
00184         // obtain file size:
00185         fseek (file , 0 , SEEK_END);
00186         long int lSize = ftell (file);
00187         rewind(file);
00188 
00189         // allocate memory to contain the whole file:
00190         unsigned char * buffer = (unsigned char*) malloc (sizeof(unsigned char)*lSize);
00191         if (buffer == NULL) {printf("Memory error"); exit (5);}
00192         if (pgm)
00193                 readPGM(file, buffer, width, height);
00194         else
00195                 readRasterfile<unsigned char>(file, buffer, width, height);
00196         fclose (file);
00197         size_t numPoints = width*height;
00198 //      pointCloud.width = width; does not work due to undefined points
00199 //      pointCloud.height = height; //TODO make this work somehow
00200 //      ROS_INFO("width %d, height %d, numPoints %lu, lSize %ld", width, height, numPoints, lSize);
00201 
00202         // the whole file is now loaded in the memory buffer.
00203         double* P[3];
00204         P[0] = new double[numPoints];
00205         P[1] = new double[numPoints];
00206         P[2] = new double[numPoints];
00207         int cal = 0;
00208         std::string calList[] = {"0", "19", "21", "25", "27", "28", "29"};
00209         unsigned int calListSize = 7;
00210         std::string prefix = "abw.test.";
00211         std::string suffix = ".range";
00212         for (unsigned int i = 0; i < calListSize; ++i){
00213                 std::string fnCheck = prefix;
00214                 fnCheck.append(calList[i]);
00215                 fnCheck.append(suffix);
00216 //              std::cout << fnCheck << " " << filename << std::endl;
00217                 if (filename.find(fnCheck) != filename.npos){
00218                         cal = 1;
00219 //                      std::cout << "chose first calibration for this file" << std::endl;
00220                         break;
00221                 }
00222         }
00223         ConvertABWRangeToCartesian(buffer, width, height, P, cal);
00224         undefPoints.clear();
00225         for(size_t i = 0; i < numPoints; i++){
00226                 if ((P[0][i]!= -1.0)&&(P[1][i]!= -1.0)&&(P[2][i]!= -1.0)){
00227                         PointT p;
00228                         p.x = -((P[2][i] / 100.0) - 4.0);
00229                         p.y = -P[0][i] / 100.0;
00230                         p.z = P[1][i] / 100.0;
00231                         p.rgb = 0x00000000;
00232                         if (fabsf(p.x < 10) && fabsf(p.y < 10) && fabsf(p.z < 10))
00233                                 pointCloud.points.push_back(p);
00234                         else {
00235                                 printf("point (%f, %f, %f) out of range", p.x, p.y, p.z);
00236                                 undefPoints.push_back((unsigned int)i);
00237                         }
00238                 } else {
00239                         undefPoints.push_back((unsigned int)i);
00240                 }
00241         }
00242 
00243         // terminate
00244         free (buffer);
00245         delete [](P[0]);
00246         delete [](P[1]);
00247         delete [](P[2]);
00248 }
00249 
00250 void readPointCloudFromPERCEPTRONFile(PointCloud& pointCloud, const std::string& filename, unsigned int& width, unsigned int& height, std::vector<unsigned int>& undefPoints){
00251         FILE * file = NULL;
00252         if ( (file = fopen(filename.c_str(),"r")) == NULL) {printf("cannot read file"); exit (2);}
00253         // obtain file size:
00254         fseek (file , 0 , SEEK_END);
00255         long int lSize = ftell (file);
00256         rewind(file);
00257 
00258         // allocate memory to contain the whole file:
00259         short int * buffer = (short int*) malloc (sizeof(short int)*lSize);
00260         if (buffer == NULL) {printf("Memory error"); exit (5);}
00261         readPerceptronFile(file, buffer, width, height);
00262         fclose (file);
00263         size_t numPoints = width*height;
00264 //      pointCloud.width = width; does not work due to undefined points
00265 //      pointCloud.height = height; //TODO make this work somehow
00266 //      ROS_INFO("width %d, height %d, numPoints %lu, lSize %ld", width, height, numPoints, lSize);
00267 
00268         // the whole file is now loaded in the memory buffer.
00269         double* P[3];
00270         P[0] = new double[numPoints];
00271         P[1] = new double[numPoints];
00272         P[2] = new double[numPoints];
00273 
00274         ConvertPerceptronRangeToCartesian(buffer, P, width, height);
00275         undefPoints.clear();
00276 
00277         for(size_t i = 0; i < numPoints; i++){
00278                 if ((P[0][i]!= -1.0)&&(P[1][i]!= -1.0)&&(P[2][i]!= -1.0)){
00279                         PointT p;
00280                         p.x = -((P[2][i] / 100.0) - 4.0);
00281                         p.y = -P[0][i] / 100.0;
00282                         p.z = P[1][i] / 100.0;
00283                         p.rgb = 0x00000000;
00284                         if (fabsf(p.x < 10) && fabsf(p.y < 10) && fabsf(p.z < 10))
00285                                 pointCloud.points.push_back(p);
00286                         else {
00287                                 printf("point (%f, %f, %f) out of range", p.x, p.y, p.z);
00288                                 undefPoints.push_back((unsigned int)i);
00289                         }
00290                 } else {
00291                         undefPoints.push_back((unsigned int)i);
00292                 }
00293         }
00294 
00295         // terminate
00296         free (buffer);
00297         delete [](P[0]);
00298         delete [](P[1]);
00299         delete [](P[2]);
00300 }


structure_coloring_fkie
Author(s): Bastian Gaspers
autogenerated on Sun Jan 5 2014 11:38:09