Intel® RealSense™ Cross Platform API
Intel Realsense Cross-platform API
colorizer.h
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2017 Intel Corporation. All Rights Reserved.
3 
4 #pragma once
5 
6 #include <map>
7 #include <vector>
8 
9 namespace rs2
10 {
11  class stream_profile;
12 }
13 
14 namespace librealsense {
15 
16  class color_map
17  {
18  public:
19  color_map(std::map<float, float3> map, int steps = 4000) : _map(map)
20  {
21  initialize(steps);
22  }
23 
24  color_map(const std::vector<float3>& values, int steps = 4000)
25  {
26  for (size_t i = 0; i < values.size(); i++)
27  {
28  _map[(float)i / (values.size() - 1)] = values[i];
29  }
30  initialize(steps);
31  }
32 
33  color_map() {}
34 
35  inline float3 get(float value) const
36  {
37  if (_max == _min) return *_data;
38  auto t = (value - _min) / (_max - _min);
39  t = clamp_val(t, 0.f, 1.f);
40  return _data[(int)(t * (_size - 1))];
41  }
42 
43  float min_key() const { return _min; }
44  float max_key() const { return _max; }
45 
46  private:
47  inline float3 lerp(const float3& a, const float3& b, float t) const
48  {
49  return b * t + a * (1 - t);
50  }
51 
52  float3 calc(float value) const
53  {
54  if (_map.size() == 0) return{ value, value, value };
55  // if we have exactly this value in the map, just return it
56  if (_map.find(value) != _map.end()) return _map.at(value);
57  // if we are beyond the limits, return the first/last element
58  if (value < _map.begin()->first) return _map.begin()->second;
59  if (value > _map.rbegin()->first) return _map.rbegin()->second;
60 
61  auto lower = _map.lower_bound(value) == _map.begin() ? _map.begin() : --(_map.lower_bound(value));
62  auto upper = _map.upper_bound(value);
63 
64  auto t = (value - lower->first) / (upper->first - lower->first);
65  auto c1 = lower->second;
66  auto c2 = upper->second;
67  return lerp(c1, c2, t);
68  }
69 
70  void initialize(int steps)
71  {
72  if (_map.size() == 0) return;
73 
74  _min = _map.begin()->first;
75  _max = _map.rbegin()->first;
76 
77  _cache.resize(steps + 1);
78  for (int i = 0; i <= steps; i++)
79  {
80  auto t = (float)i / steps;
81  auto x = _min + t*(_max - _min);
82  _cache[i] = calc(x);
83  }
84 
85  // Save size and data to avoid STL checks penalties in DEBUG
86  _size = _cache.size();
87  _data = _cache.data();
88  }
89 
90  std::map<float, float3> _map;
91  std::vector<float3> _cache;
92  float _min, _max;
93  size_t _size; float3* _data;
94  };
95 
96  class colorizer : public processing_block
97  {
98  public:
99  colorizer();
100 
101  private:
102  float _min, _max;
103  bool _equalize;
104  std::vector<color_map*> _maps;
105  int _map_index = 0;
106  int _preset = 0;
107  std::mutex _mutex;
108  std::shared_ptr<rs2::stream_profile> _stream;
109  };
110 }
Definition: backend.h:351
color_map(const std::vector< float3 > &values, int steps=4000)
Definition: colorizer.h:24
Definition: synthetic-stream.h:41
Definition: rs_context.hpp:11
float max_key() const
Definition: colorizer.h:44
float min_key() const
Definition: colorizer.h:43
Definition: algo.h:16
color_map()
Definition: colorizer.h:33
Definition: colorizer.h:16
Definition: types.h:414
T clamp_val(T val, const T &min, const T &max)
Definition: types.h:118
Definition: colorizer.h:96
color_map(std::map< float, float3 > map, int steps=4000)
Definition: colorizer.h:19