jet.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 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 "jet.h"
  9. #include "colon.h"
  10. #ifndef IGL_NO_EIGEN
  11. //void igl::jet(const int m, Eigen::MatrixXd & J)
  12. //{
  13. // using namespace Eigen;
  14. // // Applications/MATLAB_R2012b.app/toolbox/matlab/graph3d/jet.m
  15. // const int n = ceil(m/4);
  16. // // resize output
  17. // J.resize(m,3);
  18. // // u = [(1:1:n)/n ones(1,n-1) (n:-1:1)/n]';
  19. // VectorXd u(n*3-1);
  20. // u.block(0,0,n-1,1) = colon(1,n)/double(n);
  21. // VectorXd g;
  22. // colon(0,n*3-1-1,g);
  23. // g.array() = g.array() + ceil(n/2) - int((m%4)==1);
  24. // VectorXd r = (g.array() + n).eval().matrix();
  25. // VectorXd b = (g.array() - n).eval().matrix();
  26. // int ri = 0;
  27. // int gi = 0;
  28. // int sb = 0;
  29. // // count number of indices in b
  30. // for(int j = 0;j<g.rows();j++)
  31. // {
  32. // sb += b(j)<m;
  33. // }
  34. //
  35. // for(int j = 0;j<g.rows();j++)
  36. // {
  37. // if(r(j)<m)
  38. // {
  39. // J(r(j),0) = u(ri++);
  40. // }
  41. // if(g(j)<m)
  42. // {
  43. // J(g(j),1) = u(gi++);
  44. // }
  45. // if(b(j)<m)
  46. // {
  47. // //J(b(j),2) = u(m- --sb);
  48. // }
  49. // }
  50. //}
  51. #endif
  52. template <typename T>
  53. IGL_INLINE void igl::jet(const T x, T * rgb)
  54. {
  55. return jet(x,rgb[0],rgb[1],rgb[2]);
  56. }
  57. template <typename T>
  58. IGL_INLINE void igl::jet(const T x_in, T & r, T & g, T & b)
  59. {
  60. // Only important if the number of colors is small. In which case the rest is
  61. // still wrong anyway
  62. // x = linspace(0,1,jj)' * (1-1/jj) + 1/jj;
  63. //
  64. const double rone = 0.8;
  65. const double gone = 1.0;
  66. const double bone = 1.0;
  67. T x = x_in;
  68. x = (x_in<0?0:(x>1?1:x));
  69. if(x<1./8.)
  70. {
  71. r = 0;
  72. g = 0;
  73. b = bone*(0.5+(x)/(1./8.)*0.5);
  74. }else if(x<3./8.)
  75. {
  76. r = 0;
  77. g = gone*(x-1./8.)/(3./8.-1./8.);
  78. b = bone;
  79. }else if(x<5./8.)
  80. {
  81. r = rone*(x-3./8.)/(5./8.-3./8.);
  82. g = gone;
  83. b = (bone-(x-3./8.)/(5./8.-3./8.));
  84. }else if(x<7./8.)
  85. {
  86. r = rone;
  87. g = (gone-(x-5./8.)/(7./8.-5./8.));
  88. b = 0;
  89. }else
  90. {
  91. r = (rone-(x-7./8.)/(1.-7./8.)*0.5);
  92. g = 0;
  93. b = 0;
  94. }
  95. }
  96. template <typename DerivedZ, typename DerivedC>
  97. IGL_INLINE void igl::jet(
  98. const Eigen::PlainObjectBase<DerivedZ> & Z,
  99. const bool normalize,
  100. Eigen::PlainObjectBase<DerivedC> & C)
  101. {
  102. const double min_z = (normalize?Z.minCoeff():0);
  103. const double max_z = (normalize?Z.maxCoeff():-1);
  104. return jet(Z,min_z,max_z,C);
  105. }
  106. template <typename DerivedZ, typename DerivedC>
  107. IGL_INLINE void igl::jet(
  108. const Eigen::PlainObjectBase<DerivedZ> & Z,
  109. const double min_z,
  110. const double max_z,
  111. Eigen::PlainObjectBase<DerivedC> & C)
  112. {
  113. C.resize(Z.rows(),3);
  114. for(int r = 0;r<Z.rows();r++)
  115. {
  116. jet((-min_z+Z(r,0))/(max_z-min_z),C(r,0),C(r,1),C(r,2));
  117. }
  118. }
  119. #ifdef IGL_STATIC_LIBRARY
  120. // Explicit template specialization
  121. // generated by autoexplicit.sh
  122. template void igl::jet<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> >&);
  123. template void igl::jet<double>(double, double*);
  124. template void igl::jet<double>(double, double&, double&, double&);
  125. template void igl::jet<float>(float, float*);
  126. template void igl::jet<Eigen::Array<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Array<double, -1, 1, 0, -1, 1> > const&, bool, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  127. template void igl::jet<Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, bool, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  128. template void igl::jet<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> >&);
  129. template void igl::jet<float>(float, float&, float&, float&);
  130. template void igl::jet<Eigen::Matrix<float, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 1, 0, -1, 1> > const&, double, double, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  131. template void igl::jet<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> >&);
  132. template void igl::jet<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> >&);
  133. #endif