readOFF.cpp 7.0 KB

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