readMESH.cpp 16 KB

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