Skip to content

Commit 095ace9

Browse files
authored
invoke_once (#190)
1 parent d5e7dc1 commit 095ace9

File tree

5 files changed

+40
-6
lines changed

5 files changed

+40
-6
lines changed

include/guik/viewer/async_light_viewer.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class AsyncLightViewer : public AsyncLightViewerContext {
3232

3333
void invoke(const std::function<void()>& func);
3434
void invoke_after_rendering(const std::function<void()>& func);
35+
void invoke_once(const std::string& label, const std::function<void()>& func);
3536

3637
void clear_images();
3738
void remove_image(const std::string& name);
@@ -41,7 +42,8 @@ class AsyncLightViewer : public AsyncLightViewerContext {
4142
void clear_plots(bool clear_settings = true);
4243
void remove_plot(const std::string& plot_name, const std::string& label = "");
4344
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);
44-
void link_plot_axes(const std::string& plot_name, int link_id, int axis);
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);
4547
void setup_legend(const std::string& plot_name, int loc, int flags = 0);
4648
void fit_plot(const std::string& plot_name);
4749
void fit_all_plots();

include/guik/viewer/light_viewer.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,12 @@ class LightViewer : public guik::Application, public guik::LightViewerContext {
3333
bool toggle_spin_once();
3434
virtual void register_ui_callback(const std::string& name, const std::function<void()>& callback = 0) override;
3535

36+
// Invoke a task in the visualization thread
3637
void invoke(const std::function<void()>& func);
38+
// Invoke a task after rendering in the visualization thread
3739
void invoke_after_rendering(const std::function<void()>& func);
40+
// Invoke a labeled task only once
41+
void invoke_once(const std::string& label, const std::function<void()>& func);
3842

3943
virtual void clear() override;
4044
virtual void clear_text() override;
@@ -55,7 +59,8 @@ class LightViewer : public guik::Application, public guik::LightViewerContext {
5559
void clear_plots(bool clear_settings = true);
5660
void remove_plot(const std::string& plot_name, const std::string& label = "");
5761
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);
58-
void link_plot_axes(const std::string& plot_name, int link_id, int axis);
62+
void link_plot_axis(const std::string& plot_name, int link_id, int axis); // axis = ImAxis_X1 or ImAxis_X2, ...
63+
void link_plot_axes(const std::string& plot_name, int link_id, int axes = -1); // axes = (1 << ImAxis_X1) | (1 << ImAxis_X2) ...
5964
void setup_legend(const std::string& plot_name, int loc, int flags = 0);
6065
void fit_plot(const std::string& plot_name);
6166
void fit_all_plots();
@@ -286,6 +291,7 @@ class LightViewer : public guik::Application, public guik::LightViewerContext {
286291

287292
std::mutex invoke_requests_mutex;
288293
std::deque<std::function<void()>> invoke_requests;
294+
std::unordered_set<std::string> invoke_once_called;
289295

290296
std::mutex post_render_invoke_requests_mutex;
291297
std::deque<std::function<void()>> post_render_invoke_requests;

include/guik/viewer/plot_setting.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ struct PlotSetting {
4040
int order;
4141

4242
int axis_link_id; // -1 for invalid
43-
int linked_axes; // 1 << ImAxes_X1 | 1 << ImAxes_X2 | ...
43+
int linked_axes; // 1 << ImAxis_X1 | 1 << ImAxis_X2 | ...
4444

4545
bool set_axes_to_fit;
4646
};

src/guik/viewer/async_light_viewer.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ void AsyncLightViewer::invoke_after_rendering(const std::function<void()>& func)
109109
guik::viewer()->invoke_after_rendering(func);
110110
}
111111

112+
void AsyncLightViewer::invoke_once(const std::string& label, const std::function<void()>& func) {
113+
guik::viewer()->invoke_once(label, func);
114+
}
115+
112116
void AsyncLightViewer::clear_images() {
113117
guik::viewer()->invoke([] { guik::viewer()->clear_images(); });
114118
}
@@ -136,8 +140,12 @@ void AsyncLightViewer::setup_plot(const std::string& plot_name, int width, int h
136140
guik::viewer()->invoke([=] { guik::viewer()->setup_plot(plot_name, width, height, plot_flags, x_flags, y_flags, order); });
137141
}
138142

139-
void AsyncLightViewer::link_plot_axes(const std::string& plot_name, int link_id, int axis) {
140-
guik::viewer()->invoke([=] { guik::viewer()->link_plot_axes(plot_name, link_id, axis); });
143+
void AsyncLightViewer::link_plot_axis(const std::string& plot_name, int link_id, int axis) {
144+
guik::viewer()->invoke([=] { guik::viewer()->link_plot_axis(plot_name, link_id, axis); });
145+
}
146+
147+
void AsyncLightViewer::link_plot_axes(const std::string& plot_name, int link_id, int axes) {
148+
guik::viewer()->invoke([=] { guik::viewer()->link_plot_axes(plot_name, link_id, axes); });
141149
}
142150

143151
void AsyncLightViewer::setup_legend(const std::string& plot_name, int loc, int flags) {

src/guik/viewer/light_viewer.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ void LightViewer::setup_plot(const std::string& plot_name, int width, int height
439439
setting.order = order >= 0 ? order : 8192 + plot_settings.size();
440440
}
441441

442-
void LightViewer::link_plot_axes(const std::string& plot_name, int link_id, int axis) {
442+
void LightViewer::link_plot_axis(const std::string& plot_name, int link_id, int axis) {
443443
auto& setting = plot_settings[plot_name];
444444
setting.axis_link_id = link_id;
445445
setting.linked_axes |= (1 << axis);
@@ -449,6 +449,16 @@ void LightViewer::link_plot_axes(const std::string& plot_name, int link_id, int
449449
}
450450
}
451451

452+
void LightViewer::link_plot_axes(const std::string& plot_name, int link_id, int axes) {
453+
auto& setting = plot_settings[plot_name];
454+
setting.axis_link_id = link_id;
455+
setting.linked_axes = axes;
456+
457+
if (plot_linked_axis_limits.count(link_id) == 0) {
458+
plot_linked_axis_limits[link_id] = Eigen::Matrix<double, 6, 2>::Zero();
459+
}
460+
}
461+
452462
void LightViewer::setup_legend(const std::string& plot_name, int loc, int flags) {
453463
auto& setting = plot_settings[plot_name];
454464
setting.legend_loc = loc;
@@ -694,6 +704,14 @@ void LightViewer::invoke_after_rendering(const std::function<void()>& func) {
694704
post_render_invoke_requests.push_back(func);
695705
}
696706

707+
void LightViewer::invoke_once(const std::string& label, const std::function<void()>& func) {
708+
std::lock_guard<std::mutex> lock(invoke_requests_mutex);
709+
if (invoke_once_called.count(label) == 0) {
710+
invoke_requests.push_back(func);
711+
invoke_once_called.insert(label);
712+
}
713+
}
714+
697715
std::shared_ptr<LightViewerContext> LightViewer::sub_viewer(const std::string& context_name, const Eigen::Vector2i& canvas_size) {
698716
using namespace glk::console;
699717

0 commit comments

Comments
 (0)