sparse.cpp 5.5 KB

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