readDMAT.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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')
  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
  77. W.resize(num_rows,num_cols);
  78. // Loop over columns slowly
  79. for(int j = 0;j < num_cols;j++)
  80. {
  81. // loop over rows (down columns) quickly
  82. for(int i = 0;i < num_rows;i++)
  83. {
  84. double d;
  85. if(fscanf(fp," %lg",&d) != 1)
  86. {
  87. fclose(fp);
  88. fprintf(
  89. stderr,
  90. "IOError: readDMAT() bad format after reading %d entries\n",
  91. j*num_rows + i);
  92. return false;
  93. }
  94. W(i,j) = d;
  95. }
  96. }
  97. // Try to read header for binary part
  98. head_success = readDMAT_read_header(fp,num_rows,num_cols);
  99. if(head_success == 0)
  100. {
  101. assert(W.size() == 0);
  102. // Resize for output
  103. W.resize(num_rows,num_cols);
  104. double * Wraw = new double[num_rows*num_cols];
  105. fread(Wraw, sizeof(double), num_cols*num_rows, fp);
  106. // Loop over columns slowly
  107. for(int j = 0;j < num_cols;j++)
  108. {
  109. // loop over rows (down columns) quickly
  110. for(int i = 0;i < num_rows;i++)
  111. {
  112. W(i,j) = Wraw[j*num_rows+i];
  113. }
  114. }
  115. }
  116. fclose(fp);
  117. return true;
  118. }
  119. #endif
  120. template <typename Scalar>
  121. IGL_INLINE bool igl::readDMAT(
  122. const std::string file_name,
  123. std::vector<std::vector<Scalar> > & W)
  124. {
  125. FILE * fp = fopen(file_name.c_str(),"r");
  126. if(fp == NULL)
  127. {
  128. fprintf(stderr,"IOError: readDMAT() could not open %s...\n",file_name.c_str());
  129. return false;
  130. }
  131. int num_rows,num_cols;
  132. bool head_success = readDMAT_read_header(fp,num_rows,num_cols);
  133. if(head_success != 0)
  134. {
  135. if(head_success == 1)
  136. {
  137. fprintf(stderr,
  138. "IOError: readDMAT() first row should be [num cols] [num rows]...\n");
  139. }
  140. fclose(fp);
  141. return false;
  142. }
  143. // Resize for output
  144. W.resize(num_rows,typename std::vector<Scalar>(num_cols));
  145. // Loop over columns slowly
  146. for(int j = 0;j < num_cols;j++)
  147. {
  148. // loop over rows (down columns) quickly
  149. for(int i = 0;i < num_rows;i++)
  150. {
  151. double d;
  152. if(fscanf(fp," %lg",&d) != 1)
  153. {
  154. fclose(fp);
  155. fprintf(
  156. stderr,
  157. "IOError: readDMAT() bad format after reading %d entries\n",
  158. j*num_rows + i);
  159. return false;
  160. }
  161. W[i][j] = (Scalar)d;
  162. }
  163. }
  164. // Try to read header for binary part
  165. head_success = readDMAT_read_header(fp,num_rows,num_cols);
  166. if(head_success == 0)
  167. {
  168. assert(W.size() == 0);
  169. // Resize for output
  170. W.resize(num_rows,typename std::vector<Scalar>(num_cols));
  171. double * Wraw = new double[num_rows*num_cols];
  172. fread(Wraw, sizeof(double), num_cols*num_rows, fp);
  173. // Loop over columns slowly
  174. for(int j = 0;j < num_cols;j++)
  175. {
  176. // loop over rows (down columns) quickly
  177. for(int i = 0;i < num_rows;i++)
  178. {
  179. W[i][j] = Wraw[j*num_rows+i];
  180. }
  181. }
  182. }
  183. fclose(fp);
  184. return true;
  185. }
  186. #ifdef IGL_STATIC_LIBRARY
  187. // Explicit template specialization
  188. 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> >&);
  189. 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> > > >&);
  190. 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> >&);
  191. 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> >&);
  192. 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> >&);
  193. 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> >&);
  194. template bool igl::readDMAT<Eigen::Matrix<int, -1, 2, 0, -1, 2> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 2, 0, -1, 2> >&);
  195. #endif