adjacency_matrix.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. // Bug in unsupported/Eigen/SparseExtra needs iostream first
  11. #include <iostream>
  12. #include <unsupported/Eigen/SparseExtra>
  13. #warning "old includes"
  14. #include <vector>
  15. template <typename T>
  16. IGL_INLINE void igl::adjacency_matrix(
  17. const Eigen::MatrixXi & F,
  18. Eigen::SparseMatrix<T>& A)
  19. {
  20. using namespace std;
  21. Eigen::DynamicSparseMatrix<T, Eigen::RowMajor>
  22. dyn_A(F.maxCoeff()+1, F.maxCoeff()+1);
  23. switch(F.cols())
  24. {
  25. case 3:
  26. dyn_A.reserve(8*(F.maxCoeff()+1));
  27. break;
  28. case 4:
  29. dyn_A.reserve(26*(F.maxCoeff()+1));
  30. break;
  31. }
  32. //typedef Triplet<T> IJV;
  33. //vector<IJV > ijv;
  34. //ijv.reserve(F.size()*2);
  35. // Loop over faces
  36. for(int i = 0;i<F.rows();i++)
  37. {
  38. // Loop over this face
  39. for(int j = 0;j<F.cols();j++)
  40. {
  41. // Get indices of edge: s --> d
  42. int s = F(i,j);
  43. int d = F(i,(j+1)%F.cols());
  44. dyn_A.coeffRef(s, d) = 1;
  45. dyn_A.coeffRef(d, s) = 1;
  46. }
  47. }
  48. A = Eigen::SparseMatrix<T>(dyn_A);
  49. }
  50. #ifndef IGL_HEADER_ONLY
  51. // Explicit template specialization
  52. template void igl::adjacency_matrix<int>(Eigen::Matrix<int, -1, -1, 0, -1, -1> const&, Eigen::SparseMatrix<int, 0, int>&);
  53. #endif