slice.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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_SLICE_H
  9. #define IGL_SLICE_H
  10. #include "igl_inline.h"
  11. #include <Eigen/Sparse>
  12. #include <vector>
  13. namespace igl
  14. {
  15. /// Act like the matlab X(row_indices,col_indices) operator, where
  16. /// row_indices, col_indices are non-negative integer indices.
  17. ///
  18. /// Inputs:
  19. /// X m by n matrix
  20. /// R list of row indices
  21. /// C list of column indices
  22. /// Output:
  23. /// Y #R by #C matrix
  24. ///
  25. /// \see slice_mask, slice_into
  26. ///
  27. /// \note See also Eigen's unaryExpr https://stackoverflow.com/a/49411587/148668
  28. template <
  29. typename TX,
  30. typename TY,
  31. typename DerivedR,
  32. typename DerivedC>
  33. IGL_INLINE void slice(
  34. const Eigen::SparseMatrix<TX>& X,
  35. const Eigen::DenseBase<DerivedR> & R,
  36. const Eigen::DenseBase<DerivedC> & C,
  37. Eigen::SparseMatrix<TY>& Y);
  38. /// \overload
  39. template <
  40. typename DerivedX,
  41. typename DerivedR,
  42. typename DerivedC,
  43. typename DerivedY>
  44. IGL_INLINE void slice(
  45. const Eigen::DenseBase<DerivedX> & X,
  46. const Eigen::DenseBase<DerivedR> & R,
  47. const Eigen::DenseBase<DerivedC> & C,
  48. Eigen::PlainObjectBase<DerivedY> & Y);
  49. /// \overload
  50. /// \brief Wrapper to only slice in one direction
  51. ///
  52. /// @param[in] dim dimension to slice in 1 or 2, dim=1 --> X(R,:), dim=2 --> X(:,R)
  53. ///
  54. /// \note For now this is just a cheap wrapper.
  55. template <
  56. typename MatX,
  57. typename DerivedR,
  58. typename MatY>
  59. IGL_INLINE void slice(
  60. const MatX& X,
  61. const Eigen::DenseBase<DerivedR> & R,
  62. const int dim,
  63. MatY& Y);
  64. /// \overload
  65. /// \brief Vector version
  66. template <typename DerivedX, typename DerivedY, typename DerivedR>
  67. IGL_INLINE void slice(
  68. const Eigen::DenseBase<DerivedX> & X,
  69. const Eigen::DenseBase<DerivedR> & R,
  70. Eigen::PlainObjectBase<DerivedY> & Y);
  71. /// \overload
  72. /// \brief VectorXi Y = slice(X,R);
  73. /// This templating is bad because the return type might not have the same
  74. /// size as `DerivedX`. This will probably only work if DerivedX has Dynamic
  75. /// as it's non-trivial sizes or if the number of rows in R happens to equal
  76. /// the number of rows in `DerivedX`.
  77. template <typename DerivedX, typename DerivedR>
  78. IGL_INLINE DerivedX slice(
  79. const Eigen::DenseBase<DerivedX> & X,
  80. const Eigen::DenseBase<DerivedR> & R);
  81. /// \overload
  82. template <typename DerivedX, typename DerivedR>
  83. IGL_INLINE DerivedX slice(
  84. const Eigen::DenseBase<DerivedX>& X,
  85. const Eigen::DenseBase<DerivedR> & R,
  86. const int dim);
  87. /// \overload
  88. template< class T >
  89. IGL_INLINE void slice(
  90. const std::vector<T> & X,
  91. std::vector<size_t> const & R,
  92. std::vector<T> & Y);
  93. }
  94. #ifndef IGL_STATIC_LIBRARY
  95. # include "slice.cpp"
  96. #endif
  97. #endif