read_eigen_from_CSV.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include "read_eigen_from_CSV.h"
  2. #include <sstream>
  3. #include <string>
  4. #include <fstream>
  5. #include <iostream>
  6. #include <vector>
  7. template <typename Scalar>
  8. IGL_INLINE bool igl::read_eigen_from_CSV(const std::string str, Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>& M)
  9. {
  10. using namespace std;
  11. using namespace igl;
  12. std::vector<std::vector<Scalar> > Mt;
  13. std::ifstream infile(str.c_str());
  14. std::string line;
  15. while (std::getline(infile, line))
  16. {
  17. std::istringstream iss(line);
  18. vector<Scalar> temp;
  19. Scalar a;
  20. while (iss >> a)
  21. temp.push_back(a);
  22. if (temp.size() != 0) // skip empty lines
  23. Mt.push_back(temp);
  24. }
  25. if (Mt.size() != 0)
  26. {
  27. // Verify that it is indeed a matrix
  28. for (unsigned i = 0; i<Mt.size(); ++i)
  29. {
  30. if (Mt[i].size() != Mt[0].size())
  31. {
  32. infile.close();
  33. return false;
  34. }
  35. }
  36. M.resize(Mt.size(),Mt[0].size());
  37. for (unsigned i = 0; i<Mt.size(); ++i)
  38. for (unsigned j = 0; j<Mt[i].size(); ++j)
  39. M(i,j) = Mt[i][j];
  40. // cerr << "TRUE!" << endl;
  41. return true;
  42. }
  43. infile.close();
  44. return false;
  45. }
  46. #ifndef IGL_HEADER_ONLY
  47. // Explicit template specialization
  48. // generated by autoexplicit.sh
  49. #endif