tetrahedralize.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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::vector<std::vector<REAL > > & H,
  22. const std::vector<std::vector<REAL > > & R,
  23. const std::string switches,
  24. std::vector<std::vector<REAL > > & TV,
  25. std::vector<std::vector<int > > & TT,
  26. std::vector<std::vector<int > > & TF,
  27. std::vector<std::vector<REAL > > &TR,
  28. std::vector<std::vector<int > > & TN,
  29. std::vector<std::vector<int > > & PT,
  30. std::vector<std::vector<int > > & FT,
  31. size_t & numRegions)
  32. {
  33. using namespace std;
  34. tetgenio in,out;
  35. bool success;
  36. success = mesh_to_tetgenio(V, F, H, R, in);
  37. if(!success)
  38. {
  39. return -1;
  40. }
  41. try
  42. {
  43. char * cswitches = new char[switches.size() + 1];
  44. strcpy(cswitches, switches.c_str());
  45. ::tetrahedralize(cswitches, &in, &out);
  46. delete[] cswitches;
  47. }catch(int e)
  48. {
  49. cerr <<"^"<<__FUNCTION__<<": TETGEN CRASHED...KABOOM!!"<<endl;
  50. return 1;
  51. }
  52. if(out.numberoftetrahedra == 0)
  53. {
  54. cerr<<"^"<<__FUNCTION__<<": Tetgen failed to create tets"<<endl;
  55. return 2;
  56. }
  57. success = tetgenio_to_tetmesh(out, TV, TT, TF, TR, TN, PT, FT, numRegions
  58. );
  59. if(!success)
  60. {
  61. return -1;
  62. }
  63. return 0;
  64. }
  65. template <
  66. typename DerivedV,
  67. typename DerivedF,
  68. typename DerivedH,
  69. typename DerivedR,
  70. typename DerivedTV,
  71. typename DerivedTT,
  72. typename DerivedTF,
  73. typename DerivedTR>
  74. IGL_INLINE int igl::copyleft::tetgen::tetrahedralize(
  75. const Eigen::PlainObjectBase<DerivedV>& V,
  76. const Eigen::PlainObjectBase<DerivedF>& F,
  77. const Eigen::PlainObjectBase<DerivedH>& H,
  78. const Eigen::PlainObjectBase<DerivedR>& R,
  79. const std::string switches,
  80. Eigen::PlainObjectBase<DerivedTV>& TV,
  81. Eigen::PlainObjectBase<DerivedTT>& TT,
  82. Eigen::PlainObjectBase<DerivedTF>& TF,
  83. Eigen::PlainObjectBase<DerivedTR>& TR,
  84. Eigen::PlainObjectBase<DerivedTT>& TN,
  85. Eigen::PlainObjectBase<DerivedTT>& PT,
  86. Eigen::PlainObjectBase<DerivedTT>& FT,
  87. size_t & numRegions)
  88. {
  89. using namespace std;
  90. vector<vector<REAL> > vV, vH, vR, vTV, vTR;
  91. vector<vector<int> > vF,vTT,vTF, vTN, vPT, vFT;
  92. matrix_to_list(V,vV);
  93. matrix_to_list(F,vF);
  94. matrix_to_list(H, vH);
  95. matrix_to_list(R, vR);
  96. int e = tetrahedralize(vV,vF,vH,vR,switches,vTV,vTT,vTF,vTR,vTN,vPT,vFT, numRegions);
  97. if(e == 0)
  98. {
  99. bool TV_rect = list_to_matrix(vTV,TV);
  100. if(!TV_rect)
  101. {
  102. return 3;
  103. }
  104. bool TT_rect = list_to_matrix(vTT,TT);
  105. if(!TT_rect)
  106. {
  107. return 3;
  108. }
  109. bool TF_rect = list_to_matrix(vTF,TF);
  110. if(!TF_rect)
  111. {
  112. return 3;
  113. }
  114. bool TR_rect = list_to_matrix(vTR, TR);
  115. if(!TR_rect)
  116. {
  117. return 3;
  118. }
  119. bool TN_rect = list_to_matrix(vTN, TN);
  120. if(!TN_rect)
  121. {
  122. return 3;
  123. }
  124. bool PT_rect = list_to_matrix(vPT, PT);
  125. if(!PT_rect)
  126. {
  127. return 3;
  128. }
  129. bool FT_rect = list_to_matrix(vFT, FT);
  130. if(!FT_rect)
  131. {
  132. return 3;
  133. }
  134. }
  135. return e;
  136. }
  137. IGL_INLINE int igl::copyleft::tetgen::tetrahedralize(
  138. const std::vector<std::vector<REAL > > & V,
  139. const std::vector<std::vector<int> > & F,
  140. const std::string switches,
  141. std::vector<std::vector<REAL > > & TV,
  142. std::vector<std::vector<int > > & TT,
  143. std::vector<std::vector<int> > & TF)
  144. {
  145. using namespace std;
  146. tetgenio in,out;
  147. bool success;
  148. success = mesh_to_tetgenio(V,F,in);
  149. if(!success)
  150. {
  151. return -1;
  152. }
  153. try
  154. {
  155. char * cswitches = new char[switches.size() + 1];
  156. std::strcpy(cswitches,switches.c_str());
  157. ::tetrahedralize(cswitches,&in, &out);
  158. delete[] cswitches;
  159. }catch(int e)
  160. {
  161. cerr<<"^"<<__FUNCTION__<<": TETGEN CRASHED... KABOOOM!!!"<<endl;
  162. return 1;
  163. }
  164. if(out.numberoftetrahedra == 0)
  165. {
  166. cerr<<"^"<<__FUNCTION__<<": Tetgen failed to create tets"<<endl;
  167. return 2;
  168. }
  169. success = tetgenio_to_tetmesh(out,TV,TT,TF);
  170. if(!success)
  171. {
  172. return -1;
  173. }
  174. boundary_facets(TT,TF);
  175. return 0;
  176. }
  177. template <
  178. typename DerivedV,
  179. typename DerivedF,
  180. typename DerivedTV,
  181. typename DerivedTT,
  182. typename DerivedTF>
  183. IGL_INLINE int igl::copyleft::tetgen::tetrahedralize(
  184. const Eigen::PlainObjectBase<DerivedV>& V,
  185. const Eigen::PlainObjectBase<DerivedF>& F,
  186. const std::string switches,
  187. Eigen::PlainObjectBase<DerivedTV>& TV,
  188. Eigen::PlainObjectBase<DerivedTT>& TT,
  189. Eigen::PlainObjectBase<DerivedTF>& TF)
  190. {
  191. using namespace std;
  192. vector<vector<REAL> > vV,vTV;
  193. vector<vector<int> > vF,vTT,vTF;
  194. matrix_to_list(V,vV);
  195. matrix_to_list(F,vF);
  196. int e = tetrahedralize(vV,vF,switches,vTV,vTT,vTF);
  197. if(e == 0)
  198. {
  199. bool TV_rect = list_to_matrix(vTV,TV);
  200. if(!TV_rect)
  201. {
  202. return 3;
  203. }
  204. bool TT_rect = list_to_matrix(vTT,TT);
  205. if(!TT_rect)
  206. {
  207. return 3;
  208. }
  209. bool TF_rect = list_to_matrix(vTF,TF);
  210. if(!TF_rect)
  211. {
  212. return 3;
  213. }
  214. }
  215. return e;
  216. }
  217. template <
  218. typename DerivedV,
  219. typename DerivedF,
  220. typename DerivedVM,
  221. typename DerivedFM,
  222. typename DerivedTV,
  223. typename DerivedTT,
  224. typename DerivedTF,
  225. typename DerivedTM>
  226. IGL_INLINE int igl::copyleft::tetgen::tetrahedralize(
  227. const Eigen::PlainObjectBase<DerivedV>& V,
  228. const Eigen::PlainObjectBase<DerivedF>& F,
  229. const Eigen::PlainObjectBase<DerivedVM>& VM,
  230. const Eigen::PlainObjectBase<DerivedFM>& FM,
  231. const std::string switches,
  232. Eigen::PlainObjectBase<DerivedTV>& TV,
  233. Eigen::PlainObjectBase<DerivedTT>& TT,
  234. Eigen::PlainObjectBase<DerivedTF>& TF,
  235. Eigen::PlainObjectBase<DerivedTM>& TM)
  236. {
  237. using namespace std;
  238. vector<vector<REAL> > vV,vTV;
  239. vector<vector<int> > vF,vTT,vTF;
  240. vector<int> vTM;
  241. matrix_to_list(V,vV);
  242. matrix_to_list(F,vF);
  243. vector<int> vVM = matrix_to_list(VM);
  244. vector<int> vFM = matrix_to_list(FM);
  245. int e = tetrahedralize(vV,vF,vVM,vFM,switches,vTV,vTT,vTF,vTM);
  246. if(e == 0)
  247. {
  248. bool TV_rect = list_to_matrix(vTV,TV);
  249. if(!TV_rect)
  250. {
  251. return false;
  252. }
  253. bool TT_rect = list_to_matrix(vTT,TT);
  254. if(!TT_rect)
  255. {
  256. return false;
  257. }
  258. bool TF_rect = list_to_matrix(vTF,TF);
  259. if(!TF_rect)
  260. {
  261. return false;
  262. }
  263. bool TM_rect = list_to_matrix(vTM,TM);
  264. if(!TM_rect)
  265. {
  266. return false;
  267. }
  268. }
  269. return e;
  270. }
  271. IGL_INLINE int igl::copyleft::tetgen::tetrahedralize(
  272. const std::vector<std::vector<REAL > > & V,
  273. const std::vector<std::vector<int> > & F,
  274. const std::vector<int> & VM,
  275. const std::vector<int> & FM,
  276. const std::string switches,
  277. std::vector<std::vector<REAL > > & TV,
  278. std::vector<std::vector<int > > & TT,
  279. std::vector<std::vector<int> > & TF,
  280. std::vector<int> & TM)
  281. {
  282. using namespace std;
  283. tetgenio in,out;
  284. bool success;
  285. success = mesh_to_tetgenio(V,F,in);
  286. if(!success)
  287. {
  288. return -1;
  289. }
  290. in.pointmarkerlist = new int[VM.size()];
  291. for (int i = 0; i < VM.size(); ++i) {
  292. in.pointmarkerlist[i] = VM[i];
  293. }
  294. // These have already been created in mesh_to_tetgenio.
  295. // Reset them here.
  296. for (int i = 0; i < FM.size(); ++i) {
  297. in.facetmarkerlist[i] = FM[i];
  298. }
  299. try
  300. {
  301. char * cswitches = new char[switches.size() + 1];
  302. std::strcpy(cswitches,switches.c_str());
  303. ::tetrahedralize(cswitches,&in, &out);
  304. delete[] cswitches;
  305. }catch(int e)
  306. {
  307. cerr<<"^"<<__FUNCTION__<<": TETGEN CRASHED... KABOOOM!!!"<<endl;
  308. return 1;
  309. }
  310. if(out.numberoftetrahedra == 0)
  311. {
  312. cerr<<"^"<<__FUNCTION__<<": Tetgen failed to create tets"<<endl;
  313. return 2;
  314. }
  315. success = tetgenio_to_tetmesh(out,TV,TT,TF);
  316. if(!success)
  317. {
  318. return -1;
  319. }
  320. TM.resize(out.numberofpoints);
  321. for (int i = 0; i < out.numberofpoints; ++i) {
  322. TM[i] = out.pointmarkerlist[i];
  323. }
  324. boundary_facets(TT,TF);
  325. return 0;
  326. }
  327. #ifdef IGL_STATIC_LIBRARY
  328. // Explicit template instantiation
  329. 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> >&);
  330. template int igl::copyleft::tetgen::tetrahedralize<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::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::Matrix<int, -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 Eigen::PlainObjectBase<Eigen::Matrix<int, -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> > &, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > &);
  331. template int igl::copyleft::tetgen::tetrahedralize<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::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::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&, Eigen::PlainObjectBase<Eigen::Matrix<int, -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> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  332. #endif