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>
12namespace igl
13{
33 template <typename AType, typename Func, typename DerivedB>
34 inline void redux(
35 const Eigen::SparseMatrix<AType> & A,
36 const int dim,
37 const Func & func,
38 Eigen::PlainObjectBase<DerivedB> & B);
39}
40
41// Implementation
42
43#include "for_each.h"
44
45template <typename AType, typename Func, typename DerivedB>
46inline void igl::redux(
47 const Eigen::SparseMatrix<AType> & A,
48 const int dim,
49 const Func & func,
50 Eigen::PlainObjectBase<DerivedB> & B)
51{
52 typedef typename Eigen::SparseMatrix<AType>::StorageIndex Index;
53 assert((dim == 1 || dim == 2) && "dim must be 2 or 1");
54 // Get size of input
55 int m = A.rows();
56 int n = A.cols();
57 // resize output
58 B = DerivedB::Zero(dim==1?n:m);
59 const auto func_wrap = [&func,&B,&dim](const Index i, const Index j, const AType v)
60 {
61 if(dim == 1)
62 {
63 B(j) = i == 0? v : func(B(j),v);
64 }else
65 {
66 B(i) = j == 0? v : func(B(i),v);
67 }
68 };
69 for_each(A,func_wrap);
70}
71
72
73//#ifndef IGL_STATIC_LIBRARY
74//# include "redux.cpp"
75//#endif
76#endif
Definition AABB.h:17
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:46