writeMESH.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #ifndef IGL_WRITEMESH_H
  2. #define IGL_WRITEMESH_H
  3. #include <string>
  4. #include <vector>
  5. namespace igl
  6. {
  7. // save a tetrahedral volume mesh to a .mesh file
  8. //
  9. // Templates:
  10. // Scalar type for positions and vectors (will be cast as double)
  11. // Index type for indices (will be cast to int)
  12. // Input:
  13. // mesh_file_name path of .mesh file
  14. // Outputs:
  15. // V double matrix of vertex positions #V by 3
  16. // T #T list of tet indices into vertex positions
  17. // F #F list of face indices into vertex positions
  18. template <typename Scalar, typename Index>
  19. inline bool writeMESH(
  20. const std::string mesh_file_name,
  21. std::vector<std::vector<Scalar > > & V,
  22. std::vector<std::vector<Index > > & T,
  23. std::vector<std::vector<Index > > & F);
  24. // Input:
  25. // mesh_file_name path of .mesh file
  26. // Outputs:
  27. // V eigen double matrix #V by 3
  28. // T eigen int matrix #T by 4
  29. // F eigen int matrix #F by 3
  30. inline bool writeMESH(
  31. const std::string str,
  32. Eigen::MatrixXd& V,
  33. Eigen::MatrixXi& T,
  34. Eigen::MatrixXi& F);
  35. }
  36. // Implementation
  37. #include <cstdio>
  38. #include "verbose.h"
  39. template <typename Scalar, typename Index>
  40. inline bool igl::writeMESH(
  41. const std::string mesh_file_name,
  42. std::vector<std::vector<Scalar > > & V,
  43. std::vector<std::vector<Index > > & T,
  44. std::vector<std::vector<Index > > & F)
  45. {
  46. // not implemented but should be
  47. assert(false);
  48. }
  49. #include <Eigen/Core>
  50. inline bool igl::writeMESH(
  51. const std::string str,
  52. Eigen::MatrixXd& V,
  53. Eigen::MatrixXi& T,
  54. Eigen::MatrixXi& F)
  55. {
  56. using namespace std;
  57. using namespace igl;
  58. using namespace Eigen;
  59. FILE * mesh_file = fopen(str.c_str(),"w");
  60. if(NULL==mesh_file)
  61. {
  62. fprintf(stderr,"IOError: %s could not be opened...",str.c_str());
  63. return false;
  64. }
  65. // print header
  66. fprintf(mesh_file,"MeshVersionFormatted 1\n");
  67. fprintf(mesh_file,"Dimension 3\n");
  68. // print tet vertices
  69. fprintf(mesh_file,"Vertices\n");
  70. // print number of tet vertices
  71. size_t number_of_tet_vertices = V.rows();
  72. fprintf(mesh_file,"%ld\n",number_of_tet_vertices);
  73. // loop over tet vertices
  74. for(size_t i = 0;i<number_of_tet_vertices;i++)
  75. {
  76. // print position of ith tet vertex
  77. fprintf(mesh_file,"%lg %lg %lg 1\n",
  78. (double)V(i,0),
  79. (double)V(i,1),
  80. (double)V(i,2));
  81. }
  82. verbose("WARNING: save_mesh() assumes that vertices have"
  83. " same indices in surface as volume...\n");
  84. // print faces
  85. fprintf(mesh_file,"Triangles\n");
  86. // print number of triangles
  87. size_t number_of_triangles = F.rows();
  88. fprintf(mesh_file,"%ld\n",number_of_triangles);
  89. // loop over faces
  90. for(int i = 0;i<number_of_triangles;i++)
  91. {
  92. // loop over vertices in face
  93. fprintf(mesh_file,"%d %d %d 1\n",
  94. (int)F(i,0)+1,
  95. (int)F(i,1)+1,
  96. (int)F(i,2)+1);
  97. }
  98. // print tetrahedra
  99. fprintf(mesh_file,"Tetrahedra\n");
  100. size_t number_of_tetrahedra = T.rows();
  101. // print number of tetrahedra
  102. fprintf(mesh_file,"%ld\n",number_of_tetrahedra);
  103. // loop over tetrahedra
  104. for(size_t i = 0; i < number_of_tetrahedra;i++)
  105. {
  106. // mesh standard uses 1-based indexing
  107. fprintf(mesh_file, "%d %d %d %d 1\n",
  108. (int)T(i,0)+1,
  109. (int)T(i,1)+1,
  110. (int)T(i,2)+1,
  111. (int)T(i,3)+1);
  112. }
  113. fclose(mesh_file);
  114. return true;
  115. }
  116. #endif