writeDMAT.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include "writeDMAT.h"
  2. #include <cstdio>
  3. #ifndef IGL_NO_EIGEN
  4. # include <Eigen/Dense>
  5. #endif
  6. template <class Mat>
  7. IGL_INLINE bool igl::writeDMAT(const std::string file_name, const Mat & W)
  8. {
  9. FILE * fp = fopen(file_name.c_str(),"w");
  10. if(fp == NULL)
  11. {
  12. fprintf(stderr,"IOError: writeDMAT() could not open %s...",file_name.c_str());
  13. return false;
  14. }
  15. // first line contains number of rows and number of columns
  16. fprintf(fp,"%d %d\n",(int)W.cols(),(int)W.rows());
  17. // Loop over columns slowly
  18. for(int j = 0;j < W.cols();j++)
  19. {
  20. // loop over rows (down columns) quickly
  21. for(int i = 0;i < W.rows();i++)
  22. {
  23. fprintf(fp,"%0.17lg\n",(double)W(i,j));
  24. }
  25. }
  26. fclose(fp);
  27. return true;
  28. }
  29. template <typename Scalar>
  30. IGL_INLINE bool igl::writeDMAT(
  31. const std::string file_name,
  32. const std::vector<std::vector<Scalar> > W)
  33. {
  34. FILE * fp = fopen(file_name.c_str(),"w");
  35. if(fp == NULL)
  36. {
  37. fprintf(stderr,"IOError: writeDMAT() could not open %s...",file_name.c_str());
  38. return false;
  39. }
  40. int num_rows = (int)W.size();
  41. int num_cols = 0;
  42. if(num_rows > 0)
  43. {
  44. num_cols = W[0].size();
  45. }
  46. // first line contains number of columns and number of rows
  47. fprintf(fp,"%d %d\n",num_cols,num_rows);
  48. // Loop over columns slowly
  49. for(int j = 0;j < num_cols;j++)
  50. {
  51. // loop over rows (down columns) quickly
  52. for(int i = 0;i < num_rows;i++)
  53. {
  54. // better be rectangular
  55. assert((int)W[i].size() > j);
  56. fprintf(fp,"%0.15lf\n",(double)W[i][j]);
  57. }
  58. }
  59. fclose(fp);
  60. return true;
  61. }
  62. #ifndef IGL_HEADER_ONLY
  63. // Explicit template specialization
  64. // generated by autoexplicit.sh
  65. template bool igl::writeDMAT<Eigen::Matrix<double, -1, -1, 0, -1, -1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::Matrix<double, -1, -1, 0, -1, -1> const&);
  66. template bool igl::writeDMAT<double>(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >);
  67. template bool igl::writeDMAT<Eigen::Matrix<int, -1, -1, 0, -1, -1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::Matrix<int, -1, -1, 0, -1, -1> const&);
  68. template bool igl::writeDMAT<Eigen::Matrix<double, -1, 1, 0, -1, 1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::Matrix<double, -1, 1, 0, -1, 1> const&);
  69. #endif