readOBJ.cpp 9.3 KB

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