readOBJ.cpp 9.9 KB

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