readDMAT.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "readDMAT.h"
  9. #include "verbose.h"
  10. #include <cstdio>
  11. #include <iostream>
  12. #include <cassert>
  13. // Static helper method reads the first to elements in the given file
  14. // Inputs:
  15. // fp file pointer of .dmat file that was just opened
  16. // Outputs:
  17. // num_rows number of rows
  18. // num_cols number of columns
  19. // Returns
  20. // 0 success
  21. // 1 did not find header
  22. // 2 bad num_cols
  23. // 3 bad num_rows
  24. // 4 bad line ending
  25. static inline int readDMAT_read_header(FILE * fp, int & num_rows, int & num_cols)
  26. {
  27. // first line contains number of rows and number of columns
  28. int res = fscanf(fp,"%d %d",&num_cols,&num_rows);
  29. if(res != 2)
  30. {
  31. return 1;
  32. }
  33. // check that number of columns and rows are sane
  34. if(num_cols < 0)
  35. {
  36. fprintf(stderr,"IOError: readDMAT() number of columns %d < 0\n",num_cols);
  37. return 2;
  38. }
  39. if(num_rows < 0)
  40. {
  41. fprintf(stderr,"IOError: readDMAT() number of rows %d < 0\n",num_rows);
  42. return 3;
  43. }
  44. // finish reading header
  45. char lf;
  46. if(fread(&lf, sizeof(char), 1, fp)!=1 || !(lf == '\n' || lf == '\r'))
  47. {
  48. fprintf(stderr,"IOError: bad line ending in header\n");
  49. return 4;
  50. }
  51. return 0;
  52. }
  53. #ifndef IGL_NO_EIGEN
  54. template <typename DerivedW>
  55. IGL_INLINE bool igl::readDMAT(const std::string file_name,
  56. Eigen::PlainObjectBase<DerivedW> & W)
  57. {
  58. FILE * fp = fopen(file_name.c_str(),"rb");
  59. if(fp == NULL)
  60. {
  61. fprintf(stderr,"IOError: readDMAT() could not open %s...\n",file_name.c_str());
  62. return false;
  63. }
  64. int num_rows,num_cols;
  65. int head_success = readDMAT_read_header(fp,num_rows,num_cols);
  66. if(head_success != 0)
  67. {
  68. if(head_success == 1)
  69. {
  70. fprintf(stderr,
  71. "IOError: readDMAT() first row should be [num cols] [num rows]...\n");
  72. }
  73. fclose(fp);
  74. return false;
  75. }
  76. // Resize output to fit matrix, only if non-empty since this will trigger an
  77. // error on fixed size matrices before reaching binary data.
  78. bool empty = num_rows == 0 || num_cols == 0;
  79. if(!empty)
  80. {
  81. W.resize(num_rows,num_cols);
  82. }
  83. // Loop over columns slowly
  84. for(int j = 0;j < num_cols;j++)
  85. {
  86. // loop over rows (down columns) quickly
  87. for(int i = 0;i < num_rows;i++)
  88. {
  89. double d;
  90. if(fscanf(fp," %lg",&d) != 1)
  91. {
  92. fclose(fp);
  93. fprintf(
  94. stderr,
  95. "IOError: readDMAT() bad format after reading %d entries\n",
  96. j*num_rows + i);
  97. return false;
  98. }
  99. W(i,j) = d;
  100. }
  101. }
  102. // Try to read header for binary part
  103. head_success = readDMAT_read_header(fp,num_rows,num_cols);
  104. if(head_success == 0)
  105. {
  106. assert(W.size() == 0);
  107. // Resize for output
  108. W.resize(num_rows,num_cols);
  109. double * Wraw = new double[num_rows*num_cols];
  110. fread(Wraw, sizeof(double), num_cols*num_rows, fp);
  111. // Loop over columns slowly
  112. for(int j = 0;j < num_cols;j++)
  113. {
  114. // loop over rows (down columns) quickly
  115. for(int i = 0;i < num_rows;i++)
  116. {
  117. W(i,j) = Wraw[j*num_rows+i];
  118. }
  119. }
  120. }else
  121. {
  122. // we skipped resizing before in case there was binary data
  123. if(empty)
  124. {
  125. // This could trigger an error if using fixed size matrices.
  126. W.resize(num_rows,num_cols);
  127. }
  128. }
  129. fclose(fp);
  130. return true;
  131. }
  132. #endif
  133. template <typename Scalar>
  134. IGL_INLINE bool igl::readDMAT(
  135. const std::string file_name,
  136. std::vector<std::vector<Scalar> > & W)
  137. {
  138. FILE * fp = fopen(file_name.c_str(),"r");
  139. if(fp == NULL)
  140. {
  141. fprintf(stderr,"IOError: readDMAT() could not open %s...\n",file_name.c_str());
  142. return false;
  143. }
  144. int num_rows,num_cols;
  145. bool head_success = readDMAT_read_header(fp,num_rows,num_cols);
  146. if(head_success != 0)
  147. {
  148. if(head_success == 1)
  149. {
  150. fprintf(stderr,
  151. "IOError: readDMAT() first row should be [num cols] [num rows]...\n");
  152. }
  153. fclose(fp);
  154. return false;
  155. }
  156. // Resize for output
  157. W.resize(num_rows,typename std::vector<Scalar>(num_cols));
  158. // Loop over columns slowly
  159. for(int j = 0;j < num_cols;j++)
  160. {
  161. // loop over rows (down columns) quickly
  162. for(int i = 0;i < num_rows;i++)
  163. {
  164. double d;
  165. if(fscanf(fp," %lg",&d) != 1)
  166. {
  167. fclose(fp);
  168. fprintf(
  169. stderr,
  170. "IOError: readDMAT() bad format after reading %d entries\n",
  171. j*num_rows + i);
  172. return false;
  173. }
  174. W[i][j] = (Scalar)d;
  175. }
  176. }
  177. // Try to read header for binary part
  178. head_success = readDMAT_read_header(fp,num_rows,num_cols);
  179. if(head_success == 0)
  180. {
  181. assert(W.size() == 0);
  182. // Resize for output
  183. W.resize(num_rows,typename std::vector<Scalar>(num_cols));
  184. double * Wraw = new double[num_rows*num_cols];
  185. fread(Wraw, sizeof(double), num_cols*num_rows, fp);
  186. // Loop over columns slowly
  187. for(int j = 0;j < num_cols;j++)
  188. {
  189. // loop over rows (down columns) quickly
  190. for(int i = 0;i < num_rows;i++)
  191. {
  192. W[i][j] = Wraw[j*num_rows+i];
  193. }
  194. }
  195. }
  196. fclose(fp);
  197. return true;
  198. }
  199. #ifdef IGL_STATIC_LIBRARY
  200. // Explicit template specialization
  201. template bool igl::readDMAT<Eigen::Matrix<double, -1, -1, 0, -1, -1> >(std::string, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  202. template bool igl::readDMAT<double>(std::string, std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >&);
  203. template bool igl::readDMAT<Eigen::Matrix<int, -1, -1, 0, -1, -1> >(std::string, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  204. template bool igl::readDMAT<Eigen::Matrix<double, 4, 1, 0, 4, 1> >(std::string, Eigen::PlainObjectBase<Eigen::Matrix<double, 4, 1, 0, 4, 1> >&);
  205. template bool igl::readDMAT<Eigen::Matrix<double, -1, 1, 0, -1, 1> >(std::string, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  206. template bool igl::readDMAT<Eigen::Matrix<int, -1, 1, 0, -1, 1> >(std::string, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  207. template bool igl::readDMAT<Eigen::Matrix<int, -1, 2, 0, -1, 2> >(std::string, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 2, 0, -1, 2> >&);
  208. template bool igl::readDMAT<Eigen::Matrix<double, -1, -1, 1, -1, -1> >(std::string, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 1, -1, -1> >&);
  209. template bool igl::readDMAT<Eigen::Matrix<int, -1, -1, 1, -1, -1> >(std::string, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 1, -1, -1> >&);
  210. #endif