slice.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 <
  26. typename TX,
  27. typename TY>
  28. IGL_INLINE void slice(
  29. const Eigen::SparseMatrix<TX>& X,
  30. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  31. const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
  32. Eigen::SparseMatrix<TY>& Y);
  33. // Wrapper to only slice in one direction
  34. //
  35. // Inputs:
  36. // dim dimension to slice in 1 or 2, dim=1 --> X(R,:), dim=2 --> X(:,R)
  37. //
  38. // Note: For now this is just a cheap wrapper.
  39. template <
  40. typename MatX,
  41. typename DerivedR,
  42. typename MatY>
  43. IGL_INLINE void slice(
  44. const MatX& X,
  45. const Eigen::DenseBase<DerivedR> & R,
  46. const int dim,
  47. MatY& Y);
  48. template <
  49. typename DerivedX,
  50. typename DerivedR,
  51. typename DerivedC,
  52. typename DerivedY>
  53. IGL_INLINE void slice(
  54. const Eigen::DenseBase<DerivedX> & X,
  55. const Eigen::DenseBase<DerivedR> & R,
  56. const Eigen::DenseBase<DerivedC> & C,
  57. Eigen::PlainObjectBase<DerivedY> & Y);
  58. template <typename DerivedX, typename DerivedY>
  59. IGL_INLINE void slice(
  60. const Eigen::DenseBase<DerivedX> & X,
  61. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  62. Eigen::PlainObjectBase<DerivedY> & Y);
  63. // VectorXi Y = slice(X,R);
  64. //
  65. // This templating is bad because the return type might not have the same
  66. // size as `DerivedX`. This will probably only work if DerivedX has Dynamic
  67. // as it's non-trivial sizes or if the number of rows in R happens to equal
  68. // the number of rows in `DerivedX`.
  69. template <typename DerivedX>
  70. IGL_INLINE DerivedX slice(
  71. const Eigen::DenseBase<DerivedX> & X,
  72. const Eigen::Matrix<int,Eigen::Dynamic,1> & R);
  73. template <typename DerivedX>
  74. IGL_INLINE DerivedX slice(
  75. const Eigen::DenseBase<DerivedX>& X,
  76. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  77. const int dim);
  78. }
  79. #ifndef IGL_STATIC_LIBRARY
  80. # include "slice.cpp"
  81. #endif
  82. #endif