readOFF.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <[email protected]>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "readOFF.h"
  9. #include "list_to_matrix.h"
  10. template <typename Scalar, typename Index>
  11. IGL_INLINE bool igl::readOFF(
  12. const std::string off_file_name,
  13. std::vector<std::vector<Scalar > > & V,
  14. std::vector<std::vector<Index > > & F,
  15. std::vector<std::vector<Scalar > > & N,
  16. std::vector<std::vector<Scalar > > & C)
  17. {
  18. FILE * off_file = fopen(off_file_name.c_str(),"r");
  19. if(NULL==off_file)
  20. {
  21. printf("IOError: %s could not be opened...\n",off_file_name.c_str());
  22. return false;
  23. }
  24. return readOFF(off_file,V,F,N,C);
  25. }
  26. template <typename Scalar, typename Index>
  27. IGL_INLINE bool igl::readOFF(
  28. FILE * off_file,
  29. std::vector<std::vector<Scalar > > & V,
  30. std::vector<std::vector<Index > > & F,
  31. std::vector<std::vector<Scalar > > & N,
  32. std::vector<std::vector<Scalar > > & C)
  33. {
  34. V.clear();
  35. F.clear();
  36. N.clear();
  37. C.clear();
  38. // First line is always OFF
  39. char header[1000];
  40. const std::string OFF("OFF");
  41. const std::string NOFF("NOFF");
  42. const std::string COFF("COFF");
  43. if(fscanf(off_file,"%s\n",header)!=1
  44. || !(
  45. std::string(header).compare(0, OFF.length(), OFF)==0 ||
  46. std::string(header).compare(0, COFF.length(), COFF)==0 ||
  47. std::string(header).compare(0,NOFF.length(),NOFF)==0))
  48. {
  49. printf("Error: readOFF() first line should be OFF or NOFF or COFF, not %s...",header);
  50. fclose(off_file);
  51. return false;
  52. }
  53. bool has_normals = std::string(header).compare(0,NOFF.length(),NOFF)==0;
  54. bool has_vertexColors = std::string(header).compare(0,COFF.length(),COFF)==0;
  55. // Second line is #vertices #faces #edges
  56. int number_of_vertices;
  57. int number_of_faces;
  58. int number_of_edges;
  59. char tic_tac_toe;
  60. char line[1000];
  61. bool still_comments = true;
  62. while(still_comments)
  63. {
  64. fgets(line,1000,off_file);
  65. still_comments = (line[0] == '#' || line[0] == '\n');
  66. }
  67. sscanf(line,"%d %d %d",&number_of_vertices,&number_of_faces,&number_of_edges);
  68. V.resize(number_of_vertices);
  69. if (has_normals)
  70. N.resize(number_of_vertices);
  71. if (has_vertexColors)
  72. C.resize(number_of_vertices);
  73. F.resize(number_of_faces);
  74. //printf("%s %d %d %d\n",(has_normals ? "NOFF" : "OFF"),number_of_vertices,number_of_faces,number_of_edges);
  75. // Read vertices
  76. for(int i = 0;i<number_of_vertices;)
  77. {
  78. fgets(line, 1000, off_file);
  79. double x,y,z,nx,ny,nz;
  80. if(sscanf(line, "%lg %lg %lg %lg %lg %lg",&x,&y,&z,&nx,&ny,&nz)>= 3)
  81. {
  82. std::vector<Scalar > vertex;
  83. vertex.resize(3);
  84. vertex[0] = x;
  85. vertex[1] = y;
  86. vertex[2] = z;
  87. V[i] = vertex;
  88. if (has_normals)
  89. {
  90. std::vector<Scalar > normal;
  91. normal.resize(3);
  92. normal[0] = nx;
  93. normal[1] = ny;
  94. normal[2] = nz;
  95. N[i] = normal;
  96. }
  97. if (has_vertexColors)
  98. {
  99. C[i].resize(3);
  100. C[i][0] = nx / 255.0;
  101. C[i][1] = ny / 255.0;
  102. C[i][2] = nz / 255.0;
  103. }
  104. i++;
  105. }else if(
  106. fscanf(off_file,"%[#]",&tic_tac_toe)==1)
  107. {
  108. char comment[1000];
  109. fscanf(off_file,"%[^\n]",comment);
  110. }else
  111. {
  112. printf("Error: bad line (%d)\n",i);
  113. if(feof(off_file))
  114. {
  115. fclose(off_file);
  116. return false;
  117. }
  118. }
  119. }
  120. // Read faces
  121. for(int i = 0;i<number_of_faces;)
  122. {
  123. std::vector<Index > face;
  124. int valence;
  125. if(fscanf(off_file,"%d",&valence)==1)
  126. {
  127. face.resize(valence);
  128. for(int j = 0;j<valence;j++)
  129. {
  130. int index;
  131. if(j<valence-1)
  132. {
  133. fscanf(off_file,"%d",&index);
  134. }else{
  135. fscanf(off_file,"%d%*[^\n]",&index);
  136. }
  137. face[j] = index;
  138. }
  139. F[i] = face;
  140. i++;
  141. }else if(
  142. fscanf(off_file,"%[#]",&tic_tac_toe)==1)
  143. {
  144. char comment[1000];
  145. fscanf(off_file,"%[^\n]",comment);
  146. }else
  147. {
  148. printf("Error: bad line\n");
  149. fclose(off_file);
  150. return false;
  151. }
  152. }
  153. fclose(off_file);
  154. return true;
  155. }
  156. #ifndef IGL_NO_EIGEN
  157. template <typename DerivedV, typename DerivedF>
  158. IGL_INLINE bool igl::readOFF(
  159. const std::string str,
  160. Eigen::PlainObjectBase<DerivedV>& V,
  161. Eigen::PlainObjectBase<DerivedF>& F)
  162. {
  163. std::vector<std::vector<double> > vV;
  164. std::vector<std::vector<double> > vN;
  165. std::vector<std::vector<int> > vF;
  166. std::vector<std::vector<double> > vC;
  167. bool success = igl::readOFF(str,vV,vF,vN,vC);
  168. if(!success)
  169. {
  170. // readOFF(str,vV,vF,vN,vC) should have already printed an error
  171. // message to stderr
  172. return false;
  173. }
  174. bool V_rect = igl::list_to_matrix(vV,V);
  175. if(!V_rect)
  176. {
  177. // igl::list_to_matrix(vV,V) already printed error message to std err
  178. return false;
  179. }
  180. bool F_rect = igl::list_to_matrix(vF,F);
  181. if(!F_rect)
  182. {
  183. // igl::list_to_matrix(vF,F) already printed error message to std err
  184. return false;
  185. }
  186. return true;
  187. }
  188. template <typename DerivedV, typename DerivedF>
  189. IGL_INLINE bool igl::readOFF(
  190. const std::string str,
  191. Eigen::PlainObjectBase<DerivedV>& V,
  192. Eigen::PlainObjectBase<DerivedF>& F,
  193. Eigen::PlainObjectBase<DerivedV>& N)
  194. {
  195. std::vector<std::vector<double> > vV;
  196. std::vector<std::vector<double> > vN;
  197. std::vector<std::vector<int> > vF;
  198. std::vector<std::vector<double> > vC;
  199. bool success = igl::readOFF(str,vV,vF,vN,vC);
  200. if(!success)
  201. {
  202. // readOFF(str,vV,vF,vC) 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.size())
  219. {
  220. bool N_rect = igl::list_to_matrix(vN,N);
  221. if(!N_rect)
  222. {
  223. // igl::list_to_matrix(vN,N) already printed error message to std err
  224. return false;
  225. }
  226. }
  227. //Warning: RGB colors will be returned in the N matrix
  228. if (vC.size())
  229. {
  230. bool C_rect = igl::list_to_matrix(vC,N);
  231. if(!C_rect)
  232. {
  233. // igl::list_to_matrix(vC,N) already printed error message to std err
  234. return false;
  235. }
  236. }
  237. return true;
  238. }
  239. #endif
  240. #ifdef IGL_STATIC_LIBRARY
  241. // Explicit template instantiation
  242. // generated by autoexplicit.sh
  243. template bool igl::readOFF<double, int>(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >&, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >&, std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >&, std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >&);
  244. // generated by autoexplicit.sh
  245. template bool igl::readOFF<Eigen::Matrix<double, -1, 3, 0, -1, 3>, 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, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  246. // generated by autoexplicit.sh
  247. template bool igl::readOFF<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> >&);
  248. template bool igl::readOFF<Eigen::Matrix<double, -1, 3, 1, -1, 3>, Eigen::Matrix<unsigned int, -1, -1, 1, -1, -1> >(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> >&);
  249. template bool igl::readOFF<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3> >(std::string, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> >&);
  250. template bool igl::readOFF<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> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  251. #endif