readOBJ.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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() texture coords on line %d should have 2 "
  105. "or 3 coordinates (%d)",
  106. line_no,count);
  107. fclose(obj_file);
  108. return false;
  109. }
  110. std::vector<Scalar > tex(count);
  111. for(int i = 0;i<count;i++)
  112. {
  113. tex[i] = x[i];
  114. }
  115. TC.push_back(tex);
  116. }else if(type == f)
  117. {
  118. std::vector<Index > f;
  119. std::vector<Index > ftc;
  120. std::vector<Index > fn;
  121. // Read each "word" after type
  122. char word[IGL_LINE_MAX];
  123. int offset;
  124. while(sscanf(l,"%s%n",word,&offset) == 1)
  125. {
  126. // adjust offset
  127. l += offset;
  128. // Process word
  129. unsigned int i,it,in;
  130. if(sscanf(word,"%u/%u/%u",&i,&it,&in) == 3)
  131. {
  132. f.push_back(i-1);
  133. ftc.push_back(it-1);
  134. fn.push_back(in-1);
  135. }else if(sscanf(word,"%u/%u",&i,&it) == 2)
  136. {
  137. f.push_back(i-1);
  138. ftc.push_back(it-1);
  139. }else if(sscanf(word,"%u//%u",&i,&in) == 2)
  140. {
  141. f.push_back(i-1);
  142. fn.push_back(in-1);
  143. }else if(sscanf(word,"%u",&i) == 1)
  144. {
  145. f.push_back(i-1);
  146. }else
  147. {
  148. fprintf(stderr,
  149. "Error: readOBJ() face on line %d has invalid element format\n",
  150. line_no);
  151. fclose(obj_file);
  152. return false;
  153. }
  154. }
  155. if(
  156. (f.size()>0 && fn.size() == 0 && ftc.size() == 0) ||
  157. (f.size()>0 && fn.size() == f.size() && ftc.size() == 0) ||
  158. (f.size()>0 && fn.size() == 0 && ftc.size() == f.size()) ||
  159. (f.size()>0 && fn.size() == f.size() && ftc.size() == f.size()))
  160. {
  161. // No matter what add each type to lists so that lists are the
  162. // correct lengths
  163. F.push_back(f);
  164. FTC.push_back(ftc);
  165. FN.push_back(fn);
  166. }else
  167. {
  168. fprintf(stderr,
  169. "Error: readOBJ() face on line %d has invalid format\n", line_no);
  170. fclose(obj_file);
  171. return false;
  172. }
  173. }else if(strlen(type) >= 1 && (type[0] == '#' ||
  174. type[0] == 'g' ||
  175. type[0] == 's' ||
  176. strcmp("usemtl",type)==0 ||
  177. strcmp("mtllib",type)==0))
  178. {
  179. //ignore comments or other shit
  180. }else
  181. {
  182. //ignore any other lines
  183. fprintf(stderr,
  184. "Warning: readOBJ() ignored non-comment line %d:\n %s",
  185. line_no,
  186. line);
  187. }
  188. }else
  189. {
  190. // ignore empty line
  191. }
  192. line_no++;
  193. }
  194. fclose(obj_file);
  195. assert(F.size() == FN.size());
  196. assert(F.size() == FTC.size());
  197. return true;
  198. }
  199. template <typename Scalar, typename Index>
  200. IGL_INLINE bool igl::readOBJ(
  201. const std::string obj_file_name,
  202. std::vector<std::vector<Scalar > > & V,
  203. std::vector<std::vector<Index > > & F)
  204. {
  205. std::vector<std::vector<Scalar > > TC,N;
  206. std::vector<std::vector<Index > > FTC,FN;
  207. return readOBJ(obj_file_name,V,TC,N,F,FTC,FN);
  208. }
  209. template <typename DerivedV, typename DerivedF, typename DerivedT>
  210. IGL_INLINE bool igl::readOBJ(
  211. const std::string str,
  212. Eigen::PlainObjectBase<DerivedV>& V,
  213. Eigen::PlainObjectBase<DerivedT>& TC,
  214. Eigen::PlainObjectBase<DerivedV>& CN,
  215. Eigen::PlainObjectBase<DerivedF>& F,
  216. Eigen::PlainObjectBase<DerivedF>& FTC,
  217. Eigen::PlainObjectBase<DerivedF>& FN)
  218. {
  219. std::vector<std::vector<double> > vV,vTC,vN;
  220. std::vector<std::vector<int> > vF,vFTC,vFN;
  221. bool success = igl::readOBJ(str,vV,vTC,vN,vF,vFTC,vFN);
  222. if(!success)
  223. {
  224. // readOBJ(str,vV,vTC,vN,vF,vFTC,vFN) should have already printed an error
  225. // message to stderr
  226. return false;
  227. }
  228. bool V_rect = igl::list_to_matrix(vV,V);
  229. if(!V_rect)
  230. {
  231. // igl::list_to_matrix(vV,V) already printed error message to std err
  232. return false;
  233. }
  234. bool F_rect = igl::list_to_matrix(vF,F);
  235. if(!F_rect)
  236. {
  237. // igl::list_to_matrix(vF,F) already printed error message to std err
  238. return false;
  239. }
  240. if(!vN.empty())
  241. {
  242. bool VN_rect = igl::list_to_matrix(vN,CN);
  243. if(!VN_rect)
  244. {
  245. // igl::list_to_matrix(vV,V) already printed error message to std err
  246. return false;
  247. }
  248. }
  249. if(!vFN.empty() && !vFN[0].empty())
  250. {
  251. bool FN_rect = igl::list_to_matrix(vFN,FN);
  252. if(!FN_rect)
  253. {
  254. // igl::list_to_matrix(vV,V) already printed error message to std err
  255. return false;
  256. }
  257. }
  258. if(!vTC.empty())
  259. {
  260. bool T_rect = igl::list_to_matrix(vTC,TC);
  261. if(!T_rect)
  262. {
  263. // igl::list_to_matrix(vTC,T) already printed error message to std err
  264. return false;
  265. }
  266. }
  267. if(!vFTC.empty()&& !vFTC[0].empty())
  268. {
  269. bool FTC_rect = igl::list_to_matrix(vFTC,FTC);
  270. if(!FTC_rect)
  271. {
  272. // igl::list_to_matrix(vF,F) already printed error message to std err
  273. return false;
  274. }
  275. }
  276. return true;
  277. }
  278. template <typename DerivedV, typename DerivedF>
  279. IGL_INLINE bool igl::readOBJ(
  280. const std::string str,
  281. Eigen::PlainObjectBase<DerivedV>& V,
  282. Eigen::PlainObjectBase<DerivedF>& F)
  283. {
  284. std::vector<std::vector<double> > vV,vTC,vN;
  285. std::vector<std::vector<int> > vF,vFTC,vFN;
  286. bool success = igl::readOBJ(str,vV,vTC,vN,vF,vFTC,vFN);
  287. if(!success)
  288. {
  289. // readOBJ(str,vV,vTC,vN,vF,vFTC,vFN) should have already printed an error
  290. // message to stderr
  291. return false;
  292. }
  293. bool V_rect = igl::list_to_matrix(vV,V);
  294. if(!V_rect)
  295. {
  296. // igl::list_to_matrix(vV,V) already printed error message to std err
  297. return false;
  298. }
  299. bool F_rect = igl::list_to_matrix(vF,F);
  300. if(!F_rect)
  301. {
  302. // igl::list_to_matrix(vF,F) already printed error message to std err
  303. return false;
  304. }
  305. return true;
  306. }
  307. #ifdef IGL_STATIC_LIBRARY
  308. // Explicit template specialization
  309. // generated by autoexplicit.sh
  310. 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> >&);
  311. 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> >&);
  312. 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<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> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  313. #endif