readDMAT.cpp 6.9 KB

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