readOBJ.h 8.3 KB

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