slice.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. #include <Eigen/Sparse>
  12. namespace igl
  13. {
  14. // Act like the matlab X(row_indices,col_indices) operator, where
  15. // row_indices, col_indices are non-negative integer indices.
  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. //
  24. // See also: slice_mask
  25. template <typename TX, typename TY>
  26. IGL_INLINE void slice(
  27. const Eigen::SparseMatrix<TX>& X,
  28. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  29. const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
  30. Eigen::SparseMatrix<TY>& 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 MatX, typename MatY>
  38. IGL_INLINE void slice(
  39. const MatX& X,
  40. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  41. const int dim,
  42. MatY& Y);
  43. template <typename DerivedX, typename DerivedY>
  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<DerivedY> & Y);
  49. template <typename DerivedX, typename DerivedY>
  50. IGL_INLINE void slice(
  51. const Eigen::PlainObjectBase<DerivedX> & X,
  52. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  53. Eigen::PlainObjectBase<DerivedY> & 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. #ifndef IGL_STATIC_LIBRARY
  66. # include "slice.cpp"
  67. #endif
  68. #endif