writeSTL.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "writeSTL.h"
  2. template <typename DerivedV, typename DerivedF, typename DerivedN>
  3. IGL_INLINE bool igl::writeSTL(
  4. const std::string & filename,
  5. const Eigen::PlainObjectBase<DerivedV> & V,
  6. const Eigen::PlainObjectBase<DerivedF> & F,
  7. const Eigen::PlainObjectBase<DerivedN> & N,
  8. const bool ascii)
  9. {
  10. using namespace std;
  11. assert(N.rows() == 0 || F.rows() == N.rows());
  12. if(ascii)
  13. {
  14. FILE * stl_file = fopen(filename.c_str(),"w");
  15. if(stl_file == NULL)
  16. {
  17. cerr<<"IOError: "<<filename<<" could not be opened for writing."<<endl;
  18. return false;
  19. }
  20. fprintf(stl_file,"solid %s\n",filename.c_str());
  21. for(int f = 0;f<F.rows();f++)
  22. {
  23. fprintf(stl_file,"facet normal ");
  24. if(N.rows()>0)
  25. {
  26. fprintf(stl_file,"%e %e %e\n",
  27. (float)N(f,0),
  28. (float)N(f,1),
  29. (float)N(f,2));
  30. }else
  31. {
  32. fprintf(stl_file,"0 0 0\n");
  33. }
  34. fprintf(stl_file,"outer loop\n");
  35. for(int c = 0;c<F.cols();c++)
  36. {
  37. fprintf(stl_file,"vertex %e %e %e\n",
  38. (float)V(F(f,c),0),
  39. (float)V(F(f,c),1),
  40. (float)V(F(f,c),2));
  41. }
  42. fprintf(stl_file,"endloop\n");
  43. fprintf(stl_file,"endfacet\n");
  44. }
  45. fprintf(stl_file,"endsolid %s\n",filename.c_str());
  46. fclose(stl_file);
  47. return true;
  48. }else
  49. {
  50. FILE * stl_file = fopen(filename.c_str(),"wb");
  51. if(stl_file == NULL)
  52. {
  53. cerr<<"IOError: "<<filename<<" could not be opened for writing."<<endl;
  54. return false;
  55. }
  56. // Write unused 80-char header
  57. for(char h = 0;h<80;h++)
  58. {
  59. fwrite(&h,sizeof(char),1,stl_file);
  60. }
  61. // Write number of triangles
  62. unsigned int num_tri = F.rows();
  63. fwrite(&num_tri,sizeof(unsigned int),1,stl_file);
  64. assert(F.cols() == 3);
  65. // Write each triangle
  66. for(int f = 0;f<F.rows();f++)
  67. {
  68. vector<float> n(3,0);
  69. if(N.rows() > 0)
  70. {
  71. n[0] = N(f,0);
  72. n[1] = N(f,1);
  73. n[2] = N(f,2);
  74. }
  75. fwrite(&n[0],sizeof(float),3,stl_file);
  76. for(int c = 0;c<3;c++)
  77. {
  78. vector<float> v(3);
  79. v[0] = V(F(f,c),0);
  80. v[1] = V(F(f,c),1);
  81. v[2] = V(F(f,c),2);
  82. fwrite(&v[0],sizeof(float),3,stl_file);
  83. }
  84. unsigned short att_count = 0;
  85. fwrite(&att_count,sizeof(unsigned short),1,stl_file);
  86. }
  87. fclose(stl_file);
  88. return true;
  89. }
  90. }
  91. template <typename DerivedV, typename DerivedF>
  92. IGL_INLINE bool igl::writeSTL(
  93. const std::string & filename,
  94. const Eigen::PlainObjectBase<DerivedV> & V,
  95. const Eigen::PlainObjectBase<DerivedF> & F,
  96. const bool ascii)
  97. {
  98. return writeSTL(filename,V,F, Eigen::PlainObjectBase<DerivedV>(), ascii);
  99. }