writeMESH.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "writeMESH.h"
  2. #include <cstdio>
  3. #include "verbose.h"
  4. #include "matrix_to_list.h"
  5. #include <Eigen/Core>
  6. template <typename Scalar, typename Index>
  7. IGL_INLINE bool igl::writeMESH(
  8. const std::string mesh_file_name,
  9. const std::vector<std::vector<Scalar > > & V,
  10. const std::vector<std::vector<Index > > & T,
  11. const std::vector<std::vector<Index > > & F)
  12. {
  13. Eigen::MatrixXd mV;
  14. Eigen::MatrixXi mT,mF;
  15. bool is_rect;
  16. is_rect = list_to_matrix(V,mV);
  17. if(!is_rect)
  18. {
  19. return false;
  20. }
  21. is_rect = list_to_matrix(T,mT);
  22. if(!is_rect)
  23. {
  24. return false;
  25. }
  26. is_rect = list_to_matrix(F,mF);
  27. if(!is_rect)
  28. {
  29. return false;
  30. }
  31. return igl::writeMESH(mesh_file_name,mV,mT,mF);
  32. }
  33. template <typename DerivedV, typename DerivedT, typename DerivedF>
  34. IGL_INLINE bool igl::writeMESH(
  35. const std::string str,
  36. const Eigen::MatrixBase<DerivedV> & V,
  37. const Eigen::MatrixBase<DerivedT> & T,
  38. const Eigen::MatrixBase<DerivedF> & F)
  39. {
  40. using namespace std;
  41. using namespace igl;
  42. using namespace Eigen;
  43. FILE * mesh_file = fopen(str.c_str(),"w");
  44. if(NULL==mesh_file)
  45. {
  46. fprintf(stderr,"IOError: %s could not be opened...",str.c_str());
  47. return false;
  48. }
  49. // print header
  50. fprintf(mesh_file,"MeshVersionFormatted 1\n");
  51. fprintf(mesh_file,"Dimension 3\n");
  52. // print tet vertices
  53. fprintf(mesh_file,"Vertices\n");
  54. // print number of tet vertices
  55. int number_of_tet_vertices = V.rows();
  56. fprintf(mesh_file,"%d\n",number_of_tet_vertices);
  57. // loop over tet vertices
  58. for(int i = 0;i<number_of_tet_vertices;i++)
  59. {
  60. // print position of ith tet vertex
  61. fprintf(mesh_file,"%lg %lg %lg 1\n",
  62. (double)V(i,0),
  63. (double)V(i,1),
  64. (double)V(i,2));
  65. }
  66. verbose("WARNING: save_mesh() assumes that vertices have"
  67. " same indices in surface as volume...\n");
  68. // print faces
  69. fprintf(mesh_file,"Triangles\n");
  70. // print number of triangles
  71. int number_of_triangles = F.rows();
  72. fprintf(mesh_file,"%d\n",number_of_triangles);
  73. // loop over faces
  74. for(int i = 0;i<number_of_triangles;i++)
  75. {
  76. // loop over vertices in face
  77. fprintf(mesh_file,"%d %d %d 1\n",
  78. (int)F(i,0)+1,
  79. (int)F(i,1)+1,
  80. (int)F(i,2)+1);
  81. }
  82. // print tetrahedra
  83. fprintf(mesh_file,"Tetrahedra\n");
  84. int number_of_tetrahedra = T.rows();
  85. // print number of tetrahedra
  86. fprintf(mesh_file,"%d\n",number_of_tetrahedra);
  87. // loop over tetrahedra
  88. for(int i = 0; i < number_of_tetrahedra;i++)
  89. {
  90. // mesh standard uses 1-based indexing
  91. fprintf(mesh_file, "%d %d %d %d 1\n",
  92. (int)T(i,0)+1,
  93. (int)T(i,1)+1,
  94. (int)T(i,2)+1,
  95. (int)T(i,3)+1);
  96. }
  97. fclose(mesh_file);
  98. return true;
  99. }
  100. #ifndef IGL_HEADER_ONLY
  101. // Explicit template specialization
  102. template bool igl::writeMESH<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&);
  103. #endif