orientable_patches.cpp 3.6 KB

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