readMESH.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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 "readMESH.h"
  9. #include <cstdio>
  10. #include "verbose.h"
  11. template <typename Scalar, typename Index>
  12. IGL_INLINE bool igl::readMESH(
  13. const std::string mesh_file_name,
  14. std::vector<std::vector<Scalar > > & V,
  15. std::vector<std::vector<Index > > & T,
  16. std::vector<std::vector<Index > > & F)
  17. {
  18. using namespace std;
  19. FILE * mesh_file = fopen(mesh_file_name.c_str(),"r");
  20. if(NULL==mesh_file)
  21. {
  22. fprintf(stderr,"IOError: %s could not be opened...",mesh_file_name.c_str());
  23. return false;
  24. }
  25. #ifndef LINE_MAX
  26. # define LINE_MAX 2048
  27. #endif
  28. char line[LINE_MAX];
  29. bool still_comments;
  30. V.clear();
  31. T.clear();
  32. F.clear();
  33. // eat comments at beginning of file
  34. still_comments= true;
  35. while(still_comments)
  36. {
  37. fgets(line,LINE_MAX,mesh_file);
  38. still_comments = (line[0] == '#' || line[0] == '\n');
  39. }
  40. char str[LINE_MAX];
  41. sscanf(line," %s",str);
  42. // check that first word is MeshVersionFormatted
  43. if(0!=strcmp(str,"MeshVersionFormatted"))
  44. {
  45. fprintf(stderr,
  46. "Error: first word should be MeshVersionFormatted not %s\n",str);
  47. fclose(mesh_file);
  48. return false;
  49. }
  50. int one = -1;
  51. if(2 != sscanf(line,"%s %d",str,&one))
  52. {
  53. // 1 appears on next line?
  54. fscanf(mesh_file," %d",&one);
  55. }
  56. if(one != 1)
  57. {
  58. fprintf(stderr,"Error: second word should be 1 not %d\n",one);
  59. fclose(mesh_file);
  60. return false;
  61. }
  62. // eat comments
  63. still_comments= true;
  64. while(still_comments)
  65. {
  66. fgets(line,LINE_MAX,mesh_file);
  67. still_comments = (line[0] == '#' || line[0] == '\n');
  68. }
  69. sscanf(line," %s",str);
  70. // check that third word is Dimension
  71. if(0!=strcmp(str,"Dimension"))
  72. {
  73. fprintf(stderr,"Error: third word should be Dimension not %s\n",str);
  74. fclose(mesh_file);
  75. return false;
  76. }
  77. int three = -1;
  78. if(2 != sscanf(line,"%s %d",str,&three))
  79. {
  80. // 1 appears on next line?
  81. fscanf(mesh_file," %d",&three);
  82. }
  83. if(three != 3)
  84. {
  85. fprintf(stderr,"Error: only Dimension 3 supported not %d\n",three);
  86. fclose(mesh_file);
  87. return false;
  88. }
  89. // eat comments
  90. still_comments= true;
  91. while(still_comments)
  92. {
  93. fgets(line,LINE_MAX,mesh_file);
  94. still_comments = (line[0] == '#' || line[0] == '\n');
  95. }
  96. sscanf(line," %s",str);
  97. // check that fifth word is Vertices
  98. if(0!=strcmp(str,"Vertices"))
  99. {
  100. fprintf(stderr,"Error: fifth word should be Vertices not %s\n",str);
  101. fclose(mesh_file);
  102. return false;
  103. }
  104. //fgets(line,LINE_MAX,mesh_file);
  105. int number_of_vertices;
  106. if(1 != fscanf(mesh_file," %d",&number_of_vertices) || number_of_vertices > 1000000000)
  107. {
  108. fprintf(stderr,"Error: expecting number of vertices less than 10^9...\n");
  109. fclose(mesh_file);
  110. return false;
  111. }
  112. // allocate space for vertices
  113. V.resize(number_of_vertices,vector<Scalar>(3,0));
  114. int extra;
  115. for(int i = 0;i<number_of_vertices;i++)
  116. {
  117. double x,y,z;
  118. if(4 != fscanf(mesh_file," %lg %lg %lg %d",&x,&y,&z,&extra))
  119. {
  120. fprintf(stderr,"Error: expecting vertex position...\n");
  121. fclose(mesh_file);
  122. return false;
  123. }
  124. V[i][0] = x;
  125. V[i][1] = y;
  126. V[i][2] = z;
  127. }
  128. // eat comments
  129. still_comments= true;
  130. while(still_comments)
  131. {
  132. fgets(line,LINE_MAX,mesh_file);
  133. still_comments = (line[0] == '#' || line[0] == '\n');
  134. }
  135. sscanf(line," %s",str);
  136. // check that sixth word is Triangles
  137. if(0!=strcmp(str,"Triangles"))
  138. {
  139. fprintf(stderr,"Error: sixth word should be Triangles not %s\n",str);
  140. fclose(mesh_file);
  141. return false;
  142. }
  143. int number_of_triangles;
  144. if(1 != fscanf(mesh_file," %d",&number_of_triangles))
  145. {
  146. fprintf(stderr,"Error: expecting number of triangles...\n");
  147. fclose(mesh_file);
  148. return false;
  149. }
  150. // allocate space for triangles
  151. F.resize(number_of_triangles,vector<Index>(3));
  152. // triangle indices
  153. int tri[3];
  154. for(int i = 0;i<number_of_triangles;i++)
  155. {
  156. if(4 != fscanf(mesh_file," %d %d %d %d",&tri[0],&tri[1],&tri[2],&extra))
  157. {
  158. printf("Error: expecting triangle indices...\n");
  159. return false;
  160. }
  161. for(int j = 0;j<3;j++)
  162. {
  163. F[i][j] = tri[j]-1;
  164. }
  165. }
  166. // eat comments
  167. still_comments= true;
  168. while(still_comments)
  169. {
  170. fgets(line,LINE_MAX,mesh_file);
  171. still_comments = (line[0] == '#' || line[0] == '\n');
  172. }
  173. sscanf(line," %s",str);
  174. // check that sixth word is Triangles
  175. if(0!=strcmp(str,"Tetrahedra"))
  176. {
  177. fprintf(stderr,"Error: seventh word should be Tetrahedra not %s\n",str);
  178. fclose(mesh_file);
  179. return false;
  180. }
  181. int number_of_tetrahedra;
  182. if(1 != fscanf(mesh_file," %d",&number_of_tetrahedra))
  183. {
  184. fprintf(stderr,"Error: expecting number of tetrahedra...\n");
  185. fclose(mesh_file);
  186. return false;
  187. }
  188. // allocate space for tetrahedra
  189. T.resize(number_of_tetrahedra,vector<Index>(4));
  190. // tet indices
  191. int a,b,c,d;
  192. for(int i = 0;i<number_of_tetrahedra;i++)
  193. {
  194. if(5 != fscanf(mesh_file," %d %d %d %d %d",&a,&b,&c,&d,&extra))
  195. {
  196. fprintf(stderr,"Error: expecting tetrahedra indices...\n");
  197. fclose(mesh_file);
  198. return false;
  199. }
  200. T[i][0] = a-1;
  201. T[i][1] = b-1;
  202. T[i][2] = c-1;
  203. T[i][3] = d-1;
  204. }
  205. fclose(mesh_file);
  206. return true;
  207. }
  208. #include <Eigen/Core>
  209. #include "list_to_matrix.h"
  210. template <typename DerivedV, typename DerivedF, typename DerivedT>
  211. IGL_INLINE bool igl::readMESH(
  212. const std::string mesh_file_name,
  213. Eigen::PlainObjectBase<DerivedV>& V,
  214. Eigen::PlainObjectBase<DerivedT>& T,
  215. Eigen::PlainObjectBase<DerivedF>& F)
  216. {
  217. using namespace std;
  218. FILE * mesh_file = fopen(mesh_file_name.c_str(),"r");
  219. if(NULL==mesh_file)
  220. {
  221. fprintf(stderr,"IOError: %s could not be opened...",mesh_file_name.c_str());
  222. return false;
  223. }
  224. #ifndef LINE_MAX
  225. # define LINE_MAX 2048
  226. #endif
  227. char line[LINE_MAX];
  228. bool still_comments;
  229. // eat comments at beginning of file
  230. still_comments= true;
  231. while(still_comments)
  232. {
  233. fgets(line,LINE_MAX,mesh_file);
  234. still_comments = (line[0] == '#' || line[0] == '\n');
  235. }
  236. char str[LINE_MAX];
  237. sscanf(line," %s",str);
  238. // check that first word is MeshVersionFormatted
  239. if(0!=strcmp(str,"MeshVersionFormatted"))
  240. {
  241. fprintf(stderr,
  242. "Error: first word should be MeshVersionFormatted not %s\n",str);
  243. fclose(mesh_file);
  244. return false;
  245. }
  246. int one = -1;
  247. if(2 != sscanf(line,"%s %d",str,&one))
  248. {
  249. // 1 appears on next line?
  250. fscanf(mesh_file," %d",&one);
  251. }
  252. if(one != 1)
  253. {
  254. fprintf(stderr,"Error: second word should be 1 not %d\n",one);
  255. fclose(mesh_file);
  256. return false;
  257. }
  258. // eat comments
  259. still_comments= true;
  260. while(still_comments)
  261. {
  262. fgets(line,LINE_MAX,mesh_file);
  263. still_comments = (line[0] == '#' || line[0] == '\n');
  264. }
  265. sscanf(line," %s",str);
  266. // check that third word is Dimension
  267. if(0!=strcmp(str,"Dimension"))
  268. {
  269. fprintf(stderr,"Error: third word should be Dimension not %s\n",str);
  270. fclose(mesh_file);
  271. return false;
  272. }
  273. int three = -1;
  274. if(2 != sscanf(line,"%s %d",str,&three))
  275. {
  276. // 1 appears on next line?
  277. fscanf(mesh_file," %d",&three);
  278. }
  279. if(three != 3)
  280. {
  281. fprintf(stderr,"Error: only Dimension 3 supported not %d\n",three);
  282. fclose(mesh_file);
  283. return false;
  284. }
  285. // eat comments
  286. still_comments= true;
  287. while(still_comments)
  288. {
  289. fgets(line,LINE_MAX,mesh_file);
  290. still_comments = (line[0] == '#' || line[0] == '\n');
  291. }
  292. sscanf(line," %s",str);
  293. // check that fifth word is Vertices
  294. if(0!=strcmp(str,"Vertices"))
  295. {
  296. fprintf(stderr,"Error: fifth word should be Vertices not %s\n",str);
  297. fclose(mesh_file);
  298. return false;
  299. }
  300. //fgets(line,LINE_MAX,mesh_file);
  301. int number_of_vertices;
  302. if(1 != fscanf(mesh_file," %d",&number_of_vertices) || number_of_vertices > 1000000000)
  303. {
  304. fprintf(stderr,"Error: expecting number of vertices less than 10^9...\n");
  305. fclose(mesh_file);
  306. return false;
  307. }
  308. // allocate space for vertices
  309. V.resize(number_of_vertices,3);
  310. int extra;
  311. for(int i = 0;i<number_of_vertices;i++)
  312. {
  313. double x,y,z;
  314. if(4 != fscanf(mesh_file," %lg %lg %lg %d",&x,&y,&z,&extra))
  315. {
  316. fprintf(stderr,"Error: expecting vertex position...\n");
  317. fclose(mesh_file);
  318. return false;
  319. }
  320. V(i,0) = x;
  321. V(i,1) = y;
  322. V(i,2) = z;
  323. }
  324. // eat comments
  325. still_comments= true;
  326. while(still_comments)
  327. {
  328. fgets(line,LINE_MAX,mesh_file);
  329. still_comments = (line[0] == '#' || line[0] == '\n');
  330. }
  331. sscanf(line," %s",str);
  332. // check that sixth word is Triangles
  333. if(0!=strcmp(str,"Triangles"))
  334. {
  335. fprintf(stderr,"Error: sixth word should be Triangles not %s\n",str);
  336. fclose(mesh_file);
  337. return false;
  338. }
  339. int number_of_triangles;
  340. if(1 != fscanf(mesh_file," %d",&number_of_triangles))
  341. {
  342. fprintf(stderr,"Error: expecting number of triangles...\n");
  343. fclose(mesh_file);
  344. return false;
  345. }
  346. // allocate space for triangles
  347. F.resize(number_of_triangles,3);
  348. // triangle indices
  349. int tri[3];
  350. for(int i = 0;i<number_of_triangles;i++)
  351. {
  352. if(4 != fscanf(mesh_file," %d %d %d %d",&tri[0],&tri[1],&tri[2],&extra))
  353. {
  354. printf("Error: expecting triangle indices...\n");
  355. return false;
  356. }
  357. for(int j = 0;j<3;j++)
  358. {
  359. F(i,j) = tri[j]-1;
  360. }
  361. }
  362. // eat comments
  363. still_comments= true;
  364. while(still_comments)
  365. {
  366. fgets(line,LINE_MAX,mesh_file);
  367. still_comments = (line[0] == '#' || line[0] == '\n');
  368. }
  369. sscanf(line," %s",str);
  370. // check that sixth word is Triangles
  371. if(0!=strcmp(str,"Tetrahedra"))
  372. {
  373. fprintf(stderr,"Error: seventh word should be Tetrahedra not %s\n",str);
  374. fclose(mesh_file);
  375. return false;
  376. }
  377. int number_of_tetrahedra;
  378. if(1 != fscanf(mesh_file," %d",&number_of_tetrahedra))
  379. {
  380. fprintf(stderr,"Error: expecting number of tetrahedra...\n");
  381. fclose(mesh_file);
  382. return false;
  383. }
  384. // allocate space for tetrahedra
  385. T.resize(number_of_tetrahedra,4);
  386. // tet indices
  387. int a,b,c,d;
  388. for(int i = 0;i<number_of_tetrahedra;i++)
  389. {
  390. if(5 != fscanf(mesh_file," %d %d %d %d %d",&a,&b,&c,&d,&extra))
  391. {
  392. fprintf(stderr,"Error: expecting tetrahedra indices...\n");
  393. fclose(mesh_file);
  394. return false;
  395. }
  396. T(i,0) = a-1;
  397. T(i,1) = b-1;
  398. T(i,2) = c-1;
  399. T(i,3) = d-1;
  400. }
  401. fclose(mesh_file);
  402. return true;
  403. }
  404. //{
  405. // std::vector<std::vector<double> > vV,vT,vF;
  406. // bool success = igl::readMESH(mesh_file_name,vV,vT,vF);
  407. // if(!success)
  408. // {
  409. // // readMESH already printed error message to std err
  410. // return false;
  411. // }
  412. // bool V_rect = igl::list_to_matrix(vV,V);
  413. // if(!V_rect)
  414. // {
  415. // // igl::list_to_matrix(vV,V) already printed error message to std err
  416. // return false;
  417. // }
  418. // bool T_rect = igl::list_to_matrix(vT,T);
  419. // if(!T_rect)
  420. // {
  421. // // igl::list_to_matrix(vT,T) already printed error message to std err
  422. // return false;
  423. // }
  424. // bool F_rect = igl::list_to_matrix(vF,F);
  425. // if(!F_rect)
  426. // {
  427. // // igl::list_to_matrix(vF,F) already printed error message to std err
  428. // return false;
  429. // }
  430. // assert(V.cols() == 3);
  431. // assert(T.cols() == 4);
  432. // assert(F.cols() == 3);
  433. // return true;
  434. //}
  435. #ifdef IGL_STATIC_LIBRARY
  436. // Explicit template specialization
  437. // generated by autoexplicit.sh
  438. template bool igl::readMESH<Eigen::Matrix<float, -1, 3, 1, -1, 3>, Eigen::Matrix<unsigned int, -1, 3, 1, -1, 3>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 3, 1, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<unsigned int, -1, 3, 1, -1, 3> >&);
  439. // generated by autoexplicit.sh
  440. template bool igl::readMESH<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3>, 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, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> >&);
  441. // generated by autoexplicit.sh
  442. 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> >&);
  443. template bool igl::readMESH<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, 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> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  444. template bool igl::readMESH<double, int>(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> > > >&, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >&, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >&);
  445. #endif