writeMESH.cpp 7.2 KB

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