readOBJ.cpp 10 KB

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