jet.cpp 4.1 KB

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