readDMAT.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #include "readDMAT.h"
  2. #include "verbose.h"
  3. #include <cstdio>
  4. #include <iostream>
  5. #include <cassert>
  6. // Static helper method reads the first to elements in the given file
  7. // Inputs:
  8. // fp file pointer of .dmat file that was just opened
  9. // Outputs:
  10. // num_rows number of rows
  11. // num_cols number of columns
  12. // Returns
  13. // 0 success
  14. // 1 did not find header
  15. // 2 bad num_cols
  16. // 3 bad num_rows
  17. // 4 bad line ending
  18. static inline int readDMAT_read_header(FILE * fp, int & num_rows, int & num_cols)
  19. {
  20. // first line contains number of rows and number of columns
  21. int res = fscanf(fp,"%d %d",&num_cols,&num_rows);
  22. if(res != 2)
  23. {
  24. return 1;
  25. }
  26. // check that number of columns and rows are sane
  27. if(num_cols < 0)
  28. {
  29. fprintf(stderr,"IOError: readDMAT() number of columns %d < 0\n",num_cols);
  30. return 2;
  31. }
  32. if(num_rows < 0)
  33. {
  34. fprintf(stderr,"IOError: readDMAT() number of rows %d < 0\n",num_rows);
  35. return 3;
  36. }
  37. // finish reading header
  38. char lf;
  39. if(fread(&lf, sizeof(char), 1, fp)!=1 || lf != '\n')
  40. {
  41. fprintf(stderr,"IOError: bad line ending in header\n");
  42. return 4;
  43. }
  44. return 0;
  45. }
  46. #ifndef IGL_NO_EIGEN
  47. template <typename DerivedW>
  48. IGL_INLINE bool igl::readDMAT(const std::string file_name,
  49. Eigen::PlainObjectBase<DerivedW> & W)
  50. {
  51. FILE * fp = fopen(file_name.c_str(),"r");
  52. if(fp == NULL)
  53. {
  54. fprintf(stderr,"IOError: readDMAT() could not open %s...\n",file_name.c_str());
  55. return false;
  56. }
  57. int num_rows,num_cols;
  58. int head_success = readDMAT_read_header(fp,num_rows,num_cols);
  59. if(head_success != 0)
  60. {
  61. if(head_success == 1)
  62. {
  63. fprintf(stderr,
  64. "IOError: readDMAT() first row should be [num cols] [num rows]...\n");
  65. }
  66. fclose(fp);
  67. return false;
  68. }
  69. // Resize output to fit matrix
  70. W.resize(num_rows,num_cols);
  71. // Loop over columns slowly
  72. for(int j = 0;j < num_cols;j++)
  73. {
  74. // loop over rows (down columns) quickly
  75. for(int i = 0;i < num_rows;i++)
  76. {
  77. double d;
  78. if(fscanf(fp," %lg",&d) != 1)
  79. {
  80. fclose(fp);
  81. fprintf(
  82. stderr,
  83. "IOError: readDMAT() bad format after reading %d entries\n",
  84. j*num_rows + i);
  85. return false;
  86. }
  87. W(i,j) = d;
  88. }
  89. }
  90. // Try to read header for binary part
  91. head_success = readDMAT_read_header(fp,num_rows,num_cols);
  92. if(head_success == 0)
  93. {
  94. assert(W.size() == 0);
  95. // Resize for output
  96. W.resize(num_rows,num_cols);
  97. double * Wraw = new double[num_rows*num_cols];
  98. fread(Wraw, sizeof(double), num_cols*num_rows, fp);
  99. // Loop over columns slowly
  100. for(int j = 0;j < num_cols;j++)
  101. {
  102. // loop over rows (down columns) quickly
  103. for(int i = 0;i < num_rows;i++)
  104. {
  105. W(i,j) = Wraw[j*num_rows+i];
  106. }
  107. }
  108. }
  109. fclose(fp);
  110. return true;
  111. }
  112. #endif
  113. template <typename Scalar>
  114. IGL_INLINE bool igl::readDMAT(
  115. const std::string file_name,
  116. std::vector<std::vector<Scalar> > & W)
  117. {
  118. FILE * fp = fopen(file_name.c_str(),"r");
  119. if(fp == NULL)
  120. {
  121. fprintf(stderr,"IOError: readDMAT() could not open %s...\n",file_name.c_str());
  122. return false;
  123. }
  124. int num_rows,num_cols;
  125. bool head_success = readDMAT_read_header(fp,num_rows,num_cols);
  126. if(head_success != 0)
  127. {
  128. if(head_success == 1)
  129. {
  130. fprintf(stderr,
  131. "IOError: readDMAT() first row should be [num cols] [num rows]...\n");
  132. }
  133. fclose(fp);
  134. return false;
  135. }
  136. // Resize for output
  137. W.resize(num_rows,typename std::vector<Scalar>(num_cols));
  138. // Loop over columns slowly
  139. for(int j = 0;j < num_cols;j++)
  140. {
  141. // loop over rows (down columns) quickly
  142. for(int i = 0;i < num_rows;i++)
  143. {
  144. double d;
  145. if(fscanf(fp," %lg",&d) != 1)
  146. {
  147. fclose(fp);
  148. fprintf(
  149. stderr,
  150. "IOError: readDMAT() bad format after reading %d entries\n",
  151. j*num_rows + i);
  152. return false;
  153. }
  154. W[i][j] = (Scalar)d;
  155. }
  156. }
  157. // Try to read header for binary part
  158. head_success = readDMAT_read_header(fp,num_rows,num_cols);
  159. if(head_success == 0)
  160. {
  161. assert(W.size() == 0);
  162. // Resize for output
  163. W.resize(num_rows,typename std::vector<Scalar>(num_cols));
  164. double * Wraw = new double[num_rows*num_cols];
  165. fread(Wraw, sizeof(double), num_cols*num_rows, fp);
  166. // Loop over columns slowly
  167. for(int j = 0;j < num_cols;j++)
  168. {
  169. // loop over rows (down columns) quickly
  170. for(int i = 0;i < num_rows;i++)
  171. {
  172. W[i][j] = Wraw[j*num_rows+i];
  173. }
  174. }
  175. }
  176. fclose(fp);
  177. return true;
  178. }
  179. #ifndef IGL_HEADER_ONLY
  180. // Explicit template specialization
  181. 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> >&);
  182. template bool igl::readDMAT<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> > > >&);
  183. template bool igl::readDMAT<Eigen::Matrix<int, -1, -1, 0, -1, -1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  184. template bool igl::readDMAT<Eigen::Matrix<double, 4, 1, 0, 4, 1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::PlainObjectBase<Eigen::Matrix<double, 4, 1, 0, 4, 1> >&);
  185. #endif