sparse_cached.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2017 Daniele Panozzo <[email protected]>
  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_CACHED_H
  9. #define IGL_SPARSE_CACHED_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), similarly to
  17. /// the sparse function in matlab. Divides the construction in two phases, one
  18. /// for fixing the sparsity pattern, and one to populate it with values. Compared to
  19. /// igl::sparse, this version is slower for the first time (since it requires a
  20. /// precomputation), but faster to the subsequent evaluations.
  21. ///
  22. /// @param[in] I nnz vector of row indices of non zeros entries in X
  23. /// @param[in] J nnz vector of column indices of non zeros entries in X
  24. /// @param[out] data ?? vector of ??
  25. /// @param[out] X m by n matrix of type T whose entries are to be found
  26. ///
  27. /// #### Example:
  28. ///
  29. /// Eigen::SparseMatrix<double> A;
  30. /// std::vector<Eigen::Triplet<double> > IJV;
  31. /// slim_buildA(IJV);
  32. /// if (A.rows() == 0)
  33. /// {
  34. /// A = Eigen::SparseMatrix<double>(rows,cols);
  35. /// igl::sparse_cached_precompute(IJV,A_data,A);
  36. /// }
  37. /// else
  38. /// igl::sparse_cached(IJV,A_data,A);
  39. ///
  40. template <typename DerivedI, typename Scalar>
  41. IGL_INLINE void sparse_cached_precompute(
  42. const Eigen::MatrixBase<DerivedI> & I,
  43. const Eigen::MatrixBase<DerivedI> & J,
  44. Eigen::VectorXi& data,
  45. Eigen::SparseMatrix<Scalar>& X
  46. );
  47. /// \overload
  48. /// @param[in] triplets nnz vector of triplets of non zeros entries in X
  49. template <typename Scalar>
  50. IGL_INLINE void sparse_cached_precompute(
  51. const std::vector<Eigen::Triplet<Scalar> >& triplets,
  52. Eigen::VectorXi& data,
  53. Eigen::SparseMatrix<Scalar>& X
  54. );
  55. /// Build a sparse matrix from cached list of data and values
  56. ///
  57. /// @param[in] triplets nnz vector of triplets of non zeros entries in X
  58. /// @param[in] data ?? vector of ??
  59. /// @param[in,out] X m by n matrix of type T whose entries are to be found
  60. template <typename Scalar>
  61. IGL_INLINE void sparse_cached(
  62. const std::vector<Eigen::Triplet<Scalar> >& triplets,
  63. const Eigen::VectorXi& data,
  64. Eigen::SparseMatrix<Scalar>& X);
  65. /// \overload
  66. /// @param[in] V #V list of values
  67. template <typename DerivedV, typename Scalar>
  68. IGL_INLINE void sparse_cached(
  69. const Eigen::MatrixBase<DerivedV>& V,
  70. const Eigen::VectorXi& data,
  71. Eigen::SparseMatrix<Scalar>& X
  72. );
  73. }
  74. #ifndef IGL_STATIC_LIBRARY
  75. # include "sparse_cached.cpp"
  76. #endif
  77. #endif