tetgenio_to_tetmesh.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. {
  19. using namespace std;
  20. // process points
  21. if(out.pointlist == NULL)
  22. {
  23. cerr<<"^tetgenio_to_tetmesh Error: point list is NULL\n"<<endl;
  24. return false;
  25. }
  26. V.resize(out.numberofpoints,vector<REAL>(3));
  27. // loop over points
  28. for(int i = 0;i < out.numberofpoints; i++)
  29. {
  30. V[i][0] = out.pointlist[i*3+0];
  31. V[i][1] = out.pointlist[i*3+1];
  32. V[i][2] = out.pointlist[i*3+2];
  33. }
  34. // process tets
  35. if(out.tetrahedronlist == NULL)
  36. {
  37. cerr<<"^tetgenio_to_tetmesh Error: tet list is NULL\n"<<endl;
  38. return false;
  39. }
  40. // When would this not be 4?
  41. assert(out.numberofcorners == 4);
  42. T.resize(out.numberoftetrahedra,vector<int>(out.numberofcorners));
  43. int min_index = 1e7;
  44. int max_index = -1e7;
  45. // loop over tetrahedra
  46. for(int i = 0; i < out.numberoftetrahedra; i++)
  47. {
  48. for(int j = 0; j<out.numberofcorners; j++)
  49. {
  50. int index = out.tetrahedronlist[i * out.numberofcorners + j];
  51. T[i][j] = index;
  52. min_index = (min_index > index ? index : min_index);
  53. max_index = (max_index < index ? index : max_index);
  54. }
  55. }
  56. assert(min_index >= 0);
  57. assert(max_index >= 0);
  58. assert(max_index < (int)V.size());
  59. cout<<out.numberoftrifaces<<endl;
  60. // When would this not be 4?
  61. F.clear();
  62. // loop over tetrahedra
  63. for(int i = 0; i < out.numberoftrifaces; i++)
  64. {
  65. if(out.trifacemarkerlist[i]>=0)
  66. {
  67. vector<int> face(3);
  68. for(int j = 0; j<3; j++)
  69. {
  70. face[j] = out.trifacelist[i * 3 + j];
  71. }
  72. F.push_back(face);
  73. }
  74. }
  75. return true;
  76. }
  77. IGL_INLINE bool igl::copyleft::tetgen::tetgenio_to_tetmesh(
  78. const tetgenio & out,
  79. std::vector<std::vector<REAL > > & V,
  80. std::vector<std::vector<int> > & T)
  81. {
  82. std::vector<std::vector<int> > F;
  83. return tetgenio_to_tetmesh(out,V,T,F);
  84. }
  85. template <typename DerivedV, typename DerivedT, typename DerivedF>
  86. IGL_INLINE bool igl::copyleft::tetgen::tetgenio_to_tetmesh(
  87. const tetgenio & out,
  88. Eigen::PlainObjectBase<DerivedV>& V,
  89. Eigen::PlainObjectBase<DerivedT>& T,
  90. Eigen::PlainObjectBase<DerivedF>& F)
  91. {
  92. using namespace igl;
  93. using namespace std;
  94. vector<vector<REAL> > vV;
  95. vector<vector<int> > vT;
  96. bool success = tetgenio_to_tetmesh(out,vV,vT);
  97. if(!success)
  98. {
  99. return false;
  100. }
  101. bool V_rect = list_to_matrix(vV,V);
  102. if(!V_rect)
  103. {
  104. // igl::list_to_matrix(vV,V) already printed error message to std err
  105. return false;
  106. }
  107. bool T_rect = list_to_matrix(vT,T);
  108. if(!T_rect)
  109. {
  110. // igl::list_to_matrix(vT,T) already printed error message to std err
  111. return false;
  112. }
  113. return true;
  114. }
  115. template <typename DerivedV, typename DerivedT>
  116. IGL_INLINE bool igl::copyleft::tetgen::tetgenio_to_tetmesh(
  117. const tetgenio & out,
  118. Eigen::PlainObjectBase<DerivedV>& V,
  119. Eigen::PlainObjectBase<DerivedT>& T)
  120. {
  121. Eigen::PlainObjectBase<DerivedT> F;
  122. return tetgenio_to_tetmesh(out,V,T,F);
  123. }
  124. #ifdef IGL_STATIC_LIBRARY
  125. // Explicit template specialization
  126. 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> >&);
  127. #endif