writeSTL.cpp 4.1 KB

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