IGL_INLINE bool igl::readDMAT(const std::string file_name, Eigen::PlainObjectBase<DerivedW> & W) { FILE * fp = fopen(file_name.c_str(),"r"); if(fp == NULL) { fprintf(stderr,"IOError: readDMAT() could not open %s...\n",file_name.c_str()); return false; } int num_rows,num_cols; int head_success = readDMAT_read_header(fp,num_rows,num_cols); if(head_success != 0) { if(head_success == 1) { fprintf(stderr, "IOError: readDMAT() first row should be [num cols] [num rows]...\n"); } fclose(fp); return false; } // Resize output to fit matrix W.resize(num_rows,num_cols); // Loop over columns slowly for(int j = 0;j < num_cols;j++) { // loop over rows (down columns) quickly for(int i = 0;i < num_rows;i++) { double d; if(fscanf(fp," %lg",&d) != 1) { fclose(fp); fprintf( stderr, "IOError: readDMAT() bad format after reading %d entries\n", j*num_rows + i); return false; } W(i,j) = d; } } // Try to read header for binary part head_success = readDMAT_read_header(fp,num_rows,num_cols); if(head_success == 0) { assert(W.size() == 0); // Resize for output W.resize(num_rows,num_cols); double * Wraw = new double[num_rows*num_cols]; fread(Wraw, sizeof(double), num_cols*num_rows, fp); // Loop over columns slowly for(int j = 0;j < num_cols;j++) { // loop over rows (down columns) quickly for(int i = 0;i < num_rows;i++) { W(i,j) = Wraw[j*num_rows+i]; } } } fclose(fp); return true; }
IGL_INLINE bool igl::readDMAT(const std::string file_name, Eigen::PlainObjectBase<DerivedW> & W) { FILE * fp = fopen(file_name.c_str(),"rb"); if(fp == NULL) { fprintf(stderr,"IOError: readDMAT() could not open %s...\n",file_name.c_str()); return false; } int num_rows,num_cols; int head_success = readDMAT_read_header(fp,num_rows,num_cols); if(head_success != 0) { if(head_success == 1) { fprintf(stderr, "IOError: readDMAT() first row should be [num cols] [num rows]...\n"); } fclose(fp); return false; } // Resize output to fit matrix, only if non-empty since this will trigger an // error on fixed size matrices before reaching binary data. bool empty = num_rows == 0 || num_cols == 0; if(!empty) { W.resize(num_rows,num_cols); } // Loop over columns slowly for(int j = 0;j < num_cols;j++) { // loop over rows (down columns) quickly for(int i = 0;i < num_rows;i++) { double d; if(fscanf(fp," %lg",&d) != 1) { fclose(fp); fprintf( stderr, "IOError: readDMAT() bad format after reading %d entries\n", j*num_rows + i); return false; } W(i,j) = d; } } // Try to read header for binary part head_success = readDMAT_read_header(fp,num_rows,num_cols); if(head_success == 0) { assert(W.size() == 0); // Resize for output W.resize(num_rows,num_cols); double * Wraw = new double[num_rows*num_cols]; fread(Wraw, sizeof(double), num_cols*num_rows, fp); // Loop over columns slowly for(int j = 0;j < num_cols;j++) { // loop over rows (down columns) quickly for(int i = 0;i < num_rows;i++) { W(i,j) = Wraw[j*num_rows+i]; } } }else { // we skipped resizing before in case there was binary data if(empty) { // This could trigger an error if using fixed size matrices. W.resize(num_rows,num_cols); } } fclose(fp); return true; }