adjacency_matrix.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 "adjacency_matrix.h"
  9. #include "verbose.h"
  10. #include <vector>
  11. template <typename DerivedF, typename T>
  12. IGL_INLINE void igl::adjacency_matrix(
  13. const Eigen::MatrixBase<DerivedF> & F,
  14. Eigen::SparseMatrix<T>& A)
  15. {
  16. typedef typename DerivedF::Scalar Index;
  17. typedef Eigen::Triplet<T> IJV;
  18. std::vector<IJV > ijv;
  19. ijv.reserve(F.size()*2);
  20. // Loop over **simplex** (i.e., **not quad**)
  21. for(int i = 0;i<F.rows();i++)
  22. {
  23. // Loop over this **simplex**
  24. for(int j = 0;j<F.cols();j++)
  25. for(int k = j+1;k<F.cols();k++)
  26. {
  27. // Get indices of edge: s --> d
  28. Index s = F(i,j);
  29. Index d = F(i,k);
  30. ijv.push_back(IJV(s,d,1));
  31. ijv.push_back(IJV(d,s,1));
  32. }
  33. }
  34. const Index n = F.maxCoeff()+1;
  35. A.resize(n,n);
  36. switch(F.cols())
  37. {
  38. case 3:
  39. A.reserve(6*(F.maxCoeff()+1));
  40. break;
  41. case 4:
  42. A.reserve(26*(F.maxCoeff()+1));
  43. break;
  44. }
  45. A.setFromTriplets(ijv.begin(),ijv.end());
  46. // Force all non-zeros to be one
  47. // Iterate over outside
  48. for(int k=0; k<A.outerSize(); ++k)
  49. {
  50. // Iterate over inside
  51. for(typename Eigen::SparseMatrix<T>::InnerIterator it (A,k); it; ++it)
  52. {
  53. assert(it.value() != 0);
  54. A.coeffRef(it.row(),it.col()) = 1;
  55. }
  56. }
  57. }
  58. template <typename DerivedI, typename DerivedC, typename T>
  59. IGL_INLINE void igl::adjacency_matrix(
  60. const Eigen::MatrixBase<DerivedI> & I,
  61. const Eigen::MatrixBase<DerivedC> & C,
  62. Eigen::SparseMatrix<T>& A)
  63. {
  64. typedef Eigen::Triplet<T> IJV;
  65. std::vector<IJV > ijv;
  66. ijv.reserve(C(C.size()-1)*2);
  67. typedef typename DerivedI::Scalar Index;
  68. const Index n = I.maxCoeff()+1;
  69. {
  70. // loop over polygons
  71. for(Index p = 0;p<C.size()-1;p++)
  72. {
  73. // number of edges
  74. const Index np = C(p+1)-C(p);
  75. // loop over edges
  76. for(Index c = 0;c<np;c++)
  77. {
  78. const Index i = I(C(p)+c);
  79. const Index j = I(C(p)+((c+1)%np));
  80. ijv.emplace_back(i,j,1);
  81. ijv.emplace_back(j,i,1);
  82. }
  83. }
  84. }
  85. A.resize(n,n);
  86. A.reserve(6*n);
  87. A.setFromTriplets(ijv.begin(),ijv.end());
  88. // Force all non-zeros to be one
  89. // Iterate over outside
  90. for(int k=0; k<A.outerSize(); ++k)
  91. {
  92. // Iterate over inside
  93. for(typename Eigen::SparseMatrix<T>::InnerIterator it (A,k); it; ++it)
  94. {
  95. assert(it.value() != 0);
  96. A.coeffRef(it.row(),it.col()) = 1;
  97. }
  98. }
  99. }
  100. #ifdef IGL_STATIC_LIBRARY
  101. // Explicit template instantiation
  102. template void igl::adjacency_matrix<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::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::SparseMatrix<int, 0, int>& );
  103. // generated by autoexplicit.sh
  104. template void igl::adjacency_matrix<Eigen::Matrix<int, -1, -1, 0, -1, -1>, bool>(Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::SparseMatrix<bool, 0, int>&);
  105. template void igl::adjacency_matrix<Eigen::Matrix<int, -1, -1, 0, -1, -1>, double>(Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::SparseMatrix<double, 0, int>&);
  106. template void igl::adjacency_matrix<Eigen::Matrix<int, -1, -1, 0, -1, -1>, int>(Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::SparseMatrix<int, 0, int>&);
  107. template void igl::adjacency_matrix<Eigen::Matrix<int, -1, 3, 0, -1, 3>, int>(Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::SparseMatrix<int, 0, int>&);
  108. #endif