tetgenio_to_tetmesh.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 "tetgenio_to_tetmesh.h"
  9. // IGL includes
  10. #include "../../list_to_matrix.h"
  11. // STL includes
  12. #include <iostream>
  13. IGL_INLINE bool igl::copyleft::tetgen::tetgenio_to_tetmesh(
  14. const tetgenio & out,
  15. std::vector<std::vector<REAL > > & V,
  16. std::vector<std::vector<int> > & T,
  17. std::vector<std::vector<int > > & F,
  18. std::vector<std::vector<REAL > >& R,
  19. std::vector<std::vector<int > >& N,
  20. std::vector<std::vector<int > >& PT,
  21. std::vector<std::vector<int > >& FT,
  22. size_t & nR )
  23. {
  24. using namespace std;
  25. // process points
  26. if(out.pointlist == NULL)
  27. {
  28. cerr<<"^tetgenio_to_tetmesh Error: point list is NULL\n"<<endl;
  29. return false;
  30. }
  31. V.resize(out.numberofpoints,vector<REAL>(3));
  32. // loop over points
  33. for(int i = 0;i < out.numberofpoints; i++)
  34. {
  35. V[i][0] = out.pointlist[i*3+0];
  36. V[i][1] = out.pointlist[i*3+1];
  37. V[i][2] = out.pointlist[i*3+2];
  38. }
  39. // process tets
  40. if(out.tetrahedronlist == NULL)
  41. {
  42. cerr<<"^tetgenio_to_tetmesh Error: tet list is NULL\n"<<endl;
  43. return false;
  44. }
  45. // When would this not be 4?
  46. assert(out.numberofcorners == 4);
  47. T.resize(out.numberoftetrahedra,vector<int>(out.numberofcorners));
  48. int min_index = 1e7;
  49. int max_index = -1e7;
  50. // loop over tetrahedra
  51. for(int i = 0; i < out.numberoftetrahedra; i++)
  52. {
  53. for(int j = 0; j<out.numberofcorners; j++)
  54. {
  55. int index = out.tetrahedronlist[i * out.numberofcorners + j];
  56. T[i][j] = index;
  57. min_index = (min_index > index ? index : min_index);
  58. max_index = (max_index < index ? index : max_index);
  59. }
  60. }
  61. assert(min_index >= 0);
  62. assert(max_index >= 0);
  63. assert(max_index < (int)V.size());
  64. cout<<out.numberoftrifaces<<endl;
  65. // When would this not be 4?
  66. F.clear();
  67. // loop over tetrahedra
  68. for(int i = 0; i < out.numberoftrifaces; i++)
  69. {
  70. if(out.trifacemarkerlist[i]>=0)
  71. {
  72. vector<int> face(3);
  73. for(int j = 0; j<3; j++)
  74. {
  75. face[j] = out.trifacelist[i * 3 + j];
  76. }
  77. F.push_back(face);
  78. }
  79. }
  80. R.resize(out.numberoftetrahedra, vector<REAL>(1));
  81. unordered_map<REAL, REAL> hashUniqueRegions;
  82. for(size_t i = 0; i < out.numberoftetrahedra; i++)
  83. {
  84. R[i][0] = out.tetrahedronattributelist[i];
  85. hashUniqueRegions[R[i][0]] = i;
  86. }
  87. // extract region marks
  88. nR = hashUniqueRegions.size();
  89. // extract neighbor list
  90. N.resize(out.numberoftetrahedra, vector<int>(4));
  91. for (size_t i = 0; i < out.numberoftetrahedra; i++)
  92. {
  93. for (size_t j = 0; j < 4; j++)
  94. N[i][j] = out.neighborlist[i * 4 + j];
  95. }
  96. // extract point 2 tetrahedron list
  97. PT.resize(out.numberofpoints, vector<int>(1));
  98. for (size_t i = 0; i < out.numberofpoints; i++)
  99. {
  100. PT[i][0] = out.point2tetlist[i];
  101. }
  102. //extract face to tetrahedron list
  103. FT.resize(out.numberoftrifaces, vector<int>(2));
  104. int triface;
  105. for (size_t i = 0; i < out.numberoftrifaces; i++)
  106. {
  107. for (size_t j = 0; j < 2; j++)
  108. {
  109. FT[i][j] = out.face2tetlist[0];
  110. }
  111. }
  112. return true;
  113. }
  114. IGL_INLINE bool igl::copyleft::tetgen::tetgenio_to_tetmesh(
  115. const tetgenio & out,
  116. std::vector<std::vector<REAL > > & V,
  117. std::vector<std::vector<int> > & T,
  118. std::vector<std::vector<int> > & F)
  119. {
  120. using namespace std;
  121. // process points
  122. if(out.pointlist == NULL)
  123. {
  124. cerr<<"^tetgenio_to_tetmesh Error: point list is NULL\n"<<endl;
  125. return false;
  126. }
  127. V.resize(out.numberofpoints,vector<REAL>(3));
  128. // loop over points
  129. for(int i = 0;i < out.numberofpoints; i++)
  130. {
  131. V[i][0] = out.pointlist[i*3+0];
  132. V[i][1] = out.pointlist[i*3+1];
  133. V[i][2] = out.pointlist[i*3+2];
  134. }
  135. // process tets
  136. if(out.tetrahedronlist == NULL)
  137. {
  138. cerr<<"^tetgenio_to_tetmesh Error: tet list is NULL\n"<<endl;
  139. return false;
  140. }
  141. // When would this not be 4?
  142. assert(out.numberofcorners == 4);
  143. T.resize(out.numberoftetrahedra,vector<int>(out.numberofcorners));
  144. int min_index = 1e7;
  145. int max_index = -1e7;
  146. // loop over tetrahedra
  147. for(int i = 0; i < out.numberoftetrahedra; i++)
  148. {
  149. for(int j = 0; j<out.numberofcorners; j++)
  150. {
  151. int index = out.tetrahedronlist[i * out.numberofcorners + j];
  152. T[i][j] = index;
  153. min_index = (min_index > index ? index : min_index);
  154. max_index = (max_index < index ? index : max_index);
  155. }
  156. }
  157. assert(min_index >= 0);
  158. assert(max_index >= 0);
  159. assert(max_index < (int)V.size());
  160. // When would this not be 4?
  161. F.clear();
  162. // loop over tetrahedra
  163. for(int i = 0; i < out.numberoftrifaces; i++)
  164. {
  165. if(out.trifacemarkerlist[i]>=0)
  166. {
  167. vector<int> face(3);
  168. for(int j = 0; j<3; j++)
  169. {
  170. face[j] = out.trifacelist[i * 3 + j];
  171. }
  172. F.push_back(face);
  173. }
  174. }
  175. return true;
  176. }
  177. IGL_INLINE bool igl::copyleft::tetgen::tetgenio_to_tetmesh(
  178. const tetgenio & out,
  179. std::vector<std::vector<REAL > > & V,
  180. std::vector<std::vector<int> > & T)
  181. {
  182. std::vector<std::vector<int> > F;
  183. return tetgenio_to_tetmesh(out,V,T,F);
  184. }
  185. template <typename DerivedV, typename DerivedT, typename DerivedF>
  186. IGL_INLINE bool igl::copyleft::tetgen::tetgenio_to_tetmesh(
  187. const tetgenio & out,
  188. Eigen::PlainObjectBase<DerivedV>& V,
  189. Eigen::PlainObjectBase<DerivedT>& T,
  190. Eigen::PlainObjectBase<DerivedF>& F)
  191. {
  192. using namespace std;
  193. vector<vector<REAL> > vV;
  194. vector<vector<int> > vT;
  195. vector<vector<int> > vF;
  196. bool success = tetgenio_to_tetmesh(out,vV,vT,vF);
  197. if(!success)
  198. {
  199. return false;
  200. }
  201. bool V_rect = list_to_matrix(vV,V);
  202. if(!V_rect)
  203. {
  204. // igl::list_to_matrix(vV,V) already printed error message to std err
  205. return false;
  206. }
  207. bool T_rect = list_to_matrix(vT,T);
  208. if(!T_rect)
  209. {
  210. // igl::list_to_matrix(vT,T) already printed error message to std err
  211. return false;
  212. }
  213. bool F_rect = list_to_matrix(vF,F);
  214. if(!F_rect)
  215. {
  216. return false;
  217. }
  218. return true;
  219. }
  220. template <typename DerivedV, typename DerivedT>
  221. IGL_INLINE bool igl::copyleft::tetgen::tetgenio_to_tetmesh(
  222. const tetgenio & out,
  223. Eigen::PlainObjectBase<DerivedV>& V,
  224. Eigen::PlainObjectBase<DerivedT>& T)
  225. {
  226. Eigen::Matrix<typename DerivedT::Scalar,Eigen::Dynamic,3> F;
  227. return tetgenio_to_tetmesh(out,V,T,F);
  228. }
  229. #ifdef IGL_STATIC_LIBRARY
  230. // Explicit template instantiation
  231. template bool igl::copyleft::tetgen::tetgenio_to_tetmesh<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(tetgenio const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  232. #endif