readSTL.cpp 9.6 KB

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