find.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #ifndef IGL_FIND_H
  9. #define IGL_FIND_H
  10. #include "igl_inline.h"
  11. #define EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET
  12. #include <Eigen/Dense>
  13. #include <Eigen/Sparse>
  14. #include <vector>
  15. namespace igl
  16. {
  17. /// Find the non-zero entries and there respective indices in a sparse matrix.
  18. /// Like matlab's [I,J,V] = find(X)
  19. ///
  20. /// @tparam T should be a eigen sparse matrix primitive type like int or double
  21. /// @param[in] X m by n matrix whose entries are to be found
  22. /// @param[out] I nnz vector of row indices of non zeros entries in X
  23. /// @param[out] J nnz vector of column indices of non zeros entries in X
  24. /// @param[out] V nnz vector of type T non-zeros entries in X
  25. ///
  26. template <
  27. typename T,
  28. typename DerivedI,
  29. typename DerivedJ,
  30. typename DerivedV>
  31. IGL_INLINE void find(
  32. const Eigen::SparseMatrix<T>& X,
  33. Eigen::DenseBase<DerivedI> & I,
  34. Eigen::DenseBase<DerivedJ> & J,
  35. Eigen::DenseBase<DerivedV> & V);
  36. /// \overload
  37. template < typename T>
  38. IGL_INLINE std::vector<Eigen::Triplet<T> > find(
  39. const Eigen::SparseMatrix<T>& X);
  40. /// \overload
  41. template <
  42. typename DerivedX,
  43. typename DerivedI,
  44. typename DerivedJ,
  45. typename DerivedV>
  46. IGL_INLINE void find(
  47. const Eigen::DenseBase<DerivedX>& X,
  48. Eigen::PlainObjectBase<DerivedI> & I,
  49. Eigen::PlainObjectBase<DerivedJ> & J,
  50. Eigen::PlainObjectBase<DerivedV> & V);
  51. /// \overload
  52. ///
  53. /// @param[out] I nnz vector of indices into vectorization of X
  54. template <
  55. typename DerivedX,
  56. typename DerivedI>
  57. IGL_INLINE void find(
  58. const Eigen::DenseBase<DerivedX>& X,
  59. Eigen::PlainObjectBase<DerivedI> & I);
  60. /// \overload
  61. template <typename T>
  62. IGL_INLINE void find(
  63. const Eigen::SparseVector<T>& X,
  64. Eigen::Matrix<int,Eigen::Dynamic,1> & I,
  65. Eigen::Matrix<T,Eigen::Dynamic,1> & V);
  66. /// \overload
  67. /// \brief This overload facilitates the use of bool masks for Eigen slicing
  68. /// @tparam RowsAtCompileTime number of rows in M at compile time
  69. /// @return indices of true entries in M
  70. ///
  71. /// ##### Example
  72. ///
  73. /// ```cpp
  74. /// igl::slice_mask(A,igl::find(M),igl::find(N),B);
  75. /// // Is the same as
  76. /// B = A(igl::find(M),igl::find(N));
  77. /// ```
  78. ///
  79. /// \see slice_mask
  80. template <int RowsAtCompileTime, int MaxRowsAtCompileTime>
  81. IGL_INLINE std::vector<int> find(
  82. const Eigen::Array<bool,RowsAtCompileTime,1,0,MaxRowsAtCompileTime,1> & M);
  83. }
  84. #ifndef IGL_STATIC_LIBRARY
  85. # include "find.cpp"
  86. #endif
  87. #endif