Эх сурвалжийг харах

readDMAT binary

Former-commit-id: 843d280d301635dacf94f6f137c95d2d2d2dc854
jalec 13 жил өмнө
parent
commit
88ab84a1d9

+ 5 - 1
include/igl/ReAntTweakBar.h

@@ -2,7 +2,7 @@
 #define IGL_REANTTWEAKBAR_H
 #include "igl_inline.h"
 // ReAntTweakBar is a minimal wrapper for the AntTweakBar library that allows
-// "bars" to be saved and load from disk. Changing your existing app that users
+// "bars" to be saved and load from disk. Changing your existing app that uses
 // AntTweakBar to use ReAntTweakBar is trivial.
 // 
 // Many (but not all) variable types are supported. I'll try to keep track them
@@ -26,6 +26,10 @@
 // Copyright Alec Jacobson, 2011
 //
 
+// Known bugs:
+//  TwDefineEnumFromString is not supported (use ReTwDefineEnum instead)
+//  Custom enums should not have spaces in their names
+
 // This allows the user to have a non-global, static installation of
 // AntTweakBar
 #ifdef STATIC_ANTTWEAKBAR

+ 1 - 0
include/igl/is_readable.cpp

@@ -15,6 +15,7 @@ IGL_INLINE bool igl::is_readable(const char* filename)
 #else
 #  include <sys/stat.h>
 #  include <unistd.h>
+#  include <iostream>
 IGL_INLINE bool igl::is_readable(const char* filename)
 {
   // Check if file already exists

+ 44 - 10
include/igl/readDMAT.cpp

@@ -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;
 }