7#include <unordered_map>
36 static LightViewer*
instance(
const Eigen::Vector2i& size = Eigen::Vector2i(-1, -1),
bool background =
false,
const std::string& title =
"screen");
50 virtual void register_ui_callback(
const std::string& name,
const std::function<
void()>& callback = 0)
override;
53 void invoke(
const std::function<
void()>& func);
57 void invoke_once(
const std::string& label,
const std::function<
void()>& func);
78 void update_image(
const std::string& name,
const std::shared_ptr<glk::Texture>& image,
double scale = -1.0,
int order = -1);
100 void remove_plot(
const std::string& plot_name,
const std::string& label =
"");
109 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);
124 void setup_legend(
const std::string& plot_name,
int loc,
int flags = 0);
138 void update_plot(
const std::string& plot_name,
const std::string& label,
const std::shared_ptr<const PlotData>& plot);
147 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 = 8192 * 12);
156 const std::string& plot_name,
157 const std::string& label,
158 const std::vector<double>& xs,
159 const std::vector<double>& ys,
161 size_t max_num_data = 8192 * 12);
168 void update_plot_scatter(
const std::string& plot_name,
const std::string& label,
const std::vector<double>& ys,
int scatter_flags = 0);
175 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);
182 void update_plot_stairs(
const std::string& plot_name,
const std::string& label,
const std::vector<double>& ys,
int stairs_flags = 0);
189 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);
199 const std::string& plot_name,
200 const std::string& label,
201 const std::vector<double>& xs,
203 const Eigen::Vector2d& range = Eigen::Vector2d(0.0, 0.0),
204 int histogram_flags = 0);
207 const std::string& plot_name,
208 const std::string& label,
209 const std::vector<double>& xs,
210 const std::vector<double>& ys,
213 const Eigen::Vector2d& x_range = Eigen::Vector2d(0.0, 0.0),
214 const Eigen::Vector2d& y_range = Eigen::Vector2d(0.0, 0.0),
215 int histogram_flags = 0);
219 template <
typename T>
220 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)
221 -> std::enable_if_t<std::is_arithmetic_v<T>,
void>;
223 template <
typename T1,
typename T2>
225 const std::string& plot_name,
226 const std::string& label,
227 const std::vector<T1>& xs,
228 const std::vector<T2>& ys,
230 size_t max_num_data = 8192 * 12);
232 template <
typename T,
int D,
typename Alloc>
234 const std::string& plot_name,
235 const std::string& label,
236 const std::vector<Eigen::Matrix<T, D, 1>, Alloc>& data,
238 size_t max_num_data = 8192 * 12);
240 template <
typename T,
typename Func>
242 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)
243 -> std::enable_if_t<!std::is_arithmetic_v<
decltype(transform(data[0]))>,
void> {
244 std::vector<double> xs(data.size());
245 std::vector<double> ys(data.size());
246 for (
size_t i = 0; i < data.size(); i++) {
247 const auto pt = transform(data[i]);
254 template <
typename T,
typename Func>
256 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)
257 -> std::enable_if_t<std::is_arithmetic_v<
decltype(transform(data[0]))>,
void> {
258 std::vector<double> xs(data.size());
259 std::vector<double> ys(data.size());
260 for (
size_t i = 0; i < data.size(); i++) {
261 const auto pt = transform(data[i]);
269 template <
typename T>
270 auto update_plot_scatter(
const std::string& plot_name,
const std::string& label,
const std::vector<T>& ys,
int scatter_flags = 0)
271 -> std::enable_if_t<std::is_arithmetic_v<T>,
void>;
273 template <
typename T1,
typename T2>
274 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);
276 template <
typename T,
int D,
typename Alloc>
277 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);
279 template <
typename T,
typename Func>
281 const std::string& plot_name,
282 const std::string& label,
283 const std::vector<T>& data,
284 const Func& transform,
285 int scatter_flags = 0,
286 size_t max_num_data = 8192 * 12) -> std::enable_if_t<!std::is_arithmetic_v<
decltype(transform(data[0]))>,
void> {
287 std::vector<double> xs(data.size());
288 std::vector<double> ys(data.size());
289 for (
size_t i = 0; i < data.size(); i++) {
290 const auto pt = transform(data[i]);
297 template <
typename T,
typename Func>
299 const std::string& plot_name,
300 const std::string& label,
301 const std::vector<T>& data,
302 const Func& transform,
303 int scatter_flags = 0,
304 size_t max_num_data = 8192 * 12) -> std::enable_if_t<std::is_arithmetic_v<
decltype(transform(data[0]))>,
void> {
305 std::vector<double> xs(data.size());
306 std::vector<double> ys(data.size());
307 for (
size_t i = 0; i < data.size(); i++) {
308 const auto pt = transform(data[i]);
316 template <
typename T>
317 void update_plot_stairs(
const std::string& plot_name,
const std::string& label,
const std::vector<T>& ys,
int stairs_flags = 0);
319 template <
typename T1,
typename T2>
320 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);
322 template <
typename T,
int D,
typename Alloc>
323 void update_plot_stairs(
const std::string& plot_name,
const std::string& label,
const std::vector<Eigen::Matrix<T, D, 1>, Alloc>& data,
int stairs_flags = 0);
325 template <
typename T,
typename Func>
326 void update_plot_stairs(
const std::string& plot_name,
const std::string& label,
const std::vector<T>& data,
const Func& transform,
int stairs_flags = 0);
329 template <
typename T>
331 const std::string& plot_name,
332 const std::string& label,
333 const std::vector<T>& xs,
335 const Eigen::Vector2d& range = Eigen::Vector2d(0.0, 0.0),
336 int histogram_flags = 0);
338 template <
typename T1,
typename T2>
340 const std::string& plot_name,
341 const std::string& label,
342 const std::vector<T1>& xs,
343 const std::vector<T2>& ys,
346 const Eigen::Vector2d& x_range = Eigen::Vector2d(0.0, 0.0),
347 const Eigen::Vector2d& y_range = Eigen::Vector2d(0.0, 0.0),
348 int histogram_flags = 0);
350 template <
typename T,
int D,
typename Alloc>
352 const std::string& plot_name,
353 const std::string& label,
354 const std::vector<Eigen::Matrix<T, D, 1>, Alloc>& data,
357 const Eigen::Vector2d& x_range = Eigen::Vector2d(0.0, 0.0),
358 const Eigen::Vector2d& y_range = Eigen::Vector2d(0.0, 0.0),
359 int histogram_flags = 0);
361 template <
typename T,
typename Func>
363 const std::string& plot_name,
364 const std::string& label,
365 const std::vector<T>& data,
366 const Func& transform,
369 const Eigen::Vector2d& x_range = Eigen::Vector2d(0.0, 0.0),
370 const Eigen::Vector2d& y_range = Eigen::Vector2d(0.0, 0.0),
371 int histogram_flags = 0);
384 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);
394 const std::string& plot_name,
395 const std::string& label,
398 const Eigen::Vector4f& fill = Eigen::Vector4f(0, 0, 0, -1),
400 const Eigen::Vector4f& outline = Eigen::Vector4f(0, 0, 0, -1));
411 virtual bool init(
const Eigen::Vector2i& size,
const char* glsl_version,
bool background,
const std::string& title)
override;
412 virtual void framebuffer_size_callback(
const Eigen::Vector2i& size)
override;
414 virtual void draw_ui()
override;
415 virtual void draw_gl()
override;
418 static std::unique_ptr<LightViewer> inst;
420 std::unique_ptr<ViewerUI> viewer_ui;
421 std::unique_ptr<InfoWindow> info_window;
423 std::mutex texts_mutex;
425 std::deque<std::string> texts;
427 bool toggle_spin_stop_flag;
429 std::vector<std::pair<std::string, std::function<void()>>> ui_callbacks;
431 std::unordered_map<std::string, std::tuple<double, std::shared_ptr<glk::Texture>,
int>> images;
432 std::vector<std::shared_ptr<glk::Texture>> images_in_rendering;
434 std::unordered_map<int, Eigen::Matrix<double, 6, 2>> plot_linked_axis_limits;
435 std::unordered_map<std::string, PlotSetting> plot_settings;
436 std::unordered_map<std::string, int> plot_group_orders;
437 std::unordered_map<std::string, std::vector<std::pair<PlotStyleConstPtr, PlotDataConstPtr>>> plot_data;
439 std::unordered_map<std::string, std::shared_ptr<LightViewerContext>> sub_contexts;
441 std::mutex invoke_requests_mutex;
442 std::deque<std::function<void()>> invoke_requests;
443 std::unordered_set<std::string> invoke_once_called;
445 std::mutex post_render_invoke_requests_mutex;
446 std::deque<std::function<void()>> post_render_invoke_requests;
454inline LightViewer*
viewer(
const Eigen::Vector2i& size = Eigen::Vector2i(-1, -1),
bool background =
false,
const std::string& title =
"screen") {
463inline LightViewer*
viewer(
const std::string& title,
const Eigen::Vector2i& size = Eigen::Vector2i(-1, -1),
bool background =
false) {
481 -> std::enable_if_t<std::is_arithmetic_v<T>,
void> {
482 std::vector<double> ys_(ys.size());
483 std::copy(ys.begin(), ys.end(), ys_.begin());
487template <
typename T1,
typename T2>
489 const std::string& plot_name,
490 const std::string& label,
491 const std::vector<T1>& xs,
492 const std::vector<T2>& ys,
494 size_t max_num_data) {
495 std::vector<double> xs_(xs.size());
496 std::vector<double> ys_(ys.size());
497 std::copy(xs.begin(), xs.end(), xs_.begin());
498 std::copy(ys.begin(), ys.end(), ys_.begin());
502template <
typename T,
int D,
typename Alloc>
504 const std::string& plot_name,
505 const std::string& label,
506 const std::vector<Eigen::Matrix<T, D, 1>, Alloc>& data,
508 size_t max_num_data) {
509 std::vector<double> xs(data.size());
510 std::vector<double> ys(data.size());
511 std::transform(data.begin(), data.end(), xs.begin(), [](
const Eigen::Matrix<T, D, 1>& v) { return v[0]; });
512 std::transform(data.begin(), data.end(), ys.begin(), [](
const Eigen::Matrix<T, D, 1>& v) { return v[1]; });
518 -> std::enable_if_t<std::is_arithmetic_v<T>,
void> {
519 std::vector<double> ys_(ys.size());
520 std::copy(ys.begin(), ys.end(), ys_.begin());
524template <
typename T1,
typename T2>
526 std::vector<double> xs_(xs.size());
527 std::vector<double> ys_(ys.size());
528 std::copy(xs.begin(), xs.end(), xs_.begin());
529 std::copy(ys.begin(), ys.end(), ys_.begin());
533template <
typename T,
int D,
typename Alloc>
535 std::vector<double> xs(data.size());
536 std::vector<double> ys(data.size());
537 std::transform(data.begin(), data.end(), xs.begin(), [](
const Eigen::Matrix<T, D, 1>& v) { return v[0]; });
538 std::transform(data.begin(), data.end(), ys.begin(), [](
const Eigen::Matrix<T, D, 1>& v) { return v[1]; });
544 std::vector<double> ys_(ys.size());
545 std::copy(ys.begin(), ys.end(), ys_.begin());
549template <
typename T1,
typename T2>
551 std::vector<double> xs_(xs.size());
552 std::vector<double> ys_(ys.size());
553 std::copy(xs.begin(), xs.end(), xs_.begin());
554 std::copy(ys.begin(), ys.end(), ys_.begin());
558template <
typename T,
int D,
typename Alloc>
559void LightViewer::update_plot_stairs(
const std::string& plot_name,
const std::string& label,
const std::vector<Eigen::Matrix<T, D, 1>, Alloc>& data,
int stairs_flags) {
560 std::vector<double> xs(data.size());
561 std::vector<double> ys(data.size());
562 std::transform(data.begin(), data.end(), xs.begin(), [](
const Eigen::Matrix<T, D, 1>& v) { return v[0]; });
563 std::transform(data.begin(), data.end(), ys.begin(), [](
const Eigen::Matrix<T, D, 1>& v) { return v[1]; });
567template <
typename T,
typename Func>
569 std::vector<double> xs(data.size());
570 std::vector<double> ys(data.size());
571 for (
size_t i = 0; i < data.size(); i++) {
572 const auto pt = transform(data[i], xs[i], ys[i]);
581 const std::string& plot_name,
582 const std::string& label,
583 const std::vector<T>& xs,
585 const Eigen::Vector2d& range,
586 int histogram_flags) {
587 std::vector<double> xs_(xs.size());
588 std::copy(xs.begin(), xs.end(), xs_.begin());
592template <
typename T1,
typename T2>
594 const std::string& plot_name,
595 const std::string& label,
596 const std::vector<T1>& xs,
597 const std::vector<T2>& ys,
600 const Eigen::Vector2d& x_range,
601 const Eigen::Vector2d& y_range,
602 int histogram_flags) {
603 std::vector<double> xs_(xs.size());
604 std::vector<double> ys_(ys.size());
605 std::copy(xs.begin(), xs.end(), xs_.begin());
606 std::copy(ys.begin(), ys.end(), ys_.begin());
607 update_plot_histogram(plot_name, label, xs_, ys_, x_bins, y_bins, x_range, y_range, histogram_flags);
610template <
typename T,
int D,
typename Alloc>
612 const std::string& plot_name,
613 const std::string& label,
614 const std::vector<Eigen::Matrix<T, D, 1>, Alloc>& data,
617 const Eigen::Vector2d& x_range,
618 const Eigen::Vector2d& y_range,
619 int histogram_flags) {
620 std::vector<double> xs(data.size());
621 std::vector<double> ys(data.size());
622 std::transform(data.begin(), data.end(), xs.begin(), [](
const Eigen::Matrix<T, D, 1>& v) { return v[0]; });
623 std::transform(data.begin(), data.end(), ys.begin(), [](
const Eigen::Matrix<T, D, 1>& v) { return v[1]; });
627template <
typename T,
typename Func>
629 const std::string& plot_name,
630 const std::string& label,
631 const std::vector<T>& data,
632 const Func& transform,
635 const Eigen::Vector2d& x_range,
636 const Eigen::Vector2d& y_range,
637 int histogram_flags) {
638 std::vector<double> xs(data.size());
639 std::vector<double> ys(data.size());
640 for (
size_t i = 0; i < data.size(); i++) {
641 const auto pt = transform(data[i], xs[i], ys[i]);
Definition imgui_application.hpp:12
Viewer context.
Definition light_viewer_context.hpp:37
std::string context_name
Viewer context name.
Definition light_viewer_context.hpp:520
Eigen::Vector2i canvas_size() const
Get canvas size.
Definition light_viewer_context.hpp:242
Definition info_window.hpp:9
Definition viewer_ui.hpp:11
Light viewer. The main visualization class in Iridescence.
Definition light_viewer.hpp:24
virtual void clear_text() override
Clear the text buffer.
void invoke_after_rendering(const std::function< void()> &func)
Invoke a task after rendering in the visualization thread.
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 light_viewer.hpp:256
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 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.
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.
LightViewer()
Constructor. Do not call directly. Use LightViewer::instance() instead.
bool remove_sub_viewer(const std::string &context_name)
Remove a sub viewer context 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.
std::shared_ptr< LightViewerContext > find_sub_viewer(const std::string &context_name)
Find a sub viewer context by name.
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=8192 *12)
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 setup_legend(const std::string &plot_name, int loc, int flags=0)
Setup the legend for a plot.
virtual void append_text(const std::string &text) override
Append text to the text buffer. This method is thread-safe.
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 fit_all_plots()
Fit all plots to their data ranges.
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_image(const std::string &name, const std::shared_ptr< glk::Texture > &image, double scale=-1.0, int order=-1)
Register or update an image.
bool toggle_spin_once()
Spin the viewer until a toggle flag is set to true.
std::shared_ptr< LightViewerContext > sub_viewer(const std::string &context_name, const Eigen::Vector2i &canvas_size=Eigen::Vector2i(-1, -1))
Create or get a sub viewer context.
static void destroy()
Destroy the singleton instance of LightViewer.
static bool running()
Check if the LightViewer instance is running.
void invoke(const std::function< void()> &func)
Invoke a task in the visualization thread.
void show_info_window()
Show the info window.
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=8192 *12)
Update a line 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 light_viewer.hpp:298
void fit_plot(const std::string &plot_name)
Fit the plot view to the data range.
void show_sub_viewers()
Show all sub viewer windows.
static LightViewer * instance(const Eigen::Vector2i &size=Eigen::Vector2i(-1, -1), bool background=false, const std::string &title="screen")
Get the singleton instance of LightViewer. The viewer insatnce is created upon the first call.
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 set_max_text_buffer_size(int size)
Set the maximum text buffer size.
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 light_viewer.hpp:242
void clear_plots(bool clear_settings=true)
Clear all plots.
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 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 remove_plot(const std::string &plot_name, const std::string &label="")
Remove a plot by name.
void remove_image(const std::string &name)
Remove an image by name.
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 light_viewer.hpp:280
void set_plot_style(const std::string &plot_name, const std::string &label, const PlotStyleConstPtr &style)
Set the style for a plot.
void clear_images()
Clear all images.
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.
void show_viewer_ui()
Show the viewer UI.
virtual void register_ui_callback(const std::string &name, const std::function< void()> &callback=0) override
Register a UI callback function.
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.
bool spin_until_click()
Spin the viewer until a mouse click event is received.
virtual void clear() override
Clear everything in the viewer (text, drawables, filters).
void setup_plot_group_order(const std::string &group_name, int order)
Setup the plot group order.
void link_plot_axis(const std::string &plot_name, int link_id, int axis)
Link an axis of a plot to a link ID.
Definition drawable_container.hpp:9
std::shared_ptr< const PlotStyle > PlotStyleConstPtr
Definition light_viewer.hpp:21
std::shared_ptr< const PlotData > PlotDataConstPtr
Definition light_viewer.hpp:20
void destroy()
Destroy the singleton LightViewer instance.
Definition light_viewer.hpp:468
LightViewer * viewer(const Eigen::Vector2i &size=Eigen::Vector2i(-1, -1), bool background=false, const std::string &title="screen")
Create or get the singleton LightViewer instance.
Definition light_viewer.hpp:454
bool running()
Check if the LightViewer instance is running.
Definition light_viewer.hpp:473
Definition plot_data.hpp:11
Definition plot_data.hpp:91