writeDMAT.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include "writeDMAT.h"
  2. #include <cstdio>
  3. #ifndef IGL_NO_EIGEN
  4. # include <Eigen/Core>
  5. #endif
  6. template <class Mat>
  7. IGL_INLINE bool igl::writeDMAT(
  8. const std::string file_name,
  9. const Mat & W,
  10. const bool ascii)
  11. {
  12. FILE * fp = fopen(file_name.c_str(),"w");
  13. if(fp == NULL)
  14. {
  15. fprintf(stderr,"IOError: writeDMAT() could not open %s...",file_name.c_str());
  16. return false;
  17. }
  18. if(ascii)
  19. {
  20. // first line contains number of rows and number of columns
  21. fprintf(fp,"%d %d\n",(int)W.cols(),(int)W.rows());
  22. // Loop over columns slowly
  23. for(int j = 0;j < W.cols();j++)
  24. {
  25. // loop over rows (down columns) quickly
  26. for(int i = 0;i < W.rows();i++)
  27. {
  28. fprintf(fp,"%0.17lg\n",(double)W(i,j));
  29. }
  30. }
  31. }else
  32. {
  33. // write header for ascii
  34. fprintf(fp,"0 0\n");
  35. // first line contains number of rows and number of columns
  36. fprintf(fp,"%d %d\n",(int)W.cols(),(int)W.rows());
  37. Eigen::MatrixXd Wd = W.template cast<double>();
  38. fwrite(Wd.data(),sizeof(double),Wd.size(),fp);
  39. //// Loop over columns slowly
  40. //for(int j = 0;j < W.cols();j++)
  41. //{
  42. // // loop over rows (down columns) quickly
  43. // for(int i = 0;i < W.rows();i++)
  44. // {
  45. // double d = (double)W(i,j);
  46. // fwrite(&d,sizeof(double),1,fp);
  47. // }
  48. //}
  49. }
  50. fclose(fp);
  51. return true;
  52. }
  53. template <typename Scalar>
  54. IGL_INLINE bool igl::writeDMAT(
  55. const std::string file_name,
  56. const std::vector<std::vector<Scalar> > W)
  57. {
  58. FILE * fp = fopen(file_name.c_str(),"w");
  59. if(fp == NULL)
  60. {
  61. fprintf(stderr,"IOError: writeDMAT() could not open %s...",file_name.c_str());
  62. return false;
  63. }
  64. int num_rows = (int)W.size();
  65. int num_cols = 0;
  66. if(num_rows > 0)
  67. {
  68. num_cols = W[0].size();
  69. }
  70. // first line contains number of columns and number of rows
  71. fprintf(fp,"%d %d\n",num_cols,num_rows);
  72. // Loop over columns slowly
  73. for(int j = 0;j < num_cols;j++)
  74. {
  75. // loop over rows (down columns) quickly
  76. for(int i = 0;i < num_rows;i++)
  77. {
  78. // better be rectangular
  79. assert((int)W[i].size() > j);
  80. fprintf(fp,"%0.15lf\n",(double)W[i][j]);
  81. }
  82. }
  83. fclose(fp);
  84. return true;
  85. }
  86. #ifndef IGL_HEADER_ONLY
  87. // Explicit template specialization
  88. // generated by autoexplicit.sh
  89. 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&,bool);
  90. 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> > > >);
  91. 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&, bool);
  92. 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&, bool);
  93. 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&, bool);
  94. #endif