parula.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 Alec Jacobson <alecjacobson@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 "parula.h"
  9. template <typename T>
  10. IGL_INLINE void igl::parula(const T x, T * rgb)
  11. {
  12. return parula(x,rgb[0],rgb[1],rgb[2]);
  13. }
  14. template <typename T>
  15. IGL_INLINE void igl::parula(const T f, T & r, T & g, T & b)
  16. {
  17. // clamp to (0,1)
  18. const float eff_f = (f>=1.?1.:(f<=0.?0.:f));
  19. // continuous index into array
  20. const float ff = eff_f*(PARULA_COLOR_MAP.rows()-1);
  21. size_t s = std::floor(ff);
  22. size_t d = std::ceil(ff);
  23. const float t = (s==d ? 0. : (ff-s)/float(d-s));
  24. assert(t>=0 && t<=1);
  25. const auto rgb_s = PARULA_COLOR_MAP.row(s);
  26. const auto rgb_d = PARULA_COLOR_MAP.row(d);
  27. const auto rgb = rgb_s + t*(rgb_d-rgb_s);
  28. r = rgb(0);
  29. g = rgb(1);
  30. b = rgb(2);
  31. }
  32. template <typename DerivedZ, typename DerivedC>
  33. IGL_INLINE void igl::parula(
  34. const Eigen::PlainObjectBase<DerivedZ> & Z,
  35. const bool normalize,
  36. Eigen::PlainObjectBase<DerivedC> & C)
  37. {
  38. const double min_z = (normalize?Z.minCoeff():0);
  39. const double max_z = (normalize?Z.maxCoeff():1);
  40. return parula(Z,min_z,max_z,C);
  41. }
  42. template <typename DerivedZ, typename DerivedC>
  43. IGL_INLINE void igl::parula(
  44. const Eigen::PlainObjectBase<DerivedZ> & Z,
  45. const double min_z,
  46. const double max_z,
  47. Eigen::PlainObjectBase<DerivedC> & C)
  48. {
  49. C.resize(Z.rows(),3);
  50. double denom = (max_z-min_z);
  51. denom = denom==0?1:denom;
  52. for(int r = 0;r<Z.rows();r++)
  53. {
  54. parula((-min_z+Z(r,0))/denom,C(r,0),C(r,1),C(r,2));
  55. }
  56. }
  57. #ifdef IGL_STATIC_LIBRARY
  58. // Explicit template specialization
  59. // generated by autoexplicit.sh
  60. template void igl::parula<Eigen::Array<int, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Array<int, -1, 1, 0, -1, 1> > const&, bool, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  61. template void igl::parula<double>(double, double*);
  62. template void igl::parula<double>(double, double&, double&, double&);
  63. 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> >&);
  64. 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> >&);
  65. 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&, double, double, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  66. #endif