boundary_facets.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 "boundary_facets.h"
  9. #include "face_occurrences.h"
  10. // IGL includes
  11. #include "sort.h"
  12. // STL includes
  13. #include <map>
  14. #include <iostream>
  15. #include "list_to_matrix.h"
  16. #include "matrix_to_list.h"
  17. template <typename DerivedT, typename DerivedF>
  18. IGL_INLINE void igl::boundary_facets(
  19. const Eigen::MatrixBase<DerivedT>& T,
  20. Eigen::PlainObjectBase<DerivedF>& F)
  21. {
  22. assert(T.cols() == 0 || T.cols() == 4 || T.cols() == 3);
  23. using namespace std;
  24. using namespace Eigen;
  25. // Cop out: use vector of vectors version
  26. vector<vector<typename DerivedT::Scalar> > vT;
  27. matrix_to_list(T,vT);
  28. vector<vector<typename DerivedF::Scalar> > vF;
  29. boundary_facets(vT,vF);
  30. list_to_matrix(vF,F);
  31. }
  32. template <typename IntegerT, typename IntegerF>
  33. IGL_INLINE void igl::boundary_facets(
  34. const std::vector<std::vector<IntegerT> > & T,
  35. std::vector<std::vector<IntegerF> > & F)
  36. {
  37. // Kept for legacy reasons. Could probably just delete.
  38. using namespace std;
  39. if(T.size() == 0)
  40. {
  41. F.clear();
  42. return;
  43. }
  44. int simplex_size = T[0].size();
  45. // Get a list of all faces
  46. vector<vector<IntegerF> > allF(
  47. T.size()*simplex_size,
  48. vector<IntegerF>(simplex_size-1));
  49. // Gather faces, loop over tets
  50. for(int i = 0; i< (int)T.size();i++)
  51. {
  52. assert((int)T[i].size() == simplex_size);
  53. switch(simplex_size)
  54. {
  55. case 4:
  56. // get face in correct order
  57. allF[i*simplex_size+0][0] = T[i][1];
  58. allF[i*simplex_size+0][1] = T[i][3];
  59. allF[i*simplex_size+0][2] = T[i][2];
  60. // get face in correct order
  61. allF[i*simplex_size+1][0] = T[i][0];
  62. allF[i*simplex_size+1][1] = T[i][2];
  63. allF[i*simplex_size+1][2] = T[i][3];
  64. // get face in correct order
  65. allF[i*simplex_size+2][0] = T[i][0];
  66. allF[i*simplex_size+2][1] = T[i][3];
  67. allF[i*simplex_size+2][2] = T[i][1];
  68. // get face in correct order
  69. allF[i*simplex_size+3][0] = T[i][0];
  70. allF[i*simplex_size+3][1] = T[i][1];
  71. allF[i*simplex_size+3][2] = T[i][2];
  72. break;
  73. case 3:
  74. allF[i*simplex_size+0][0] = T[i][1];
  75. allF[i*simplex_size+0][1] = T[i][2];
  76. allF[i*simplex_size+1][0] = T[i][2];
  77. allF[i*simplex_size+1][1] = T[i][0];
  78. allF[i*simplex_size+2][0] = T[i][0];
  79. allF[i*simplex_size+2][1] = T[i][1];
  80. break;
  81. }
  82. }
  83. // Counts
  84. vector<int> C;
  85. face_occurrences(allF,C);
  86. // Q: Why not just count the number of ones?
  87. // A: because we are including non-manifold edges as boundary edges
  88. int twos = (int) count(C.begin(),C.end(),2);
  89. //int ones = (int) count(C.begin(),C.end(),1);
  90. // Resize output to fit number of ones
  91. F.resize(allF.size() - twos);
  92. //F.resize(ones);
  93. int k = 0;
  94. for(int i = 0;i< (int)allF.size();i++)
  95. {
  96. if(C[i] != 2)
  97. {
  98. assert(k<(int)F.size());
  99. F[k] = allF[i];
  100. k++;
  101. }
  102. }
  103. assert(k==(int)F.size());
  104. //if(k != F.size())
  105. //{
  106. // printf("%d =? %d\n",k,F.size());
  107. //}
  108. }
  109. template <typename DerivedT, typename Ret>
  110. Ret igl::boundary_facets(
  111. const Eigen::MatrixBase<DerivedT>& T)
  112. {
  113. Ret F;
  114. igl::boundary_facets(T,F);
  115. return F;
  116. }
  117. #ifdef IGL_STATIC_LIBRARY
  118. // Explicit template instantiation
  119. // generated by autoexplicit.sh
  120. template void igl::boundary_facets<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<unsigned int, -1, 3, 1, -1, 3> >(Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<unsigned int, -1, 3, 1, -1, 3> >&);
  121. // generated by autoexplicit.sh
  122. template void igl::boundary_facets<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 3, 0, -1, 3> >(Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> >&);
  123. template void igl::boundary_facets<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  124. template void igl::boundary_facets<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> > > >&);
  125. //template Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > igl::boundary_facets(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&);
  126. template Eigen::Matrix<int, -1, -1, 0, -1, -1> igl::boundary_facets<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&);
  127. template void igl::boundary_facets<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 3, 1, -1, 3> >(Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> >&);
  128. #endif