readSTL.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 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 "readSTL.h"
  9. #include "list_to_matrix.h"
  10. #include <iostream>
  11. template <typename DerivedV, typename DerivedF, typename DerivedN>
  12. IGL_INLINE bool igl::readSTL(
  13. const std::string & filename,
  14. Eigen::PlainObjectBase<DerivedV> & V,
  15. Eigen::PlainObjectBase<DerivedF> & F,
  16. Eigen::PlainObjectBase<DerivedN> & N)
  17. {
  18. using namespace std;
  19. vector<vector<typename DerivedV::Scalar> > vV;
  20. vector<vector<typename DerivedN::Scalar> > vN;
  21. vector<vector<typename DerivedF::Scalar> > vF;
  22. if(!readSTL(filename,vV,vF,vN))
  23. {
  24. return false;
  25. }
  26. if(!list_to_matrix(vV,V))
  27. {
  28. return false;
  29. }
  30. if(!list_to_matrix(vF,F))
  31. {
  32. return false;
  33. }
  34. if(!list_to_matrix(vN,N))
  35. {
  36. return false;
  37. }
  38. return true;
  39. }
  40. template <typename TypeV, typename TypeF, typename TypeN>
  41. IGL_INLINE bool igl::readSTL(
  42. const std::string & filename,
  43. std::vector<std::vector<TypeV> > & V,
  44. std::vector<std::vector<TypeF> > & F,
  45. std::vector<std::vector<TypeN> > & N)
  46. {
  47. using namespace std;
  48. // Should test for ascii
  49. // Open file, and check for error
  50. FILE * stl_file = fopen(filename.c_str(),"r");
  51. if(NULL==stl_file)
  52. {
  53. fprintf(stderr,"IOError: %s could not be opened...\n",
  54. filename.c_str());
  55. return false;
  56. }
  57. V.clear();
  58. F.clear();
  59. N.clear();
  60. #ifndef IGL_LINE_MAX
  61. # define IGL_LINE_MAX 2048
  62. #endif
  63. char solid[IGL_LINE_MAX];
  64. if(fscanf(stl_file,"%s",solid)!=1)
  65. {
  66. // file too short
  67. cerr<<"IOError: "<<filename<<" too short."<<endl;
  68. goto close_false;
  69. }
  70. if(string("solid") == solid)
  71. {
  72. // Eat file name
  73. char name[IGL_LINE_MAX];
  74. if(NULL==fgets(name,IGL_LINE_MAX,stl_file))
  75. {
  76. cerr<<"IOError: "<<filename<<" too short."<<endl;
  77. goto close_false;
  78. }
  79. // ascii
  80. while(true)
  81. {
  82. int ret;
  83. char facet[IGL_LINE_MAX],normal[IGL_LINE_MAX];
  84. vector<TypeN > n(3);
  85. double nd[3];
  86. ret = fscanf(stl_file,"%s %s %lg %lg %lg",facet,normal,nd,nd+1,nd+2);
  87. if(string("endsolid") == facet)
  88. {
  89. break;
  90. }
  91. if(ret != 5 || string("facet") != facet || string("normal") != normal)
  92. {
  93. cerr<<"IOError: "<<filename<<" bad format (1)."<<endl;
  94. goto close_false;
  95. }
  96. // copy casts to Type
  97. n[0] = nd[0]; n[1] = nd[1]; n[2] = nd[2];
  98. N.push_back(n);
  99. char outer[IGL_LINE_MAX], loop[IGL_LINE_MAX];
  100. ret = fscanf(stl_file,"%s %s",outer,loop);
  101. if(ret != 2 || string("outer") != outer || string("loop") != loop)
  102. {
  103. cerr<<"IOError: "<<filename<<" bad format (2)."<<endl;
  104. goto close_false;
  105. }
  106. vector<TypeF> f;
  107. while(true)
  108. {
  109. char word[IGL_LINE_MAX];
  110. int ret = fscanf(stl_file,"%s",word);
  111. if(ret == 1 && string("endloop") == word)
  112. {
  113. break;
  114. }else if(ret == 1 && string("vertex") == word)
  115. {
  116. vector<TypeV> v(3);
  117. double vd[3];
  118. int ret = fscanf(stl_file,"%lg %lg %lg",vd,vd+1,vd+2);
  119. if(ret != 3)
  120. {
  121. cerr<<"IOError: "<<filename<<" bad format (3)."<<endl;
  122. goto close_false;
  123. }
  124. f.push_back(V.size());
  125. // copy casts to Type
  126. v[0] = vd[0]; v[1] = vd[1]; v[2] = vd[2];
  127. V.push_back(v);
  128. }else
  129. {
  130. cerr<<"IOError: "<<filename<<" bad format (4)."<<endl;
  131. goto close_false;
  132. }
  133. }
  134. F.push_back(f);
  135. char endfacet[IGL_LINE_MAX];
  136. ret = fscanf(stl_file,"%s",endfacet);
  137. if(ret != 1 || string("endfacet") != endfacet)
  138. {
  139. cerr<<"IOError: "<<filename<<" bad format (5)."<<endl;
  140. goto close_false;
  141. }
  142. }
  143. // read endfacet
  144. fclose(stl_file);
  145. goto close_true;
  146. }else
  147. {
  148. // Binary
  149. fclose(stl_file);
  150. stl_file = fopen(filename.c_str(),"rb");
  151. if(NULL==stl_file)
  152. {
  153. fprintf(stderr,"IOError: %s could not be opened...\n",
  154. filename.c_str());
  155. return false;
  156. }
  157. // Read 80 header
  158. char header[80];
  159. if(fread(header,sizeof(char),80,stl_file)!=80)
  160. {
  161. cerr<<"IOError: "<<filename<<" bad format (1)."<<endl;
  162. goto close_false;
  163. }
  164. // Read number of triangles
  165. unsigned int num_tri;
  166. if(fread(&num_tri,sizeof(unsigned int),1,stl_file)!=1)
  167. {
  168. cerr<<"IOError: "<<filename<<" bad format (2)."<<endl;
  169. goto close_false;
  170. }
  171. V.resize(num_tri*3,vector<TypeV >(3,0));
  172. N.resize(num_tri,vector<TypeN >(3,0));
  173. F.resize(num_tri,vector<TypeF >(3,0));
  174. for(int t = 0;t<(int)num_tri;t++)
  175. {
  176. // Read normal
  177. float n[3];
  178. if(fread(n,sizeof(float),3,stl_file)!=3)
  179. {
  180. cerr<<"IOError: "<<filename<<" bad format (3)."<<endl;
  181. goto close_false;
  182. }
  183. // Read each vertex
  184. for(int c = 0;c<3;c++)
  185. {
  186. F[t][c] = 3*t+c;
  187. N[t][c] = n[c];
  188. float v[3];
  189. if(fread(v,sizeof(float),3,stl_file)!=3)
  190. {
  191. cerr<<"IOError: "<<filename<<" bad format (4)."<<endl;
  192. goto close_false;
  193. }
  194. V[3*t+c][0] = v[0];
  195. V[3*t+c][1] = v[1];
  196. V[3*t+c][2] = v[2];
  197. }
  198. // Read attribute size
  199. unsigned short att_count;
  200. if(fread(&att_count,sizeof(unsigned short),1,stl_file)!=1)
  201. {
  202. cerr<<"IOError: "<<filename<<" bad format (5)."<<endl;
  203. goto close_false;
  204. }
  205. }
  206. goto close_true;
  207. }
  208. close_false:
  209. fclose(stl_file);
  210. return false;
  211. close_true:
  212. fclose(stl_file);
  213. return true;
  214. }
  215. #ifdef IGL_STATIC_LIBRARY
  216. // Explicit template specialization
  217. // generated by autoexplicit.sh
  218. template bool igl::readSTL<Eigen::Matrix<float, -1, 3, 1, -1, 3>, Eigen::Matrix<unsigned int, -1, 3, 1, -1, 3>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 3, 1, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<unsigned int, -1, 3, 1, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  219. // generated by autoexplicit.sh
  220. template bool igl::readSTL<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  221. // generated by autoexplicit.sh
  222. template bool igl::readSTL<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, 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> >&);
  223. template bool igl::readSTL<Eigen::Matrix<float, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<float, -1, -1, 0, -1, -1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> >&);
  224. #endif