bfs_orient.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "bfs_orient.h"
  2. #include "manifold_patches.h"
  3. #include <Eigen/Sparse>
  4. #include <queue>
  5. template <typename DerivedF, typename DerivedFF, typename DerivedC>
  6. void igl::bfs_orient(
  7. const Eigen::PlainObjectBase<DerivedF> & F,
  8. Eigen::PlainObjectBase<DerivedFF> & FF,
  9. Eigen::PlainObjectBase<DerivedC> & C)
  10. {
  11. using namespace Eigen;
  12. using namespace igl;
  13. using namespace std;
  14. SparseMatrix<int> A;
  15. manifold_patches(F,C,A);
  16. // number of faces
  17. const int m = F.rows();
  18. // number of patches
  19. const int num_cc = C.maxCoeff()+1;
  20. VectorXi seen = VectorXi::Zero(m);
  21. // Edge sets
  22. const int ES[3][2] = {{1,2},{2,0},{0,1}};
  23. if(&FF != &F)
  24. {
  25. FF = F;
  26. }
  27. // loop over patches
  28. for(int c = 0;c<num_cc;c++)
  29. {
  30. queue<int> Q;
  31. // find first member of patch c
  32. for(int f = 0;f<FF.rows();f++)
  33. {
  34. if(C(f) == c)
  35. {
  36. Q.push(f);
  37. break;
  38. }
  39. }
  40. assert(!Q.empty());
  41. while(!Q.empty())
  42. {
  43. const int f = Q.front();
  44. Q.pop();
  45. if(seen(f) > 0)
  46. {
  47. continue;
  48. }
  49. seen(f)++;
  50. // loop over neighbors of f
  51. for(typename SparseMatrix<int>::InnerIterator it (A,f); it; ++it)
  52. {
  53. // might be some lingering zeros, and skip self-adjacency
  54. if(it.value() != 0 && it.row() != f)
  55. {
  56. const int n = it.row();
  57. assert(n != f);
  58. // loop over edges of f
  59. for(int efi = 0;efi<3;efi++)
  60. {
  61. // efi'th edge of face f
  62. Vector2i ef(FF(f,ES[efi][0]),FF(f,ES[efi][1]));
  63. // loop over edges of n
  64. for(int eni = 0;eni<3;eni++)
  65. {
  66. // eni'th edge of face n
  67. Vector2i en(FF(n,ES[eni][0]),FF(n,ES[eni][1]));
  68. // Match (half-edges go same direction)
  69. if(ef(0) == en(0) && ef(1) == en(1))
  70. {
  71. // flip face n
  72. FF.row(n) = FF.row(n).reverse().eval();
  73. }
  74. }
  75. }
  76. // add neighbor to queue
  77. Q.push(n);
  78. }
  79. }
  80. }
  81. }
  82. // make sure flip is OK if &FF = &F
  83. }
  84. #ifndef IGL_HEADER_ONLY
  85. // Explicit template specialization
  86. template void igl::bfs_orient<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::PlainObjectBase<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> >&);
  87. #endif