瀏覽代碼

accumarray implementation

Alec Jacobson 6 年之前
父節點
當前提交
b5875d5ea1
共有 3 個文件被更改,包括 91 次插入0 次删除
  1. 32 0
      include/igl/accumarray.cpp
  2. 37 0
      include/igl/accumarray.h
  3. 22 0
      tests/include/igl/accumarray.cpp

+ 32 - 0
include/igl/accumarray.cpp

@@ -0,0 +1,32 @@
+// This file is part of libigl, a simple c++ geometry processing library.
+// 
+// Copyright (C) 2018 Alec Jacobson <alecjacobson@gmail.com>
+// 
+// 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/.
+#include "accumarray.h"
+#include <cassert>
+
+template <
+  typename DerivedS,
+  typename DerivedV,
+  typename DerivedA
+  >
+void igl::accumarray(
+  const Eigen::MatrixBase<DerivedS> & S,
+  const Eigen::MatrixBase<DerivedV> & V,
+  Eigen::PlainObjectBase<DerivedA> & A)
+{
+  assert(V.size() == S.size() && "S and V should be same size");
+  A.setZero(S.maxCoeff()+1,1);
+  for(int s = 0;s<S.size();s++)
+  {
+    A(S(s)) += V(s);
+  }
+}
+
+#ifdef IGL_STATIC_LIBRARY
+// Explicit template instantiations
+template void igl::accumarray<Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
+#endif

+ 37 - 0
include/igl/accumarray.h

@@ -0,0 +1,37 @@
+// This file is part of libigl, a simple c++ geometry processing library.
+// 
+// Copyright (C) 2018 Alec Jacobson <alecjacobson@gmail.com>
+// 
+// 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 ACCUMARRY_H
+#define ACCUMARRY_H
+#include "igl_inline.h"
+#include <Eigen/Core>
+namespace igl
+{
+  // ACCUMARRY Like Matlab's accumarray. Accumulate values in V using subscripts
+  // in S.
+  //
+  // Inputs:
+  //   S  #S list of subscripts
+  //   V  #V list of values
+  // Outputs:
+  //   A  max(subs)+1 list of accumulated values
+  template <
+    typename DerivedS,
+    typename DerivedV,
+    typename DerivedA
+    >
+  void accumarray(
+    const Eigen::MatrixBase<DerivedS> & S,
+    const Eigen::MatrixBase<DerivedV> & V,
+    Eigen::PlainObjectBase<DerivedA> & A);
+}
+
+#ifndef IGL_STATIC_LIBRARY
+#  include "accumarray.cpp"
+#endif
+
+#endif

+ 22 - 0
tests/include/igl/accumarray.cpp

@@ -0,0 +1,22 @@
+// This file is part of libigl, a simple c++ geometry processing library.
+// 
+// Copyright (C) 2018 Alec Jacobson <alecjacobson@gmail.com>
+// 
+// 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/.
+#include <test_common.h>
+#include <igl/accumarray.h>
+
+TEST_CASE("accumarray: matlab_help", "[igl]")
+{
+  const Eigen::VectorXd V = 
+    (Eigen::VectorXd(5) << 101,102,103,104,105).finished();
+  const Eigen::VectorXi S = 
+    (Eigen::VectorXi(5) << 0,2,3,2,3).finished();
+  Eigen::VectorXd A;
+  igl::accumarray(S,V,A);
+  const Eigen::VectorXd Agt  = 
+    (Eigen::VectorXd(4) << 101,0,206,208).finished();
+  test_common::assert_eq(A,Agt);
+}