readOBJ.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 "readOBJ.h"
  9. #include "list_to_matrix.h"
  10. #include <iostream>
  11. #include <cstdio>
  12. #include <fstream>
  13. template <typename Scalar, typename Index>
  14. IGL_INLINE bool igl::readOBJ(
  15. const std::string obj_file_name,
  16. std::vector<std::vector<Scalar > > & V,
  17. std::vector<std::vector<Scalar > > & TC,
  18. std::vector<std::vector<Scalar > > & N,
  19. std::vector<std::vector<Index > > & F,
  20. std::vector<std::vector<Index > > & FTC,
  21. std::vector<std::vector<Index > > & FN)
  22. {
  23. // Open file, and check for error
  24. FILE * obj_file = fopen(obj_file_name.c_str(),"r");
  25. if(NULL==obj_file)
  26. {
  27. fprintf(stderr,"IOError: %s could not be opened...\n",
  28. obj_file_name.c_str());
  29. return false;
  30. }
  31. // File open was succesfull so clear outputs
  32. V.clear();
  33. TC.clear();
  34. N.clear();
  35. F.clear();
  36. FTC.clear();
  37. FN.clear();
  38. // variables an constants to assist parsing the .obj file
  39. // Constant strings to compare against
  40. std::string v("v");
  41. std::string vn("vn");
  42. std::string vt("vt");
  43. std::string f("f");
  44. std::string tic_tac_toe("#");
  45. #ifndef IGL_LINE_MAX
  46. # define IGL_LINE_MAX 2048
  47. #endif
  48. char line[IGL_LINE_MAX];
  49. int line_no = 1;
  50. while (fgets(line, IGL_LINE_MAX, obj_file) != NULL)
  51. {
  52. char type[IGL_LINE_MAX];
  53. // Read first word containing type
  54. if(sscanf(line, "%s",type) == 1)
  55. {
  56. // Get pointer to rest of line right after type
  57. char * l = &line[strlen(type)];
  58. if(type == v)
  59. {
  60. double x[4];
  61. int count =
  62. sscanf(l,"%lf %lf %lf %lf\n",&x[0],&x[1],&x[2],&x[3]);
  63. if(count != 3 && count != 4)
  64. {
  65. fprintf(stderr,
  66. "Error: readOBJ() vertex on line %d should have 3 or 4 coordinates",
  67. line_no);
  68. fclose(obj_file);
  69. return false;
  70. }
  71. std::vector<Scalar > vertex(count);
  72. for(int i = 0;i<count;i++)
  73. {
  74. vertex[i] = x[i];
  75. }
  76. V.push_back(vertex);
  77. }else if(type == vn)
  78. {
  79. double x[3];
  80. int count =
  81. sscanf(l,"%lf %lf %lf\n",&x[0],&x[1],&x[2]);
  82. if(count != 3)
  83. {
  84. fprintf(stderr,
  85. "Error: readOBJ() normal on line %d should have 3 coordinates",
  86. line_no);
  87. fclose(obj_file);
  88. return false;
  89. }
  90. std::vector<Scalar > normal(count);
  91. for(int i = 0;i<count;i++)
  92. {
  93. normal[i] = x[i];
  94. }
  95. N.push_back(normal);
  96. }else if(type == vt)
  97. {
  98. double x[3];
  99. int count =
  100. sscanf(l,"%lf %lf %lf\n",&x[0],&x[1],&x[2]);
  101. if(count != 2 && count != 3)
  102. {
  103. fprintf(stderr,
  104. "Error: readOBJ() vertex on line %d should have 2 or 3 coordinates (%d)",
  105. line_no,count);
  106. fclose(obj_file);
  107. return false;
  108. }
  109. std::vector<Scalar > tex(count);
  110. for(int i = 0;i<count;i++)
  111. {
  112. tex[i] = x[i];
  113. }
  114. TC.push_back(tex);
  115. }else if(type == f)
  116. {
  117. std::vector<Index > f;
  118. std::vector<Index > ftc;
  119. std::vector<Index > fn;
  120. // Read each "word" after type
  121. char word[IGL_LINE_MAX];
  122. int offset;
  123. while(sscanf(l,"%s%n",word,&offset) == 1)
  124. {
  125. // adjust offset
  126. l += offset;
  127. // Process word
  128. unsigned int i,it,in;
  129. if(sscanf(word,"%u/%u/%u",&i,&it,&in) == 3)
  130. {
  131. f.push_back(i-1);
  132. ftc.push_back(it-1);
  133. fn.push_back(in-1);
  134. }else if(sscanf(word,"%u/%u",&i,&it) == 2)
  135. {
  136. f.push_back(i-1);
  137. ftc.push_back(it-1);
  138. }else if(sscanf(word,"%u//%u",&i,&in) == 2)
  139. {
  140. f.push_back(i-1);
  141. fn.push_back(in-1);
  142. }else if(sscanf(word,"%u",&i) == 1)
  143. {
  144. f.push_back(i-1);
  145. }else
  146. {
  147. fprintf(stderr,
  148. "Error: readOBJ() face on line %d has invalid element format\n",
  149. line_no);
  150. fclose(obj_file);
  151. return false;
  152. }
  153. }
  154. if(
  155. (f.size()>0 && fn.size() == 0 && ftc.size() == 0) ||
  156. (f.size()>0 && fn.size() == f.size() && ftc.size() == 0) ||
  157. (f.size()>0 && fn.size() == 0 && ftc.size() == f.size()) ||
  158. (f.size()>0 && fn.size() == f.size() && ftc.size() == f.size()))
  159. {
  160. // No matter what add each type to lists so that lists are the
  161. // correct lengths
  162. F.push_back(f);
  163. FTC.push_back(ftc);
  164. FN.push_back(fn);
  165. }else
  166. {
  167. fprintf(stderr,
  168. "Error: readOBJ() face on line %d has invalid format\n", line_no);
  169. fclose(obj_file);
  170. return false;
  171. }
  172. }else if(strlen(type) >= 1 && (type[0] == '#' ||
  173. type[0] == 'g' ||
  174. type[0] == 's' ||
  175. strcmp("usemtl",type)==0 ||
  176. strcmp("mtllib",type)==0))
  177. {
  178. //ignore comments or other shit
  179. }else
  180. {
  181. //ignore any other lines
  182. fprintf(stderr,
  183. "Warning: readOBJ() ignored non-comment line %d:\n %s",
  184. line_no,
  185. line);
  186. }
  187. }else
  188. {
  189. // ignore empty line
  190. }
  191. line_no++;
  192. }
  193. fclose(obj_file);
  194. assert(F.size() == FN.size());
  195. assert(F.size() == FTC.size());
  196. return true;
  197. }
  198. template <typename Scalar, typename Index>
  199. IGL_INLINE bool igl::readOBJ(
  200. const std::string obj_file_name,
  201. std::vector<std::vector<Scalar > > & V,
  202. std::vector<std::vector<Index > > & F)
  203. {
  204. std::vector<std::vector<Scalar > > TC,N;
  205. std::vector<std::vector<Index > > FTC,FN;
  206. return readOBJ(obj_file_name,V,TC,N,F,FTC,FN);
  207. }
  208. template <typename DerivedV, typename DerivedF, typename DerivedT>
  209. IGL_INLINE bool igl::readOBJ(
  210. const std::string str,
  211. Eigen::PlainObjectBase<DerivedV>& V,
  212. Eigen::PlainObjectBase<DerivedF>& F,
  213. Eigen::PlainObjectBase<DerivedV>& CN,
  214. Eigen::PlainObjectBase<DerivedF>& FN,
  215. Eigen::PlainObjectBase<DerivedT>& TC,
  216. Eigen::PlainObjectBase<DerivedF>& FTC)
  217. {
  218. std::vector<std::vector<double> > vV,vTC,vN;
  219. std::vector<std::vector<int> > vF,vFTC,vFN;
  220. bool success = igl::readOBJ(str,vV,vTC,vN,vF,vFTC,vFN);
  221. if(!success)
  222. {
  223. // readOBJ(str,vV,vTC,vN,vF,vFTC,vFN) should have already printed an error
  224. // message to stderr
  225. return false;
  226. }
  227. bool V_rect = igl::list_to_matrix(vV,V);
  228. if(!V_rect)
  229. {
  230. // igl::list_to_matrix(vV,V) already printed error message to std err
  231. return false;
  232. }
  233. bool F_rect = igl::list_to_matrix(vF,F);
  234. if(!F_rect)
  235. {
  236. // igl::list_to_matrix(vF,F) already printed error message to std err
  237. return false;
  238. }
  239. if(!vN.empty())
  240. {
  241. bool VN_rect = igl::list_to_matrix(vN,CN);
  242. if(!VN_rect)
  243. {
  244. // igl::list_to_matrix(vV,V) already printed error message to std err
  245. return false;
  246. }
  247. }
  248. if(!vFN.empty() && !vFN[0].empty())
  249. {
  250. bool FN_rect = igl::list_to_matrix(vFN,FN);
  251. if(!FN_rect)
  252. {
  253. // igl::list_to_matrix(vV,V) already printed error message to std err
  254. return false;
  255. }
  256. }
  257. if(!vTC.empty())
  258. {
  259. bool T_rect = igl::list_to_matrix(vTC,TC);
  260. if(!T_rect)
  261. {
  262. // igl::list_to_matrix(vTC,T) already printed error message to std err
  263. return false;
  264. }
  265. }
  266. if(!vFTC.empty()&& !vFTC[0].empty())
  267. {
  268. bool FTC_rect = igl::list_to_matrix(vFTC,FTC);
  269. if(!FTC_rect)
  270. {
  271. // igl::list_to_matrix(vF,F) already printed error message to std err
  272. return false;
  273. }
  274. }
  275. // Legacy
  276. if(F.cols() != 3)
  277. {
  278. fprintf(stderr,
  279. "Error: readOBJ(filename,V,F) is meant for reading triangle-only"
  280. " meshes. This mesh has faces all with size %d. See readOBJ.h for other"
  281. " options.\n",
  282. (int)F.cols());
  283. return false;
  284. }
  285. return true;
  286. }
  287. template <typename DerivedV, typename DerivedF, typename DerivedT, typename Index>
  288. IGL_INLINE bool igl::readOBJPoly(
  289. const std::string str,
  290. Eigen::PlainObjectBase<DerivedV>& V,
  291. std::vector<std::vector< Index > >& F,
  292. Eigen::PlainObjectBase<DerivedV>& CN,
  293. Eigen::PlainObjectBase<DerivedF>& FN,
  294. Eigen::PlainObjectBase<DerivedT>& TC,
  295. Eigen::PlainObjectBase<DerivedF>& FTC)
  296. {
  297. std::vector<std::vector<double> > vV,vTC,vN;
  298. std::vector<std::vector<Index> > vF,vFTC,vFN;
  299. bool success = igl::readOBJ(str,vV,vTC,vN,vF,vFTC,vFN);
  300. if(!success)
  301. return false;
  302. bool V_rect = igl::list_to_matrix(vV,V);
  303. if(!V_rect)
  304. return false;
  305. F = vF;
  306. if(!vN.empty())
  307. {
  308. bool VN_rect = igl::list_to_matrix(vN,CN);
  309. if(!VN_rect)
  310. return false;
  311. }
  312. if(!vFN.empty())
  313. {
  314. bool FN_rect = igl::list_to_matrix(vFN,FN);
  315. if(!FN_rect)
  316. return false;
  317. }
  318. if(!vTC.empty())
  319. {
  320. bool T_rect = igl::list_to_matrix(vTC,TC);
  321. if(!T_rect)
  322. return false;
  323. }
  324. if(!vFTC.empty())
  325. {
  326. bool FTC_rect = igl::list_to_matrix(vFTC,FTC);
  327. if(!FTC_rect)
  328. return false;
  329. }
  330. return true;
  331. }
  332. template <typename DerivedV, typename DerivedF>
  333. IGL_INLINE bool igl::readOBJ(
  334. const std::string str,
  335. Eigen::PlainObjectBase<DerivedV>& V,
  336. Eigen::PlainObjectBase<DerivedF>& F)
  337. {
  338. std::vector<std::vector<double> > vV,vTC,vN;
  339. std::vector<std::vector<int> > vF,vFTC,vFN;
  340. bool success = igl::readOBJ(str,vV,vTC,vN,vF,vFTC,vFN);
  341. if(!success)
  342. {
  343. // readOBJ(str,vV,vTC,vN,vF,vFTC,vFN) should have already printed an error
  344. // message to stderr
  345. return false;
  346. }
  347. bool V_rect = igl::list_to_matrix(vV,V);
  348. if(!V_rect)
  349. {
  350. // igl::list_to_matrix(vV,V) already printed error message to std err
  351. return false;
  352. }
  353. bool F_rect = igl::list_to_matrix(vF,F);
  354. if(!F_rect)
  355. {
  356. // igl::list_to_matrix(vF,F) already printed error message to std err
  357. return false;
  358. }
  359. // THIS IS ANNOYING, I WANT TO READ QUADS, Why not?
  360. //// Legacy
  361. //if(F.cols() != 3)
  362. //{
  363. // fprintf(stderr,
  364. // "Error: readOBJ(filename,V,F) is meant for reading triangle-only"
  365. // " meshes. This mesh has faces all with size %d. See readOBJ.h for other"
  366. // " options.\n",
  367. // (int)F.cols());
  368. // return false;
  369. //}
  370. return true;
  371. }
  372. #ifdef IGL_STATIC_LIBRARY
  373. // Explicit template specialization
  374. // generated by autoexplicit.sh
  375. template bool igl::readOBJ<Eigen::Matrix<double, -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> >&);
  376. template bool igl::readOBJ<Eigen::Matrix<double, -1, 3, 1, -1, 3>, Eigen::Matrix<unsigned int, -1, -1, 1, -1, -1>, Eigen::Matrix<double, -1, 2, 1, -1, 2> >(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<unsigned int, -1, -1, 1, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 1, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<unsigned int, -1, -1, 1, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 2, 1, -1, 2> >&, Eigen::PlainObjectBase<Eigen::Matrix<unsigned int, -1, -1, 1, -1, -1> >&);
  377. template bool igl::readOBJ<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<int, -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> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  378. template bool igl::readOBJ<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3> >(std::string, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> >&);
  379. template bool igl::readOBJPoly<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, int>(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -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> >&);
  380. #endif