|
@@ -3,6 +3,7 @@
|
|
|
#include "verbose.h"
|
|
|
#include <cstdio>
|
|
|
#include <iostream>
|
|
|
+#include <cassert>
|
|
|
|
|
|
// Static helper method reads the first to elements in the given file
|
|
|
// Inputs:
|
|
@@ -10,28 +11,31 @@
|
|
|
// Outputs:
|
|
|
// num_rows number of rows
|
|
|
// num_cols number of columns
|
|
|
-// Returns true on success, false on failure
|
|
|
-static inline bool readDMAT_read_header(FILE * fp, int & num_rows, int & num_cols)
|
|
|
+// Returns
|
|
|
+// 0 success
|
|
|
+// 1 did not find header
|
|
|
+// 2 bad num_cols
|
|
|
+// 3 bad num_rows
|
|
|
+static inline int readDMAT_read_header(FILE * fp, int & num_rows, int & num_cols)
|
|
|
{
|
|
|
// first line contains number of rows and number of columns
|
|
|
int res = fscanf(fp,"%d %d\n",&num_cols,&num_rows);
|
|
|
if(res != 2)
|
|
|
{
|
|
|
- fprintf(stderr,"IOError: readDMAT() first row should be [num cols] [num rows]...\n");
|
|
|
- return false;
|
|
|
+ return 1;
|
|
|
}
|
|
|
// check that number of columns and rows are sane
|
|
|
if(num_cols < 0)
|
|
|
{
|
|
|
fprintf(stderr,"IOError: readDMAT() number of columns %d < 0\n",num_cols);
|
|
|
- return false;
|
|
|
+ return 2;
|
|
|
}
|
|
|
if(num_rows < 0)
|
|
|
{
|
|
|
fprintf(stderr,"IOError: readDMAT() number of rows %d < 0\n",num_rows);
|
|
|
- return false;
|
|
|
+ return 3;
|
|
|
}
|
|
|
- return true;
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
#ifndef IGL_NO_EIGEN
|
|
@@ -46,9 +50,14 @@ IGL_INLINE bool igl::readDMAT(const std::string file_name,
|
|
|
return false;
|
|
|
}
|
|
|
int num_rows,num_cols;
|
|
|
- bool head_success = readDMAT_read_header(fp,num_rows,num_cols);
|
|
|
- if(!head_success)
|
|
|
+ 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;
|
|
|
}
|
|
@@ -94,8 +103,13 @@ IGL_INLINE bool igl::readDMAT(
|
|
|
}
|
|
|
int num_rows,num_cols;
|
|
|
bool head_success = readDMAT_read_header(fp,num_rows,num_cols);
|
|
|
- if(!head_success)
|
|
|
+ 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;
|
|
|
}
|
|
@@ -123,6 +137,26 @@ IGL_INLINE bool igl::readDMAT(
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ // 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,typename std::vector<Scalar>(num_cols));
|
|
|
+ double * Wraw = new double[num_rows*num_cols];
|
|
|
+ fread(Wraw, 8, 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;
|
|
|
}
|