Browse Source

writedmat vectors

Former-commit-id: 85a7f4eb4265020080cf013026c3f3efa3233db7
jalec 13 years ago
parent
commit
ae1d358ed8
2 changed files with 42 additions and 1 deletions
  1. 37 1
      include/igl/writeDMAT.cpp
  2. 5 0
      include/igl/writeDMAT.h

+ 37 - 1
include/igl/writeDMAT.cpp

@@ -1,7 +1,9 @@
 #include "writeDMAT.h"
 #include "writeDMAT.h"
 
 
 #include <cstdio>
 #include <cstdio>
-#include <Eigen/Dense>
+#ifndef IGL_NO_EIGEN
+#  include <Eigen/Dense>
+#endif
 
 
 template <class Mat>
 template <class Mat>
 IGL_INLINE bool igl::writeDMAT(const std::string file_name, const Mat & W)
 IGL_INLINE bool igl::writeDMAT(const std::string file_name, const Mat & W)
@@ -27,6 +29,40 @@ IGL_INLINE bool igl::writeDMAT(const std::string file_name, const Mat & W)
   return true;
   return true;
 }
 }
 
 
+template <typename Scalar>
+IGL_INLINE bool igl::writeDMAT(
+  const std::string file_name, 
+  const std::vector<std::vector<Scalar> > W)
+{
+  FILE * fp = fopen(file_name.c_str(),"w");
+  if(fp == NULL)
+  {
+    fprintf(stderr,"IOError: writeDMAT() could not open %s...",file_name.c_str());
+    return false; 
+  }
+  int num_rows = (int)W.size();
+  int num_cols = 0;
+  if(num_rows > 0)
+  {
+    num_cols = W[0].size();
+  }
+  // first line contains number of rows and number of columns
+  fprintf(fp,"%d %d\n",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++)
+    {
+      // better be rectangular
+      assert(W[i].size() > j);
+      fprintf(fp,"%0.15lf\n",(double)W[i][j]);
+    }
+  }
+  fclose(fp);
+  return true;
+}
+
 #ifndef IGL_HEADER_ONLY
 #ifndef IGL_HEADER_ONLY
 // Explicit template specialization
 // Explicit template specialization
 // generated by autoexplicit.sh
 // generated by autoexplicit.sh

+ 5 - 0
include/igl/writeDMAT.h

@@ -3,6 +3,7 @@
 #include "igl_inline.h"
 #include "igl_inline.h"
 // See writeDMAT.h for a description of the .dmat file type
 // See writeDMAT.h for a description of the .dmat file type
 #include <string>
 #include <string>
+#include <vector>
 namespace igl
 namespace igl
 {
 {
   // Write a matrix using ascii dmat file type
   // Write a matrix using ascii dmat file type
@@ -16,6 +17,10 @@ namespace igl
   //
   //
   template <class Mat>
   template <class Mat>
   IGL_INLINE bool writeDMAT(const std::string file_name, const Mat & W);
   IGL_INLINE bool writeDMAT(const std::string file_name, const Mat & W);
+  template <typename Scalar>
+  IGL_INLINE bool writeDMAT(
+    const std::string file_name, 
+    const std::vector<std::vector<Scalar> > W);
 }
 }
 
 
 #ifdef IGL_HEADER_ONLY
 #ifdef IGL_HEADER_ONLY