readOBJ.cpp 13 KB

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