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