parula.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include "parula.h"
  2. template <typename T>
  3. IGL_INLINE void igl::parula(const T x, T * rgb)
  4. {
  5. return parula(x,rgb[0],rgb[1],rgb[2]);
  6. }
  7. template <typename T>
  8. IGL_INLINE void igl::parula(const T f, T & r, T & g, T & b)
  9. {
  10. // clamp to (0,1)
  11. const float eff_f = (f>=1.?1.:(f<=0.?0.:f));
  12. // continuous index into array
  13. const float ff = eff_f*(PARULA_COLOR_MAP.rows()-1);
  14. size_t s = floor(ff);
  15. size_t d = ceil(ff);
  16. const float t = (s==d ? 0. : (ff-s)/float(d-s));
  17. assert(t>=0 && t<=1);
  18. const auto rgb_s = PARULA_COLOR_MAP.row(s);
  19. const auto rgb_d = PARULA_COLOR_MAP.row(d);
  20. const auto rgb = rgb_s + t*(rgb_d-rgb_s);
  21. r = rgb(0);
  22. g = rgb(1);
  23. b = rgb(2);
  24. }
  25. template <typename DerivedZ, typename DerivedC>
  26. IGL_INLINE void igl::parula(
  27. const Eigen::PlainObjectBase<DerivedZ> & Z,
  28. const bool normalize,
  29. Eigen::PlainObjectBase<DerivedC> & C)
  30. {
  31. const double min_z = (normalize?Z.minCoeff():0);
  32. const double max_z = (normalize?Z.maxCoeff():1);
  33. return parula(Z,min_z,max_z,C);
  34. }
  35. template <typename DerivedZ, typename DerivedC>
  36. IGL_INLINE void igl::parula(
  37. const Eigen::PlainObjectBase<DerivedZ> & Z,
  38. const double min_z,
  39. const double max_z,
  40. Eigen::PlainObjectBase<DerivedC> & C)
  41. {
  42. C.resize(Z.rows(),3);
  43. for(int r = 0;r<Z.rows();r++)
  44. {
  45. parula((-min_z+Z(r,0))/(max_z-min_z),C(r,0),C(r,1),C(r,2));
  46. }
  47. }
  48. #ifdef IGL_STATIC_LIBRARY
  49. // Explicit template instanciation
  50. template void igl::parula<double>(double, double*);
  51. template void igl::parula<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&, bool, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  52. #endif