slice.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. // THIS MAY HAVE BEEN SUPERSEDED BY EIGEN'S select FUNCTION
  16. //
  17. // Act like the matlab X(row_indices,col_indices) operator
  18. //
  19. // Inputs:
  20. // X m by n matrix
  21. // R list of row indices
  22. // C list of column indices
  23. // Output:
  24. // Y #R by #C matrix
  25. template <typename T>
  26. IGL_INLINE void slice(
  27. const Eigen::SparseMatrix<T>& X,
  28. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  29. const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
  30. Eigen::SparseMatrix<T>& Y);
  31. // Wrapper to only slice in one direction
  32. //
  33. // Inputs:
  34. // dim dimension to slice in 1 or 2, dim=1 --> X(R,:), dim=2 --> X(:,R)
  35. //
  36. // Note: For now this is just a cheap wrapper.
  37. template <typename Mat>
  38. IGL_INLINE void slice(
  39. const Mat& X,
  40. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  41. const int dim,
  42. Mat& Y);
  43. template <typename DerivedX>
  44. IGL_INLINE void slice(
  45. const Eigen::PlainObjectBase<DerivedX> & X,
  46. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  47. const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
  48. Eigen::PlainObjectBase<DerivedX> & Y);
  49. template <typename DerivedX>
  50. IGL_INLINE void slice(
  51. const Eigen::PlainObjectBase<DerivedX> & X,
  52. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  53. Eigen::PlainObjectBase<DerivedX> & Y);
  54. // VectorXi Y = slice(X,R);
  55. template <typename DerivedX>
  56. IGL_INLINE Eigen::PlainObjectBase<DerivedX> slice(
  57. const Eigen::PlainObjectBase<DerivedX> & X,
  58. const Eigen::Matrix<int,Eigen::Dynamic,1> & R);
  59. template <typename DerivedX>
  60. IGL_INLINE Eigen::PlainObjectBase<DerivedX> slice(
  61. const Eigen::PlainObjectBase<DerivedX>& X,
  62. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  63. const int dim);
  64. }
  65. #ifdef IGL_HEADER_ONLY
  66. # include "slice.cpp"
  67. #endif
  68. #endif