tetrahedralize.cpp 3.2 KB

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