example-imgui.hpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2021 Intel Corporation. All Rights Reserved.
3 
4 #pragma once
5 
6 #include <string>
7 #include <cmath>
8 #include <map>
9 
10 #include "example.hpp"
11 #include <imgui.h>
12 #include <imgui_impl_glfw.h>
13 
14 
16 // ImGui Helpers //
18 
19 //slider for ImGui
20 class slider {
21 public:
22  slider(const char* name, int seq_id, float init_value, float min_value, float max_value, ImVec2 position, ImVec2 size) :
23  _name(name), _seq_id(seq_id), _value(init_value), _min_value(min_value), _max_value(max_value), _position(position), _size(size) {}
24 
25  void virtual show()=0;
26 
27 public:
28  const char* _name;
29  int _seq_id;
30  float _value;
31  float _max_value;
32  float _min_value;
35 
36 };
37 
38 class hdr_slider : public slider {
39 public:
40  hdr_slider(const char* name, int seq_id, float init_value, rs2::sensor& sensor,
41  rs2_option option, rs2::option_range range, ImVec2 position, ImVec2 size) : slider(name, seq_id, init_value, range.min, range.max, position, size),
42  _sensor(sensor), _option(option), _range(range){}
43 
44  void show() override
45  {
48  //concate the name given with seq_id in order to make a unique name (uniqeness is needed for Begin())
50  ImGui::Begin(name_id.c_str(), nullptr, _sliders_flags);
51  ImGui::Text("%s",_name);
52  bool is_changed =
53  ImGui::SliderFloat("", &_value, _min_value, _max_value, "%.3f", 5.0f, false); //5.0f for logarithmic scale
54  if (is_changed) {
55  _sensor.set_option(RS2_OPTION_SEQUENCE_ID, float(_seq_id));
56  _sensor.set_option(_option, _value);
57  }
58  ImGui::End();
59  }
60 
61 public:
65  //flags for the sliders
66  const static int _sliders_flags = ImGuiWindowFlags_NoCollapse
73 };
74 
75 //text box for ImGui
76 class text_box {
77 public:
78  text_box(const char* name, ImVec2 position, ImVec2 size) : _name(name), _position(position), _size(size) {}
79 
80  void show(const char* text)
81  {
84  ImGui::Begin(_name, nullptr, _text_box_flags);
85  ImGui::Text("%s",text);
86 
87  ImGui::End();
88  }
90  _text_box_flags |= ImGuiWindowFlags_NoTitleBar;
91  }
92 
93 public:
94  const char* _name;
97  // flags for displaying text box
98  int _text_box_flags = ImGuiWindowFlags_NoCollapse
107 };
108 
109 
110 class hdr_widgets {
111 public:
112  // c'tor that creats all 4 sliders and text boxes
113  // needed to init in an init list because no default c'tor for sliders and they are allocated inside hdr_widgets
115  _exposure_slider_seq_1("Exposure", 1, 8000,
116  depth_sensor, RS2_OPTION_EXPOSURE, depth_sensor.get_option_range(RS2_OPTION_EXPOSURE), { 130, 180 }, { 350, 40 }),
117  _exposure_slider_seq_2("Exposure", 2, 18,
118  depth_sensor, RS2_OPTION_EXPOSURE, depth_sensor.get_option_range(RS2_OPTION_EXPOSURE), { 390, 180 }, { 350, 40 }),
119  _gain_slider_seq_1("Gain", 1, 25,
120  depth_sensor, RS2_OPTION_GAIN, depth_sensor.get_option_range(RS2_OPTION_GAIN), { 130, 220 }, { 350, 40 }),
121  _gain_slider_seq_2("Gain", 2, 16,
122  depth_sensor, RS2_OPTION_GAIN, depth_sensor.get_option_range(RS2_OPTION_GAIN), { 390, 220 }, { 350, 40 }),
123  _text_box_hdr_explain("HDR Tutorial", { 120, 20 }, { 1000, 140 }),
124  _text_box_first_frame("frame 1", { 200, 150 }, { 170, 40 }),
125  _text_box_second_frame("frame 2", { 460, 150 }, { 170, 40 }),
126  _text_box_hdr_frame("hdr", { 850, 280 }, { 170, 40 })
127  {
128  // init frames map
129  //for initilize only - an empty frame with its properties
130  rs2::frame frame;
131 
132  //set each frame with its properties:
133  // { tile's x coordinate, tiles's y coordinate, tile's width (in tiles), tile's height (in tiles), priority (default value=0) }, (x=0,y=0) <-> left bottom corner
134  //priority sets the order of drawing frame when two frames share part of the same tile,
135  //meaning if there are two frames: frame1 with priority=-1 and frame2 with priority=0, both with { 0,0,1,1 } as property,
136  //frame2 will be drawn on top of frame1
137  _frames_map[IR1] = frame_and_tile_property(frame, { 0,0,1,1,Priority::high });
138  _frames_map[IR2] = frame_and_tile_property(frame, { 1,0,1,1,Priority::high });
139  _frames_map[DEPTH1] = frame_and_tile_property(frame,{ 0,1,1,1,Priority::high });
140  _frames_map[DEPTH2] = frame_and_tile_property(frame, { 1,1,1,1,Priority::high });
141  _frames_map[HDR] = frame_and_tile_property(frame, { 2,0,2,2,Priority::high });
142  }
143 
144  //show the features of the ImGui we have created
145  //we need slider 2 to be showen before slider 1 (otherwise slider 1 padding is covering slider 2)
146  void render_widgets() {
147 
148  //start a new frame of ImGui
150 
151  _exposure_slider_seq_2.show();
152  _exposure_slider_seq_1.show();
153  _gain_slider_seq_2.show();
154  _gain_slider_seq_1.show();
155 
156  _text_box_first_frame.remove_title_bar();
157  _text_box_first_frame.show("Sequence 1");
158 
159  _text_box_second_frame.remove_title_bar();
160  _text_box_second_frame.show("Sequence 2");
161 
162  _text_box_hdr_frame.remove_title_bar();
163  _text_box_hdr_frame.show("HDR Stream");
164  _text_box_hdr_explain.show("This demo provides a quick overview of the High Dynamic Range (HDR) feature.\nThe HDR configures and operates on sequences of two frames configurations, for which separate exposure and gain values are defined.\nBoth configurations are streamed and the HDR feature uses both frames in order to provide the best depth image.\nChange the values of the sliders to see the impact on the HDR Depth Image.");
165 
166  //render the ImGui features: sliders and text
167  ImGui::Render();
168 
169  }
170 
171  // return a reference to frames map
173  return _frames_map;
174  }
175 
176  void update_frames_map(const rs2::video_frame& infrared_frame, const rs2::frame& depth_frame,
177  const rs2::frame& hdr_frame, rs2_metadata_type hdr_seq_id, rs2_metadata_type hdr_seq_size) {
178 
179  // frame index in frames_map are according to hdr_seq_id and hdr_seq_size
180  int infrared_index = int(hdr_seq_id);
181  int depth_index = int(hdr_seq_id + hdr_seq_size);
182  int hdr_index = int(hdr_seq_id + hdr_seq_size + 1);
183 
184  //work-around, 'get_frame_metadata' sometimes (after changing exposure or gain values) sets hdr_seq_size to 0 even though it 2 for few frames
185  //so we update the frames only if hdr_seq_size > 0. (hdr_seq_size==0 <-> frame is invalid)
186  if (hdr_seq_size > 0) {
187  _frames_map[infrared_index].first = infrared_frame;
188  _frames_map[depth_index].first = depth_frame;
189  _frames_map[hdr_index].first = hdr_frame; //HDR shall be after IR1/2 & DEPTH1/2
190  }
191  }
192 
193 public:
194 
196 
201 
206 
207  enum frame_id { IR1, IR2, DEPTH1, DEPTH2, HDR };
208 
209 };
210 
ImVec2 _position
rs2_option _option
void render_widgets()
GLuint const GLchar * name
rs2_option
Defines general configuration controls. These can generally be mapped to camera UVC controls...
Definition: rs_option.h:22
text_box _text_box_hdr_explain
rs2::sensor & _sensor
Definition: imgui.h:88
rs2::option_range _range
IMGUI_API void SetNextWindowPos(const ImVec2 &pos, ImGuiSetCond cond=0)
Definition: imgui.cpp:4923
text_box _text_box_first_frame
const char * _name
void update_frames_map(const rs2::video_frame &infrared_frame, const rs2::frame &depth_frame, const rs2::frame &hdr_frame, rs2_metadata_type hdr_seq_id, rs2_metadata_type hdr_seq_size)
virtual void show()=0
GLsizei const GLchar *const * string
void remove_title_bar()
text_box(const char *name, ImVec2 position, ImVec2 size)
ImVec2 _size
text_box _text_box_hdr_frame
void show() override
hdr_slider _gain_slider_seq_2
text_box _text_box_second_frame
GLdouble f
IMGUI_API bool Begin(const char *name, bool *p_open=NULL, ImGuiWindowFlags flags=0)
Definition: imgui.cpp:3772
Definition: getopt.h:41
GLsizeiptr size
ImVec2 _position
slider(const char *name, int seq_id, float init_value, float min_value, float max_value, ImVec2 position, ImVec2 size)
frames_mosaic & get_frames_map()
std::map< int, frame_and_tile_property > frames_mosaic
Definition: example.hpp:101
IMGUI_API void SetNextWindowSize(const ImVec2 &size, ImGuiSetCond cond=0)
Definition: imgui.cpp:4937
std::pair< rs2::frame, tile_properties > frame_and_tile_property
Definition: example.hpp:100
IMGUI_API void Text(const char *fmt,...) IM_PRINTFARGS(1)
Definition: imgui.cpp:5223
IMGUI_API void End()
Definition: imgui.cpp:4330
ImVec2 _size
const char * _name
void show(const char *text)
float _max_value
void ImGui_ImplGlfw_NewFrame(float scale_factor)
IMGUI_API bool SliderFloat(const char *label, float *v, float v_min, float v_max, const char *display_format="%.3f", float power=1.0f, bool render_bg=false)
Definition: imgui.cpp:6570
long long rs2_metadata_type
Definition: rs_types.h:301
int min(int a, int b)
Definition: lz4s.c:73
hdr_slider _exposure_slider_seq_2
float _value
GLsizei range
float _min_value
hdr_slider(const char *name, int seq_id, float init_value, rs2::sensor &sensor, rs2_option option, rs2::option_range range, ImVec2 position, ImVec2 size)
hdr_slider _gain_slider_seq_1
hdr_widgets(rs2::depth_sensor &depth_sensor)
hdr_slider _exposure_slider_seq_1
IMGUI_API void Render()
Definition: imgui.cpp:2619
frames_mosaic _frames_map
std::string to_string(T value)


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:14