readSTL.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. if(NULL==stl_file)
  116. {
  117. fprintf(stderr,"IOError: stl file could not be reopened as ascii ...\n");
  118. return false;
  119. }
  120. // Read 80 header
  121. // Eat file name
  122. #ifndef IGL_LINE_MAX
  123. # define IGL_LINE_MAX 2048
  124. #endif
  125. char name[IGL_LINE_MAX];
  126. if(NULL==fgets(name,IGL_LINE_MAX,stl_file))
  127. {
  128. cerr<<"IOError: ascii too short (2)."<<endl;
  129. goto close_false;
  130. }
  131. // ascii
  132. while(true)
  133. {
  134. int ret;
  135. char facet[IGL_LINE_MAX],normal[IGL_LINE_MAX];
  136. vector<TypeN > n(3);
  137. double nd[3];
  138. ret = fscanf(stl_file,"%s %s %lg %lg %lg",facet,normal,nd,nd+1,nd+2);
  139. if(string("endsolid") == facet)
  140. {
  141. break;
  142. }
  143. if(ret != 5 ||
  144. !(string("facet") == facet ||
  145. string("faced") == facet) ||
  146. string("normal") != normal)
  147. {
  148. cout<<"facet: "<<facet<<endl;
  149. cout<<"normal: "<<normal<<endl;
  150. cerr<<"IOError: bad format (1)."<<endl;
  151. goto close_false;
  152. }
  153. // copy casts to Type
  154. n[0] = nd[0]; n[1] = nd[1]; n[2] = nd[2];
  155. N.push_back(n);
  156. char outer[IGL_LINE_MAX], loop[IGL_LINE_MAX];
  157. ret = fscanf(stl_file,"%s %s",outer,loop);
  158. if(ret != 2 || string("outer") != outer || string("loop") != loop)
  159. {
  160. cerr<<"IOError: bad format (2)."<<endl;
  161. goto close_false;
  162. }
  163. vector<TypeF> f;
  164. while(true)
  165. {
  166. char word[IGL_LINE_MAX];
  167. int ret = fscanf(stl_file,"%s",word);
  168. if(ret == 1 && string("endloop") == word)
  169. {
  170. break;
  171. }else if(ret == 1 && string("vertex") == word)
  172. {
  173. vector<TypeV> v(3);
  174. double vd[3];
  175. int ret = fscanf(stl_file,"%lg %lg %lg",vd,vd+1,vd+2);
  176. if(ret != 3)
  177. {
  178. cerr<<"IOError: bad format (3)."<<endl;
  179. goto close_false;
  180. }
  181. f.push_back(V.size());
  182. // copy casts to Type
  183. v[0] = vd[0]; v[1] = vd[1]; v[2] = vd[2];
  184. V.push_back(v);
  185. }else
  186. {
  187. cerr<<"IOError: bad format (4)."<<endl;
  188. goto close_false;
  189. }
  190. }
  191. F.push_back(f);
  192. char endfacet[IGL_LINE_MAX];
  193. ret = fscanf(stl_file,"%s",endfacet);
  194. if(ret != 1 || string("endfacet") != endfacet)
  195. {
  196. cerr<<"IOError: bad format (5)."<<endl;
  197. goto close_false;
  198. }
  199. }
  200. // read endfacet
  201. goto close_true;
  202. }else
  203. {
  204. // Binary
  205. stl_file = freopen(NULL,"rb",stl_file);
  206. // Read 80 header
  207. char header[80];
  208. if(fread(header,sizeof(char),80,stl_file)!=80)
  209. {
  210. cerr<<"IOError: bad format (6)."<<endl;
  211. goto close_false;
  212. }
  213. // Read number of triangles
  214. unsigned int num_tri;
  215. if(fread(&num_tri,sizeof(unsigned int),1,stl_file)!=1)
  216. {
  217. cerr<<"IOError: bad format (7)."<<endl;
  218. goto close_false;
  219. }
  220. V.resize(num_tri*3,vector<TypeV >(3,0));
  221. N.resize(num_tri,vector<TypeN >(3,0));
  222. F.resize(num_tri,vector<TypeF >(3,0));
  223. for(int t = 0;t<(int)num_tri;t++)
  224. {
  225. // Read normal
  226. float n[3];
  227. if(fread(n,sizeof(float),3,stl_file)!=3)
  228. {
  229. cerr<<"IOError: bad format (8)."<<endl;
  230. goto close_false;
  231. }
  232. // Read each vertex
  233. for(int c = 0;c<3;c++)
  234. {
  235. F[t][c] = 3*t+c;
  236. N[t][c] = n[c];
  237. float v[3];
  238. if(fread(v,sizeof(float),3,stl_file)!=3)
  239. {
  240. cerr<<"IOError: bad format (9)."<<endl;
  241. goto close_false;
  242. }
  243. V[3*t+c][0] = v[0];
  244. V[3*t+c][1] = v[1];
  245. V[3*t+c][2] = v[2];
  246. }
  247. // Read attribute size
  248. unsigned short att_count;
  249. if(fread(&att_count,sizeof(unsigned short),1,stl_file)!=1)
  250. {
  251. cerr<<"IOError: bad format (10)."<<endl;
  252. goto close_false;
  253. }
  254. }
  255. goto close_true;
  256. }
  257. close_false:
  258. fclose(stl_file);
  259. return false;
  260. close_true:
  261. fclose(stl_file);
  262. return true;
  263. }
  264. #ifdef IGL_STATIC_LIBRARY
  265. // Explicit template specialization
  266. // generated by autoexplicit.sh
  267. 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> >&);
  268. // generated by autoexplicit.sh
  269. 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> >&);
  270. // generated by autoexplicit.sh
  271. 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> >&);
  272. // generated by autoexplicit.sh
  273. 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> >&);
  274. 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> >&);
  275. 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> >&);
  276. #endif