writeMESH.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 "list_to_matrix.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 Eigen;
  51. //// This is (surprisingly) slower than the C-ish code below
  52. //ofstream mesh_file;
  53. //mesh_file.open(str.c_str());
  54. //if(!mesh_file.is_open())
  55. //{
  56. // cerr<<"IOError: "<<str<<" could not be opened..."<<endl;
  57. // return false;
  58. //}
  59. //IOFormat format(FullPrecision,DontAlignCols," ","\n",""," 1","","");
  60. //mesh_file<<"MeshVersionFormatted 1\n";
  61. //mesh_file<<"Dimension 3\n";
  62. //mesh_file<<"Vertices\n";
  63. //mesh_file<<V.rows()<<"\n";
  64. //mesh_file<<V.format(format)<<"\n";
  65. //mesh_file<<"Triangles\n";
  66. //mesh_file<<F.rows()<<"\n";
  67. //mesh_file<<(F.array()+1).eval().format(format)<<"\n";
  68. //mesh_file<<"Tetrahedra\n";
  69. //mesh_file<<T.rows()<<"\n";
  70. //mesh_file<<(T.array()+1).eval().format(format)<<"\n";
  71. //mesh_file.close();
  72. FILE * mesh_file = fopen(str.c_str(),"w");
  73. if(NULL==mesh_file)
  74. {
  75. fprintf(stderr,"IOError: %s could not be opened...",str.c_str());
  76. return false;
  77. }
  78. // print header
  79. fprintf(mesh_file,"MeshVersionFormatted 1\n");
  80. fprintf(mesh_file,"Dimension 3\n");
  81. // print tet vertices
  82. fprintf(mesh_file,"Vertices\n");
  83. // print number of tet vertices
  84. int number_of_tet_vertices = V.rows();
  85. fprintf(mesh_file,"%d\n",number_of_tet_vertices);
  86. // loop over tet vertices
  87. for(int i = 0;i<number_of_tet_vertices;i++)
  88. {
  89. // print position of ith tet vertex
  90. fprintf(mesh_file,"%.17lg %.17lg %.17lg 1\n",
  91. (double)V(i,0),
  92. (double)V(i,1),
  93. (double)V(i,2));
  94. }
  95. verbose("WARNING: save_mesh() assumes that vertices have"
  96. " same indices in surface as volume...\n");
  97. // print faces
  98. fprintf(mesh_file,"Triangles\n");
  99. // print number of triangles
  100. int number_of_triangles = F.rows();
  101. fprintf(mesh_file,"%d\n",number_of_triangles);
  102. // loop over faces
  103. for(int i = 0;i<number_of_triangles;i++)
  104. {
  105. // loop over vertices in face
  106. fprintf(mesh_file,"%d %d %d 1\n",
  107. (int)F(i,0)+1,
  108. (int)F(i,1)+1,
  109. (int)F(i,2)+1);
  110. }
  111. // print tetrahedra
  112. fprintf(mesh_file,"Tetrahedra\n");
  113. int number_of_tetrahedra = T.rows();
  114. // print number of tetrahedra
  115. fprintf(mesh_file,"%d\n",number_of_tetrahedra);
  116. // loop over tetrahedra
  117. for(int i = 0; i < number_of_tetrahedra;i++)
  118. {
  119. // mesh standard uses 1-based indexing
  120. fprintf(mesh_file, "%d %d %d %d 1\n",
  121. (int)T(i,0)+1,
  122. (int)T(i,1)+1,
  123. (int)T(i,2)+1,
  124. (int)T(i,3)+1);
  125. }
  126. fclose(mesh_file);
  127. return true;
  128. }
  129. #ifdef IGL_STATIC_LIBRARY
  130. // Explicit template specialization
  131. // generated by autoexplicit.sh
  132. template bool igl::writeMESH<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 3, 0, -1, 3> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&);
  133. // generated by autoexplicit.sh
  134. template bool igl::writeMESH<Eigen::Matrix<double, 8, 3, 0, 8, 3>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, 12, 3, 0, 12, 3> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::PlainObjectBase<Eigen::Matrix<double, 8, 3, 0, 8, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, 12, 3, 0, 12, 3> > const&);
  135. //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&);
  136. 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&);
  137. template bool igl::writeMESH<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -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<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&);
  138. template bool igl::writeMESH<double, int>(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > > const&, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > const&, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > const&);
  139. #endif