readMESH.h 7.1 KB

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