iridescence
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
11class AsyncLightViewer : public AsyncLightViewerContext {
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
34 static bool running();
35
37 void invoke(const std::function<void()>& func);
39 void invoke_after_rendering(const std::function<void()>& func);
41 void invoke_once(const std::string& label, const std::function<void()>& func);
42
46 void remove_image(const std::string& name);
54 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);
55
56 // Plotting methods
59 void clear_plots(bool clear_settings = true);
63 void remove_plot(const std::string& plot_name, const std::string& label = "");
72 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);
77 void link_plot_axis(const std::string& plot_name, int link_id, int axis);
82 void link_plot_axes(const std::string& plot_name, int link_id, int axes = -1);
87 void setup_legend(const std::string& plot_name, int loc, int flags = 0);
90 void fit_plot(const std::string& plot_name);
96 void setup_plot_group_order(const std::string& group_name, int order);
97
98 // Update plot methods
103 void update_plot(const std::string& plot_name, const std::string& label, const std::shared_ptr<const PlotData>& plot);
110 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);
119 const std::string& plot_name,
120 const std::string& label,
121 const std::vector<double>& xs,
122 const std::vector<double>& ys,
123 int line_flags = 0,
124 size_t max_num_data = 2048);
130 void update_plot_scatter(const std::string& plot_name, const std::string& label, const std::vector<double>& ys, int scatter_flags = 0);
137 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);
143 void update_plot_stairs(const std::string& plot_name, const std::string& label, const std::vector<double>& ys, int stairs_flags = 0);
150 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);
159 const std::string& plot_name,
160 const std::string& label,
161 const std::vector<double>& xs,
162 int bins = -2,
163 const Eigen::Vector2d& range = Eigen::Vector2d(0.0, 0.0),
164 int histogram_flags = 0);
167 const std::string& plot_name,
168 const std::string& label,
169 const std::vector<double>& xs,
170 const std::vector<double>& ys,
171 int x_bins = -2,
172 int y_bins = -2,
173 const Eigen::Vector2d& x_range = Eigen::Vector2d(0.0, 0.0),
174 const Eigen::Vector2d& y_range = Eigen::Vector2d(0.0, 0.0),
175 int histogram_flags = 0);
176
177 // Update plot template methods
179 template <typename T>
180 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)
181 -> std::enable_if_t<std::is_arithmetic_v<T>, void>;
183 template <typename T1, typename T2>
184 void update_plot_line(
185 const std::string& plot_name,
186 const std::string& label,
187 const std::vector<T1>& xs,
188 const std::vector<T2>& ys,
189 int line_flags = 0,
190 size_t max_num_data = 8192 * 12);
192 template <typename T, int D, typename Alloc>
193 void update_plot_line(
194 const std::string& plot_name,
195 const std::string& label,
196 const std::vector<Eigen::Matrix<T, D, 1>, Alloc>& data,
197 int line_flags = 0,
198 size_t max_num_data = 8192 * 12);
199
201 template <typename T, typename Func>
202 auto
203 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)
204 -> std::enable_if_t<!std::is_arithmetic_v<decltype(transform(data[0]))>, void> {
205 std::vector<double> xs(data.size());
206 std::vector<double> ys(data.size());
207 for (size_t i = 0; i < data.size(); i++) {
208 const auto pt = transform(data[i]);
209 xs[i] = pt[0];
210 ys[i] = pt[1];
211 }
212 update_plot_line(plot_name, label, xs, ys, line_flags, max_num_data);
213 }
214
215 template <typename T, typename Func>
216 auto
217 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)
218 -> std::enable_if_t<std::is_arithmetic_v<decltype(transform(data[0]))>, void> {
219 std::vector<double> xs(data.size());
220 std::vector<double> ys(data.size());
221 for (size_t i = 0; i < data.size(); i++) {
222 const auto pt = transform(data[i]);
223 xs[i] = i;
224 ys[i] = pt;
225 }
226 update_plot_line(plot_name, label, xs, ys, line_flags, max_num_data);
227 }
228
230 template <typename T>
231 auto update_plot_scatter(const std::string& plot_name, const std::string& label, const std::vector<T>& ys, int scatter_flags = 0)
232 -> std::enable_if_t<std::is_arithmetic_v<T>, void>;
234 template <typename T1, typename T2>
235 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);
237 template <typename T, int D, typename Alloc>
238 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);
240 template <typename T, typename Func>
242 const std::string& plot_name,
243 const std::string& label,
244 const std::vector<T>& data,
245 const Func& transform,
246 int scatter_flags = 0,
247 size_t max_num_data = 8192 * 12) -> std::enable_if_t<!std::is_arithmetic_v<decltype(transform(data[0]))>, void> {
248 std::vector<double> xs(data.size());
249 std::vector<double> ys(data.size());
250 for (size_t i = 0; i < data.size(); i++) {
251 const auto pt = transform(data[i]);
252 xs[i] = pt[0];
253 ys[i] = pt[1];
254 }
255 update_plot_scatter(plot_name, label, xs, ys, scatter_flags);
256 }
257
258 template <typename T, typename Func>
260 const std::string& plot_name,
261 const std::string& label,
262 const std::vector<T>& data,
263 const Func& transform,
264 int scatter_flags = 0,
265 size_t max_num_data = 8192 * 12) -> std::enable_if_t<std::is_arithmetic_v<decltype(transform(data[0]))>, void> {
266 std::vector<double> xs(data.size());
267 std::vector<double> ys(data.size());
268 for (size_t i = 0; i < data.size(); i++) {
269 const auto pt = transform(data[i]);
270 xs[i] = i;
271 ys[i] = pt;
272 }
273 update_plot_scatter(plot_name, label, xs, ys, scatter_flags);
274 }
275
277 template <typename T>
278 void update_plot_stairs(const std::string& plot_name, const std::string& label, const std::vector<T>& ys, int stairs_flags = 0);
280 template <typename T1, typename T2>
281 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);
283 template <typename T>
285 const std::string& plot_name,
286 const std::string& label,
287 const std::vector<T>& xs,
288 int bins = -2,
289 const Eigen::Vector2d& range = Eigen::Vector2d(0.0, 0.0),
290 int histogram_flags = 0);
292 template <typename T1, typename T2>
294 const std::string& plot_name,
295 const std::string& label,
296 const std::vector<T1>& xs,
297 const std::vector<T2>& ys,
298 int x_bins = -2,
299 int y_bins = -2,
300 const Eigen::Vector2d& x_range = Eigen::Vector2d(0.0, 0.0),
301 const Eigen::Vector2d& y_range = Eigen::Vector2d(0.0, 0.0),
302 int histogram_flags = 0);
303
304 // Set plot style methods
309 void set_plot_style(const std::string& plot_name, const std::string& label, const PlotStyleConstPtr& style);
315 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);
325 const std::string& plot_name,
326 const std::string& label,
327 int marker = 0,
328 float size = -1,
329 const Eigen::Vector4f& fill = Eigen::Vector4f(0, 0, 0, -1),
330 float weight = -1,
331 const Eigen::Vector4f& outline = Eigen::Vector4f(0, 0, 0, -1));
332
335 AsyncLightViewerContext async_sub_viewer(const std::string& context_name, const Eigen::Vector2i& canvas_size = Eigen::Vector2i(-1, -1));
336
339 void remove_async_sub_viewer(const std::string& context_name);
340
341private:
342 void ui_callback();
343
344private:
345 static std::unique_ptr<AsyncLightViewer> inst;
346
347 std::atomic_bool kill_switch;
348 std::thread thread;
349
350 std::atomic_bool toggle_state;
351 std::atomic_bool toggle_step;
352 std::atomic_bool show_toggle;
353 std::atomic_uint64_t toggle_count;
354};
355
356inline AsyncLightViewer* async_viewer(const Eigen::Vector2i& size = Eigen::Vector2i(-1, -1), bool background = false, const std::string& title = "screen") {
357 return AsyncLightViewer::instance(size, background, title);
358}
359
360inline void async_destroy() {
362}
363
364inline void async_wait() {
366}
367
371
375
376inline bool async_running() {
378}
379
380// Template methods
381
382template <typename T>
383auto 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)
384 -> std::enable_if_t<std::is_arithmetic_v<T>, void> {
385 std::vector<double> ys_(ys.size());
386 std::copy(ys.begin(), ys.end(), ys_.begin());
387 update_plot_line(plot_name, label, ys_, line_flags, max_num_data);
388}
389
390template <typename T1, typename T2>
392 const std::string& plot_name,
393 const std::string& label,
394 const std::vector<T1>& xs,
395 const std::vector<T2>& ys,
396 int line_flags,
397 size_t max_num_data) {
398 std::vector<double> xs_(xs.size());
399 std::vector<double> ys_(ys.size());
400 std::copy(xs.begin(), xs.end(), xs_.begin());
401 std::copy(ys.begin(), ys.end(), ys_.begin());
402 update_plot_line(plot_name, label, xs_, ys_, line_flags, max_num_data);
403}
404
405template <typename T, int D, typename Alloc>
407 const std::string& plot_name,
408 const std::string& label,
409 const std::vector<Eigen::Matrix<T, D, 1>, Alloc>& data,
410 int line_flags,
411 size_t max_num_data) {
412 std::vector<double> xs_(data.size());
413 std::vector<double> ys_(data.size());
414 std::transform(data.begin(), data.end(), xs_.begin(), [](const Eigen::Matrix<T, D, 1>& v) { return v[0]; });
415 std::transform(data.begin(), data.end(), ys_.begin(), [](const Eigen::Matrix<T, D, 1>& v) { return v[1]; });
416 update_plot_line(plot_name, label, xs_, ys_, line_flags, max_num_data);
417}
418
419template <typename T>
420auto AsyncLightViewer::update_plot_scatter(const std::string& plot_name, const std::string& label, const std::vector<T>& ys, int scatter_flags)
421 -> std::enable_if_t<std::is_arithmetic_v<T>, void> {
422 std::vector<double> ys_(ys.size());
423 std::copy(ys.begin(), ys.end(), ys_.begin());
424 update_plot_scatter(plot_name, label, ys_, scatter_flags);
425}
426
427template <typename T1, typename T2>
428void 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) {
429 std::vector<double> xs_(xs.size());
430 std::vector<double> ys_(ys.size());
431 std::copy(xs.begin(), xs.end(), xs_.begin());
432 std::copy(ys.begin(), ys.end(), ys_.begin());
433 update_plot_scatter(plot_name, label, xs_, ys_, scatter_flags);
434}
435
436template <typename T, int D, typename Alloc>
437void 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) {
438 std::vector<double> xs_(data.size());
439 std::vector<double> ys_(data.size());
440 std::transform(data.begin(), data.end(), xs_.begin(), [](const Eigen::Matrix<T, D, 1>& v) { return v[0]; });
441 std::transform(data.begin(), data.end(), ys_.begin(), [](const Eigen::Matrix<T, D, 1>& v) { return v[1]; });
442 update_plot_scatter(plot_name, label, xs_, ys_, scatter_flags);
443}
444
445template <typename T>
446void AsyncLightViewer::update_plot_stairs(const std::string& plot_name, const std::string& label, const std::vector<T>& ys, int stairs_flags) {
447 std::vector<double> ys_(ys.size());
448 std::copy(ys.begin(), ys.end(), ys_.begin());
449 update_plot_stairs(plot_name, label, ys_, stairs_flags);
450}
451
452template <typename T1, typename T2>
453void 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) {
454 std::vector<double> xs_(xs.size());
455 std::vector<double> ys_(ys.size());
456 std::copy(xs.begin(), xs.end(), xs_.begin());
457 std::copy(ys.begin(), ys.end(), ys_.begin());
458 update_plot_stairs(plot_name, label, xs_, ys_, stairs_flags);
459}
460
461template <typename T>
463 const std::string& plot_name,
464 const std::string& label,
465 const std::vector<T>& xs,
466 int bins,
467 const Eigen::Vector2d& range,
468 int histogram_flags) {
469 //
470 std::vector<double> xs_(xs.size());
471 std::copy(xs.begin(), xs.end(), xs_.begin());
472 update_plot_histogram(plot_name, label, xs_, bins, range, histogram_flags);
473}
474
475template <typename T1, typename T2>
477 const std::string& plot_name,
478 const std::string& label,
479 const std::vector<T1>& xs,
480 const std::vector<T2>& ys,
481 int x_bins,
482 int y_bins,
483 const Eigen::Vector2d& x_range,
484 const Eigen::Vector2d& y_range,
485 int histogram_flags) {
486 //
487 std::vector<double> xs_(xs.size());
488 std::vector<double> ys_(ys.size());
489 std::copy(xs.begin(), xs.end(), xs_.begin());
490 std::copy(ys.begin(), ys.end(), ys_.begin());
491 update_plot_histogram(plot_name, label, xs_, ys_, x_bins, y_bins, x_range, y_range, histogram_flags);
492}
493
494} // namespace guik
495
496#endif
Definition async_light_viewer.hpp:11
static void wait()
Wait for the viewer to be closed.
void invoke(const std::function< void()> &func)
Invoke a task in the visualization thread.
void fit_plot(const std::string &plot_name)
Fit the plot view to the data range.
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 >
Update a scatter plot.
Definition async_light_viewer.hpp:241
void link_plot_axes(const std::string &plot_name, int link_id, int axes=-1)
Link multiple axes of a plot to a link ID.
void setup_plot_group_order(const std::string &group_name, int order)
Setup the plot group order.
void remove_async_sub_viewer(const std::string &context_name)
Remove an async sub viewer context.
void clear_plots(bool clear_settings=true)
Clear all plots.
void set_plot_style(const std::string &plot_name, const std::string &label, const PlotStyleConstPtr &style)
Set the style for a plot.
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 >
Update a line plot.
Definition async_light_viewer.hpp:203
void remove_image(const std::string &name)
Remove an image by name.
static void destroy()
Destroy the viewer in the background thread.
void setup_legend(const std::string &plot_name, int loc, int flags=0)
Setup the legend for a plot.
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 >
Update a scatter plot.
Definition async_light_viewer.hpp:259
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)
Update a line plot.
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 >
Update a line plot.
Definition async_light_viewer.hpp:217
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)
Update a stairs plot.
static bool running()
Check if the viewer is currently running.
void invoke_after_rendering(const std::function< void()> &func)
Invoke a task after rendering in the visualization thread.
AsyncLightViewerContext async_sub_viewer(const std::string &context_name, const Eigen::Vector2i &canvas_size=Eigen::Vector2i(-1, -1))
Create a async sub viewer context.
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)
Setup a plot.
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)
Update a histogram plot.
void link_plot_axis(const std::string &plot_name, int link_id, int axis)
Link an axis of a plot to a link ID.
void update_plot_scatter(const std::string &plot_name, const std::string &label, const std::vector< double > &ys, int scatter_flags=0)
Update a scatter plot.
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)
Set the line style for a plot.
void update_plot(const std::string &plot_name, const std::string &label, const std::shared_ptr< const PlotData > &plot)
Update a plot with new data.
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)
Update a 2D histogram plot.
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 fit_all_plots()
Fit all plots to their data ranges.
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)
Register or update an image.
static void toggle_wait()
Wait while a toggle checkbox is checked.
void clear_images()
Clear all images.
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)
Update a scatter plot.
void invoke_once(const std::string &label, const std::function< void()> &func)
Invoke a labeled task only once in the life time of the viewer.
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)
Update a line plot.
void update_plot_stairs(const std::string &plot_name, const std::string &label, const std::vector< double > &ys, int stairs_flags=0)
Update a stairs plot.
void remove_plot(const std::string &plot_name, const std::string &label="")
Remove a plot by name.
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))
Set the scatter style for a plot.
Definition drawable_container.hpp:9
std::shared_ptr< const PlotStyle > PlotStyleConstPtr
Definition light_viewer.hpp:21
bool async_running()
Definition async_light_viewer.hpp:376
void async_toggle_wait()
Definition async_light_viewer.hpp:372
void async_wait()
Definition async_light_viewer.hpp:364
void async_destroy()
Definition async_light_viewer.hpp:360
AsyncLightViewer * async_viewer(const Eigen::Vector2i &size=Eigen::Vector2i(-1, -1), bool background=false, const std::string &title="screen")
Definition async_light_viewer.hpp:356
void async_wait_until_click()
Definition async_light_viewer.hpp:368