slice.h 2.2 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. #ifndef IGL_SLICE_H
  9. #define IGL_SLICE_H
  10. #include "igl_inline.h"
  11. #define EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET
  12. #include <Eigen/Sparse>
  13. namespace igl
  14. {
  15. // Act like the matlab X(row_indices,col_indices) operator
  16. //
  17. // Inputs:
  18. // X m by n matrix
  19. // R list of row indices
  20. // C list of column indices
  21. // Output:
  22. // Y #R by #C matrix
  23. template <typename T>
  24. IGL_INLINE void slice(
  25. const Eigen::SparseMatrix<T>& X,
  26. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  27. const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
  28. Eigen::SparseMatrix<T>& Y);
  29. // Wrapper to only slice in one direction
  30. //
  31. // Inputs:
  32. // dim dimension to slice in 1 or 2, dim=1 --> X(R,:), dim=2 --> X(:,R)
  33. //
  34. // Note: For now this is just a cheap wrapper.
  35. template <typename Mat>
  36. IGL_INLINE void slice(
  37. const Mat& X,
  38. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  39. const int dim,
  40. Mat& Y);
  41. template <typename DerivedX>
  42. IGL_INLINE void slice(
  43. const Eigen::PlainObjectBase<DerivedX> & X,
  44. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  45. const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
  46. Eigen::PlainObjectBase<DerivedX> & Y);
  47. template <typename DerivedX>
  48. IGL_INLINE void slice(
  49. const Eigen::PlainObjectBase<DerivedX> & X,
  50. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  51. Eigen::PlainObjectBase<DerivedX> & Y);
  52. // VectorXi Y = slice(X,R);
  53. template <typename DerivedX>
  54. IGL_INLINE Eigen::PlainObjectBase<DerivedX> slice(
  55. const Eigen::PlainObjectBase<DerivedX> & X,
  56. const Eigen::Matrix<int,Eigen::Dynamic,1> & R);
  57. template <typename DerivedX>
  58. IGL_INLINE Eigen::PlainObjectBase<DerivedX> slice(
  59. const Eigen::PlainObjectBase<DerivedX>& X,
  60. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  61. const int dim);
  62. }
  63. #ifndef IGL_STATIC_LIBRARY
  64. # include "slice.cpp"
  65. #endif
  66. #endif