adjacency_matrix.cpp 3.8 KB

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