gtsam_points
Loading...
Searching...
No Matches
async_light_viewer.hpp
Go to the documentation of this file.
1#ifndef GUIK_ASYNC_LIGHT_VIEWER_HPP
2#define GUIK_ASYNC_LIGHT_VIEWER_HPP
3
4#include <atomic>
5#include <thread>
8
9namespace guik {
10
12private:
13 AsyncLightViewer(const Eigen::Vector2i& size, bool background, const std::string& title);
14
15public:
17
19 static AsyncLightViewer* instance(const Eigen::Vector2i& size = Eigen::Vector2i(-1, -1), bool background = false, const std::string& title = "screen");
20
22 static void destroy();
23
25 static void wait();
26
28 static void wait_until_click();
29
31 static void toggle_wait();
32
33 void invoke(const std::function<void()>& func);
34 void invoke_after_rendering(const std::function<void()>& func);
35 void invoke_once(const std::string& label, const std::function<void()>& func);
36
38 void remove_image(const std::string& name);
39 void update_image(const std::string& name, int width, int height, const std::vector<unsigned char>& rgba_bytes, double scale = -1.0, int order = -1);
40
41 // Plotting methods
42 void clear_plots(bool clear_settings = true);
43 void remove_plot(const std::string& plot_name, const std::string& label = "");
44 void setup_plot(const std::string& plot_name, int width, int height, int plot_flags = 0, int x_flags = 0, int y_flags = 0, int order = -1);
45 void link_plot_axis(const std::string& plot_name, int link_id, int axis);
46 void link_plot_axes(const std::string& plot_name, int link_id, int axes = -1);
47 void setup_legend(const std::string& plot_name, int loc, int flags = 0);
48 void fit_plot(const std::string& plot_name);
50 void setup_plot_group_order(const std::string& group_name, int order);
51
52 // Update plot methods
53 void update_plot(const std::string& plot_name, const std::string& label, const std::shared_ptr<const PlotData>& plot);
54 void update_plot_line(const std::string& plot_name, const std::string& label, const std::vector<double>& ys, int line_flags = 0, size_t max_num_data = 2048);
56 const std::string& plot_name,
57 const std::string& label,
58 const std::vector<double>& xs,
59 const std::vector<double>& ys,
60 int line_flags = 0,
61 size_t max_num_data = 2048);
62 void update_plot_scatter(const std::string& plot_name, const std::string& label, const std::vector<double>& ys, int scatter_flags = 0);
63 void update_plot_scatter(const std::string& plot_name, const std::string& label, const std::vector<double>& xs, const std::vector<double>& ys, int scatter_flags = 0);
64 void update_plot_stairs(const std::string& plot_name, const std::string& label, const std::vector<double>& ys, int stairs_flags = 0);
65 void update_plot_stairs(const std::string& plot_name, const std::string& label, const std::vector<double>& xs, const std::vector<double>& ys, int stairs_flags = 0);
67 const std::string& plot_name,
68 const std::string& label,
69 const std::vector<double>& xs,
70 int bins = -2,
71 const Eigen::Vector2d& range = Eigen::Vector2d(0.0, 0.0),
72 int histogram_flags = 0);
74 const std::string& plot_name,
75 const std::string& label,
76 const std::vector<double>& xs,
77 const std::vector<double>& ys,
78 int x_bins = -2,
79 int y_bins = -2,
80 const Eigen::Vector2d& x_range = Eigen::Vector2d(0.0, 0.0),
81 const Eigen::Vector2d& y_range = Eigen::Vector2d(0.0, 0.0),
82 int histogram_flags = 0);
83
84 // Update plot template methods
85 template <typename T>
86 auto update_plot_line(const std::string& plot_name, const std::string& label, const std::vector<T>& ys, int line_flags = 0, size_t max_num_data = 8192 * 12)
87 -> std::enable_if_t<std::is_arithmetic_v<T>, void>;
88 template <typename T1, typename T2>
90 const std::string& plot_name,
91 const std::string& label,
92 const std::vector<T1>& xs,
93 const std::vector<T2>& ys,
94 int line_flags = 0,
95 size_t max_num_data = 8192 * 12);
96 template <typename T, int D, typename Alloc>
98 const std::string& plot_name,
99 const std::string& label,
100 const std::vector<Eigen::Matrix<T, D, 1>, Alloc>& data,
101 int line_flags = 0,
102 size_t max_num_data = 8192 * 12);
103
104 template <typename T, typename Func>
105 auto
106 update_plot_line(const std::string& plot_name, const std::string& label, const std::vector<T>& data, const Func& transform, int line_flags = 0, size_t max_num_data = 8192 * 12)
107 -> std::enable_if_t<!std::is_arithmetic_v<decltype(transform(data[0]))>, void> {
108 std::vector<double> xs(data.size());
109 std::vector<double> ys(data.size());
110 for (size_t i = 0; i < data.size(); i++) {
111 const auto pt = transform(data[i]);
112 xs[i] = pt[0];
113 ys[i] = pt[1];
114 }
115 update_plot_line(plot_name, label, xs, ys, line_flags, max_num_data);
116 }
117 template <typename T, typename Func>
118 auto
119 update_plot_line(const std::string& plot_name, const std::string& label, const std::vector<T>& data, const Func& transform, int line_flags = 0, size_t max_num_data = 8192 * 12)
120 -> std::enable_if_t<std::is_arithmetic_v<decltype(transform(data[0]))>, void> {
121 std::vector<double> xs(data.size());
122 std::vector<double> ys(data.size());
123 for (size_t i = 0; i < data.size(); i++) {
124 const auto pt = transform(data[i]);
125 xs[i] = i;
126 ys[i] = pt;
127 }
128 update_plot_line(plot_name, label, xs, ys, line_flags, max_num_data);
129 }
130
131 template <typename T>
132 auto update_plot_scatter(const std::string& plot_name, const std::string& label, const std::vector<T>& ys, int scatter_flags = 0)
133 -> std::enable_if_t<std::is_arithmetic_v<T>, void>;
134 template <typename T1, typename T2>
135 void update_plot_scatter(const std::string& plot_name, const std::string& label, const std::vector<T1>& xs, const std::vector<T2>& ys, int scatter_flags = 0);
136 template <typename T, int D, typename Alloc>
137 void update_plot_scatter(const std::string& plot_name, const std::string& label, const std::vector<Eigen::Matrix<T, D, 1>, Alloc>& data, int scatter_flags = 0);
138 template <typename T, typename Func>
140 const std::string& plot_name,
141 const std::string& label,
142 const std::vector<T>& data,
143 const Func& transform,
144 int scatter_flags = 0,
145 size_t max_num_data = 8192 * 12) -> std::enable_if_t<!std::is_arithmetic_v<decltype(transform(data[0]))>, void> {
146 std::vector<double> xs(data.size());
147 std::vector<double> ys(data.size());
148 for (size_t i = 0; i < data.size(); i++) {
149 const auto pt = transform(data[i]);
150 xs[i] = pt[0];
151 ys[i] = pt[1];
152 }
153 update_plot_scatter(plot_name, label, xs, ys, scatter_flags);
154 }
155 template <typename T, typename Func>
157 const std::string& plot_name,
158 const std::string& label,
159 const std::vector<T>& data,
160 const Func& transform,
161 int scatter_flags = 0,
162 size_t max_num_data = 8192 * 12) -> std::enable_if_t<std::is_arithmetic_v<decltype(transform(data[0]))>, void> {
163 std::vector<double> xs(data.size());
164 std::vector<double> ys(data.size());
165 for (size_t i = 0; i < data.size(); i++) {
166 const auto pt = transform(data[i]);
167 xs[i] = i;
168 ys[i] = pt;
169 }
170 update_plot_scatter(plot_name, label, xs, ys, scatter_flags);
171 }
172
173 template <typename T>
174 void update_plot_stairs(const std::string& plot_name, const std::string& label, const std::vector<T>& ys, int stairs_flags = 0);
175 template <typename T1, typename T2>
176 void update_plot_stairs(const std::string& plot_name, const std::string& label, const std::vector<T1>& xs, const std::vector<T2>& ys, int stairs_flags = 0);
177 template <typename T>
179 const std::string& plot_name,
180 const std::string& label,
181 const std::vector<T>& xs,
182 int bins = -2,
183 const Eigen::Vector2d& range = Eigen::Vector2d(0.0, 0.0),
184 int histogram_flags = 0);
185 template <typename T1, typename T2>
187 const std::string& plot_name,
188 const std::string& label,
189 const std::vector<T1>& xs,
190 const std::vector<T2>& ys,
191 int x_bins = -2,
192 int y_bins = -2,
193 const Eigen::Vector2d& x_range = Eigen::Vector2d(0.0, 0.0),
194 const Eigen::Vector2d& y_range = Eigen::Vector2d(0.0, 0.0),
195 int histogram_flags = 0);
196
197 // Set plot style methods
198 void set_plot_style(const std::string& plot_name, const std::string& label, const PlotStyleConstPtr& style);
199 void set_line_style(const std::string& plot_name, const std::string& label, const Eigen::Vector4f& color = Eigen::Vector4f(0, 0, 0, -1), float weight = -1);
201 const std::string& plot_name,
202 const std::string& label,
203 int marker = 0,
204 float size = -1,
205 const Eigen::Vector4f& fill = Eigen::Vector4f(0, 0, 0, -1),
206 float weight = -1,
207 const Eigen::Vector4f& outline = Eigen::Vector4f(0, 0, 0, -1));
208
209 // This method causes synchronization with the visualization thread.
210 // Do not call this frequently.
211 AsyncLightViewerContext async_sub_viewer(const std::string& context_name, const Eigen::Vector2i& canvas_size = Eigen::Vector2i(-1, -1));
212
213private:
214 void ui_callback();
215
216private:
217 static std::unique_ptr<AsyncLightViewer> inst;
218
219 std::atomic_bool kill_switch;
220 std::thread thread;
221
222 std::atomic_bool toggle_state;
223 std::atomic_bool toggle_step;
224 std::atomic_bool show_toggle;
225 std::atomic_uint64_t toggle_count;
226};
227
228inline AsyncLightViewer* async_viewer(const Eigen::Vector2i& size = Eigen::Vector2i(-1, -1), bool background = false, const std::string& title = "screen") {
229 return AsyncLightViewer::instance(size, background, title);
230}
231
232inline void async_destroy() {
234}
235
236inline void async_wait() {
238}
239
243
247
248// Template methods
249
250template <typename T>
251auto AsyncLightViewer::update_plot_line(const std::string& plot_name, const std::string& label, const std::vector<T>& ys, int line_flags, size_t max_num_data)
252 -> std::enable_if_t<std::is_arithmetic_v<T>, void> {
253 std::vector<double> ys_(ys.size());
254 std::copy(ys.begin(), ys.end(), ys_.begin());
255 update_plot_line(plot_name, label, ys_, line_flags, max_num_data);
256}
257
258template <typename T1, typename T2>
260 const std::string& plot_name,
261 const std::string& label,
262 const std::vector<T1>& xs,
263 const std::vector<T2>& ys,
264 int line_flags,
265 size_t max_num_data) {
266 std::vector<double> xs_(xs.size());
267 std::vector<double> ys_(ys.size());
268 std::copy(xs.begin(), xs.end(), xs_.begin());
269 std::copy(ys.begin(), ys.end(), ys_.begin());
270 update_plot_line(plot_name, label, xs_, ys_, line_flags, max_num_data);
271}
272
273template <typename T, int D, typename Alloc>
275 const std::string& plot_name,
276 const std::string& label,
277 const std::vector<Eigen::Matrix<T, D, 1>, Alloc>& data,
278 int line_flags,
279 size_t max_num_data) {
280 std::vector<double> xs_(data.size());
281 std::vector<double> ys_(data.size());
282 std::transform(data.begin(), data.end(), xs_.begin(), [](const Eigen::Matrix<T, D, 1>& v) { return v[0]; });
283 std::transform(data.begin(), data.end(), ys_.begin(), [](const Eigen::Matrix<T, D, 1>& v) { return v[1]; });
284 update_plot_line(plot_name, label, xs_, ys_, line_flags, max_num_data);
285}
286
287template <typename T>
288auto AsyncLightViewer::update_plot_scatter(const std::string& plot_name, const std::string& label, const std::vector<T>& ys, int scatter_flags)
289 -> std::enable_if_t<std::is_arithmetic_v<T>, void> {
290 std::vector<double> ys_(ys.size());
291 std::copy(ys.begin(), ys.end(), ys_.begin());
292 update_plot_scatter(plot_name, label, ys_, scatter_flags);
293}
294
295template <typename T1, typename T2>
296void AsyncLightViewer::update_plot_scatter(const std::string& plot_name, const std::string& label, const std::vector<T1>& xs, const std::vector<T2>& ys, int scatter_flags) {
297 std::vector<double> xs_(xs.size());
298 std::vector<double> ys_(ys.size());
299 std::copy(xs.begin(), xs.end(), xs_.begin());
300 std::copy(ys.begin(), ys.end(), ys_.begin());
301 update_plot_scatter(plot_name, label, xs_, ys_, scatter_flags);
302}
303
304template <typename T, int D, typename Alloc>
305void AsyncLightViewer::update_plot_scatter(const std::string& plot_name, const std::string& label, const std::vector<Eigen::Matrix<T, D, 1>, Alloc>& data, int scatter_flags) {
306 std::vector<double> xs_(data.size());
307 std::vector<double> ys_(data.size());
308 std::transform(data.begin(), data.end(), xs_.begin(), [](const Eigen::Matrix<T, D, 1>& v) { return v[0]; });
309 std::transform(data.begin(), data.end(), ys_.begin(), [](const Eigen::Matrix<T, D, 1>& v) { return v[1]; });
310 update_plot_scatter(plot_name, label, xs_, ys_, scatter_flags);
311}
312
313template <typename T>
314void AsyncLightViewer::update_plot_stairs(const std::string& plot_name, const std::string& label, const std::vector<T>& ys, int stairs_flags) {
315 std::vector<double> ys_(ys.size());
316 std::copy(ys.begin(), ys.end(), ys_.begin());
317 update_plot_stairs(plot_name, label, ys_, stairs_flags);
318}
319
320template <typename T1, typename T2>
321void AsyncLightViewer::update_plot_stairs(const std::string& plot_name, const std::string& label, const std::vector<T1>& xs, const std::vector<T2>& ys, int stairs_flags) {
322 std::vector<double> xs_(xs.size());
323 std::vector<double> ys_(ys.size());
324 std::copy(xs.begin(), xs.end(), xs_.begin());
325 std::copy(ys.begin(), ys.end(), ys_.begin());
326 update_plot_stairs(plot_name, label, xs_, ys_, stairs_flags);
327}
328
329template <typename T>
331 const std::string& plot_name,
332 const std::string& label,
333 const std::vector<T>& xs,
334 int bins,
335 const Eigen::Vector2d& range,
336 int histogram_flags) {
337 //
338 std::vector<double> xs_(xs.size());
339 std::copy(xs.begin(), xs.end(), xs_.begin());
340 update_plot_histogram(plot_name, label, xs_, bins, range, histogram_flags);
341}
342
343template <typename T1, typename T2>
345 const std::string& plot_name,
346 const std::string& label,
347 const std::vector<T1>& xs,
348 const std::vector<T2>& ys,
349 int x_bins,
350 int y_bins,
351 const Eigen::Vector2d& x_range,
352 const Eigen::Vector2d& y_range,
353 int histogram_flags) {
354 //
355 std::vector<double> xs_(xs.size());
356 std::vector<double> ys_(ys.size());
357 std::copy(xs.begin(), xs.end(), xs_.begin());
358 std::copy(ys.begin(), ys.end(), ys_.begin());
359 update_plot_histogram(plot_name, label, xs_, ys_, x_bins, y_bins, x_range, y_range, histogram_flags);
360}
361
362} // namespace guik
363
364#endif
Async interface to manipulate LightViewerContext. All OpenGL operations will be done in the viewer th...
Definition async_light_viewer_context.hpp:13
Definition async_light_viewer.hpp:11
static void wait()
Wait for the viewer to be closed.
void invoke(const std::function< void()> &func)
void fit_plot(const std::string &plot_name)
static void wait_until_click()
Wait for a break button to be clicked.
auto update_plot_scatter(const std::string &plot_name, const std::string &label, const std::vector< T > &data, const Func &transform, int scatter_flags=0, size_t max_num_data=8192 *12) -> std::enable_if_t<!std::is_arithmetic_v< decltype(transform(data[0]))>, void >
Definition async_light_viewer.hpp:139
void link_plot_axes(const std::string &plot_name, int link_id, int axes=-1)
void setup_plot_group_order(const std::string &group_name, int order)
void clear_plots(bool clear_settings=true)
void set_plot_style(const std::string &plot_name, const std::string &label, const PlotStyleConstPtr &style)
auto update_plot_line(const std::string &plot_name, const std::string &label, const std::vector< T > &data, const Func &transform, int line_flags=0, size_t max_num_data=8192 *12) -> std::enable_if_t<!std::is_arithmetic_v< decltype(transform(data[0]))>, void >
Definition async_light_viewer.hpp:106
void remove_image(const std::string &name)
static void destroy()
Destroy the viewer in the background thread.
void setup_legend(const std::string &plot_name, int loc, int flags=0)
auto update_plot_scatter(const std::string &plot_name, const std::string &label, const std::vector< T > &data, const Func &transform, int scatter_flags=0, size_t max_num_data=8192 *12) -> std::enable_if_t< std::is_arithmetic_v< decltype(transform(data[0]))>, void >
Definition async_light_viewer.hpp:156
void update_plot_line(const std::string &plot_name, const std::string &label, const std::vector< double > &xs, const std::vector< double > &ys, int line_flags=0, size_t max_num_data=2048)
auto update_plot_line(const std::string &plot_name, const std::string &label, const std::vector< T > &data, const Func &transform, int line_flags=0, size_t max_num_data=8192 *12) -> std::enable_if_t< std::is_arithmetic_v< decltype(transform(data[0]))>, void >
Definition async_light_viewer.hpp:119
void update_plot_stairs(const std::string &plot_name, const std::string &label, const std::vector< double > &xs, const std::vector< double > &ys, int stairs_flags=0)
void invoke_after_rendering(const std::function< void()> &func)
AsyncLightViewerContext async_sub_viewer(const std::string &context_name, const Eigen::Vector2i &canvas_size=Eigen::Vector2i(-1, -1))
void setup_plot(const std::string &plot_name, int width, int height, int plot_flags=0, int x_flags=0, int y_flags=0, int order=-1)
void update_plot_histogram(const std::string &plot_name, const std::string &label, const std::vector< double > &xs, int bins=-2, const Eigen::Vector2d &range=Eigen::Vector2d(0.0, 0.0), int histogram_flags=0)
void link_plot_axis(const std::string &plot_name, int link_id, int axis)
void update_plot_scatter(const std::string &plot_name, const std::string &label, const std::vector< double > &ys, int scatter_flags=0)
void set_line_style(const std::string &plot_name, const std::string &label, const Eigen::Vector4f &color=Eigen::Vector4f(0, 0, 0, -1), float weight=-1)
void update_plot(const std::string &plot_name, const std::string &label, const std::shared_ptr< const PlotData > &plot)
void update_plot_histogram(const std::string &plot_name, const std::string &label, const std::vector< double > &xs, const std::vector< double > &ys, int x_bins=-2, int y_bins=-2, const Eigen::Vector2d &x_range=Eigen::Vector2d(0.0, 0.0), const Eigen::Vector2d &y_range=Eigen::Vector2d(0.0, 0.0), int histogram_flags=0)
static AsyncLightViewer * instance(const Eigen::Vector2i &size=Eigen::Vector2i(-1, -1), bool background=false, const std::string &title="screen")
Create and run the viewer in a background thread.
void update_image(const std::string &name, int width, int height, const std::vector< unsigned char > &rgba_bytes, double scale=-1.0, int order=-1)
static void toggle_wait()
Wait while a toggle checkbox is checked.
void update_plot_scatter(const std::string &plot_name, const std::string &label, const std::vector< double > &xs, const std::vector< double > &ys, int scatter_flags=0)
void invoke_once(const std::string &label, const std::function< void()> &func)
void update_plot_line(const std::string &plot_name, const std::string &label, const std::vector< double > &ys, int line_flags=0, size_t max_num_data=2048)
void update_plot_stairs(const std::string &plot_name, const std::string &label, const std::vector< double > &ys, int stairs_flags=0)
void remove_plot(const std::string &plot_name, const std::string &label="")
void set_scatter_style(const std::string &plot_name, const std::string &label, int marker=0, float size=-1, const Eigen::Vector4f &fill=Eigen::Vector4f(0, 0, 0, -1), float weight=-1, const Eigen::Vector4f &outline=Eigen::Vector4f(0, 0, 0, -1))
Definition drawable_container.hpp:9
void async_toggle_wait()
Definition async_light_viewer.hpp:244
void async_wait()
Definition async_light_viewer.hpp:236
void async_destroy()
Definition async_light_viewer.hpp:232
std::shared_ptr< const PlotStyle > PlotStyleConstPtr
Definition light_viewer.hpp:21
AsyncLightViewer * async_viewer(const Eigen::Vector2i &size=Eigen::Vector2i(-1, -1), bool background=false, const std::string &title="screen")
Definition async_light_viewer.hpp:228
void async_wait_until_click()
Definition async_light_viewer.hpp:240