tetgenio_to_tetmesh.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 <igl/list_to_matrix.h>
  11. // STL includes
  12. #include <iostream>
  13. IGL_INLINE bool igl::tetgenio_to_tetmesh(
  14. const tetgenio & out,
  15. std::vector<std::vector<REAL > > & V,
  16. std::vector<std::vector<int> > & T)
  17. {
  18. using namespace std;
  19. // process points
  20. if(out.pointlist == NULL)
  21. {
  22. cerr<<"^tetgenio_to_tetmesh Error: point list is NULL\n"<<endl;
  23. return false;
  24. }
  25. V.resize(out.numberofpoints,vector<REAL>(3));
  26. // loop over points
  27. for(int i = 0;i < out.numberofpoints; i++)
  28. {
  29. V[i][0] = out.pointlist[i*3+0];
  30. V[i][1] = out.pointlist[i*3+1];
  31. V[i][2] = out.pointlist[i*3+2];
  32. }
  33. // process tets
  34. if(out.tetrahedronlist == NULL)
  35. {
  36. cerr<<"^tetgenio_to_tetmesh Error: tet list is NULL\n"<<endl;
  37. return false;
  38. }
  39. // When would this not be 4?
  40. assert(out.numberofcorners == 4);
  41. T.resize(out.numberoftetrahedra,vector<int>(out.numberofcorners));
  42. int min_index = 1e7;
  43. int max_index = -1e7;
  44. // loop over tetrahedra
  45. for(int i = 0; i < out.numberoftetrahedra; i++)
  46. {
  47. for(int j = 0; j<out.numberofcorners; j++)
  48. {
  49. int index = out.tetrahedronlist[i * out.numberofcorners + j];
  50. T[i][j] = index;
  51. min_index = (min_index > index ? index : min_index);
  52. max_index = (max_index < index ? index : max_index);
  53. }
  54. }
  55. assert(min_index >= 0);
  56. assert(max_index >= 0);
  57. assert(max_index < (int)V.size());
  58. return true;
  59. }
  60. template <typename DerivedV, typename DerivedT>
  61. IGL_INLINE bool igl::tetgenio_to_tetmesh(
  62. const tetgenio & out,
  63. Eigen::PlainObjectBase<DerivedV>& V,
  64. Eigen::PlainObjectBase<DerivedT>& T)
  65. {
  66. using namespace igl;
  67. using namespace std;
  68. vector<vector<REAL> > vV;
  69. vector<vector<int> > vT;
  70. bool success = tetgenio_to_tetmesh(out,vV,vT);
  71. if(!success)
  72. {
  73. return false;
  74. }
  75. bool V_rect = list_to_matrix(vV,V);
  76. if(!V_rect)
  77. {
  78. // igl::list_to_matrix(vV,V) already printed error message to std err
  79. return false;
  80. }
  81. bool T_rect = list_to_matrix(vT,T);
  82. if(!T_rect)
  83. {
  84. // igl::list_to_matrix(vT,T) already printed error message to std err
  85. return false;
  86. }
  87. return true;
  88. }
  89. #ifndef IGL_HEADER_ONLY
  90. // Explicit template specialization
  91. template bool igl::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> >&);
  92. #endif