readOBJ.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 DerivedV, typename DerivedF, typename DerivedT>
  199. IGL_INLINE bool igl::readOBJ(
  200. const std::string str,
  201. Eigen::PlainObjectBase<DerivedV>& V,
  202. Eigen::PlainObjectBase<DerivedF>& F,
  203. Eigen::PlainObjectBase<DerivedV>& CN,
  204. Eigen::PlainObjectBase<DerivedF>& FN,
  205. Eigen::PlainObjectBase<DerivedT>& TC,
  206. Eigen::PlainObjectBase<DerivedF>& FTC)
  207. {
  208. std::vector<std::vector<double> > vV,vTC,vN;
  209. std::vector<std::vector<int> > vF,vFTC,vFN;
  210. bool success = igl::readOBJ(str,vV,vTC,vN,vF,vFTC,vFN);
  211. if(!success)
  212. {
  213. // readOBJ(str,vV,vTC,vN,vF,vFTC,vFN) should have already printed an error
  214. // message to stderr
  215. return false;
  216. }
  217. bool V_rect = igl::list_to_matrix(vV,V);
  218. if(!V_rect)
  219. {
  220. // igl::list_to_matrix(vV,V) already printed error message to std err
  221. return false;
  222. }
  223. bool F_rect = igl::list_to_matrix(vF,F);
  224. if(!F_rect)
  225. {
  226. // igl::list_to_matrix(vF,F) already printed error message to std err
  227. return false;
  228. }
  229. if(!vN.empty())
  230. {
  231. bool VN_rect = igl::list_to_matrix(vN,CN);
  232. if(!VN_rect)
  233. {
  234. // igl::list_to_matrix(vV,V) already printed error message to std err
  235. return false;
  236. }
  237. }
  238. if(!vFN.empty())
  239. {
  240. bool FN_rect = igl::list_to_matrix(vFN,FN);
  241. if(!FN_rect)
  242. {
  243. // igl::list_to_matrix(vV,V) already printed error message to std err
  244. return false;
  245. }
  246. }
  247. if(!vTC.empty())
  248. {
  249. bool T_rect = igl::list_to_matrix(vTC,TC);
  250. if(!T_rect)
  251. {
  252. // igl::list_to_matrix(vTC,T) already printed error message to std err
  253. return false;
  254. }
  255. }
  256. if(!vFTC.empty())
  257. {
  258. bool FTC_rect = igl::list_to_matrix(vFTC,FTC);
  259. if(!FTC_rect)
  260. {
  261. // igl::list_to_matrix(vF,F) already printed error message to std err
  262. return false;
  263. }
  264. }
  265. // Legacy
  266. if(F.cols() != 3)
  267. {
  268. fprintf(stderr,
  269. "Error: readOBJ(filename,V,F) is meant for reading triangle-only"
  270. " meshes. This mesh has faces all with size %d. See readOBJ.h for other"
  271. " options.\n",
  272. (int)F.cols());
  273. return false;
  274. }
  275. return true;
  276. }
  277. template <typename DerivedV, typename DerivedF, typename DerivedT, typename Index>
  278. IGL_INLINE bool igl::readOBJPoly(
  279. const std::string str,
  280. Eigen::PlainObjectBase<DerivedV>& V,
  281. std::vector<std::vector< Index > >& F,
  282. Eigen::PlainObjectBase<DerivedV>& CN,
  283. Eigen::PlainObjectBase<DerivedF>& FN,
  284. Eigen::PlainObjectBase<DerivedT>& TC,
  285. Eigen::PlainObjectBase<DerivedF>& FTC)
  286. {
  287. std::vector<std::vector<double> > vV,vTC,vN;
  288. std::vector<std::vector<Index> > vF,vFTC,vFN;
  289. bool success = igl::readOBJ(str,vV,vTC,vN,vF,vFTC,vFN);
  290. if(!success)
  291. return false;
  292. bool V_rect = igl::list_to_matrix(vV,V);
  293. if(!V_rect)
  294. return false;
  295. F = vF;
  296. if(!vN.empty())
  297. {
  298. bool VN_rect = igl::list_to_matrix(vN,CN);
  299. if(!VN_rect)
  300. return false;
  301. }
  302. if(!vFN.empty())
  303. {
  304. bool FN_rect = igl::list_to_matrix(vFN,FN);
  305. if(!FN_rect)
  306. return false;
  307. }
  308. if(!vTC.empty())
  309. {
  310. bool T_rect = igl::list_to_matrix(vTC,TC);
  311. if(!T_rect)
  312. return false;
  313. }
  314. if(!vFTC.empty())
  315. {
  316. bool FTC_rect = igl::list_to_matrix(vFTC,FTC);
  317. if(!FTC_rect)
  318. return false;
  319. }
  320. return true;
  321. }
  322. template <typename DerivedV, typename DerivedF>
  323. IGL_INLINE bool igl::readOBJ(
  324. const std::string str,
  325. Eigen::PlainObjectBase<DerivedV>& V,
  326. Eigen::PlainObjectBase<DerivedF>& F)
  327. {
  328. std::vector<std::vector<double> > vV,vTC,vN;
  329. std::vector<std::vector<int> > vF,vFTC,vFN;
  330. bool success = igl::readOBJ(str,vV,vTC,vN,vF,vFTC,vFN);
  331. if(!success)
  332. {
  333. // readOBJ(str,vV,vTC,vN,vF,vFTC,vFN) should have already printed an error
  334. // message to stderr
  335. return false;
  336. }
  337. bool V_rect = igl::list_to_matrix(vV,V);
  338. if(!V_rect)
  339. {
  340. // igl::list_to_matrix(vV,V) already printed error message to std err
  341. return false;
  342. }
  343. bool F_rect = igl::list_to_matrix(vF,F);
  344. if(!F_rect)
  345. {
  346. // igl::list_to_matrix(vF,F) already printed error message to std err
  347. return false;
  348. }
  349. // Legacy
  350. if(F.cols() != 3)
  351. {
  352. fprintf(stderr,
  353. "Error: readOBJ(filename,V,F) is meant for reading triangle-only"
  354. " meshes. This mesh has faces all with size %d. See readOBJ.h for other"
  355. " options.\n",
  356. (int)F.cols());
  357. return false;
  358. }
  359. return true;
  360. }
  361. #ifndef IGL_HEADER_ONLY
  362. // Explicit template specialization
  363. // generated by autoexplicit.sh
  364. 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> >&);
  365. 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> >&);
  366. 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> >&);
  367. 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> >&);
  368. #endif