orientable_patches.cpp 4.1 KB

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