浏览代码

oops, missing rgb2fromhsv

Former-commit-id: c7dc4a34ed54a61c967f56a2fc01c5852d2f4131
Alec Jacobson (jalec 12 年之前
父节点
当前提交
2355a62a2d
共有 6 个文件被更改,包括 294 次插入0 次删除
  1. 36 0
      include/igl/hsv_to_rgb.cpp
  2. 29 0
      include/igl/hsv_to_rgb.h
  3. 96 0
      include/igl/jet.cpp
  4. 38 0
      include/igl/jet.h
  5. 70 0
      include/igl/rgb_to_hsv.cpp
  6. 25 0
      include/igl/rgb_to_hsv.h

+ 36 - 0
include/igl/hsv_to_rgb.cpp

@@ -0,0 +1,36 @@
+#include "hsv_to_rgb.h"
+#include <cmath>
+
+
+template <typename T>
+void igl::hsv_to_rgb(const T * hsv, T * rgb)
+{
+  igl::hsv_to_rgb( 
+      hsv[0],hsv[1],hsv[2],
+      rgb[0],rgb[1],rgb[2]);
+}
+
+template <typename T>
+void igl::hsv_to_rgb( 
+  const T & h, const T & s, const T & v, 
+  T & r, T & g, T & b)
+{
+  // From medit
+  double f,p,q,t;
+  int    i,hh;
+  hh = ((int)h % 360) / 60.;
+  i = (int)floor((double)hh);    /* largest int <= h     */
+  f = hh - i;                    /* fractional part of h */
+  p = v * (1.0 - s);
+  q = v * (1.0 - (s * f));
+  t = v * (1.0 - (s * (1.0 - f)));
+
+  switch(i) {
+  case 0: r = v; g = t; b = p; break;
+  case 1: r = q; g = v; b = p; break;
+  case 2: r = p; g = v; b = t; break;
+  case 3: r = p; g = q; b = v; break;
+  case 4: r = t; g = p; b = v; break;
+  case 5: r = v; g = p; b = q; break;
+  }
+}

+ 29 - 0
include/igl/hsv_to_rgb.h

@@ -0,0 +1,29 @@
+#ifndef IGL_HSV_TO_RGB_H
+#define IGL_HSV_TO_RGB_H
+#include "igl_inline.h"
+namespace igl
+{
+  // Convert RGB to HSV
+  //
+  // Inputs:
+  //   h  hue value (degrees: [0,360])
+  //   s  saturation value ([0,1])
+  //   v  value value ([0,1])
+  // Outputs:
+  //   r  red value ([0,1]) 
+  //   g  green value ([0,1])
+  //   b  blue value ([0,1])
+  template <typename T>
+  void hsv_to_rgb(const T * hsv, T * rgb);
+  template <typename T>
+  void hsv_to_rgb( 
+    const T & h, const T & s, const T & v, 
+    T & r, T & g, T & b);
+};
+
+#ifdef IGL_HEADER_ONLY
+#  include "hsv_to_rgb.cpp"
+#endif
+
+#endif
+

+ 96 - 0
include/igl/jet.cpp

@@ -0,0 +1,96 @@
+#include "jet.h"
+#include "colon.h"
+
+#ifndef IGL_NO_EIGEN
+//void igl::jet(const int m, Eigen::MatrixXd & J)
+//{
+//  using namespace Eigen;
+//  using namespace igl;
+//  // Applications/MATLAB_R2012b.app/toolbox/matlab/graph3d/jet.m
+//  const int n = ceil(m/4);
+//  // resize output
+//  J.resize(m,3);
+//  // u = [(1:1:n)/n ones(1,n-1) (n:-1:1)/n]';
+//  VectorXd u(n*3-1);
+//  u.block(0,0,n-1,1) = colon(1,n)/double(n);
+//  VectorXd g;
+//  colon(0,n*3-1-1,g);
+//  g.array() = g.array() + ceil(n/2) - int((m%4)==1);
+//  VectorXd r = (g.array() + n).eval().matrix();
+//  VectorXd b = (g.array() - n).eval().matrix();
+//  int ri = 0;
+//  int gi = 0;
+//  int sb = 0;
+//  // count number of indices in b
+//  for(int j = 0;j<g.rows();j++)
+//  {
+//    sb += b(j)<m;
+//  }
+//
+//  for(int j = 0;j<g.rows();j++)
+//  {
+//    if(r(j)<m)
+//    {
+//      J(r(j),0) = u(ri++);
+//    }
+//    if(g(j)<m)
+//    {
+//      J(g(j),1) = u(gi++);
+//    }
+//    if(b(j)<m)
+//    {
+//      //J(b(j),2) = u(m- --sb);
+//    }
+//  }
+//}
+#endif
+
+
+template <typename T>
+void igl::jet(const T x, T * rgb)
+{
+  igl::jet(x,rgb[0],rgb[1],rgb[2]);
+}
+
+template <typename T>
+void igl::jet(const T x, T & r, T & g, T & b)
+{
+  // Only important if the number of colors is small. In which case the rest is
+  // still wrong anyway
+  // x = linspace(0,1,jj)' * (1-1/jj) + 1/jj;
+  //
+  const double rone = 0.8;
+  const double gone = 1.0;
+  const double bone = 1.0;
+
+  if(x<1/8.)
+  {
+    r = 0;
+    g = 0;
+    b = bone*(0.5+(x)/(1/8.)*0.5);
+  }else if(x<3/8.)
+  {
+    r = 0;
+    g = gone*(x-1/8.)/(3/8.-1/8.);
+    b = bone; 
+  }else if(x<5/8.)
+  {
+    r = rone*(x-3/8.)/(5/8.-3/8.);
+    g = gone;
+    b = (bone-(x-3/8.)/(5/8.-3/8.));
+  }else if(x<7/8.)
+  {
+    r = rone;
+    g = (gone-(x-5/8.)/(7/8.-5/8.));
+    b = 0;
+  }else
+  {
+    r = (bone-(x-7/8.)/(1.-7/8.)*0.5);
+    g = 0;
+    b = 0;
+  }
+}
+
+#ifndef IGL_NO_HEADER
+template void igl::jet<double>(double, double*);
+#endif

+ 38 - 0
include/igl/jet.h

@@ -0,0 +1,38 @@
+#ifndef IGL_JET_H
+#define IGL_JET_H
+#include "igl_inline.h"
+//#ifndef IGL_NO_EIGEN
+//#  include <Eigen/Dense>
+//#endif
+namespace igl
+{
+  // JET like MATLAB's jet
+  //
+  // Inputs:
+  //   m  number of colors 
+  // Outputs:
+  //   J  m by list of RGB colors between 0 and 1
+  //
+//#ifndef IGL_NO_EIGEN
+//  void jet(const int m, Eigen::MatrixXd & J);
+//#endif
+  // Wrapper for directly computing [r,g,b] values for a given factor f between
+  // 0 and 1
+  //
+  // Inputs:
+  //   f  factor determining color value as if 0 was min and 1 was max
+  // Outputs:
+  //   r  red value
+  //   g  green value
+  //   b  blue value
+  template <typename T>
+  void jet(const T f, T * rgb);
+  template <typename T>
+  void jet(const T f, T & r, T & g, T & b);
+};
+
+#ifdef IGL_HEADER_ONLY
+#  include "jet.cpp"
+#endif
+
+#endif

+ 70 - 0
include/igl/rgb_to_hsv.cpp

@@ -0,0 +1,70 @@
+#include "rgb_to_hsv.h"
+
+template <typename R,typename H>
+void igl::rgb_to_hsv(const R * rgb, H * hsv)
+{
+  // http://en.literateprograms.org/RGB_to_HSV_color_space_conversion_%28C%29
+  R rgb_max = 0.0;
+  R rgb_min = 1.0;
+  rgb_max = (rgb[0]>rgb_max?rgb[0]:rgb_max);
+  rgb_max = (rgb[1]>rgb_max?rgb[1]:rgb_max);
+  rgb_max = (rgb[2]>rgb_max?rgb[2]:rgb_max);
+  rgb_min = (rgb[0]<rgb_min?rgb[0]:rgb_min);
+  rgb_min = (rgb[1]<rgb_min?rgb[1]:rgb_min);
+  rgb_min = (rgb[2]<rgb_min?rgb[2]:rgb_min);
+  //hsv[2] = rgb_max;
+  hsv[2] = rgb_max;
+  if(hsv[2] == 0)
+  {
+    hsv[0]=hsv[1]=0;
+    return;
+  }
+  // normalize
+  R rgb_n[3];
+  rgb_n[0] = rgb[0]/hsv[2];
+  rgb_n[1] = rgb[1]/hsv[2];
+  rgb_n[2] = rgb[2]/hsv[2];
+  // Recomput max min?
+  rgb_max = 0;
+  rgb_max = (rgb_n[0]>rgb_max?rgb_n[0]:rgb_max);
+  rgb_max = (rgb_n[1]>rgb_max?rgb_n[1]:rgb_max);
+  rgb_max = (rgb_n[2]>rgb_max?rgb_n[2]:rgb_max);
+  rgb_min = 1;
+  rgb_min = (rgb_n[0]<rgb_min?rgb_n[0]:rgb_min);
+  rgb_min = (rgb_n[1]<rgb_min?rgb_n[1]:rgb_min);
+  rgb_min = (rgb_n[2]<rgb_min?rgb_n[2]:rgb_min);
+  hsv[1] = rgb_max - rgb_min;
+  if(hsv[1] == 0)
+  {
+    hsv[0] = 0;
+    return;
+  }
+  rgb_n[0] = (rgb_n[0] - rgb_min)/(rgb_max - rgb_min);
+  rgb_n[1] = (rgb_n[1] - rgb_min)/(rgb_max - rgb_min);
+  rgb_n[2] = (rgb_n[2] - rgb_min)/(rgb_max - rgb_min);
+  // Recomput max min?
+  rgb_max = 0;
+  rgb_max = (rgb_n[0]>rgb_max?rgb_n[0]:rgb_max);
+  rgb_max = (rgb_n[1]>rgb_max?rgb_n[1]:rgb_max);
+  rgb_max = (rgb_n[2]>rgb_max?rgb_n[2]:rgb_max);
+  rgb_min = 1;
+  rgb_min = (rgb_n[0]<rgb_min?rgb_n[0]:rgb_min);
+  rgb_min = (rgb_n[1]<rgb_min?rgb_n[1]:rgb_min);
+  rgb_min = (rgb_n[2]<rgb_min?rgb_n[2]:rgb_min);
+  if (rgb_max == rgb_n[0]) {
+    hsv[0] = 0.0 + 60.0*(rgb_n[1] - rgb_n[2]);
+    if (hsv[0] < 0.0) {
+      hsv[0] += 360.0;
+    }
+  } else if (rgb_max == rgb_n[1]) {
+    hsv[0] = 120.0 + 60.0*(rgb_n[2] - rgb_n[0]);
+  } else /* rgb_max == rgb_n[2] */ {
+    hsv[0] = 240.0 + 60.0*(rgb_n[0] - rgb_n[1]);
+  }
+}
+
+#ifndef IGL_HEADER_ONLY
+// Explicit instanciation
+template void igl::rgb_to_hsv<float, double>(float const*, double*);
+template void igl::rgb_to_hsv<double, double>(double const*, double*);
+#endif

+ 25 - 0
include/igl/rgb_to_hsv.h

@@ -0,0 +1,25 @@
+#ifndef IGL_RGB_TO_HSV_H
+#define IGL_RGB_TO_HSV_H
+#include "igl_inline.h"
+namespace igl
+{
+  // Convert RGB to HSV
+  //
+  // Inputs:
+  //   r  red value ([0,1]) 
+  //   g  green value ([0,1])
+  //   b  blue value ([0,1])
+  // Outputs:
+  //   h  hue value (degrees: [0,360])
+  //   s  saturation value ([0,1])
+  //   v  value value ([0,1])
+  template <typename R,typename H>
+  void rgb_to_hsv(const R * rgb, H * hsv);
+};
+
+#ifdef IGL_HEADER_ONLY
+#  include "rgb_to_hsv.cpp"
+#endif
+
+#endif
+