slice.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 "placeholders.h"
  12. /// @file slice.h
  13. ///
  14. /// \deprecated The following dense versions are deprecated in favor of using
  15. /// [Eigen v3.4's native
  16. /// slicing](https://eigen.tuxfamily.org/dox-devel/group__TutorialSlicingIndexing.html)
  17. /// which is more efficient, more flexible, and has better syntax.
  18. ///
  19. /// | igl | Eigen v3.4 |
  20. /// |-------------------0--------|------------------------------------| |
  21. /// `igl::slice(X,I,J,Y)` | `Y = X(I,J)` | |
  22. /// `igl::slice(X,I,1,Y)` | `Y = X(I,igl::placeholders::all)` | |
  23. /// `igl::slice(X,J,2,Y)` | `Y = X(igl::placeholders::all,J)` | |
  24. /// `igl::slice_into(Z,I,J,X)` | `X(I,J) = Z` | |
  25. /// `igl::slice_into(Z,I,1,X)` | `X(I,igl::placeholders::all) = Z` | |
  26. /// `igl::slice_into(Z,J,2,X)` | `X(igl::placeholders::all,J) = Z` | |
  27. /// `igl::slice_mask(X,M,N,Y)` | `Y = X(igl::find(M),igl::find(N))` | | _not
  28. /// available_ | `X(igl::find(M),igl::find(N)) = Z` |
  29. ///
  30. /// Eigen's slicing supports much more than arrays of indices as input, as well.
  31. ///
  32. /// Unfortunately, Eigen v3.4 does not support slicing on sparse matrices.
  33. #include <Eigen/Sparse>
  34. #include <vector>
  35. namespace igl
  36. {
  37. /// Act like the matlab X(row_indices,col_indices) operator, where
  38. /// row_indices, col_indices are non-negative integer indices.
  39. ///
  40. /// @param[in] X m by n matrix
  41. /// @param[in] R list of row indices
  42. /// @param[in] C list of column indices
  43. /// @param[out] Y #R by #C matrix
  44. ///
  45. /// \see slice_mask, slice_into
  46. ///
  47. /// \note See also Eigen's unaryExpr https://stackoverflow.com/a/49411587/148668
  48. template <
  49. typename TX,
  50. typename TY,
  51. typename DerivedR,
  52. typename DerivedC>
  53. IGL_INLINE void slice(
  54. const Eigen::SparseMatrix<TX>& X,
  55. const Eigen::DenseBase<DerivedR> & R,
  56. const Eigen::DenseBase<DerivedC> & C,
  57. Eigen::SparseMatrix<TY>& Y);
  58. /// \overload
  59. /// \brief Wrapper to only slice in one direction
  60. ///
  61. /// @param[in] dim dimension to slice in 1 or 2, dim=1 --> X(R,:), dim=2 --> X(:,R)
  62. ///
  63. /// \note For now this is just a cheap wrapper.
  64. template <
  65. typename MatX,
  66. typename DerivedR,
  67. typename MatY>
  68. IGL_INLINE void slice(
  69. const MatX& X,
  70. const Eigen::DenseBase<DerivedR> & R,
  71. const int dim,
  72. MatY& Y);
  73. /// \overload
  74. template< class T >
  75. IGL_INLINE void slice(
  76. const std::vector<T> & X,
  77. std::vector<size_t> const & R,
  78. std::vector<T> & Y);
  79. /// \overload
  80. /// \brief Vector version
  81. /// \bug these templates are out of order
  82. template <typename DerivedX, typename DerivedY, typename DerivedR>
  83. IGL_INLINE void slice(
  84. const Eigen::DenseBase<DerivedX> & X,
  85. const Eigen::DenseBase<DerivedR> & R,
  86. Eigen::PlainObjectBase<DerivedY> & Y);
  87. /// \overload
  88. ///
  89. /// \deprecated
  90. ///
  91. /// See slice.h for more details
  92. template <
  93. typename DerivedX,
  94. typename DerivedR,
  95. typename DerivedC,
  96. typename DerivedY>
  97. IGL_INLINE void slice(
  98. const Eigen::DenseBase<DerivedX> & X,
  99. const Eigen::DenseBase<DerivedR> & R,
  100. const Eigen::DenseBase<DerivedC> & C,
  101. Eigen::PlainObjectBase<DerivedY> & Y);
  102. /// \overload
  103. /// \brief Eigen::VectorXi Y = slice(X,R);
  104. /// This templating is bad because the return type might not have the same
  105. /// size as `DerivedX`. This will probably only work if DerivedX has Dynamic
  106. /// as it's non-trivial sizes or if the number of rows in R happens to equal
  107. /// the number of rows in `DerivedX`.
  108. template <typename DerivedX, typename DerivedR>
  109. IGL_INLINE DerivedX slice(
  110. const Eigen::DenseBase<DerivedX> & X,
  111. const Eigen::DenseBase<DerivedR> & R);
  112. /// \overload
  113. template <typename DerivedX, typename DerivedR>
  114. IGL_INLINE DerivedX slice(
  115. const Eigen::DenseBase<DerivedX>& X,
  116. const Eigen::DenseBase<DerivedR> & R,
  117. const int dim);
  118. }
  119. #ifndef IGL_STATIC_LIBRARY
  120. # include "slice.cpp"
  121. #endif
  122. #endif