readOBJ.cpp 11 KB

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