00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
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 =='#') {
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
00053
00054 break;
00055 }
00056
00057
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
00077
00078
00079
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);
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
00142
00143
00144
00145
00146
00147
00148
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
00185 fseek (file , 0 , SEEK_END);
00186 long int lSize = ftell (file);
00187 rewind(file);
00188
00189
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
00199
00200
00201
00202
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
00217 if (filename.find(fnCheck) != filename.npos){
00218 cal = 1;
00219
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
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
00254 fseek (file , 0 , SEEK_END);
00255 long int lSize = ftell (file);
00256 rewind(file);
00257
00258
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
00265
00266
00267
00268
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
00296 free (buffer);
00297 delete [](P[0]);
00298 delete [](P[1]);
00299 delete [](P[2]);
00300 }