sparse.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 "sparse.h"
  9. // Bug in unsupported/Eigen/SparseExtra needs iostream first
  10. #include <iostream>
  11. #include <vector>
  12. #include <unsupported/Eigen/SparseExtra>
  13. template <class IndexVector, class ValueVector, typename T>
  14. IGL_INLINE void igl::sparse(
  15. const IndexVector & I,
  16. const IndexVector & J,
  17. const ValueVector & V,
  18. Eigen::SparseMatrix<T>& X)
  19. {
  20. size_t m = (size_t)I.maxCoeff()+1;
  21. size_t n = (size_t)J.maxCoeff()+1;
  22. return igl::sparse(I,J,V,m,n,X);
  23. }
  24. #include "verbose.h"
  25. template <class IndexVector, class ValueVector, typename T>
  26. IGL_INLINE void igl::sparse(
  27. const IndexVector & I,
  28. const IndexVector & J,
  29. const ValueVector & V,
  30. const size_t m,
  31. const size_t n,
  32. Eigen::SparseMatrix<T>& X)
  33. {
  34. using namespace std;
  35. using namespace Eigen;
  36. assert((int)I.maxCoeff() < (int)m);
  37. assert((int)I.minCoeff() >= 0);
  38. assert((int)J.maxCoeff() < (int)n);
  39. assert((int)J.minCoeff() >= 0);
  40. assert(I.size() == J.size());
  41. assert(J.size() == V.size());
  42. // Really we just need .size() to be the same, but this is safer
  43. assert(I.rows() == J.rows());
  44. assert(J.rows() == V.rows());
  45. assert(I.cols() == J.cols());
  46. assert(J.cols() == V.cols());
  47. //// number of values
  48. //int nv = V.size();
  49. //Eigen::DynamicSparseMatrix<T, Eigen::RowMajor> dyn_X(m,n);
  50. //// over estimate the number of entries
  51. //dyn_X.reserve(I.size());
  52. //for(int i = 0;i < nv;i++)
  53. //{
  54. // dyn_X.coeffRef((int)I(i),(int)J(i)) += (T)V(i);
  55. //}
  56. //X = Eigen::SparseMatrix<T>(dyn_X);
  57. vector<Triplet<T> > IJV;
  58. IJV.reserve(I.size());
  59. for(int x = 0;x<I.size();x++)
  60. {
  61. IJV.push_back(Triplet<T >(I(x),J(x),V(x)));
  62. }
  63. X.resize(m,n);
  64. X.setFromTriplets(IJV.begin(),IJV.end());
  65. }
  66. template <typename DerivedD, typename T>
  67. IGL_INLINE void igl::sparse(
  68. const Eigen::PlainObjectBase<DerivedD>& D,
  69. Eigen::SparseMatrix<T>& X)
  70. {
  71. assert(false && "Obsolete. Just call D.sparseView() directly");
  72. using namespace std;
  73. using namespace Eigen;
  74. vector<Triplet<T> > DIJV;
  75. const int m = D.rows();
  76. const int n = D.cols();
  77. for(int i = 0;i<m;i++)
  78. {
  79. for(int j = 0;j<n;j++)
  80. {
  81. if(D(i,j)!=0)
  82. {
  83. DIJV.push_back(Triplet<T>(i,j,D(i,j)));
  84. }
  85. }
  86. }
  87. X.resize(m,n);
  88. X.setFromTriplets(DIJV.begin(),DIJV.end());
  89. }
  90. template <typename DerivedD>
  91. IGL_INLINE Eigen::SparseMatrix<typename DerivedD::Scalar > igl::sparse(
  92. const Eigen::PlainObjectBase<DerivedD>& D)
  93. {
  94. assert(false && "Obsolete. Just call D.sparseView() directly");
  95. Eigen::SparseMatrix<typename DerivedD::Scalar > X;
  96. igl::sparse(D,X);
  97. return X;
  98. }
  99. #ifndef IGL_HEADER_ONLY
  100. // Explicit template specialization
  101. template void igl::sparse<Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1>, double>(Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, Eigen::Matrix<double, -1, 1, 0, -1, 1> const&, size_t, size_t, Eigen::SparseMatrix<double, 0, int>&);
  102. template void igl::sparse<Eigen::Matrix<double, -1, -1, 0, -1, -1>, double>(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::SparseMatrix<double, 0, int>&);
  103. template Eigen::SparseMatrix<Eigen::Matrix<double, -1, -1, 0, -1, -1>::Scalar, 0, int> igl::sparse<Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&);
  104. template Eigen::SparseMatrix<Eigen::Matrix<double, -1, 1, 0, -1, 1>::Scalar, 0, int> igl::sparse<Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&);
  105. template void igl::sparse<Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >, double>(Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, unsigned long, unsigned long, Eigen::SparseMatrix<double, 0, int>&);
  106. #endif