1#ifndef GUIK_ASYNC_LIGHT_VIEWER_HPP
2#define GUIK_ASYNC_LIGHT_VIEWER_HPP
13 AsyncLightViewer(
const Eigen::Vector2i& size,
bool background,
const std::string& title);
19 static AsyncLightViewer*
instance(
const Eigen::Vector2i& size = Eigen::Vector2i(-1, -1),
bool background =
false,
const std::string& title =
"screen");
37 void invoke(
const std::function<
void()>& func);
41 void invoke_once(
const std::string& label,
const std::function<
void()>& func);
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);
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);
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);
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,
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,
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,
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);
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>
185 const std::string& plot_name,
186 const std::string& label,
187 const std::vector<T1>& xs,
188 const std::vector<T2>& ys,
190 size_t max_num_data = 8192 * 12);
192 template <
typename T,
int D,
typename Alloc>
194 const std::string& plot_name,
195 const std::string& label,
196 const std::vector<Eigen::Matrix<T, D, 1>, Alloc>& data,
198 size_t max_num_data = 8192 * 12);
201 template <
typename T,
typename Func>
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]);
215 template <
typename T,
typename Func>
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]);
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]);
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]);
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,
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,
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);
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,
329 const Eigen::Vector4f& fill = Eigen::Vector4f(0, 0, 0, -1),
331 const Eigen::Vector4f& outline = Eigen::Vector4f(0, 0, 0, -1));
345 static std::unique_ptr<AsyncLightViewer> inst;
347 std::atomic_bool kill_switch;
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;
356inline AsyncLightViewer*
async_viewer(
const Eigen::Vector2i& size = Eigen::Vector2i(-1, -1),
bool background =
false,
const std::string& title =
"screen") {
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());
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,
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());
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,
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]; });
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());
427template <
typename T1,
typename T2>
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());
436template <
typename T,
int D,
typename Alloc>
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]; });
447 std::vector<double> ys_(ys.size());
448 std::copy(ys.begin(), ys.end(), ys_.begin());
452template <
typename T1,
typename T2>
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());
463 const std::string& plot_name,
464 const std::string& label,
465 const std::vector<T>& xs,
467 const Eigen::Vector2d& range,
468 int histogram_flags) {
470 std::vector<double> xs_(xs.size());
471 std::copy(xs.begin(), xs.end(), xs_.begin());
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,
483 const Eigen::Vector2d& x_range,
484 const Eigen::Vector2d& y_range,
485 int histogram_flags) {
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);
AsyncLightViewerContext()
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
virtual ~AsyncLightViewer()
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