writeDMAT.h 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #ifndef IGL_WRITEDMAT_H
  2. #define IGL_WRITEDMAT_H
  3. // See writeDMAT.h for a description of the .dmat file type
  4. #include <Eigen/Core>
  5. #include <string>
  6. namespace igl
  7. {
  8. // Write a matrix using ascii dmat file type
  9. //
  10. // Inputs:
  11. // file_name path to .dmat file
  12. // W eigen matrix containing to-be-written coefficients
  13. // Returns true on success, false on error
  14. //
  15. inline bool writeDMAT(const std::string file_name, const Eigen::MatrixXd & W);
  16. }
  17. // Implementation
  18. #include <cstdio>
  19. inline bool igl::writeDMAT(const std::string file_name, const Eigen::MatrixXd & W)
  20. {
  21. FILE * fp = fopen(file_name.c_str(),"w");
  22. if(fp == NULL)
  23. {
  24. fclose(fp);
  25. fprintf(stderr,"IOError: writeDMAT() could not open %s...",file_name.c_str());
  26. return false;
  27. }
  28. // first line contains number of rows and number of columns
  29. fprintf(fp,"%d %d\n",(int)W.cols(),(int)W.rows());
  30. // Loop over columns slowly
  31. for(int j = 0;j < W.cols();j++)
  32. {
  33. // loop over rows (down columns) quickly
  34. for(int i = 0;i < W.rows();i++)
  35. {
  36. fprintf(fp,"%lg\n",W(i,j));
  37. }
  38. }
  39. fclose(fp);
  40. return true;
  41. }
  42. #endif