manifold_patches.cpp 3.1 KB

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