writeDMAT.h 1.2 KB

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