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 #ifndef IGL_SPARSE_H 9 #define IGL_SPARSE_H 10 #include "igl_inline.h" 11 #define EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET 12 #include <Eigen/Dense> 13 #include <Eigen/Sparse> 14 namespace igl 15 { 16 // Build a sparse matrix from list of indices and values (I,J,V), functions 17 // like the sparse function in matlab 18 // 19 // Templates: 20 // IndexVector list of indices, value should be non-negative and should 21 // expect to be cast to an index. Must implement operator(i) to retrieve 22 // ith element 23 // ValueVector list of values, value should be expect to be cast to type 24 // T. Must implement operator(i) to retrieve ith element 25 // T should be a eigen sparse matrix primitive type like int or double 26 // Input: 27 // I nnz vector of row indices of non zeros entries in X 28 // J nnz vector of column indices of non zeros entries in X 29 // V nnz vector of non-zeros entries in X 30 // Optional: 31 // m number of rows 32 // n number of cols 33 // Outputs: 34 // X m by n matrix of type T whose entries are to be found 35 // 36 template <class IndexVector, class ValueVector, typename T> 37 IGL_INLINE void sparse( 38 const IndexVector & I, 39 const IndexVector & J, 40 const ValueVector & V, 41 Eigen::SparseMatrix<T>& X); 42 template < 43 class IndexVectorI, 44 class IndexVectorJ, 45 class ValueVector, 46 typename T> 47 IGL_INLINE void sparse( 48 const IndexVectorI & I, 49 const IndexVectorJ & J, 50 const ValueVector & V, 51 const size_t m, 52 const size_t n, 53 Eigen::SparseMatrix<T>& X); 54 // THIS MAY BE SUPERSEDED BY EIGEN'S .sparseView Indeed it is. 55 // Convert a full, dense matrix to a sparse one 56 // 57 // Templates: 58 // T should be a eigen sparse matrix primitive type like int or double 59 // Input: 60 // D m by n full, dense matrix 61 // Output: 62 // X m by n sparse matrix 63 template <typename DerivedD, typename T> 64 IGL_INLINE void sparse( 65 const Eigen::PlainObjectBase<DerivedD>& D, 66 Eigen::SparseMatrix<T>& X); 67 // Wrapper with return 68 template <typename DerivedD> 69 IGL_INLINE Eigen::SparseMatrix<typename DerivedD::Scalar > sparse( 70 const Eigen::PlainObjectBase<DerivedD>& D); 71 72 } 73 74 #ifndef IGL_STATIC_LIBRARY 75 # include "sparse.cpp" 76 #endif 77 78 #endif 79