Browse Source

more missing files

Former-commit-id: 4bdafeaca30feae588fdd0d99fdf60af517684d6
Daniele Panozzo 11 years ago
parent
commit
e24089fabf
2 changed files with 98 additions and 0 deletions
  1. 64 0
      include/igl/read_eigen_from_CSV.cpp
  2. 34 0
      include/igl/read_eigen_from_CSV.h

+ 64 - 0
include/igl/read_eigen_from_CSV.cpp

@@ -0,0 +1,64 @@
+#include "read_eigen_from_CSV.h"
+
+#include <sstream>
+#include <string>
+#include <fstream>
+
+#include <vector>
+
+namespace igl
+{
+
+template <typename Scalar>
+IGL_INLINE bool read_eigen_from_CSV(const std::string str, Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>& M)
+{
+  using namespace std;
+  using namespace igl;
+
+  std::vector<std::vector<Scalar> > Mt;
+  
+  std::ifstream infile(str.c_str());
+  std::string line;
+  while (std::getline(infile, line))
+  {
+    std::istringstream iss(line);
+    vector<Scalar> temp;
+    Scalar a;
+    while (iss >> a)
+      temp.push_back(a);
+
+    if (temp.size() != 0) // skip empty lines
+      Mt.push_back(temp);
+  }
+  
+  if (Mt.size() != 0)
+  {
+    // Verify that it is indeed a matrix
+    for (unsigned i = 0; i<Mt.size(); ++i)
+    {
+      if (Mt[i].size() != Mt[0].size())
+      {
+        infile.close();
+        return false;
+      }
+    }
+    
+    M.resize(Mt.size(),Mt[0].size());
+    for (unsigned i = 0; i<Mt.size(); ++i)
+      for (unsigned j = 0; j<Mt[i].size(); ++j)
+        M(i,j) = Mt[i][j];
+    
+//    cerr << "TRUE!" << endl;
+    return true;
+  }
+  
+  infile.close();
+  return false;
+}
+
+}
+
+#ifndef IGL_HEADER_ONLY
+// Explicit template specialization
+// generated by autoexplicit.sh
+#endif

+ 34 - 0
include/igl/read_eigen_from_CSV.h

@@ -0,0 +1,34 @@
+//
+//  IGL Lib - Simple C++ mesh library 
+//
+//  Copyright 2011, Daniele Panozzo. All rights reserved.
+
+// History:
+
+
+#ifndef IGL_READEIGENFROMCSV_H
+#define IGL_READEIGENFROMCSV_H
+
+#include "igl/igl_inline.h"
+#include <Eigen/Core>
+#include <string>
+#include <vector>
+
+namespace igl 
+{
+  // read a matrix from a csv file into a Eigen matrix
+  // Templates:
+  //   Scalar  type for the matrix
+  // Inputs:
+  //   str  path to .csv file
+  // Outputs:
+  //   M  eigen matrix 
+  template <typename Scalar>
+  IGL_INLINE bool read_eigen_from_CSV(const std::string str, Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>& M);
+}
+
+//#ifdef IGL_HEADER_ONLY
+#  include "read_eigen_from_CSV.cpp"
+//#endif
+
+#endif