readOFF.cpp 8.7 KB

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