bfs_orient.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #pragma omp parallel for
  29. for(int c = 0;c<num_cc;c++)
  30. {
  31. queue<int> Q;
  32. // find first member of patch c
  33. for(int f = 0;f<FF.rows();f++)
  34. {
  35. if(C(f) == c)
  36. {
  37. Q.push(f);
  38. break;
  39. }
  40. }
  41. assert(!Q.empty());
  42. while(!Q.empty())
  43. {
  44. const int f = Q.front();
  45. Q.pop();
  46. if(seen(f) > 0)
  47. {
  48. continue;
  49. }
  50. seen(f)++;
  51. // loop over neighbors of f
  52. for(typename SparseMatrix<int>::InnerIterator it (A,f); it; ++it)
  53. {
  54. // might be some lingering zeros, and skip self-adjacency
  55. if(it.value() != 0 && it.row() != f)
  56. {
  57. const int n = it.row();
  58. assert(n != f);
  59. // loop over edges of f
  60. for(int efi = 0;efi<3;efi++)
  61. {
  62. // efi'th edge of face f
  63. Vector2i ef(FF(f,ES[efi][0]),FF(f,ES[efi][1]));
  64. // loop over edges of n
  65. for(int eni = 0;eni<3;eni++)
  66. {
  67. // eni'th edge of face n
  68. Vector2i en(FF(n,ES[eni][0]),FF(n,ES[eni][1]));
  69. // Match (half-edges go same direction)
  70. if(ef(0) == en(0) && ef(1) == en(1))
  71. {
  72. // flip face n
  73. FF.row(n) = FF.row(n).reverse().eval();
  74. }
  75. }
  76. }
  77. // add neighbor to queue
  78. Q.push(n);
  79. }
  80. }
  81. }
  82. }
  83. // make sure flip is OK if &FF = &F
  84. }
  85. #ifndef IGL_HEADER_ONLY
  86. // Explicit template specialization
  87. 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> >&);
  88. #endif