adjacency_matrix.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 "adjacency_matrix.h"
  9. #include "verbose.h"
  10. #include <vector>
  11. template <typename T>
  12. IGL_INLINE void igl::adjacency_matrix(
  13. const Eigen::MatrixXi & F,
  14. Eigen::SparseMatrix<T>& A)
  15. {
  16. using namespace std;
  17. using namespace Eigen;
  18. typedef Triplet<T> IJV;
  19. vector<IJV > ijv;
  20. ijv.reserve(F.size()*2);
  21. // Loop over faces
  22. for(int i = 0;i<F.rows();i++)
  23. {
  24. // Loop over this face
  25. for(int j = 0;j<F.cols();j++)
  26. {
  27. // Get indices of edge: s --> d
  28. int s = F(i,j);
  29. int d = F(i,(j+1)%F.cols());
  30. ijv.push_back(IJV(s,d,1));
  31. ijv.push_back(IJV(d,s,1));
  32. }
  33. }
  34. const int 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. #ifdef IGL_STATIC_LIBRARY
  59. // Explicit template specialization
  60. template void igl::adjacency_matrix<int>(Eigen::Matrix<int, -1, -1, 0, -1, -1> const&, Eigen::SparseMatrix<int, 0, int>&);
  61. template void igl::adjacency_matrix<double>(Eigen::Matrix<int, -1, -1, 0, -1, -1> const&, Eigen::SparseMatrix<double, 0, int>&);
  62. #endif