readOBJ.cpp 18 KB

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