libigl v2.5.0
Loading...
Searching...
No Matches
redux.h
Go to the documentation of this file.
1// This file is part of libigl, a simple c++ geometry processing library.
2//
3// Copyright (C) 2023 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_REDUX_H
9#define IGL_REDUX_H
10#include <Eigen/Core>
11#include <Eigen/Sparse>
12
13#include <cassert>
14namespace igl
15{
35 template <typename AType, typename Func, typename DerivedB>
36 inline void redux(
37 const Eigen::SparseMatrix<AType> & A,
38 const int dim,
39 const Func & func,
40 Eigen::PlainObjectBase<DerivedB> & B);
41}
42
43// Implementation
44
45#include "for_each.h"
46
47template <typename AType, typename Func, typename DerivedB>
48inline void igl::redux(
49 const Eigen::SparseMatrix<AType> & A,
50 const int dim,
51 const Func & func,
52 Eigen::PlainObjectBase<DerivedB> & B)
53{
54 typedef typename Eigen::SparseMatrix<AType>::StorageIndex Index;
55 assert((dim == 1 || dim == 2) && "dim must be 2 or 1");
56 // Get size of input
57 int m = A.rows();
58 int n = A.cols();
59 // resize output
60 B = DerivedB::Zero(dim==1?n:m);
61 const auto func_wrap = [&func,&B,&dim](const Index i, const Index j, const AType v)
62 {
63 if(dim == 1)
64 {
65 B(j) = i == 0? v : func(B(j),v);
66 }else
67 {
68 B(i) = j == 0? v : func(B(i),v);
69 }
70 };
71 for_each(A,func_wrap);
72}
73
74
75//#ifndef IGL_STATIC_LIBRARY
76//# include "redux.cpp"
77//#endif
78#endif
Definition AABB.h:18
void for_each(const Eigen::SparseMatrix< AType > &A, const Func &func)
FOR_EACH Call a given function for each non-zero (i.e., explicit value might actually be ==0) in a Sp...
Definition for_each.h:31
void redux(const Eigen::SparseMatrix< AType > &A, const int dim, const Func &func, Eigen::PlainObjectBase< DerivedB > &B)
Perform reductions on the rows or columns of a SparseMatrix.
Definition redux.h:48