readOFF.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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...",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. float x,y,z,nx,ny,nz;
  54. if((has_normals && fscanf(off_file, "%g %g %g %g %g %g\n",&x,&y,&z,&nx,&ny,&nz)==6) ||
  55. (!has_normals && fscanf(off_file, "%g %g %g\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. template <typename DerivedV, typename DerivedF>
  122. IGL_INLINE bool igl::readOFF(
  123. const std::string str,
  124. Eigen::PlainObjectBase<DerivedV>& V,
  125. Eigen::PlainObjectBase<DerivedF>& F)
  126. {
  127. std::vector<std::vector<double> > vV;
  128. std::vector<std::vector<double> > vN;
  129. std::vector<std::vector<int> > vF;
  130. bool success = igl::readOFF(str,vV,vF,vN);
  131. if(!success)
  132. {
  133. // readOFF(str,vV,vF) should have already printed an error
  134. // message to stderr
  135. return false;
  136. }
  137. bool V_rect = igl::list_to_matrix(vV,V);
  138. if(!V_rect)
  139. {
  140. // igl::list_to_matrix(vV,V) already printed error message to std err
  141. return false;
  142. }
  143. bool F_rect = igl::list_to_matrix(vF,F);
  144. if(!F_rect)
  145. {
  146. // igl::list_to_matrix(vF,F) already printed error message to std err
  147. return false;
  148. }
  149. return true;
  150. }
  151. template <typename DerivedV, typename DerivedF>
  152. IGL_INLINE bool igl::readOFF(
  153. const std::string str,
  154. Eigen::PlainObjectBase<DerivedV>& V,
  155. Eigen::PlainObjectBase<DerivedF>& F,
  156. Eigen::PlainObjectBase<DerivedV>& N)
  157. {
  158. std::vector<std::vector<double> > vV;
  159. std::vector<std::vector<double> > vN;
  160. std::vector<std::vector<int> > vF;
  161. bool success = igl::readOFF(str,vV,vF,vN);
  162. if(!success)
  163. {
  164. // readOFF(str,vV,vF) should have already printed an error
  165. // message to stderr
  166. return false;
  167. }
  168. bool V_rect = igl::list_to_matrix(vV,V);
  169. if(!V_rect)
  170. {
  171. // igl::list_to_matrix(vV,V) already printed error message to std err
  172. return false;
  173. }
  174. bool F_rect = igl::list_to_matrix(vF,F);
  175. if(!F_rect)
  176. {
  177. // igl::list_to_matrix(vF,F) already printed error message to std err
  178. return false;
  179. }
  180. if (vN.size())
  181. {
  182. bool N_rect = igl::list_to_matrix(vN,N);
  183. if(!N_rect)
  184. {
  185. // igl::list_to_matrix(vN,N) already printed error message to std err
  186. return false;
  187. }
  188. }
  189. return true;
  190. }
  191. #ifndef IGL_HEADER_ONLY
  192. // Explicit template specialization
  193. // generated by autoexplicit.sh
  194. 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> >&);
  195. 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> >&);
  196. #endif