readOFF.cpp 5.7 KB

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