readOBJ.cpp 9.8 KB

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