writeMESH.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "writeMESH.h"
  2. #include "verbose.h"
  3. #include "matrix_to_list.h"
  4. #include <Eigen/Core>
  5. #include <iostream>
  6. #include <fstream>
  7. #include <cstdio>
  8. template <typename Scalar, typename Index>
  9. IGL_INLINE bool igl::writeMESH(
  10. const std::string mesh_file_name,
  11. const std::vector<std::vector<Scalar > > & V,
  12. const std::vector<std::vector<Index > > & T,
  13. const std::vector<std::vector<Index > > & F)
  14. {
  15. Eigen::MatrixXd mV;
  16. Eigen::MatrixXi mT,mF;
  17. bool is_rect;
  18. is_rect = list_to_matrix(V,mV);
  19. if(!is_rect)
  20. {
  21. return false;
  22. }
  23. is_rect = list_to_matrix(T,mT);
  24. if(!is_rect)
  25. {
  26. return false;
  27. }
  28. is_rect = list_to_matrix(F,mF);
  29. if(!is_rect)
  30. {
  31. return false;
  32. }
  33. return igl::writeMESH(mesh_file_name,mV,mT,mF);
  34. }
  35. template <typename DerivedV, typename DerivedT, typename DerivedF>
  36. IGL_INLINE bool igl::writeMESH(
  37. const std::string str,
  38. const Eigen::PlainObjectBase<DerivedV> & V,
  39. const Eigen::PlainObjectBase<DerivedT> & T,
  40. const Eigen::PlainObjectBase<DerivedF> & F)
  41. {
  42. using namespace std;
  43. using namespace igl;
  44. using namespace Eigen;
  45. //// This is (surprisingly) slower than the C-ish code below
  46. //ofstream mesh_file;
  47. //mesh_file.open(str.c_str());
  48. //if(!mesh_file.is_open())
  49. //{
  50. // cerr<<"IOError: "<<str<<" could not be opened..."<<endl;
  51. // return false;
  52. //}
  53. //IOFormat format(FullPrecision,DontAlignCols," ","\n",""," 1","","");
  54. //mesh_file<<"MeshVersionFormatted 1\n";
  55. //mesh_file<<"Dimension 3\n";
  56. //mesh_file<<"Vertices\n";
  57. //mesh_file<<V.rows()<<"\n";
  58. //mesh_file<<V.format(format)<<"\n";
  59. //mesh_file<<"Triangles\n";
  60. //mesh_file<<F.rows()<<"\n";
  61. //mesh_file<<(F.array()+1).eval().format(format)<<"\n";
  62. //mesh_file<<"Tetrahedra\n";
  63. //mesh_file<<T.rows()<<"\n";
  64. //mesh_file<<(T.array()+1).eval().format(format)<<"\n";
  65. //mesh_file.close();
  66. FILE * mesh_file = fopen(str.c_str(),"w");
  67. if(NULL==mesh_file)
  68. {
  69. fprintf(stderr,"IOError: %s could not be opened...",str.c_str());
  70. return false;
  71. }
  72. // print header
  73. fprintf(mesh_file,"MeshVersionFormatted 1\n");
  74. fprintf(mesh_file,"Dimension 3\n");
  75. // print tet vertices
  76. fprintf(mesh_file,"Vertices\n");
  77. // print number of tet vertices
  78. int number_of_tet_vertices = V.rows();
  79. fprintf(mesh_file,"%d\n",number_of_tet_vertices);
  80. // loop over tet vertices
  81. for(int i = 0;i<number_of_tet_vertices;i++)
  82. {
  83. // print position of ith tet vertex
  84. fprintf(mesh_file,"%lg %lg %lg 1\n",
  85. (double)V(i,0),
  86. (double)V(i,1),
  87. (double)V(i,2));
  88. }
  89. verbose("WARNING: save_mesh() assumes that vertices have"
  90. " same indices in surface as volume...\n");
  91. // print faces
  92. fprintf(mesh_file,"Triangles\n");
  93. // print number of triangles
  94. int number_of_triangles = F.rows();
  95. fprintf(mesh_file,"%d\n",number_of_triangles);
  96. // loop over faces
  97. for(int i = 0;i<number_of_triangles;i++)
  98. {
  99. // loop over vertices in face
  100. fprintf(mesh_file,"%d %d %d 1\n",
  101. (int)F(i,0)+1,
  102. (int)F(i,1)+1,
  103. (int)F(i,2)+1);
  104. }
  105. // print tetrahedra
  106. fprintf(mesh_file,"Tetrahedra\n");
  107. int number_of_tetrahedra = T.rows();
  108. // print number of tetrahedra
  109. fprintf(mesh_file,"%d\n",number_of_tetrahedra);
  110. // loop over tetrahedra
  111. for(int i = 0; i < number_of_tetrahedra;i++)
  112. {
  113. // mesh standard uses 1-based indexing
  114. fprintf(mesh_file, "%d %d %d %d 1\n",
  115. (int)T(i,0)+1,
  116. (int)T(i,1)+1,
  117. (int)T(i,2)+1,
  118. (int)T(i,3)+1);
  119. }
  120. fclose(mesh_file);
  121. return true;
  122. }
  123. #ifndef IGL_HEADER_ONLY
  124. // Explicit template specialization
  125. //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&);
  126. 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::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&);
  127. #endif