sparse_fast.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 <unordered_map>
  12. #include <map>
  13. #include <utility>
  14. // Hashing function
  15. // namespace std {
  16. // template<>
  17. // struct hash<pair<int, int> > {
  18. // size_t operator()(const pair<int, int>& p) const {
  19. // size_t seed = 0;
  20. // hash<int> h;
  21. // seed ^= h(p.first) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
  22. // seed ^= h(p.second) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
  23. // return seed;
  24. // }
  25. // };
  26. // }
  27. IGL_INLINE void igl::sparse_fast_precompute(
  28. const Eigen::VectorXi & I,
  29. const Eigen::VectorXi & J,
  30. Eigen::SparseMatrix<double>& X,
  31. Eigen::VectorXi& data)
  32. {
  33. // Generates the triplets
  34. std::vector<Eigen::Triplet<double> > t(I.size());
  35. for (unsigned i = 0; i<I.size(); ++i)
  36. t[i] = Eigen::Triplet<double>(I[i],J[i],1);
  37. // Call the triplets version
  38. sparse_fast_precompute(t,X,data);
  39. }
  40. IGL_INLINE void igl::sparse_fast_precompute(
  41. const std::vector<Eigen::Triplet<double> >& triplets,
  42. Eigen::SparseMatrix<double>& X,
  43. Eigen::VectorXi& data)
  44. {
  45. // Construct an empty sparse matrix
  46. X.setFromTriplets(triplets.begin(),triplets.end());
  47. X.makeCompressed();
  48. // Build hash table for all nnz entries
  49. // TODO: this is slow and could be done in nlogn
  50. std::map<std::pair<int,int>,int> id;
  51. for (unsigned k=0; k<X.outerSize(); ++k)
  52. {
  53. unsigned outer_index = *(X.outerIndexPtr()+k);
  54. unsigned next_outer_index = (k+1 == X.outerSize()) ? X.nonZeros() : *(X.outerIndexPtr()+k+1);
  55. for (unsigned l=outer_index; l<next_outer_index; ++l)
  56. {
  57. int col = k;
  58. int row = *(X.innerIndexPtr()+l);
  59. int value_index = l;
  60. std::pair<int,int> rc = std::make_pair(row,col);
  61. id[rc] = value_index;
  62. }
  63. }
  64. // Compute the indices
  65. data.resize(triplets.size());
  66. for (unsigned i=0; i<triplets.size(); ++i)
  67. data[i] = id[std::make_pair(triplets[i].row(),triplets[i].col())];
  68. }
  69. IGL_INLINE void igl::sparse_fast(
  70. const std::vector<Eigen::Triplet<double> >& triplets,
  71. Eigen::SparseMatrix<double>& X,
  72. const Eigen::VectorXi& data)
  73. {
  74. assert(triplets.size() == data.size());
  75. // Clear it first
  76. for (unsigned i = 0; i<data.size(); ++i)
  77. *(X.valuePtr() + data[i]) = 0;
  78. // Then sum them up
  79. for (unsigned i = 0; i<triplets.size(); ++i)
  80. *(X.valuePtr() + data[i]) += triplets[i].value();
  81. }
  82. IGL_INLINE void igl::sparse_fast(
  83. const Eigen::VectorXd & V,
  84. Eigen::SparseMatrix<double>& X,
  85. const Eigen::VectorXi& data)
  86. {
  87. assert(V.size() == data.size());
  88. // Clear it first
  89. for (unsigned i = 0; i<data.size(); ++i)
  90. *(X.valuePtr() + data[i]) = 0;
  91. // Then sum them up
  92. for (unsigned i = 0; i<V.size(); ++i)
  93. *(X.valuePtr() + data[i]) += V[i];
  94. }
  95. #ifdef IGL_STATIC_LIBRARY
  96. #endif