sparse_cached.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2017 Daniele Panozzo <daniele.panozzo@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_cached.h"
  9. #include <iostream>
  10. #include <vector>
  11. #include <array>
  12. #include <unordered_map>
  13. #include <map>
  14. #include <utility>
  15. template <typename DerivedI, typename Scalar>
  16. IGL_INLINE void igl::sparse_cached_precompute(
  17. const Eigen::MatrixBase<DerivedI> & I,
  18. const Eigen::MatrixBase<DerivedI> & J,
  19. Eigen::VectorXi& data,
  20. Eigen::SparseMatrix<Scalar>& X)
  21. {
  22. // Generates the triplets
  23. std::vector<Eigen::Triplet<double> > t(I.size());
  24. for (unsigned i = 0; i<I.size(); ++i)
  25. t[i] = Eigen::Triplet<double>(I[i],J[i],1);
  26. // Call the triplets version
  27. sparse_cached_precompute(t,X,data);
  28. }
  29. template <typename Scalar>
  30. IGL_INLINE void igl::sparse_cached_precompute(
  31. const std::vector<Eigen::Triplet<Scalar> >& triplets,
  32. Eigen::VectorXi& data,
  33. Eigen::SparseMatrix<Scalar>& X)
  34. {
  35. // Construct an empty sparse matrix
  36. X.setFromTriplets(triplets.begin(),triplets.end());
  37. X.makeCompressed();
  38. std::vector<std::array<int,3> > T(triplets.size());
  39. for (unsigned i=0; i<triplets.size(); ++i)
  40. {
  41. T[i][0] = triplets[i].col();
  42. T[i][1] = triplets[i].row();
  43. T[i][2] = i;
  44. }
  45. std::sort(T.begin(), T.end());
  46. data.resize(triplets.size());
  47. int t = 0;
  48. for (unsigned k=0; k<X.outerSize(); ++k)
  49. {
  50. unsigned outer_index = *(X.outerIndexPtr()+k);
  51. unsigned next_outer_index = (k+1 == X.outerSize()) ? X.nonZeros() : *(X.outerIndexPtr()+k+1);
  52. for (unsigned l=outer_index; l<next_outer_index; ++l)
  53. {
  54. int col = k;
  55. int row = *(X.innerIndexPtr()+l);
  56. int value_index = l;
  57. assert(col < X.cols());
  58. assert(col >= 0);
  59. assert(row < X.rows());
  60. assert(row >= 0);
  61. assert(value_index >= 0);
  62. assert(value_index < X.nonZeros());
  63. std::pair<int,int> p_m = std::make_pair(row,col);
  64. while (t<T.size() && (p_m == std::make_pair(T[t][1],T[t][0])))
  65. data[T[t++][2]] = value_index;
  66. }
  67. }
  68. assert(t==T.size());
  69. }
  70. template <typename Scalar>
  71. IGL_INLINE void igl::sparse_cached(
  72. const std::vector<Eigen::Triplet<Scalar> >& triplets,
  73. const Eigen::VectorXi& data,
  74. Eigen::SparseMatrix<Scalar>& X)
  75. {
  76. assert(triplets.size() == data.size());
  77. // Clear it first
  78. for (unsigned i = 0; i<data.size(); ++i)
  79. *(X.valuePtr() + data[i]) = 0;
  80. // Then sum them up
  81. for (unsigned i = 0; i<triplets.size(); ++i)
  82. *(X.valuePtr() + data[i]) += triplets[i].value();
  83. }
  84. template <typename DerivedV, typename Scalar>
  85. IGL_INLINE void igl::sparse_cached(
  86. const Eigen::MatrixBase<DerivedV>& V,
  87. const Eigen::VectorXi& data,
  88. Eigen::SparseMatrix<Scalar>& X)
  89. {
  90. assert(V.size() == data.size());
  91. // Clear it first
  92. for (unsigned i = 0; i<data.size(); ++i)
  93. *(X.valuePtr() + data[i]) = 0;
  94. // Then sum them up
  95. for (unsigned i = 0; i<V.size(); ++i)
  96. *(X.valuePtr() + data[i]) += V[i];
  97. }
  98. #ifdef IGL_STATIC_LIBRARY
  99. #if EIGEN_VERSION_AT_LEAST(3,3,0)
  100. template void igl::sparse_cached<double>(std::vector<Eigen::Triplet<double, Eigen::SparseMatrix<double, 0, int>::StorageIndex>, std::allocator<Eigen::Triplet<double, Eigen::SparseMatrix<double, 0, int>::StorageIndex> > > const&, Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, Eigen::SparseMatrix<double, 0, int>&);
  101. template void igl::sparse_cached_precompute<double>(std::vector<Eigen::Triplet<double, Eigen::SparseMatrix<double, 0, int>::StorageIndex>, std::allocator<Eigen::Triplet<double, Eigen::SparseMatrix<double, 0, int>::StorageIndex> > > const&, Eigen::Matrix<int, -1, 1, 0, -1, 1>&, Eigen::SparseMatrix<double, 0, int>&);
  102. #else
  103. template void igl::sparse_cached<double>(std::vector<Eigen::Triplet<double, Eigen::SparseMatrix<double, 0, int>::Index>, std::allocator<Eigen::Triplet<double, Eigen::SparseMatrix<double, 0, int>::Index> > > const&, Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, Eigen::SparseMatrix<double, 0, int>&);
  104. template void igl::sparse_cached_precompute<double>(std::vector<Eigen::Triplet<double, Eigen::SparseMatrix<double, 0, int>::Index>, std::allocator<Eigen::Triplet<double, Eigen::SparseMatrix<double, 0, int>::Index> > > const&, Eigen::Matrix<int, -1, 1, 0, -1, 1>&, Eigen::SparseMatrix<double, 0, int>&);
  105. #endif
  106. #endif