adjacency_matrix.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. const typename DerivedI::Scalar n = I.maxCoeff()+1;
  72. {
  73. // loop over polygons
  74. for(int p = 0;p<C.size()-1;p++)
  75. {
  76. // number of edges
  77. const int np = C(p+1)-C(p);
  78. // loop over edges
  79. for(int c = 0;c<np;c++)
  80. {
  81. const int i = I(C(p)+c);
  82. const int j = I(C(p)+((c+1)%np));
  83. ijv.emplace_back(i,j,1);
  84. ijv.emplace_back(j,i,1);
  85. }
  86. }
  87. }
  88. A.resize(n,n);
  89. A.reserve(6*n);
  90. A.setFromTriplets(ijv.begin(),ijv.end());
  91. // Force all non-zeros to be one
  92. // Iterate over outside
  93. for(int k=0; k<A.outerSize(); ++k)
  94. {
  95. // Iterate over inside
  96. for(typename Eigen::SparseMatrix<T>::InnerIterator it (A,k); it; ++it)
  97. {
  98. assert(it.value() != 0);
  99. A.coeffRef(it.row(),it.col()) = 1;
  100. }
  101. }
  102. }
  103. #ifdef IGL_STATIC_LIBRARY
  104. // Explicit template instantiation
  105. 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>& );
  106. // generated by autoexplicit.sh
  107. 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>&);
  108. 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>&);
  109. 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>&);
  110. 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>&);
  111. #endif