readOFF.cpp 7.8 KB

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