writeMESH.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. return false;
  49. }
  50. #include <Eigen/Core>
  51. inline bool igl::writeMESH(
  52. const std::string str,
  53. Eigen::MatrixXd& V,
  54. Eigen::MatrixXi& T,
  55. Eigen::MatrixXi& F)
  56. {
  57. using namespace std;
  58. using namespace igl;
  59. using namespace Eigen;
  60. FILE * mesh_file = fopen(str.c_str(),"w");
  61. if(NULL==mesh_file)
  62. {
  63. fprintf(stderr,"IOError: %s could not be opened...",str.c_str());
  64. return false;
  65. }
  66. // print header
  67. fprintf(mesh_file,"MeshVersionFormatted 1\n");
  68. fprintf(mesh_file,"Dimension 3\n");
  69. // print tet vertices
  70. fprintf(mesh_file,"Vertices\n");
  71. // print number of tet vertices
  72. int number_of_tet_vertices = V.rows();
  73. fprintf(mesh_file,"%d\n",number_of_tet_vertices);
  74. // loop over tet vertices
  75. for(int i = 0;i<number_of_tet_vertices;i++)
  76. {
  77. // print position of ith tet vertex
  78. fprintf(mesh_file,"%lg %lg %lg 1\n",
  79. (double)V(i,0),
  80. (double)V(i,1),
  81. (double)V(i,2));
  82. }
  83. verbose("WARNING: save_mesh() assumes that vertices have"
  84. " same indices in surface as volume...\n");
  85. // print faces
  86. fprintf(mesh_file,"Triangles\n");
  87. // print number of triangles
  88. int number_of_triangles = F.rows();
  89. fprintf(mesh_file,"%d\n",number_of_triangles);
  90. // loop over faces
  91. for(int i = 0;i<number_of_triangles;i++)
  92. {
  93. // loop over vertices in face
  94. fprintf(mesh_file,"%d %d %d 1\n",
  95. (int)F(i,0)+1,
  96. (int)F(i,1)+1,
  97. (int)F(i,2)+1);
  98. }
  99. // print tetrahedra
  100. fprintf(mesh_file,"Tetrahedra\n");
  101. int number_of_tetrahedra = T.rows();
  102. // print number of tetrahedra
  103. fprintf(mesh_file,"%d\n",number_of_tetrahedra);
  104. // loop over tetrahedra
  105. for(int i = 0; i < number_of_tetrahedra;i++)
  106. {
  107. // mesh standard uses 1-based indexing
  108. fprintf(mesh_file, "%d %d %d %d 1\n",
  109. (int)T(i,0)+1,
  110. (int)T(i,1)+1,
  111. (int)T(i,2)+1,
  112. (int)T(i,3)+1);
  113. }
  114. fclose(mesh_file);
  115. return true;
  116. }
  117. #endif