on_boundary.cpp 4.5 KB

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