on_boundary.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 "on_boundary.h"
  9. // IGL includes
  10. #include "sort.h"
  11. #include "face_occurrences.h"
  12. // STL includes
  13. template <typename IntegerT>
  14. IGL_INLINE void igl::on_boundary(
  15. const std::vector<std::vector<IntegerT> > & T,
  16. std::vector<bool> & I,
  17. std::vector<std::vector<bool> > & C)
  18. {
  19. using namespace std;
  20. if(T.empty())
  21. {
  22. I.clear();
  23. C.clear();
  24. return;
  25. }
  26. switch(T[0].size())
  27. {
  28. case 3:
  29. {
  30. // Get a list of all faces
  31. vector<vector<IntegerT> > F(T.size()*3,vector<IntegerT>(2));
  32. // Gather faces, loop over tets
  33. for(int i = 0; i< (int)T.size();i++)
  34. {
  35. assert(T[i].size() == 3);
  36. // get face in correct order
  37. F[i*3+0][0] = T[i][1];
  38. F[i*3+0][1] = T[i][2];
  39. F[i*3+1][0] = T[i][2];
  40. F[i*3+1][1] = T[i][0];
  41. F[i*3+2][0] = T[i][0];
  42. F[i*3+2][1] = T[i][1];
  43. }
  44. // Counts
  45. vector<int> FC;
  46. face_occurrences(F,FC);
  47. C.resize(T.size(),vector<bool>(3));
  48. I.resize(T.size(),false);
  49. for(int i = 0; i< (int)T.size();i++)
  50. {
  51. for(int j = 0;j<3;j++)
  52. {
  53. assert(FC[i*3+j] == 2 || FC[i*3+j] == 1);
  54. C[i][j] = FC[i*3+j]==1;
  55. // if any are on boundary set to true
  56. I[i] = I[i] || C[i][j];
  57. }
  58. }
  59. return;
  60. }
  61. case 4:
  62. {
  63. // Get a list of all faces
  64. vector<vector<IntegerT> > F(T.size()*4,vector<IntegerT>(3));
  65. // Gather faces, loop over tets
  66. for(int i = 0; i< (int)T.size();i++)
  67. {
  68. assert(T[i].size() == 4);
  69. // get face in correct order
  70. F[i*4+0][0] = T[i][1];
  71. F[i*4+0][1] = T[i][3];
  72. F[i*4+0][2] = T[i][2];
  73. // get face in correct order
  74. F[i*4+1][0] = T[i][0];
  75. F[i*4+1][1] = T[i][2];
  76. F[i*4+1][2] = T[i][3];
  77. // get face in correct order
  78. F[i*4+2][0] = T[i][0];
  79. F[i*4+2][1] = T[i][3];
  80. F[i*4+2][2] = T[i][1];
  81. // get face in correct order
  82. F[i*4+3][0] = T[i][0];
  83. F[i*4+3][1] = T[i][1];
  84. F[i*4+3][2] = T[i][2];
  85. }
  86. // Counts
  87. vector<int> FC;
  88. face_occurrences(F,FC);
  89. C.resize(T.size(),vector<bool>(4));
  90. I.resize(T.size(),false);
  91. for(int i = 0; i< (int)T.size();i++)
  92. {
  93. for(int j = 0;j<4;j++)
  94. {
  95. assert(FC[i*4+j] == 2 || FC[i*4+j] == 1);
  96. C[i][j] = FC[i*4+j]==1;
  97. // if any are on boundary set to true
  98. I[i] = I[i] || C[i][j];
  99. }
  100. }
  101. return;
  102. }
  103. }
  104. }
  105. #include "list_to_matrix.h"
  106. #include "matrix_to_list.h"
  107. template <typename DerivedT, typename DerivedI, typename DerivedC>
  108. IGL_INLINE void igl::on_boundary(
  109. const Eigen::MatrixBase<DerivedT>& T,
  110. Eigen::PlainObjectBase<DerivedI>& I,
  111. Eigen::PlainObjectBase<DerivedC>& C)
  112. {
  113. assert(T.cols() == 0 || T.cols() == 4 || T.cols() == 3);
  114. using namespace std;
  115. using namespace Eigen;
  116. // Cop out: use vector of vectors version
  117. vector<vector<typename DerivedT::Scalar> > vT;
  118. matrix_to_list(T,vT);
  119. vector<bool> vI;
  120. vector<vector<bool> > vC;
  121. on_boundary(vT,vI,vC);
  122. list_to_matrix(vI,I);
  123. list_to_matrix(vC,C);
  124. }
  125. #ifdef IGL_STATIC_LIBRARY
  126. // Explicit template instantiation
  127. // generated by autoexplicit.sh
  128. template void igl::on_boundary<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Array<bool, -1, 1, 0, -1, 1>, Eigen::Array<bool, -1, 3, 0, -1, 3> >(Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Array<bool, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Array<bool, -1, 3, 0, -1, 3> >&);
  129. template void igl::on_boundary<Eigen::Matrix<int, -1, -1, 0, -1, -1>, 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> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  130. template void igl::on_boundary<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Array<bool, -1, 1, 0, -1, 1>, Eigen::Array<bool, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Array<bool, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Array<bool, -1, -1, 0, -1, -1> >&);
  131. #endif