histc.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Alec Jacobson <[email protected]>
  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 "histc.h"
  9. #include "parallel_for.h"
  10. #include <cassert>
  11. #include <iostream>
  12. template <typename DerivedX, typename DerivedE, typename DerivedN, typename DerivedB>
  13. IGL_INLINE void igl::histc(
  14. const Eigen::MatrixBase<DerivedX > & X,
  15. const Eigen::MatrixBase<DerivedE > & E,
  16. Eigen::PlainObjectBase<DerivedN > & N,
  17. Eigen::PlainObjectBase<DerivedB > & B)
  18. {
  19. histc(X,E,B);
  20. const int n = E.size();
  21. const int m = X.size();
  22. assert(m == B.size());
  23. N.resize(n,1);
  24. N.setConstant(0);
  25. for(int j = 0;j<m;j++)
  26. {
  27. if(B(j) >= 0)
  28. {
  29. N(int(B(j)))++;
  30. }
  31. }
  32. }
  33. template <typename DerivedX, typename DerivedE, typename DerivedB>
  34. IGL_INLINE void igl::histc(
  35. const Eigen::MatrixBase<DerivedX > & X,
  36. const Eigen::MatrixBase<DerivedE > & E,
  37. Eigen::PlainObjectBase<DerivedB > & B)
  38. {
  39. const int m = X.size();
  40. assert(
  41. (E.bottomRightCorner(E.size()-1,1) -
  42. E.topLeftCorner(E.size()-1,1)).maxCoeff() >= 0 &&
  43. "E should be monotonically increasing");
  44. B.resize(m,1);
  45. parallel_for(m,[&](const int j)
  46. {
  47. const double x = X(j);
  48. // Boring one-offs
  49. if(x < E(0) || x > E(E.size()-1))
  50. {
  51. B(j) = -1;
  52. return;
  53. }
  54. // Find x in E
  55. int l = 0;
  56. int h = E.size()-1;
  57. int k = l;
  58. while((h-l)>1)
  59. {
  60. assert(x >= E(l));
  61. assert(x <= E(h));
  62. k = (h+l)/2;
  63. if(x < E(k))
  64. {
  65. h = k;
  66. }else
  67. {
  68. l = k;
  69. }
  70. }
  71. if(x == E(h))
  72. {
  73. k = h;
  74. }else
  75. {
  76. k = l;
  77. }
  78. B(j) = k;
  79. },1000);
  80. }
  81. template <typename DerivedE>
  82. IGL_INLINE void igl::histc(
  83. const typename DerivedE::Scalar & x,
  84. const Eigen::MatrixBase<DerivedE > & E,
  85. typename DerivedE::Index & b)
  86. {
  87. Eigen::Matrix<typename DerivedE::Scalar,1,1> X;
  88. X(0) = x;
  89. Eigen::Matrix<typename DerivedE::Index,1,1> B;
  90. hist(X,E,B);
  91. b = B(0);
  92. }
  93. #ifdef IGL_STATIC_LIBRARY
  94. // Explicit template instantiation
  95. 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::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::MatrixBase<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> >&);
  96. 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::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 1, 0, 1, 1> >&);
  97. 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::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  98. 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::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  99. 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::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::MatrixBase<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> >&);
  100. 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::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::MatrixBase<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> >&);
  101. template void igl::histc<Eigen::Matrix<float, -1, 1, 0, -1, 1>, Eigen::Matrix<float, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<float, -1, 1, 0, -1, 1> > const&, Eigen::MatrixBase<Eigen::Matrix<float, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  102. template void igl::histc<Eigen::Matrix<float, -1, 1, 0, -1, 1>, Eigen::Matrix<float, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<float, -1, 1, 0, -1, 1> > const&, Eigen::MatrixBase<Eigen::Matrix<float, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  103. #if EIGEN_VERSION_AT_LEAST(3,3,0)
  104. #else
  105. template void igl::histc<Eigen::CwiseNullaryOp<Eigen::internal::linspaced_op<int, true>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::CwiseNullaryOp<Eigen::internal::linspaced_op<int, true>, Eigen::Matrix<int, -1, 1, 0, -1, 1> > > const&, Eigen::MatrixBase<Eigen::Matrix<int, -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> >&);
  106. #endif
  107. #endif