bfs_orient.cpp 2.8 KB

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