1 // This file is part of libigl, a simple c++ geometry processing library.
2 //
3 // Copyright (C) 2013 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 #include "find.h"
9 
10 #include "verbose.h"
11 #include <iostream>
12 
13 template <
14   typename T,
15   typename DerivedI,
16   typename DerivedJ,
17   typename DerivedV>
find(const Eigen::SparseMatrix<T> & X,Eigen::DenseBase<DerivedI> & I,Eigen::DenseBase<DerivedJ> & J,Eigen::DenseBase<DerivedV> & V)18 IGL_INLINE void igl::find(
19   const Eigen::SparseMatrix<T>& X,
20   Eigen::DenseBase<DerivedI> & I,
21   Eigen::DenseBase<DerivedJ> & J,
22   Eigen::DenseBase<DerivedV> & V)
23 {
24   // Resize outputs to fit nonzeros
25   I.derived().resize(X.nonZeros(),1);
26   J.derived().resize(X.nonZeros(),1);
27   V.derived().resize(X.nonZeros(),1);
28 
29   int i = 0;
30   // Iterate over outside
31   for(int k=0; k<X.outerSize(); ++k)
32   {
33     // Iterate over inside
34     for(typename Eigen::SparseMatrix<T>::InnerIterator it (X,k); it; ++it)
35     {
36       V(i) = it.value();
37       I(i) = it.row();
38       J(i) = it.col();
39       i++;
40     }
41   }
42 }
43 
44 template <
45   typename DerivedX,
46   typename DerivedI,
47   typename DerivedJ,
48   typename DerivedV>
find(const Eigen::DenseBase<DerivedX> & X,Eigen::PlainObjectBase<DerivedI> & I,Eigen::PlainObjectBase<DerivedJ> & J,Eigen::PlainObjectBase<DerivedV> & V)49 IGL_INLINE void igl::find(
50   const Eigen::DenseBase<DerivedX>& X,
51   Eigen::PlainObjectBase<DerivedI> & I,
52   Eigen::PlainObjectBase<DerivedJ> & J,
53   Eigen::PlainObjectBase<DerivedV> & V)
54 {
55   const int nnz = X.count();
56   I.resize(nnz,1);
57   J.resize(nnz,1);
58   V.resize(nnz,1);
59   {
60     int k = 0;
61     for(int j = 0;j<X.cols();j++)
62     {
63       for(int i = 0;i<X.rows();i++)
64       {
65         if(X(i,j))
66         {
67           I(k) = i;
68           J(k) = j;
69           V(k) = X(i,j);
70           k++;
71         }
72       }
73     }
74   }
75 }
76 
77 template <
78   typename DerivedX,
79   typename DerivedI>
find(const Eigen::DenseBase<DerivedX> & X,Eigen::PlainObjectBase<DerivedI> & I)80 IGL_INLINE void igl::find(
81   const Eigen::DenseBase<DerivedX>& X,
82   Eigen::PlainObjectBase<DerivedI> & I)
83 {
84   const int nnz = X.count();
85   I.resize(nnz,1);
86   {
87     int k = 0;
88     for(int j = 0;j<X.cols();j++)
89     {
90       for(int i = 0;i<X.rows();i++)
91       {
92         if(X(i,j))
93         {
94           I(k) = i+X.rows()*j;
95           k++;
96         }
97       }
98     }
99   }
100 }
101 
102 template <typename T>
find(const Eigen::SparseVector<T> & X,Eigen::Matrix<int,Eigen::Dynamic,1> & I,Eigen::Matrix<T,Eigen::Dynamic,1> & V)103 IGL_INLINE void igl::find(
104   const Eigen::SparseVector<T>& X,
105   Eigen::Matrix<int,Eigen::Dynamic,1> & I,
106   Eigen::Matrix<T,Eigen::Dynamic,1> & V)
107 {
108   // Resize outputs to fit nonzeros
109   I.resize(X.nonZeros());
110   V.resize(X.nonZeros());
111 
112   int i = 0;
113   // loop over non-zeros
114   for(typename Eigen::SparseVector<T>::InnerIterator it(X); it; ++it)
115   {
116     I(i) = it.index();
117     V(i) = it.value();
118     i++;
119   }
120 }
121 
122 #ifdef IGL_STATIC_LIBRARY
123 // Explicit template instantiation
124 
125 template void igl::find<bool, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Array<bool, -1, 1, 0, -1, 1> >(Eigen::SparseMatrix<bool, 0, int> const&, Eigen::DenseBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::DenseBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::DenseBase<Eigen::Array<bool, -1, 1, 0, -1, 1> >&);
126 template void igl::find<Eigen::Array<bool, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::DenseBase<Eigen::Array<bool, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
127 template void igl::find<double, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::SparseMatrix<double, 0, int> const&, Eigen::DenseBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::DenseBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::DenseBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
128 template void igl::find<double, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::SparseMatrix<double, 0, int> const&, Eigen::DenseBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&, Eigen::DenseBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&, Eigen::DenseBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
129 template void igl::find<double, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::SparseMatrix<double, 0, int> const&, Eigen::DenseBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::DenseBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::DenseBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
130 template void igl::find<Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::DenseBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
131 template void igl::find<double, Eigen::Matrix<long, -1, 1, 0, -1, 1>, Eigen::Matrix<long, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::SparseMatrix<double, 0, int> const&, Eigen::DenseBase<Eigen::Matrix<long, -1, 1, 0, -1, 1> >&, Eigen::DenseBase<Eigen::Matrix<long, -1, 1, 0, -1, 1> >&, Eigen::DenseBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
132 template void igl::find<Eigen::Matrix<bool, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::DenseBase<Eigen::Matrix<bool, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
133 #if EIGEN_VERSION_AT_LEAST(3,3,0)
134 #else
135 template void igl::find<Eigen::CwiseBinaryOp<Eigen::internal::scalar_cmp_op<long, (Eigen::internal::ComparisonName)0>, Eigen::PartialReduxExpr<Eigen::Array<bool, -1, 3, 0, -1, 3>, Eigen::internal::member_count<long>, 1> const, Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<long>, Eigen::Array<long, -1, 1, 0, -1, 1> > const>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::DenseBase<Eigen::CwiseBinaryOp<Eigen::internal::scalar_cmp_op<long, (Eigen::internal::ComparisonName)0>, Eigen::PartialReduxExpr<Eigen::Array<bool, -1, 3, 0, -1, 3>, Eigen::internal::member_count<long>, 1> const, Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<long>, Eigen::Array<long, -1, 1, 0, -1, 1> > const> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
136 template void igl::find<Eigen::CwiseBinaryOp<Eigen::internal::scalar_cmp_op<int, (Eigen::internal::ComparisonName)0>, Eigen::Array<int, -1, 1, 0, -1, 1> const, Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<int>, Eigen::Array<int, -1, 1, 0, -1, 1> > const>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::DenseBase<Eigen::CwiseBinaryOp<Eigen::internal::scalar_cmp_op<int, (Eigen::internal::ComparisonName)0>, Eigen::Array<int, -1, 1, 0, -1, 1> const, Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<int>, Eigen::Array<int, -1, 1, 0, -1, 1> > const> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
137 #endif
138 
139 #endif
140