print_ijv.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #ifndef IGL_PRINT_IJV_H
  2. #define IGL_PRINT_IJV_H
  3. #define EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET
  4. #include <Eigen/Dense>
  5. #include <Eigen/Sparse>
  6. namespace igl
  7. {
  8. // Prints a 3 column matrix representing [I,J,V] = find(X). That is, each
  9. // row is the row index, column index and value for each non zero entry. Each
  10. // row is printed on a new line
  11. //
  12. // Templates:
  13. // T should be a eigen sparse matrix primitive type like int or double
  14. // Input:
  15. // X m by n matrix whose entries are to be sorted
  16. // offset optional offset for I and J indices {0}
  17. template <typename T>
  18. inline void print_ijv(
  19. const Eigen::SparseMatrix<T>& X,
  20. const int offset=0);
  21. }
  22. // Implementation
  23. #include "find.h"
  24. #include <iostream>
  25. template <typename T>
  26. inline void igl::print_ijv(
  27. const Eigen::SparseMatrix<T>& X,
  28. const int offset)
  29. {
  30. Eigen::Matrix<int,Eigen::Dynamic,1> I;
  31. Eigen::Matrix<int,Eigen::Dynamic,1> J;
  32. Eigen::Matrix<T,Eigen::Dynamic,1> V;
  33. igl::find(X,I,J,V);
  34. // Concatenate I,J,V
  35. Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> IJV(I.size(),3);
  36. IJV.col(0) = I.cast<T>();
  37. IJV.col(1) = J.cast<T>();
  38. IJV.col(2) = V;
  39. // Offset
  40. if(offset != 0)
  41. {
  42. IJV.col(0).array() += offset;
  43. IJV.col(1).array() += offset;
  44. }
  45. std::cout<<IJV;
  46. }
  47. #endif