writeMESH.cpp 4.7 KB

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