readSTL.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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(),"rb");
  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. // Specifically 80 character header
  61. char header[80];
  62. char solid[80];
  63. bool is_ascii = true;
  64. if(fread(header,1,80,stl_file) != 80)
  65. {
  66. cerr<<"IOError: "<<filename<<" too short (1)."<<endl;
  67. goto close_false;
  68. }
  69. sscanf(header,"%s",solid);
  70. if(string("solid") != solid)
  71. {
  72. // definitely **not** ascii
  73. is_ascii = false;
  74. }else
  75. {
  76. // might still be binary
  77. char buf[4];
  78. if(fread(buf,1,4,stl_file) != 4)
  79. {
  80. cerr<<"IOError: "<<filename<<" too short (3)."<<endl;
  81. goto close_false;
  82. }
  83. size_t num_faces = *reinterpret_cast<unsigned int*>(buf);
  84. fseek(stl_file,0,SEEK_END);
  85. int file_size = ftell(stl_file);
  86. if(file_size == 80 + 4 + (4*12 + 2) * num_faces)
  87. {
  88. is_ascii = false;
  89. }else
  90. {
  91. is_ascii = true;
  92. }
  93. }
  94. fclose(stl_file);
  95. if(is_ascii)
  96. {
  97. // Rewind to end of header
  98. stl_file = fopen(filename.c_str(),"r");
  99. // Eat file name
  100. #ifndef IGL_LINE_MAX
  101. # define IGL_LINE_MAX 2048
  102. #endif
  103. char name[IGL_LINE_MAX];
  104. if(NULL==fgets(name,IGL_LINE_MAX,stl_file))
  105. {
  106. cerr<<"IOError: "<<filename<<" ascii too short (2)."<<endl;
  107. goto close_false;
  108. }
  109. // ascii
  110. while(true)
  111. {
  112. int ret;
  113. char facet[IGL_LINE_MAX],normal[IGL_LINE_MAX];
  114. vector<TypeN > n(3);
  115. double nd[3];
  116. ret = fscanf(stl_file,"%s %s %lg %lg %lg",facet,normal,nd,nd+1,nd+2);
  117. if(string("endsolid") == facet)
  118. {
  119. break;
  120. }
  121. if(ret != 5 ||
  122. !(string("facet") == facet ||
  123. string("faced") == facet) ||
  124. string("normal") != normal)
  125. {
  126. cout<<"facet: "<<facet<<endl;
  127. cout<<"normal: "<<normal<<endl;
  128. cerr<<"IOError: "<<filename<<" bad format (1)."<<endl;
  129. goto close_false;
  130. }
  131. // copy casts to Type
  132. n[0] = nd[0]; n[1] = nd[1]; n[2] = nd[2];
  133. N.push_back(n);
  134. char outer[IGL_LINE_MAX], loop[IGL_LINE_MAX];
  135. ret = fscanf(stl_file,"%s %s",outer,loop);
  136. if(ret != 2 || string("outer") != outer || string("loop") != loop)
  137. {
  138. cerr<<"IOError: "<<filename<<" bad format (2)."<<endl;
  139. goto close_false;
  140. }
  141. vector<TypeF> f;
  142. while(true)
  143. {
  144. char word[IGL_LINE_MAX];
  145. int ret = fscanf(stl_file,"%s",word);
  146. if(ret == 1 && string("endloop") == word)
  147. {
  148. break;
  149. }else if(ret == 1 && string("vertex") == word)
  150. {
  151. vector<TypeV> v(3);
  152. double vd[3];
  153. int ret = fscanf(stl_file,"%lg %lg %lg",vd,vd+1,vd+2);
  154. if(ret != 3)
  155. {
  156. cerr<<"IOError: "<<filename<<" bad format (3)."<<endl;
  157. goto close_false;
  158. }
  159. f.push_back(V.size());
  160. // copy casts to Type
  161. v[0] = vd[0]; v[1] = vd[1]; v[2] = vd[2];
  162. V.push_back(v);
  163. }else
  164. {
  165. cerr<<"IOError: "<<filename<<" bad format (4)."<<endl;
  166. goto close_false;
  167. }
  168. }
  169. F.push_back(f);
  170. char endfacet[IGL_LINE_MAX];
  171. ret = fscanf(stl_file,"%s",endfacet);
  172. if(ret != 1 || string("endfacet") != endfacet)
  173. {
  174. cerr<<"IOError: "<<filename<<" bad format (5)."<<endl;
  175. goto close_false;
  176. }
  177. }
  178. // read endfacet
  179. fclose(stl_file);
  180. goto close_true;
  181. }else
  182. {
  183. // Binary
  184. stl_file = fopen(filename.c_str(),"rb");
  185. if(NULL==stl_file)
  186. {
  187. fprintf(stderr,"IOError: %s could not be opened...\n",
  188. filename.c_str());
  189. return false;
  190. }
  191. // Read 80 header
  192. char header[80];
  193. if(fread(header,sizeof(char),80,stl_file)!=80)
  194. {
  195. cerr<<"IOError: "<<filename<<" bad format (6)."<<endl;
  196. goto close_false;
  197. }
  198. // Read number of triangles
  199. unsigned int num_tri;
  200. if(fread(&num_tri,sizeof(unsigned int),1,stl_file)!=1)
  201. {
  202. cerr<<"IOError: "<<filename<<" bad format (7)."<<endl;
  203. goto close_false;
  204. }
  205. V.resize(num_tri*3,vector<TypeV >(3,0));
  206. N.resize(num_tri,vector<TypeN >(3,0));
  207. F.resize(num_tri,vector<TypeF >(3,0));
  208. for(int t = 0;t<(int)num_tri;t++)
  209. {
  210. // Read normal
  211. float n[3];
  212. if(fread(n,sizeof(float),3,stl_file)!=3)
  213. {
  214. cerr<<"IOError: "<<filename<<" bad format (8)."<<endl;
  215. goto close_false;
  216. }
  217. // Read each vertex
  218. for(int c = 0;c<3;c++)
  219. {
  220. F[t][c] = 3*t+c;
  221. N[t][c] = n[c];
  222. float v[3];
  223. if(fread(v,sizeof(float),3,stl_file)!=3)
  224. {
  225. cerr<<"IOError: "<<filename<<" bad format (9)."<<endl;
  226. goto close_false;
  227. }
  228. V[3*t+c][0] = v[0];
  229. V[3*t+c][1] = v[1];
  230. V[3*t+c][2] = v[2];
  231. }
  232. // Read attribute size
  233. unsigned short att_count;
  234. if(fread(&att_count,sizeof(unsigned short),1,stl_file)!=1)
  235. {
  236. cerr<<"IOError: "<<filename<<" bad format (10)."<<endl;
  237. goto close_false;
  238. }
  239. }
  240. goto close_true;
  241. }
  242. close_false:
  243. fclose(stl_file);
  244. return false;
  245. close_true:
  246. fclose(stl_file);
  247. return true;
  248. }
  249. #ifdef IGL_STATIC_LIBRARY
  250. // Explicit template specialization
  251. // generated by autoexplicit.sh
  252. 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> >&);
  253. // generated by autoexplicit.sh
  254. 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> >&);
  255. // generated by autoexplicit.sh
  256. 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> >&);
  257. 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> >&);
  258. #endif