readOBJ.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. IGL_INLINE bool igl::readOBJ(const std::string str, Eigen::MatrixXd& V, Eigen::MatrixXi& F)
  193. {
  194. std::vector<std::vector<double> > vV,vTC,vN;
  195. std::vector<std::vector<int> > vF,vFTC,vFN;
  196. bool success = igl::readOBJ(str,vV,vTC,vN,vF,vFTC,vFN);
  197. if(!success)
  198. {
  199. // readOBJ(str,vV,vTC,vN,vF,vFTC,vFN) should have already printed an error
  200. // message to stderr
  201. return false;
  202. }
  203. bool V_rect = igl::list_to_matrix(vV,V);
  204. if(!V_rect)
  205. {
  206. // igl::list_to_matrix(vV,V) already printed error message to std err
  207. return false;
  208. }
  209. bool F_rect = igl::list_to_matrix(vF,F);
  210. if(!F_rect)
  211. {
  212. // igl::list_to_matrix(vF,F) already printed error message to std err
  213. return false;
  214. }
  215. // Legacy
  216. if(F.cols() != 3)
  217. {
  218. fprintf(stderr,
  219. "Error: readOBJ(filename,V,F) is meant for reading triangle-only"
  220. " meshes. This mesh has faces all with size %d. See readOBJ.h for other"
  221. " options.\n",
  222. (int)F.cols());
  223. return false;
  224. }
  225. return true;
  226. }
  227. #ifndef IGL_HEADER_ONLY
  228. // Explicit template specialization
  229. #endif