histc.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include "histc.h"
  2. #include <cassert>
  3. template <typename DerivedX, typename DerivedE, typename DerivedN, typename DerivedB>
  4. IGL_INLINE void igl::histc(
  5. const Eigen::PlainObjectBase<DerivedX > & X,
  6. const Eigen::PlainObjectBase<DerivedE > & E,
  7. Eigen::PlainObjectBase<DerivedN > & N,
  8. Eigen::PlainObjectBase<DerivedB > & B)
  9. {
  10. const int m = X.size();
  11. const int n = E.size();
  12. assert(
  13. (E.topLeftCorner(E.size()-1) -
  14. E.bottomRightCorner(E.size()-1)).maxCoeff() >= 0 &&
  15. "E should be monotonically increasing");
  16. N.resize(n,1);
  17. B.resize(m,1);
  18. N.setConstant(0);
  19. #pragma omp parallel for
  20. for(int j = 0;j<m;j++)
  21. {
  22. const double x = X(j);
  23. // Boring one-offs
  24. if(x < E(0) || x > E(E.size()-1))
  25. {
  26. B(j) = -1;
  27. continue;
  28. }
  29. // Find x in E
  30. int l = 0;
  31. int h = E.size()-1;
  32. int k = l;
  33. while((h-l)>1)
  34. {
  35. assert(x >= E(l));
  36. assert(x <= E(h));
  37. k = (h+l)/2;
  38. if(x < E(k))
  39. {
  40. h = k;
  41. }else
  42. {
  43. l = k;
  44. }
  45. }
  46. if(x == E(h))
  47. {
  48. k = h;
  49. }else
  50. {
  51. k = l;
  52. }
  53. B(j) = k;
  54. // Not sure if this pragma is needed
  55. #pragma omp atomic
  56. N(k)++;
  57. }
  58. }
  59. #ifndef IGL_HEADER_ONLY
  60. // Explicit template specialization
  61. 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> >&);
  62. 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> >&);
  63. 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> >&);
  64. #endif