readOBJ.cpp 12 KB

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