readOBJ.cpp 9.2 KB

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