bfs_orient.cpp 2.8 KB

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