sparse_fast.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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_fast.h"
  9. #include <iostream>
  10. #include <vector>
  11. #include <array>
  12. #include <unordered_map>
  13. #include <map>
  14. #include <utility>
  15. IGL_INLINE void igl::sparse_fast_precompute(
  16. const Eigen::VectorXi & I,
  17. const Eigen::VectorXi & J,
  18. Eigen::SparseMatrix<double>& X,
  19. Eigen::VectorXi& data)
  20. {
  21. // Generates the triplets
  22. std::vector<Eigen::Triplet<double> > t(I.size());
  23. for (unsigned i = 0; i<I.size(); ++i)
  24. t[i] = Eigen::Triplet<double>(I[i],J[i],1);
  25. // Call the triplets version
  26. sparse_fast_precompute(t,X,data);
  27. }
  28. IGL_INLINE void igl::sparse_fast_precompute(
  29. const std::vector<Eigen::Triplet<double> >& triplets,
  30. Eigen::SparseMatrix<double>& X,
  31. Eigen::VectorXi& data)
  32. {
  33. // Construct an empty sparse matrix
  34. X.setFromTriplets(triplets.begin(),triplets.end());
  35. X.makeCompressed();
  36. std::vector<std::array<int,3> > T(triplets.size());
  37. for (unsigned i=0; i<triplets.size(); ++i)
  38. {
  39. T[i][0] = triplets[i].col();
  40. T[i][1] = triplets[i].row();
  41. T[i][2] = i;
  42. }
  43. std::sort(T.begin(), T.end());
  44. data.resize(triplets.size());
  45. int t = 0;
  46. for (unsigned k=0; k<X.outerSize(); ++k)
  47. {
  48. unsigned outer_index = *(X.outerIndexPtr()+k);
  49. unsigned next_outer_index = (k+1 == X.outerSize()) ? X.nonZeros() : *(X.outerIndexPtr()+k+1);
  50. for (unsigned l=outer_index; l<next_outer_index; ++l)
  51. {
  52. int col = k;
  53. int row = *(X.innerIndexPtr()+l);
  54. int value_index = l;
  55. assert(col < X.cols());
  56. assert(col >= 0);
  57. assert(row < X.rows());
  58. assert(row >= 0);
  59. assert(value_index >= 0);
  60. assert(value_index < X.nonZeros());
  61. std::pair<int,int> p_m = std::make_pair(row,col);
  62. while (t<T.size() && (p_m == std::make_pair(T[t][1],T[t][0])))
  63. data[T[t++][2]] = value_index;
  64. }
  65. }
  66. assert(t==T.size());
  67. }
  68. IGL_INLINE void igl::sparse_fast(
  69. const std::vector<Eigen::Triplet<double> >& triplets,
  70. Eigen::SparseMatrix<double>& X,
  71. const Eigen::VectorXi& data)
  72. {
  73. assert(triplets.size() == data.size());
  74. // Clear it first
  75. for (unsigned i = 0; i<data.size(); ++i)
  76. *(X.valuePtr() + data[i]) = 0;
  77. // Then sum them up
  78. for (unsigned i = 0; i<triplets.size(); ++i)
  79. *(X.valuePtr() + data[i]) += triplets[i].value();
  80. }
  81. IGL_INLINE void igl::sparse_fast(
  82. const Eigen::VectorXd & V,
  83. Eigen::SparseMatrix<double>& X,
  84. const Eigen::VectorXi& data)
  85. {
  86. assert(V.size() == data.size());
  87. // Clear it first
  88. for (unsigned i = 0; i<data.size(); ++i)
  89. *(X.valuePtr() + data[i]) = 0;
  90. // Then sum them up
  91. for (unsigned i = 0; i<V.size(); ++i)
  92. *(X.valuePtr() + data[i]) += V[i];
  93. }
  94. #ifdef IGL_STATIC_LIBRARY
  95. #endif