readMESH.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #include "readMESH.h"
  2. #include <cstdio>
  3. #include "verbose.h"
  4. template <typename Scalar, typename Index>
  5. IGL_INLINE bool igl::readMESH(
  6. const std::string mesh_file_name,
  7. std::vector<std::vector<Scalar > > & V,
  8. std::vector<std::vector<Index > > & T,
  9. std::vector<std::vector<Index > > & F)
  10. {
  11. using namespace std;
  12. using namespace igl;
  13. FILE * mesh_file = fopen(mesh_file_name.c_str(),"r");
  14. if(NULL==mesh_file)
  15. {
  16. fprintf(stderr,"IOError: %s could not be opened...",mesh_file_name.c_str());
  17. return false;
  18. }
  19. #ifndef LINE_MAX
  20. # define LINE_MAX 2048
  21. #endif
  22. char line[LINE_MAX];
  23. bool still_comments;
  24. V.clear();
  25. T.clear();
  26. F.clear();
  27. // eat comments at beginning of file
  28. still_comments= true;
  29. while(still_comments)
  30. {
  31. fgets(line,LINE_MAX,mesh_file);
  32. still_comments = (line[0] == '#' || line[0] == '\n');
  33. }
  34. char str[LINE_MAX];
  35. sscanf(line," %s",str);
  36. // check that first word is MeshVersionFormatted
  37. if(0!=strcmp(str,"MeshVersionFormatted"))
  38. {
  39. fprintf(stderr,
  40. "Error: first word should be MeshVersionFormatted not %s\n",str);
  41. fclose(mesh_file);
  42. return false;
  43. }
  44. int one = -1;
  45. if(2 != sscanf(line,"%s %d",str,&one))
  46. {
  47. // 1 appears on next line?
  48. fscanf(mesh_file," %d",&one);
  49. }
  50. if(one != 1)
  51. {
  52. fprintf(stderr,"Error: second word should be 1 not %d\n",one);
  53. fclose(mesh_file);
  54. return false;
  55. }
  56. // eat comments
  57. still_comments= true;
  58. while(still_comments)
  59. {
  60. fgets(line,LINE_MAX,mesh_file);
  61. still_comments = (line[0] == '#' || line[0] == '\n');
  62. }
  63. sscanf(line," %s",str);
  64. // check that third word is Dimension
  65. if(0!=strcmp(str,"Dimension"))
  66. {
  67. fprintf(stderr,"Error: third word should be Dimension not %s\n",str);
  68. fclose(mesh_file);
  69. return false;
  70. }
  71. int three = -1;
  72. if(2 != sscanf(line,"%s %d",str,&three))
  73. {
  74. // 1 appears on next line?
  75. fscanf(mesh_file," %d",&three);
  76. }
  77. if(three != 3)
  78. {
  79. fprintf(stderr,"Error: only Dimension 3 supported not %d\n",three);
  80. fclose(mesh_file);
  81. return false;
  82. }
  83. // eat comments
  84. still_comments= true;
  85. while(still_comments)
  86. {
  87. fgets(line,LINE_MAX,mesh_file);
  88. still_comments = (line[0] == '#' || line[0] == '\n');
  89. }
  90. sscanf(line," %s",str);
  91. // check that fifth word is Vertices
  92. if(0!=strcmp(str,"Vertices"))
  93. {
  94. fprintf(stderr,"Error: fifth word should be Vertices not %s\n",str);
  95. fclose(mesh_file);
  96. return false;
  97. }
  98. //fgets(line,LINE_MAX,mesh_file);
  99. int number_of_vertices;
  100. if(1 != fscanf(mesh_file," %d",&number_of_vertices) || number_of_vertices > 1000000000)
  101. {
  102. fprintf(stderr,"Error: expecting number of vertices less than 10^9...\n");
  103. fclose(mesh_file);
  104. return false;
  105. }
  106. // allocate space for vertices
  107. V.resize(number_of_vertices,vector<Scalar>(3,0));
  108. int extra;
  109. for(int i = 0;i<number_of_vertices;i++)
  110. {
  111. double x,y,z;
  112. if(4 != fscanf(mesh_file," %lg %lg %lg %d",&x,&y,&z,&extra))
  113. {
  114. fprintf(stderr,"Error: expecting vertex position...\n");
  115. fclose(mesh_file);
  116. return false;
  117. }
  118. V[i][0] = x;
  119. V[i][1] = y;
  120. V[i][2] = z;
  121. }
  122. // eat comments
  123. still_comments= true;
  124. while(still_comments)
  125. {
  126. fgets(line,LINE_MAX,mesh_file);
  127. still_comments = (line[0] == '#' || line[0] == '\n');
  128. }
  129. sscanf(line," %s",str);
  130. // check that sixth word is Triangles
  131. if(0!=strcmp(str,"Triangles"))
  132. {
  133. fprintf(stderr,"Error: sixth word should be Triangles not %s\n",str);
  134. fclose(mesh_file);
  135. return false;
  136. }
  137. int number_of_triangles;
  138. if(1 != fscanf(mesh_file," %d",&number_of_triangles))
  139. {
  140. fprintf(stderr,"Error: expecting number of triangles...\n");
  141. fclose(mesh_file);
  142. return false;
  143. }
  144. // allocate space for triangles
  145. F.resize(number_of_triangles,vector<Index>(3));
  146. // triangle indices
  147. int tri[3];
  148. for(int i = 0;i<number_of_triangles;i++)
  149. {
  150. if(4 != fscanf(mesh_file," %d %d %d %d",&tri[0],&tri[1],&tri[2],&extra))
  151. {
  152. printf("Error: expecting triangle indices...\n");
  153. return false;
  154. }
  155. for(int j = 0;j<3;j++)
  156. {
  157. F[i][j] = tri[j]-1;
  158. }
  159. }
  160. // eat comments
  161. still_comments= true;
  162. while(still_comments)
  163. {
  164. fgets(line,LINE_MAX,mesh_file);
  165. still_comments = (line[0] == '#' || line[0] == '\n');
  166. }
  167. sscanf(line," %s",str);
  168. // check that sixth word is Triangles
  169. if(0!=strcmp(str,"Tetrahedra"))
  170. {
  171. fprintf(stderr,"Error: seventh word should be Tetrahedra not %s\n",str);
  172. fclose(mesh_file);
  173. return false;
  174. }
  175. int number_of_tetrahedra;
  176. if(1 != fscanf(mesh_file," %d",&number_of_tetrahedra))
  177. {
  178. fprintf(stderr,"Error: expecting number of tetrahedra...\n");
  179. fclose(mesh_file);
  180. return false;
  181. }
  182. // allocate space for tetrahedra
  183. T.resize(number_of_tetrahedra,vector<Index>(4));
  184. // tet indices
  185. int a,b,c,d;
  186. for(int i = 0;i<number_of_tetrahedra;i++)
  187. {
  188. if(5 != fscanf(mesh_file," %d %d %d %d %d",&a,&b,&c,&d,&extra))
  189. {
  190. fprintf(stderr,"Error: expecting tetrahedra indices...\n");
  191. fclose(mesh_file);
  192. return false;
  193. }
  194. T[i][0] = a-1;
  195. T[i][1] = b-1;
  196. T[i][2] = c-1;
  197. T[i][3] = d-1;
  198. }
  199. fclose(mesh_file);
  200. return true;
  201. }
  202. #include <Eigen/Core>
  203. #include "list_to_matrix.h"
  204. template <typename DerivedV, typename DerivedF, typename DerivedT>
  205. IGL_INLINE bool igl::readMESH(
  206. const std::string str,
  207. Eigen::PlainObjectBase<DerivedV>& V,
  208. Eigen::PlainObjectBase<DerivedT>& T,
  209. Eigen::PlainObjectBase<DerivedF>& F)
  210. {
  211. std::vector<std::vector<double> > vV,vT,vF;
  212. bool success = igl::readMESH(str,vV,vT,vF);
  213. if(!success)
  214. {
  215. // readOBJ(str,vV,vTC,vN,vF,vFTC,vFN) should have already printed an error
  216. // message to stderr
  217. return false;
  218. }
  219. bool V_rect = igl::list_to_matrix(vV,V);
  220. if(!V_rect)
  221. {
  222. // igl::list_to_matrix(vV,V) already printed error message to std err
  223. return false;
  224. }
  225. bool T_rect = igl::list_to_matrix(vT,T);
  226. if(!T_rect)
  227. {
  228. // igl::list_to_matrix(vT,T) already printed error message to std err
  229. return false;
  230. }
  231. bool F_rect = igl::list_to_matrix(vF,F);
  232. if(!F_rect)
  233. {
  234. // igl::list_to_matrix(vF,F) already printed error message to std err
  235. return false;
  236. }
  237. assert(V.cols() == 3);
  238. assert(T.cols() == 4);
  239. assert(F.cols() == 3);
  240. return true;
  241. }
  242. #ifndef IGL_HEADER_ONLY
  243. // Explicit template specialization
  244. // generated by autoexplicit.sh
  245. template bool igl::readMESH<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -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> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  246. #endif