histc.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "histc.h"
  2. #include "matlab_format.h"
  3. #include <cassert>
  4. #include <iostream>
  5. template <typename DerivedX, typename DerivedE, typename DerivedN, typename DerivedB>
  6. IGL_INLINE void igl::histc(
  7. const Eigen::PlainObjectBase<DerivedX > & X,
  8. const Eigen::PlainObjectBase<DerivedE > & E,
  9. Eigen::PlainObjectBase<DerivedN > & N,
  10. Eigen::PlainObjectBase<DerivedB > & B)
  11. {
  12. histc(X,E,B);
  13. const int n = E.size();
  14. const int m = X.size();
  15. assert(m == B.size());
  16. N.resize(n,1);
  17. N.setConstant(0);
  18. #pragma omp parallel for
  19. for(int j = 0;j<m;j++)
  20. {
  21. if(B(j) >= 0)
  22. {
  23. #pragma omp atomic
  24. N(B(j))++;
  25. }
  26. }
  27. }
  28. template <typename DerivedX, typename DerivedE, typename DerivedB>
  29. IGL_INLINE void igl::histc(
  30. const Eigen::PlainObjectBase<DerivedX > & X,
  31. const Eigen::PlainObjectBase<DerivedE > & E,
  32. Eigen::PlainObjectBase<DerivedB > & B)
  33. {
  34. const int m = X.size();
  35. using namespace std;
  36. assert(
  37. (E.bottomRightCorner(E.size()-1,1) -
  38. E.topLeftCorner(E.size()-1,1)).maxCoeff() >= 0 &&
  39. "E should be monotonically increasing");
  40. B.resize(m,1);
  41. #pragma omp parallel for
  42. for(int j = 0;j<m;j++)
  43. {
  44. const double x = X(j);
  45. // Boring one-offs
  46. if(x < E(0) || x > E(E.size()-1))
  47. {
  48. B(j) = -1;
  49. continue;
  50. }
  51. // Find x in E
  52. int l = 0;
  53. int h = E.size()-1;
  54. int k = l;
  55. while((h-l)>1)
  56. {
  57. assert(x >= E(l));
  58. assert(x <= E(h));
  59. k = (h+l)/2;
  60. if(x < E(k))
  61. {
  62. h = k;
  63. }else
  64. {
  65. l = k;
  66. }
  67. }
  68. if(x == E(h))
  69. {
  70. k = h;
  71. }else
  72. {
  73. k = l;
  74. }
  75. B(j) = k;
  76. }
  77. }
  78. template <typename DerivedE>
  79. IGL_INLINE void igl::histc(
  80. const typename DerivedE::Scalar & x,
  81. const Eigen::PlainObjectBase<DerivedE > & E,
  82. typename DerivedE::Index & b)
  83. {
  84. Eigen::Matrix<typename DerivedE::Scalar,1,1> X;
  85. X(0) = x;
  86. Eigen::Matrix<typename DerivedE::Index,1,1> B;
  87. hist(X,E,B);
  88. b = B(0);
  89. }
  90. #ifndef IGL_HEADER_ONLY
  91. // Explicit template specialization
  92. template void igl::histc<Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  93. template void igl::histc<Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  94. template void igl::histc<Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  95. template void igl::histc<Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  96. template void igl::histc<Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  97. #endif