tetrahedralize.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include "tetrahedralize.h"
  2. #include "mesh_to_tetgenio.h"
  3. #include "tetgenio_to_tetmesh.h"
  4. // IGL includes
  5. #include <igl/matrix_to_list.h>
  6. #include <igl/list_to_matrix.h>
  7. #include <igl/boundary_faces.h>
  8. // STL includes
  9. #include <cassert>
  10. #include <iostream>
  11. IGL_INLINE int igl::tetrahedralize(
  12. const std::vector<std::vector<REAL > > & V,
  13. const std::vector<std::vector<int> > & F,
  14. const std::string switches,
  15. std::vector<std::vector<REAL > > & TV,
  16. std::vector<std::vector<int > > & TT,
  17. std::vector<std::vector<int> > & TF)
  18. {
  19. using namespace std;
  20. tetgenio in,out;
  21. bool success;
  22. success = mesh_to_tetgenio(V,F,in);
  23. if(!success)
  24. {
  25. return -1;
  26. }
  27. try
  28. {
  29. char * cswitches = new char[switches.size() + 1];
  30. std::copy(switches.begin(), switches.end(), cswitches);
  31. ::tetrahedralize(cswitches,&in, &out);
  32. delete[] cswitches;
  33. }catch(int e)
  34. {
  35. cerr<<"^"<<__FUNCTION__<<": TETGEN CRASHED... KABOOOM!!!"<<endl;
  36. return 1;
  37. }
  38. if(out.numberoftetrahedra == 0)
  39. {
  40. cerr<<"^"<<__FUNCTION__<<": Tetgen failed to create tets"<<endl;
  41. return 2;
  42. }
  43. success = tetgenio_to_tetmesh(out,TV,TT);
  44. if(!success)
  45. {
  46. return -1;
  47. }
  48. boundary_faces(TT,TF);
  49. return 0;
  50. }
  51. template <
  52. typename DerivedV,
  53. typename DerivedF,
  54. typename DerivedTV,
  55. typename DerivedTT,
  56. typename DerivedTF>
  57. IGL_INLINE int igl::tetrahedralize(
  58. const Eigen::PlainObjectBase<DerivedV>& V,
  59. const Eigen::PlainObjectBase<DerivedF>& F,
  60. const std::string switches,
  61. Eigen::PlainObjectBase<DerivedTV>& TV,
  62. Eigen::PlainObjectBase<DerivedTT>& TT,
  63. Eigen::PlainObjectBase<DerivedTF>& TF)
  64. {
  65. using namespace igl;
  66. using namespace std;
  67. vector<vector<REAL> > vV,vTV;
  68. vector<vector<int> > vF,vTT,vTF;
  69. matrix_to_list(V,vV);
  70. matrix_to_list(F,vF);
  71. int e = tetrahedralize(vV,vF,switches,vTV,vTT,vTF);
  72. if(e == 0)
  73. {
  74. bool TV_rect = list_to_matrix(vTV,TV);
  75. if(!TV_rect)
  76. {
  77. return false;
  78. }
  79. bool TT_rect = list_to_matrix(vTT,TT);
  80. if(!TT_rect)
  81. {
  82. return false;
  83. }
  84. bool TF_rect = list_to_matrix(vTF,TF);
  85. if(!TF_rect)
  86. {
  87. return false;
  88. }
  89. }
  90. return e;
  91. }
  92. #ifndef IGL_HEADER_ONLY
  93. // Explicit template specialization
  94. template int igl::tetrahedralize<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  95. #endif