Ver Fonte

added another overload of slice

Teseo Schneider há 6 anos atrás
pai
commit
7f6f1670b4
2 ficheiros alterados com 59 adições e 6 exclusões
  1. 42 0
      include/igl/slice.cpp
  2. 17 6
      include/igl/slice.h

+ 42 - 0
include/igl/slice.cpp

@@ -173,6 +173,48 @@ IGL_INLINE void igl::slice(
   }
 }
 
+template <
+    typename DerivedX,
+    typename DerivedR,
+    typename DerivedC,
+    typename DerivedY>
+IGL_INLINE void igl::slice(
+    const Eigen::ArrayBase<DerivedX> &X,
+    const Eigen::MatrixBase<DerivedR> &R,
+    const Eigen::MatrixBase<DerivedC> &C,
+    Eigen::PlainObjectBase<DerivedY> &Y)
+{
+#ifndef NDEBUG
+  int xm = X.rows();
+  int xn = X.cols();
+#endif
+  int ym = R.size();
+  int yn = C.size();
+
+  // special case when R or C is empty
+  if (ym == 0 || yn == 0)
+  {
+    Y.resize(ym, yn);
+    return;
+  }
+
+  assert(R.minCoeff() >= 0);
+  assert(R.maxCoeff() < xm);
+  assert(C.minCoeff() >= 0);
+  assert(C.maxCoeff() < xn);
+
+  // Resize output
+  Y.resize(ym, yn);
+  // loop over output rows, then columns
+  for (int i = 0; i < ym; i++)
+  {
+    for (int j = 0; j < yn; j++)
+    {
+      Y(i, j) = X(R(i, 0), C(j, 0));
+    }
+  }
+}
+
 template <
     typename DerivedX,
     typename DerivedR,

+ 17 - 6
include/igl/slice.h

@@ -1,9 +1,9 @@
 // This file is part of libigl, a simple c++ geometry processing library.
-// 
+//
 // Copyright (C) 2013 Alec Jacobson <[email protected]>
 //
-// This Source Code Form is subject to the terms of the Mozilla Public License 
-// v. 2.0. If a copy of the MPL was not distributed with this file, You can 
+// This Source Code Form is subject to the terms of the Mozilla Public License
+// v. 2.0. If a copy of the MPL was not distributed with this file, You can
 // obtain one at http://mozilla.org/MPL/2.0/.
 #ifndef IGL_SLICE_H
 #define IGL_SLICE_H
@@ -14,7 +14,7 @@ namespace igl
 {
   // Act like the matlab X(row_indices,col_indices) operator, where
   // row_indices, col_indices are non-negative integer indices.
-  // 
+  //
   // Inputs:
   //   X  m by n matrix
   //   R  list of row indices
@@ -24,7 +24,7 @@ namespace igl
   //
   // See also: slice_mask
   template <
-    typename TX, 
+    typename TX,
     typename TY,
     typename DerivedR,
     typename DerivedC>
@@ -49,7 +49,7 @@ namespace igl
     const int dim,
     MatY& Y);
   template <
-    typename DerivedX, 
+    typename DerivedX,
     typename DerivedR,
     typename DerivedC,
     typename DerivedY>
@@ -59,6 +59,17 @@ namespace igl
     const Eigen::MatrixBase<DerivedC> & C,
     Eigen::PlainObjectBase<DerivedY> & Y);
 
+  template <
+    typename DerivedX,
+    typename DerivedR,
+    typename DerivedC,
+    typename DerivedY>
+IGL_INLINE void slice(
+    const Eigen::ArrayBase<DerivedX> &X,
+    const Eigen::MatrixBase<DerivedR> &R,
+    const Eigen::MatrixBase<DerivedC> &C,
+    Eigen::PlainObjectBase<DerivedY> &Y);
+
   template <typename DerivedX, typename DerivedY, typename DerivedR>
   IGL_INLINE void slice(
     const Eigen::MatrixBase<DerivedX> & X,