readDMAT.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include "readDMAT.h"
  2. #include "verbose.h"
  3. #include <cstdio>
  4. #include <iostream>
  5. // Static helper method reads the first to elements in the given file
  6. // Inputs:
  7. // fp file pointer of .dmat file that was just opened
  8. // Outputs:
  9. // num_rows number of rows
  10. // num_cols number of columns
  11. // Returns true on success, false on failure
  12. static inline bool readDMAT_read_header(FILE * fp, int & num_rows, int & num_cols)
  13. {
  14. // first line contains number of rows and number of columns
  15. int res = fscanf(fp,"%d %d\n",&num_cols,&num_rows);
  16. if(res != 2)
  17. {
  18. fprintf(stderr,"IOError: readDMAT() first row should be [num cols] [num rows]...\n");
  19. return false;
  20. }
  21. // check that number of columns and rows are sane
  22. if(num_cols < 0)
  23. {
  24. fprintf(stderr,"IOError: readDMAT() number of columns %d < 0\n",num_cols);
  25. return false;
  26. }
  27. if(num_rows < 0)
  28. {
  29. fprintf(stderr,"IOError: readDMAT() number of rows %d < 0\n",num_rows);
  30. return false;
  31. }
  32. return true;
  33. }
  34. #ifndef IGL_NO_EIGEN
  35. template <typename DerivedW>
  36. IGL_INLINE bool igl::readDMAT(const std::string file_name,
  37. Eigen::PlainObjectBase<DerivedW> & W)
  38. {
  39. FILE * fp = fopen(file_name.c_str(),"r");
  40. if(fp == NULL)
  41. {
  42. fprintf(stderr,"IOError: readDMAT() could not open %s...\n",file_name.c_str());
  43. return false;
  44. }
  45. int num_rows,num_cols;
  46. bool head_success = readDMAT_read_header(fp,num_rows,num_cols);
  47. if(!head_success)
  48. {
  49. fclose(fp);
  50. return false;
  51. }
  52. // Resize output to fit matrix
  53. W.resize(num_rows,num_cols);
  54. // Loop over columns slowly
  55. for(int j = 0;j < num_cols;j++)
  56. {
  57. // loop over rows (down columns) quickly
  58. for(int i = 0;i < num_rows;i++)
  59. {
  60. double d;
  61. if(fscanf(fp," %lg",&d) != 1)
  62. {
  63. fclose(fp);
  64. fprintf(
  65. stderr,
  66. "IOError: readDMAT() bad format after reading %d entries\n",
  67. j*num_rows + i);
  68. return false;
  69. }
  70. W(i,j) = d;
  71. }
  72. }
  73. fclose(fp);
  74. return true;
  75. }
  76. #endif
  77. template <typename Scalar>
  78. IGL_INLINE bool igl::readDMAT(
  79. const std::string file_name,
  80. std::vector<std::vector<Scalar> > & W)
  81. {
  82. FILE * fp = fopen(file_name.c_str(),"r");
  83. if(fp == NULL)
  84. {
  85. fprintf(stderr,"IOError: readDMAT() could not open %s...\n",file_name.c_str());
  86. return false;
  87. }
  88. int num_rows,num_cols;
  89. bool head_success = readDMAT_read_header(fp,num_rows,num_cols);
  90. if(!head_success)
  91. {
  92. fclose(fp);
  93. return false;
  94. }
  95. // Resize for output
  96. W.resize(num_rows,typename std::vector<Scalar>(num_cols));
  97. // Loop over columns slowly
  98. for(int j = 0;j < num_cols;j++)
  99. {
  100. // loop over rows (down columns) quickly
  101. for(int i = 0;i < num_rows;i++)
  102. {
  103. double d;
  104. if(fscanf(fp," %lg",&d) != 1)
  105. {
  106. fclose(fp);
  107. fprintf(
  108. stderr,
  109. "IOError: readDMAT() bad format after reading %d entries\n",
  110. j*num_rows + i);
  111. return false;
  112. }
  113. W[i][j] = (Scalar)d;
  114. }
  115. }
  116. fclose(fp);
  117. return true;
  118. }
  119. #ifndef IGL_HEADER_ONLY
  120. // Explicit template specialization
  121. template bool igl::readDMAT<Eigen::Matrix<double, -1, -1, 0, -1, -1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  122. #endif