readOFF.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 in %s\n",off_file_name.c_str());
  91. fclose(off_file);
  92. return false;
  93. }
  94. }
  95. // Read faces
  96. for(int i = 0;i<number_of_faces;)
  97. {
  98. std::vector<Index > face;
  99. int valence;
  100. if(fscanf(off_file,"%d",&valence)==1)
  101. {
  102. face.resize(valence);
  103. for(int j = 0;j<valence;j++)
  104. {
  105. int index;
  106. if(j<valence-1)
  107. {
  108. fscanf(off_file,"%d",&index);
  109. }else{
  110. fscanf(off_file,"%d%*[^\n]",&index);
  111. }
  112. face[j] = index;
  113. }
  114. F[i] = face;
  115. i++;
  116. }else if(
  117. fscanf(off_file,"%[#]",&tic_tac_toe)==1)
  118. {
  119. char comment[1000];
  120. fscanf(off_file,"%[^\n]",comment);
  121. }else
  122. {
  123. printf("Error: bad line in %s\n",off_file_name.c_str());
  124. fclose(off_file);
  125. return false;
  126. }
  127. }
  128. fclose(off_file);
  129. return true;
  130. }
  131. #ifndef IGL_NO_EIGEN
  132. template <typename DerivedV, typename DerivedF>
  133. IGL_INLINE bool igl::readOFF(
  134. const std::string str,
  135. Eigen::PlainObjectBase<DerivedV>& V,
  136. Eigen::PlainObjectBase<DerivedF>& F)
  137. {
  138. std::vector<std::vector<double> > vV;
  139. std::vector<std::vector<double> > vN;
  140. std::vector<std::vector<int> > vF;
  141. bool success = igl::readOFF(str,vV,vF,vN);
  142. if(!success)
  143. {
  144. // readOFF(str,vV,vF) should have already printed an error
  145. // message to stderr
  146. return false;
  147. }
  148. bool V_rect = igl::list_to_matrix(vV,V);
  149. if(!V_rect)
  150. {
  151. // igl::list_to_matrix(vV,V) already printed error message to std err
  152. return false;
  153. }
  154. bool F_rect = igl::list_to_matrix(vF,F);
  155. if(!F_rect)
  156. {
  157. // igl::list_to_matrix(vF,F) already printed error message to std err
  158. return false;
  159. }
  160. return true;
  161. }
  162. template <typename DerivedV, typename DerivedF>
  163. IGL_INLINE bool igl::readOFF(
  164. const std::string str,
  165. Eigen::PlainObjectBase<DerivedV>& V,
  166. Eigen::PlainObjectBase<DerivedF>& F,
  167. Eigen::PlainObjectBase<DerivedV>& N)
  168. {
  169. std::vector<std::vector<double> > vV;
  170. std::vector<std::vector<double> > vN;
  171. std::vector<std::vector<int> > vF;
  172. bool success = igl::readOFF(str,vV,vF,vN);
  173. if(!success)
  174. {
  175. // readOFF(str,vV,vF) should have already printed an error
  176. // message to stderr
  177. return false;
  178. }
  179. bool V_rect = igl::list_to_matrix(vV,V);
  180. if(!V_rect)
  181. {
  182. // igl::list_to_matrix(vV,V) already printed error message to std err
  183. return false;
  184. }
  185. bool F_rect = igl::list_to_matrix(vF,F);
  186. if(!F_rect)
  187. {
  188. // igl::list_to_matrix(vF,F) already printed error message to std err
  189. return false;
  190. }
  191. if (vN.size())
  192. {
  193. bool N_rect = igl::list_to_matrix(vN,N);
  194. if(!N_rect)
  195. {
  196. // igl::list_to_matrix(vN,N) already printed error message to std err
  197. return false;
  198. }
  199. }
  200. return true;
  201. }
  202. #endif
  203. #ifdef IGL_STATIC_LIBRARY
  204. // Explicit template specialization
  205. // generated by autoexplicit.sh
  206. 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> >&);
  207. // generated by autoexplicit.sh
  208. 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> >&);
  209. 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> >&);
  210. 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> >&);
  211. #endif