boundary_faces.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include "boundary_faces.h"
  2. #include "face_occurences.h"
  3. // IGL includes
  4. #include "sort.h"
  5. // STL includes
  6. #include <map>
  7. #include <iostream>
  8. template <typename IntegerT, typename IntegerF>
  9. IGL_INLINE void igl::boundary_faces(
  10. const std::vector<std::vector<IntegerT> > & T,
  11. std::vector<std::vector<IntegerF> > & F)
  12. {
  13. using namespace std;
  14. using namespace igl;
  15. if(T.size() == 0)
  16. {
  17. F.clear();
  18. return;
  19. }
  20. int simplex_size = T[0].size();
  21. // Get a list of all faces
  22. vector<vector<IntegerF> > allF(
  23. T.size()*simplex_size,
  24. vector<IntegerF>(simplex_size-1));
  25. // Gather faces, loop over tets
  26. for(int i = 0; i< (int)T.size();i++)
  27. {
  28. assert(T[i].size() == simplex_size);
  29. switch(simplex_size)
  30. {
  31. case 4:
  32. // get face in correct order
  33. allF[i*simplex_size+0][0] = T[i][1];
  34. allF[i*simplex_size+0][1] = T[i][3];
  35. allF[i*simplex_size+0][2] = T[i][2];
  36. // get face in correct order
  37. allF[i*simplex_size+1][0] = T[i][0];
  38. allF[i*simplex_size+1][1] = T[i][2];
  39. allF[i*simplex_size+1][2] = T[i][3];
  40. // get face in correct order
  41. allF[i*simplex_size+2][0] = T[i][0];
  42. allF[i*simplex_size+2][1] = T[i][3];
  43. allF[i*simplex_size+2][2] = T[i][1];
  44. // get face in correct order
  45. allF[i*simplex_size+3][0] = T[i][0];
  46. allF[i*simplex_size+3][1] = T[i][1];
  47. allF[i*simplex_size+3][2] = T[i][2];
  48. break;
  49. case 3:
  50. allF[i*simplex_size+0][0] = T[i][0];
  51. allF[i*simplex_size+0][1] = T[i][1];
  52. allF[i*simplex_size+1][0] = T[i][1];
  53. allF[i*simplex_size+1][1] = T[i][2];
  54. allF[i*simplex_size+2][0] = T[i][2];
  55. allF[i*simplex_size+2][1] = T[i][0];
  56. break;
  57. }
  58. }
  59. // Counts
  60. vector<int> C;
  61. face_occurences(allF,C);
  62. // Q: Why not just count the number of ones?
  63. int twos = (int) count(C.begin(),C.end(),2);
  64. // Resize output to fit number of ones
  65. F.resize(allF.size() - twos);
  66. int k = 0;
  67. for(int i = 0;i< (int)allF.size();i++)
  68. {
  69. if(C[i] == 1)
  70. {
  71. assert(k<(int)F.size());
  72. F[k] = allF[i];
  73. k++;
  74. }
  75. }
  76. }
  77. #ifndef IGL_NO_EIGEN
  78. #include "list_to_matrix.h"
  79. #include "matrix_to_list.h"
  80. template <typename DerivedT, typename DerivedF>
  81. IGL_INLINE void igl::boundary_faces(
  82. const Eigen::PlainObjectBase<DerivedT>& T,
  83. Eigen::PlainObjectBase<DerivedF>& F)
  84. {
  85. assert(T.cols() == 0 || T.cols() == 4 || T.cols() == 3);
  86. using namespace std;
  87. using namespace Eigen;
  88. using namespace igl;
  89. // Cop out: use vector of vectors version
  90. vector<vector<typename Eigen::PlainObjectBase<DerivedT>::Scalar> > vT;
  91. matrix_to_list(T,vT);
  92. vector<vector<typename Eigen::PlainObjectBase<DerivedF>::Scalar> > vF;
  93. boundary_faces(vT,vF);
  94. list_to_matrix(vF,F);
  95. }
  96. template <typename DerivedT, typename DerivedF>
  97. IGL_INLINE Eigen::PlainObjectBase<DerivedF> igl::boundary_faces(
  98. const Eigen::PlainObjectBase<DerivedT>& T)
  99. {
  100. Eigen::Matrix<typename DerivedF::Scalar, Eigen::Dynamic, Eigen::Dynamic> F;
  101. igl::boundary_faces(T,F);
  102. return F;
  103. }
  104. #endif
  105. #ifndef IGL_HEADER_ONLY
  106. // Explicit template specialization
  107. template void igl::boundary_faces<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  108. template void igl::boundary_faces<int, int>(std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > const&, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >&);
  109. template Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > igl::boundary_faces(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&);
  110. #endif