colormap.h
Go to the documentation of this file.
1 #pragma once
2 #include <cassert>
3 #include <cmath>
4 #include <algorithm>
5 #include <vector>
6 #include <string>
7 #include <memory>
8 
9 namespace colormap
10 {
11 
12  struct Color
13  {
14  double r, g, b, a;
15  };
16 
17  class Colormap
18  {
19  public:
20  virtual ~Colormap()
21  {}
22 
23  virtual Color getColor(double x) const = 0;
24 
25  virtual std::string getTitle() const = 0;
26 
27  virtual std::string getCategory() const = 0;
28 
29  virtual std::string getSource() const = 0;
30 
31  protected:
32  struct vec4
33  {
34  vec4(double a0, double a1, double a2, double a3)
35  : x(a0)
36  , y(a1)
37  , z(a2)
38  , w(a3)
39  {
40  }
41 
42  double operator[](size_t index) const
43  {
44  assert(index < 4);
45  if (index < 2)
46  {
47  if (index < 1)
48  {
49  return x;
50  }
51  else
52  {
53  return y;
54  }
55  }
56  else
57  {
58  if (index < 3)
59  {
60  return z;
61  }
62  else
63  {
64  return w;
65  }
66  }
67  }
68 
69  double& operator[](size_t index)
70  {
71  assert(index < 4);
72  if (index < 2)
73  {
74  if (index < 1)
75  {
76  return x;
77  }
78  else
79  {
80  return y;
81  }
82  }
83  else
84  {
85  if (index < 3)
86  {
87  return z;
88  }
89  else
90  {
91  return w;
92  }
93  }
94  }
95 
96  bool operator==(vec4 const& o) const
97  {
98  return x == o.x && y == o.y && z == o.z && w == o.w;
99  }
100 
101  vec4 operator*(double v) const
102  {
103  return vec4(r * v, g * v, b * v, a * v);
104  }
105 
106  vec4 operator+(vec4 const& v) const
107  {
108  return vec4(r + v.r, g + v.g, b + v.b, a + v.a);
109  }
110 
111  std::string to_string() const
112  {
113  return
114  std::string("{") +
115  std::to_string(x) + std::string(",") +
116  std::to_string(y) + std::string(",") +
117  std::to_string(z) + std::string(",") +
118  std::to_string(w) +
119  std::string("}");
120  }
121 
122  union
123  {
124  double r;
125  double x;
126  };
127  union
128  {
129  double g;
130  double y;
131  };
132  union
133  {
134  double b;
135  double z;
136  };
137  union
138  {
139  double a;
140  double w;
141  };
142  };
143 
145  {
146  public:
147  virtual ~WrapperBase()
148  {}
149 
150  protected:
151  typedef double local_real_t;
152 
153  template <class Value, class MinMax>
154  typename std::common_type<Value, MinMax>::type clamp(Value v, MinMax min, MinMax max) const
155  {
156  if (v < min)
157  {
158  return min;
159  }
160  else if (max < v)
161  {
162  return max;
163  }
164  else
165  {
166  return v;
167  }
168  }
169 
170  template <class Value>
171  Value sign(Value v) const
172  {
173  if (v < (Value)0)
174  {
175  return (Value) - 1;
176  }
177  else if ((Value)0 < v)
178  {
179  return (Value)1;
180  }
181  else
182  {
183  return (Value)0;
184  }
185  }
186 
188  {
189  return std::fabs(v);
190  }
191 
193  {
194  return std::fmod(x, y);
195  }
196  };
197  };
198 
199 } // namespace colormap
200 
201 #include "./private/all_colormaps.h"
202 
203 namespace colormap
204 {
206  {
207  public:
208  static std::vector<std::shared_ptr<Colormap const>> getAll()
209  {
210  return
211  {
213  };
214  }
215 
216  private:
217  ColormapList()
218  {
219  }
220 
221  ColormapList(ColormapList const&) = delete;
222  ColormapList(ColormapList&&) = delete;
223  ColormapList& operator=(ColormapList const&) = delete;
224  ColormapList& operator=(ColormapList&&) = delete;
225  };
226 
227 } // namespace colormap
colormap::Colormap::vec4::to_string
std::string to_string() const
Definition: colormap.h:111
colormap::Colormap::vec4::operator==
bool operator==(vec4 const &o) const
Definition: colormap.h:96
colormap::Colormap::vec4::vec4
vec4(double a0, double a1, double a2, double a3)
Definition: colormap.h:34
ColorMap::Value
Value
Color maps that associate a color to every float from [0..1].
Definition: color.h:16
colormap::Colormap::WrapperBase::sign
Value sign(Value v) const
Definition: colormap.h:171
colormap::Colormap
Definition: colormap.h:17
colormap::Colormap::vec4::w
double w
Definition: colormap.h:140
init_colormap_list.inc
colormap::Colormap::vec4::operator+
vec4 operator+(vec4 const &v) const
Definition: colormap.h:106
colormap::Colormap::vec4::y
double y
Definition: colormap.h:130
colormap::Colormap::vec4::r
double r
Definition: colormap.h:124
index
uint8_t index
Definition: EtherCATFrame.h:59
colormap::Colormap::getTitle
virtual std::string getTitle() const =0
colormap::Colormap::WrapperBase::clamp
std::common_type< Value, MinMax >::type clamp(Value v, MinMax min, MinMax max) const
Definition: colormap.h:154
colormap::Color::g
double g
Definition: colormap.h:14
colormap::Colormap::vec4::operator[]
double & operator[](size_t index)
Definition: colormap.h:69
colormap::ColormapList
Definition: colormap.h:205
colormap::Colormap::vec4
Definition: colormap.h:32
colormap::Colormap::vec4::z
double z
Definition: colormap.h:135
colormap::Colormap::vec4::operator*
vec4 operator*(double v) const
Definition: colormap.h:101
colormap::Colormap::vec4::x
double x
Definition: colormap.h:125
colormap::Colormap::WrapperBase::abs
local_real_t abs(local_real_t v) const
Definition: colormap.h:187
colormap::Colormap::getSource
virtual std::string getSource() const =0
colormap::Colormap::getColor
virtual Color getColor(double x) const =0
colormap::Color::a
double a
Definition: colormap.h:14
colormap::Colormap::vec4::operator[]
double operator[](size_t index) const
Definition: colormap.h:42
colormap::Color::b
double b
Definition: colormap.h:14
max
T max(T t1, T t2)
Definition: gdiam.h:48
colormap::Colormap::~Colormap
virtual ~Colormap()
Definition: colormap.h:20
all_colormaps.h
colormap::Colormap::getCategory
virtual std::string getCategory() const =0
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
colormap::Color
Definition: colormap.h:12
colormap::Colormap::WrapperBase::local_real_t
double local_real_t
Definition: colormap.h:151
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
colormap
This file was automatically created with "create_c++_header.sh".
Definition: colormap.h:9
colormap::Color::r
double r
Definition: colormap.h:14
colormap::Colormap::WrapperBase::mod
local_real_t mod(local_real_t x, local_real_t y) const
Definition: colormap.h:192
colormap::Colormap::WrapperBase
Definition: colormap.h:144
colormap::Colormap::vec4::g
double g
Definition: colormap.h:129
min
T min(T t1, T t2)
Definition: gdiam.h:42
colormap::ColormapList::getAll
static std::vector< std::shared_ptr< Colormap const > > getAll()
Definition: colormap.h:208
colormap::Colormap::WrapperBase::~WrapperBase
virtual ~WrapperBase()
Definition: colormap.h:147
colormap::Colormap::vec4::b
double b
Definition: colormap.h:134
colormap::Colormap::vec4::a
double a
Definition: colormap.h:139