readOFF.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. {
  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. V.clear();
  25. F.clear();
  26. N.clear();
  27. // First line is always OFF
  28. char header[1000];
  29. const std::string OFF("OFF");
  30. const std::string NOFF("NOFF");
  31. if(fscanf(off_file,"%s\n",header)!=1
  32. || !(OFF == header || NOFF == header))
  33. {
  34. printf("Error: %s's first line should be OFF or NOFF not %s...",off_file_name.c_str(),header);
  35. fclose(off_file);
  36. return false;
  37. }
  38. bool has_normals = NOFF==header;
  39. // Second line is #vertices #faces #edges
  40. int number_of_vertices;
  41. int number_of_faces;
  42. int number_of_edges;
  43. char tic_tac_toe;
  44. char line[1000];
  45. bool still_comments = true;
  46. while(still_comments)
  47. {
  48. fgets(line,1000,off_file);
  49. still_comments = line[0] == '#';
  50. }
  51. sscanf(line,"%d %d %d",&number_of_vertices,&number_of_faces,&number_of_edges);
  52. V.resize(number_of_vertices);
  53. if (has_normals)
  54. N.resize(number_of_vertices);
  55. F.resize(number_of_faces);
  56. //printf("%s %d %d %d\n",(has_normals ? "NOFF" : "OFF"),number_of_vertices,number_of_faces,number_of_edges);
  57. // Read vertices
  58. for(int i = 0;i<number_of_vertices;)
  59. {
  60. fgets(line, 1000, off_file);
  61. double x,y,z,nx,ny,nz;
  62. if(sscanf(line, "%lg %lg %lg %lg %lg %lg",&x,&y,&z,&nx,&ny,&nz)>= 3)
  63. {
  64. std::vector<Scalar > vertex;
  65. vertex.resize(3);
  66. vertex[0] = x;
  67. vertex[1] = y;
  68. vertex[2] = z;
  69. V[i] = vertex;
  70. if (has_normals)
  71. {
  72. std::vector<Scalar > normal;
  73. normal.resize(3);
  74. normal[0] = nx;
  75. normal[1] = ny;
  76. normal[2] = nz;
  77. N[i] = normal;
  78. }
  79. i++;
  80. }else if(
  81. fscanf(off_file,"%[#]",&tic_tac_toe)==1)
  82. {
  83. char comment[1000];
  84. fscanf(off_file,"%[^\n]",comment);
  85. }else
  86. {
  87. printf("Error: bad line in %s\n",off_file_name.c_str());
  88. fclose(off_file);
  89. return false;
  90. }
  91. }
  92. // Read faces
  93. for(int i = 0;i<number_of_faces;)
  94. {
  95. std::vector<Index > face;
  96. int valence;
  97. if(fscanf(off_file,"%d",&valence)==1)
  98. {
  99. face.resize(valence);
  100. for(int j = 0;j<valence;j++)
  101. {
  102. int index;
  103. if(j<valence-1)
  104. {
  105. fscanf(off_file,"%d",&index);
  106. }else{
  107. fscanf(off_file,"%d%*[^\n]",&index);
  108. }
  109. face[j] = index;
  110. }
  111. F[i] = face;
  112. i++;
  113. }else if(
  114. fscanf(off_file,"%[#]",&tic_tac_toe)==1)
  115. {
  116. char comment[1000];
  117. fscanf(off_file,"%[^\n]",comment);
  118. }else
  119. {
  120. printf("Error: bad line in %s\n",off_file_name.c_str());
  121. fclose(off_file);
  122. return false;
  123. }
  124. }
  125. fclose(off_file);
  126. return true;
  127. }
  128. #ifndef IGL_NO_EIGEN
  129. template <typename DerivedV, typename DerivedF>
  130. IGL_INLINE bool igl::readOFF(
  131. const std::string str,
  132. Eigen::PlainObjectBase<DerivedV>& V,
  133. Eigen::PlainObjectBase<DerivedF>& F)
  134. {
  135. std::vector<std::vector<double> > vV;
  136. std::vector<std::vector<double> > vN;
  137. std::vector<std::vector<int> > vF;
  138. bool success = igl::readOFF(str,vV,vF,vN);
  139. if(!success)
  140. {
  141. // readOFF(str,vV,vF) should have already printed an error
  142. // message to stderr
  143. return false;
  144. }
  145. bool V_rect = igl::list_to_matrix(vV,V);
  146. if(!V_rect)
  147. {
  148. // igl::list_to_matrix(vV,V) already printed error message to std err
  149. return false;
  150. }
  151. bool F_rect = igl::list_to_matrix(vF,F);
  152. if(!F_rect)
  153. {
  154. // igl::list_to_matrix(vF,F) already printed error message to std err
  155. return false;
  156. }
  157. return true;
  158. }
  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. Eigen::PlainObjectBase<DerivedV>& N)
  165. {
  166. std::vector<std::vector<double> > vV;
  167. std::vector<std::vector<double> > vN;
  168. std::vector<std::vector<int> > vF;
  169. bool success = igl::readOFF(str,vV,vF,vN);
  170. if(!success)
  171. {
  172. // readOFF(str,vV,vF) 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. if (vN.size())
  189. {
  190. bool N_rect = igl::list_to_matrix(vN,N);
  191. if(!N_rect)
  192. {
  193. // igl::list_to_matrix(vN,N) already printed error message to std err
  194. return false;
  195. }
  196. }
  197. return true;
  198. }
  199. #endif
  200. #ifdef IGL_STATIC_LIBRARY
  201. // Explicit template specialization
  202. // generated by autoexplicit.sh
  203. 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> >&);
  204. // generated by autoexplicit.sh
  205. 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> >&);
  206. 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> >&);
  207. 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> >&);
  208. #endif