manifold_patches.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include "manifold_patches.h"
  2. #include "components.h"
  3. #include <igl/sort.h>
  4. #include <igl/unique.h>
  5. #include <igl/matlab_format.h>
  6. #include <vector>
  7. #include <iostream>
  8. template <typename DerivedF, typename DerivedC, typename AScalar>
  9. void igl::manifold_patches(
  10. const Eigen::PlainObjectBase<DerivedF> & F,
  11. Eigen::PlainObjectBase<DerivedC> & C,
  12. Eigen::SparseMatrix<AScalar> & A)
  13. {
  14. using namespace Eigen;
  15. using namespace std;
  16. // simplex size
  17. assert(F.cols() == 3);
  18. // List of all "half"-edges: 3*#F by 2
  19. Matrix<typename DerivedF::Scalar, Dynamic, 2> allE,sortallE,uE;
  20. allE.resize(F.rows()*3,2);
  21. Matrix<int,Dynamic,2> IX;
  22. VectorXi IA,IC;
  23. allE.block(0*F.rows(),0,F.rows(),1) = F.col(1);
  24. allE.block(0*F.rows(),1,F.rows(),1) = F.col(2);
  25. allE.block(1*F.rows(),0,F.rows(),1) = F.col(2);
  26. allE.block(1*F.rows(),1,F.rows(),1) = F.col(0);
  27. allE.block(2*F.rows(),0,F.rows(),1) = F.col(0);
  28. allE.block(2*F.rows(),1,F.rows(),1) = F.col(1);
  29. // Sort each row
  30. sort(allE,2,true,sortallE,IX);
  31. //IC(i) tells us where to find sortallE(i,:) in uE:
  32. // so that sortallE(i,:) = uE(IC(i),:)
  33. unique_rows(sortallE,uE,IA,IC);
  34. // uE2FT(e,f) = 1 means face f is adjacent to unique edge e
  35. vector<Triplet<AScalar> > uE2FTijv(IC.rows());
  36. for(int e = 0;e<IC.rows();e++)
  37. {
  38. uE2FTijv[e] = Triplet<AScalar>(e%F.rows(),IC(e),1);
  39. }
  40. SparseMatrix<AScalar> uE2FT(F.rows(),uE.rows());
  41. uE2FT.setFromTriplets(uE2FTijv.begin(),uE2FTijv.end());
  42. // kill non-manifold edges
  43. for(int j=0; j<(int)uE2FT.outerSize();j++)
  44. {
  45. int degree = 0;
  46. for(typename SparseMatrix<AScalar>::InnerIterator it (uE2FT,j); it; ++it)
  47. {
  48. degree++;
  49. }
  50. // Iterate over inside
  51. if(degree > 2)
  52. {
  53. for(typename SparseMatrix<AScalar>::InnerIterator it (uE2FT,j); it; ++it)
  54. {
  55. uE2FT.coeffRef(it.row(),it.col()) = 0;
  56. }
  57. }
  58. }
  59. // Face-face Adjacency matrix
  60. SparseMatrix<AScalar> uE2F;
  61. uE2F = uE2FT.transpose().eval();
  62. A = uE2FT*uE2F;
  63. // All ones
  64. for(int j=0; j<A.outerSize();j++)
  65. {
  66. // Iterate over inside
  67. for(typename SparseMatrix<AScalar>::InnerIterator it (A,j); it; ++it)
  68. {
  69. if(it.value() > 1)
  70. {
  71. A.coeffRef(it.row(),it.col()) = 1;
  72. }
  73. }
  74. }
  75. //% Connected components are patches
  76. //%C = components(A); % alternative to graphconncomp from matlab_bgl
  77. //[~,C] = graphconncomp(A);
  78. // graph connected components using boost
  79. components(A,C);
  80. }
  81. #ifndef IGL_HEADER_ONLY
  82. // Explicit template specialization
  83. template void igl::manifold_patches<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, int>(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::SparseMatrix<int, 0, int>&);
  84. #endif