-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.cpp
436 lines (379 loc) · 13.1 KB
/
Client.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#include "Client.h"
#include "widgets/ArrayInput.h"
#include "widgets/ArrayLabel.h"
#include "widgets/Button.h"
#include "widgets/Checkbox.h"
#include "widgets/ComboInput.h"
#include "widgets/DataComboInput.h"
#include "widgets/Form.h"
#include "widgets/IntegerInput.h"
#include "widgets/Label.h"
#include "widgets/NumberInput.h"
#include "widgets/NumberSlider.h"
#include "widgets/Schema.h"
#include "widgets/StringInput.h"
#include "widgets/Table.h"
#include <boost/filesystem.hpp>
namespace bfs = boost::filesystem;
namespace mc_rtc::imgui
{
Client::Client() : mc_control::ControllerClient()
{
std::string socket = fmt::format("ipc://{}", (bfs::temp_directory_path() / "mc_rtc_").string());
connect(socket + "pub.ipc", socket + "rep.ipc");
timeout(3.0);
}
void Client::update()
{
run(buffer_, t_last_);
}
void Client::draw2D(ImVec2 windowSize)
{
if(!bold_font_)
{
ImGuiIO & io = ImGui::GetIO();
bold_font_ = io.FontDefault;
}
auto left_margin = 15;
auto top_margin = 50;
auto bottom_margin = 50;
auto width = windowSize.x - left_margin;
auto height = windowSize.y - top_margin - bottom_margin;
if(!root_.empty())
{
ImGui::SetNextWindowPos(ImVec2(left_margin, top_margin), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowSize(ImVec2(0.4f * width, 0.7f * height), ImGuiCond_FirstUseEver);
ImGui::Begin("mc_rtc");
root_.draw2D();
ImGui::End();
}
if(active_plots_.size() || inactive_plots_.size())
{
bool open_plots = true;
ImGui::Begin("Plots", active_plots_.size() != 0 ? nullptr : &open_plots);
ImGuiTabBarFlags tab_bar_flags = ImGuiTabBarFlags_Reorderable;
if(ImGui::BeginTabBar("Plots", tab_bar_flags))
{
size_t id = 0;
for(auto & p : active_plots_)
{
auto tab_id = fmt::format("{}##{}", p.second->title(), id++);
enable_bold_font();
if(ImGui::BeginTabItem(tab_id.c_str()))
{
disable_bold_font();
p.second->do_plot();
ImGui::EndTabItem();
}
else { disable_bold_font(); }
}
for(auto it = inactive_plots_.begin(); it != inactive_plots_.end();)
{
auto & p = *it;
auto tab_id = fmt::format("{}##{}", p->title(), id++);
bool open_ = true;
if(ImGui::BeginTabItem(tab_id.c_str(), &open_))
{
p->do_plot();
ImGui::EndTabItem();
}
it = open_ ? std::next(it) : inactive_plots_.erase(it);
}
ImGui::EndTabBar();
}
ImGui::End();
if(!open_plots) { inactive_plots_.clear(); }
}
}
void Client::draw3D()
{
root_.draw3D();
}
void Client::started()
{
root_.started();
}
void Client::stopped()
{
root_.stopped();
for(auto it = active_plots_.begin(); it != active_plots_.end();)
{
if(!it->second->seen())
{
inactive_plots_.push_back(it->second);
it = active_plots_.erase(it);
}
else { ++it; }
}
}
void Client::clear()
{
root_.categories.clear();
root_.widgets.clear();
}
void Client::enable_bold_font()
{
if(bold_font_) { ImGui::PushFont(bold_font_); }
}
void Client::disable_bold_font()
{
if(bold_font_) { ImGui::PopFont(); }
}
/** We rely on widgets to create categories */
void Client::category(const std::vector<std::string> &, const std::string &) {}
void Client::label(const ElementId & id, const std::string & txt)
{
widget<Label>(id).data(txt);
}
void Client::array_label(const ElementId & id, const std::vector<std::string> & labels, const Eigen::VectorXd & data)
{
widget<ArrayLabel>(id).data(labels, data);
}
void Client::button(const ElementId & id)
{
widget<Button>(id);
}
void Client::checkbox(const ElementId & id, bool state)
{
widget<Checkbox>(id).data(state);
}
void Client::string_input(const ElementId & id, const std::string & data)
{
widget<StringInput>(id).data(data);
}
void Client::integer_input(const ElementId & id, int data)
{
widget<IntegerInput>(id).data(data);
}
void Client::number_input(const ElementId & id, double data)
{
widget<NumberInput>(id).data(data);
}
void Client::number_slider(const ElementId & id, double data, double min, double max)
{
widget<NumberSlider>(id).data(data, min, max);
}
void Client::array_input(const ElementId & id, const std::vector<std::string> & labels, const Eigen::VectorXd & data)
{
widget<ArrayInput>(id).data(labels, data);
}
void Client::combo_input(const ElementId & id, const std::vector<std::string> & values, const std::string & data)
{
widget<ComboInput>(id).data(values, data);
}
void Client::data_combo_input(const ElementId & id, const std::vector<std::string> & values, const std::string & data)
{
widget<DataComboInput>(id).data(values, data);
}
void Client::table_start(const ElementId & id, const std::vector<std::string> & header)
{
widget<Table>(id).start(header);
}
void Client::table_row(const ElementId & id, const std::vector<std::string> & data)
{
widget<Table>(id).row(data);
}
void Client::table_end(const ElementId & id)
{
widget<Table>(id).end();
}
void Client::form(const ElementId & id)
{
active_form_ = widget<Form>(id).parentForm();
}
template<typename T>
std::optional<T> null_or_default(const T & value, bool user_default)
{
if(user_default) { return value; }
return std::nullopt;
}
void Client::form_checkbox(const ElementId & /*id*/,
const std::string & name,
bool required,
bool default_,
bool user_default)
{
active_form_->widget<form::Checkbox>(name, required, null_or_default(default_, user_default));
}
void Client::form_integer_input(const ElementId & /*id*/,
const std::string & name,
bool required,
int default_,
bool user_default)
{
active_form_->widget<form::IntegerInput>(name, required, null_or_default(default_, user_default));
}
void Client::form_number_input(const ElementId & /*id*/,
const std::string & name,
bool required,
double default_,
bool user_default)
{
active_form_->widget<form::NumberInput>(name, required, null_or_default(default_, user_default));
}
void Client::form_string_input(const ElementId & /*id*/,
const std::string & name,
bool required,
const std::string & default_,
bool user_default)
{
active_form_->widget<form::StringInput>(name, required, null_or_default(default_, user_default));
}
void Client::form_array_input(const ElementId & /*id*/,
const std::string & name,
bool required,
const std::vector<std::string> & labels,
const Eigen::VectorXd & default_,
bool fixed_size,
bool user_default)
{
active_form_->widget<form::ArrayInput>(name, required, labels, null_or_default(default_, user_default), fixed_size);
}
void Client::form_point3d_input(const ElementId & /*id*/,
const std::string & name,
bool required,
const Eigen::Vector3d & default_,
bool user_default,
bool interactive)
{
active_form_->widget<form::Point3DInput>(name, required, null_or_default(default_, user_default), interactive);
}
void Client::form_rotation_input(const ElementId & /*id*/,
const std::string & name,
bool required,
const sva::PTransformd & default_,
bool user_default,
bool interactive)
{
active_form_->widget<form::RotationInput>(name, required, null_or_default(default_, user_default), interactive);
}
void Client::form_transform_input(const ElementId & /*id*/,
const std::string & name,
bool required,
const sva::PTransformd & default_,
bool user_default,
bool interactive)
{
active_form_->widget<form::TransformInput>(name, required, null_or_default(default_, user_default), interactive);
}
void Client::form_combo_input(const ElementId & /*id*/,
const std::string & name,
bool required,
const std::vector<std::string> & values,
bool send_index,
int user_default)
{
active_form_->widget<form::ComboInput>(name, required, values, send_index, user_default);
}
void Client::form_data_combo_input(const ElementId & /*id*/,
const std::string & name,
bool required,
const std::vector<std::string> & ref,
bool send_index)
{
active_form_->widget<form::DataComboInput>(name, required, ref, send_index);
}
void Client::schema(const ElementId & id, const std::string & schema)
{
widget<Schema>(id).data(schema);
}
void Client::start_form_object_input(const std::string & name, bool required)
{
require_active_form();
active_form_ = active_form_->widget<form::ObjectWidget>(name, required, active_form_);
}
void Client::end_form_object_input()
{
require_active_form();
active_form_ = active_form_->parentForm();
}
void Client::start_form_generic_array_input(const std::string & name,
bool required,
std::optional<std::vector<mc_rtc::Configuration>> data)
{
require_active_form();
active_form_ = active_form_->widget<form::GenericArrayWidget>(name, required, required, active_form_, data);
}
void Client::end_form_generic_array_input()
{
require_active_form();
active_form_ = active_form_->parentForm();
}
void Client::start_form_one_of_input(const std::string & name,
bool required,
const std::optional<std::pair<size_t, mc_rtc::Configuration>> & data)
{
require_active_form();
active_form_ = active_form_->widget<form::OneOfWidget>(name, required, active_form_, data);
}
void Client::end_form_one_of_input()
{
require_active_form();
active_form_ = active_form_->parentForm();
}
auto Client::getCategory(const std::vector<std::string> & category) -> Category &
{
std::reference_wrapper<Category> out(root_);
for(size_t i = 0; i < category.size(); ++i)
{
auto & cat = out.get();
auto & next = category[i];
auto it = std::find_if(cat.categories.begin(), cat.categories.end(), [&](auto & c) { return c->name == next; });
if(it != cat.categories.end()) { out = std::ref(*it->get()); }
else { out = *cat.categories.emplace_back(std::make_unique<Category>(next, cat.depth + 1)); }
}
return out.get();
}
void Client::start_plot(uint64_t id, const std::string & title)
{
if(!active_plots_.count(id)) { active_plots_[id] = std::make_shared<Plot>(title); }
if(active_plots_[id]->title() != title)
{
inactive_plots_.push_back(active_plots_[id]);
active_plots_.erase(id);
active_plots_[id] = std::make_shared<Plot>(title);
}
active_plots_[id]->start_plot();
}
void Client::plot_setup_xaxis(uint64_t id, const std::string & legend, const mc_rtc::gui::plot::Range & range)
{
active_plots_[id]->setup_xaxis(legend, range);
}
void Client::plot_setup_yaxis_left(uint64_t id, const std::string & legend, const mc_rtc::gui::plot::Range & range)
{
active_plots_[id]->setup_yaxis_left(legend, range);
}
void Client::plot_setup_yaxis_right(uint64_t id, const std::string & legend, const mc_rtc::gui::plot::Range & range)
{
active_plots_[id]->setup_yaxis_right(legend, range);
}
void Client::plot_point(uint64_t id,
uint64_t did,
const std::string & legend,
double x,
double y,
mc_rtc::gui::Color color,
mc_rtc::gui::plot::Style style,
mc_rtc::gui::plot::Side side)
{
active_plots_[id]->plot_point(did, legend, x, y, color, style, side);
}
void Client::plot_polygon(uint64_t id,
uint64_t did,
const std::string & legend,
const mc_rtc::gui::plot::PolygonDescription & polygon,
mc_rtc::gui::plot::Side side)
{
active_plots_[id]->plot_polygon(did, legend, polygon, side);
}
void Client::plot_polygons(uint64_t id,
uint64_t did,
const std::string & legend,
const std::vector<mc_rtc::gui::plot::PolygonDescription> & polygons,
mc_rtc::gui::plot::Side side)
{
active_plots_[id]->plot_polygons(did, legend, polygons, side);
}
void Client::end_plot(uint64_t) {}
InteractiveMarker::~InteractiveMarker() {}
} // namespace mc_rtc::imgui