mc_rtc  2.14.0
types.h
Go to the documentation of this file.
1 /*
2  * Copyright 2015-2019 CNRS-UM LIRMM, CNRS-AIST JRL
3  */
4 
5 #pragma once
6 
10 #include <mc_rtc/Configuration.h>
11 #include <mc_rtc/gui/api.h>
12 #include <mc_rtc/logging.h>
13 
14 namespace mc_rtc
15 {
16 
17 namespace gui
18 {
20 {
21  Color() {}
22  Color(double r, double g, double b, double a = 1.0) : r(r), g(g), b(b), a(a) {}
23  Color(const Eigen::Vector3d & color) : r(color.x()), g(color.y()), b(color.z()) {}
24  Color(const Eigen::Vector4d & color) : r(color.x()), g(color.y()), b(color.z()), a(color[3]) {}
25  Color(const mc_rtc::Configuration & config) { fromConfig(config); }
26  double r = 1.0;
27  double g = 0.0;
28  double b = 0.0;
29  double a = 1.0;
30 
31  bool operator==(const Color & rhs) const { return r == rhs.r && g == rhs.g && b == rhs.b && a == rhs.a; }
32 
33  bool operator!=(const Color & rhs) const { return !(*this == rhs); }
34 
36  {
37  std::array<double, 4> data = config;
38  r = data[0];
39  g = data[1];
40  b = data[2];
41  a = data[3];
42  }
43 
44  void fromConfig(const mc_rtc::Configuration & config)
45  {
46  try
47  {
48  std::array<double, 4> color = config;
49  r = color[0];
50  g = color[1];
51  b = color[2];
52  a = color[3];
53  }
55  {
56  e.silence();
57  try
58  {
59  std::string color = config;
60  if(ColorMap.count(color)) { *this = ColorMap.at(color); }
61  else
62  {
63  auto msg = fmt::format("No color named {} ", color);
64  mc_rtc::log::error(msg);
65  throw mc_rtc::Configuration::Exception(msg, config);
66  }
67  }
69  {
70  e.silence();
71  auto msg = std::string{"Color is neither an array of size 4, nor a valid color string, attempted to extract "}
72  + config.dump(true, true);
73  mc_rtc::log::error(msg);
74  throw mc_rtc::Configuration::Exception(msg, config);
75  }
76  }
77  }
78 
80  {
82  conf.push(r);
83  conf.push(g);
84  conf.push(b);
85  conf.push(a);
86  return conf;
87  }
88 
89  static constexpr size_t write_size() { return 1; }
90 
91  void write(mc_rtc::MessagePackBuilder & builder) const
92  {
93  builder.start_array(4);
94  builder.write(r);
95  builder.write(g);
96  builder.write(b);
97  builder.write(a);
98  builder.finish_array();
99  }
100 
101  static const Color White;
102  static const Color Black;
103  static const Color Red;
104  static const Color Green;
105  static const Color Blue;
106  static const Color Cyan;
107  static const Color Magenta;
108  static const Color Yellow;
109  static const Color Gray;
110  static const Color LightGray;
111  static const std::map<std::string, Color> ColorMap;
112 };
113 } // namespace gui
114 
115 template<>
117 {
119  {
120  mc_rtc::gui::Color color;
121  color.fromConfig(config);
122  return color;
123  }
124 
125  static mc_rtc::Configuration save(const mc_rtc::gui::Color & color) { return color.saveConfig(); }
126 };
127 
128 namespace gui
129 {
130 enum class LineStyle
131 {
132  Solid,
133  Dotted
134 };
135 
137 {
138  Color color = {1, 0, 0};
139  double width = 0.01;
141 
143  LineConfig(const Color & color, double width = 0.01, const LineStyle & style = LineStyle::Solid)
144  : color(color), width(width), style(style)
145  {
146  }
147  LineConfig(const mc_rtc::Configuration & config) { fromConfig(config); }
148 
150  {
151  color.fromMessagePack(config[0]);
152  width = config[1];
153  style = LineStyle(static_cast<typename std::underlying_type<LineStyle>::type>(config[2]));
154  }
155 
156  void fromConfig(const mc_rtc::Configuration & config)
157  {
158  config("color", color);
159  config("width", width);
160  if(config.has("style"))
161  {
162  auto s = config("style");
163  if(s == "solid") { style = LineStyle::Solid; }
164  else if(s == "dotted") { style = LineStyle::Dotted; }
165  }
166  }
167 
169  {
170  mc_rtc::Configuration config;
171  config.add("color", color);
172  config.add("width", width);
173  if(style == LineStyle::Solid) { config.add("style", "solid"); }
174  else if(style == LineStyle::Dotted) { config.add("style", "dotted"); }
175  return config;
176  }
177 
178  static constexpr size_t write_size() { return 1; }
179 
181  {
182  out.start_array(3);
183  color.write(out);
184  out.write(width);
185  out.write(static_cast<typename std::underlying_type<LineStyle>::type>(style));
186  out.finish_array();
187  }
188 };
189 } // namespace gui
190 
191 template<>
193 {
195  {
197  line.fromConfig(config);
198  return line;
199  }
200 
201  static mc_rtc::Configuration save(const mc_rtc::gui::LineConfig & line) { return line.saveConfig(); }
202 };
203 
204 namespace gui
205 {
207 {
208  ArrowConfig() : color(0., 1., 0.) {}
209  ArrowConfig(const Color & color) : color(color) {}
210  ArrowConfig(const mc_rtc::Configuration & config) { fromConfig(config); };
211 
213  {
214  head_diam = config[0];
215  head_len = config[1];
216  shaft_diam = config[2];
217  scale = config[3];
218  start_point_scale = config[4];
219  end_point_scale = config[5];
220  color.fromMessagePack(config[6]);
221  }
222 
223  void fromConfig(const mc_rtc::Configuration & config)
224  {
225  config("head_diam", head_diam);
226  config("head_len", head_len);
227  config("shaft_diam", shaft_diam);
228  config("scale", scale);
229  config("start_point_scale", start_point_scale);
230  config("end_point_scale", end_point_scale);
231  config("color", color);
232  }
233 
235  {
237  conf.add("head_diam", head_diam);
238  conf.add("head_len", head_len);
239  conf.add("shaft_diam", shaft_diam);
240  conf.add("scale", scale);
241  conf.add("start_point_scale", start_point_scale);
242  conf.add("end_point_scale", end_point_scale);
243  conf.add("color", color);
244  return conf;
245  }
246 
247  static constexpr size_t write_size() { return 1; }
248 
250  {
251  out.start_array(7);
252  out.write(head_diam);
253  out.write(head_len);
254  out.write(shaft_diam);
255  out.write(scale);
256  out.write(start_point_scale);
257  out.write(end_point_scale);
258  color.write(out);
259  out.finish_array();
260  }
261 
262  double head_diam = 0.015;
263  double head_len = 0.05;
264  double shaft_diam = 0.015;
265  double scale = 0.0015;
266  double start_point_scale = 0.0;
267  double end_point_scale = 0.0;
269 };
270 } // namespace gui
271 
272 template<>
274 {
276  {
278  arrow.fromConfig(config);
279  return arrow;
280  }
281 
282  static mc_rtc::Configuration save(const mc_rtc::gui::ArrowConfig & arrow) { return arrow.saveConfig(); }
283 };
284 
285 namespace gui
286 {
287 struct ForceConfig : public ArrowConfig
288 {
291  ForceConfig(const mc_rtc::Configuration & config) { fromConfig(config); };
292 
294  {
295  ArrowConfig::fromMessagePack(config[0]);
296  force_scale = config[1];
297  }
298 
299  void fromConfig(const mc_rtc::Configuration & config)
300  {
301  ArrowConfig::fromConfig(config);
302  config("force_scale", force_scale);
303  }
304 
306  {
308  conf.add("force_scale", force_scale);
309  return conf;
310  }
311 
312  static constexpr size_t write_size() { return 1; }
313 
315  {
316  out.start_array(2);
317  ArrowConfig::write(out);
318  out.write(force_scale);
319  out.finish_array();
320  }
321 
322  double force_scale = 0.0015;
323 };
324 
325 } // namespace gui
326 
327 template<>
329 {
331  {
333  force.fromConfig(config);
334  return force;
335  }
336 
337  static mc_rtc::Configuration save(const mc_rtc::gui::ForceConfig & force) { return force.saveConfig(); }
338 };
339 
340 namespace gui
341 {
343 {
345  PointConfig(const Color & color, double scale = 0.02) : color(color), scale(scale) {}
346  PointConfig(const mc_rtc::Configuration & config) { fromConfig(config); }
347 
349  {
350  color.fromMessagePack(config[0]);
351  scale = config[1];
352  }
353 
354  void fromConfig(const mc_rtc::Configuration & config)
355  {
356  config("scale", scale);
357  config("color", color);
358  }
359 
361  {
363  conf.add("color", color);
364  conf.add("scale", scale);
365  return conf;
366  }
367 
368  static constexpr size_t write_size() { return 1; }
369 
371  {
372  out.start_array(2);
373  color.write(out);
374  out.write(scale);
375  out.finish_array();
376  }
377 
379  double scale = 0.02;
380 };
381 } // namespace gui
382 
383 template<>
385 {
387  {
389  point.fromConfig(config);
390  return point;
391  }
392 
393  static mc_rtc::Configuration save(const mc_rtc::gui::PointConfig & point) { return point.saveConfig(); }
394 };
395 
396 namespace gui
397 {
398 
400 {
402 
403  PolyhedronConfig(const mc_rtc::Configuration & config) { fromConfig(config); }
404 
405  void fromConfig(const mc_rtc::Configuration & config)
406  {
407  config("triangle_color", triangle_color);
408  config("show_triangle", show_triangle);
409  config("use_triangle_color", use_triangle_color);
410  config("edges", edge_config);
411  config("show_edges", show_edges);
412  config("fixed_edge_color", fixed_edge_color);
413  config("vertices", vertices_config);
414  config("show_vertices", show_vertices);
415  config("fixed_vertices_color", fixed_vertices_color);
416  }
417 
419  {
421  conf.add("triangle_color", triangle_color);
422  conf.add("show_triangle", show_triangle);
423  conf.add("use_triangle_color", use_triangle_color);
424  conf.add("edges", edge_config);
425  conf.add("show_edges", show_edges);
426  conf.add("fixed_edge_color", fixed_edge_color);
427  conf.add("vertices", vertices_config);
428  conf.add("show_vertices", show_vertices);
429  conf.add("fixed_vertices_color", fixed_vertices_color);
430  return conf;
431  }
432 
434  {
435  triangle_color.fromMessagePack(config[0]);
436  show_triangle = config[1];
437  use_triangle_color = config[2];
438  edge_config.fromMessagePack(config[3]);
439  show_edges = config[4];
440  fixed_edge_color = config[5];
441  vertices_config.fromMessagePack(config[6]);
442  show_vertices = config[7];
443  fixed_vertices_color = config[8];
444  }
445 
446  static constexpr size_t write_size() { return 1; }
447 
449  {
450  out.start_array(9);
451  triangle_color.write(out);
452  out.write(show_triangle);
453  out.write(use_triangle_color);
454 
455  edge_config.write(out);
456  out.write(show_edges);
457  out.write(fixed_edge_color);
458 
459  vertices_config.write(out);
460  out.write(show_vertices);
461  out.write(fixed_vertices_color);
462  out.finish_array();
463  }
464 
467  bool show_triangle = true;
469  bool use_triangle_color = false;
471  bool show_edges = true;
473  bool fixed_edge_color = true;
475  bool show_vertices = true;
478  bool fixed_vertices_color = true;
479 };
480 
483 {
484  RobotMsgData() = default;
485  RobotMsgData(const std::vector<std::string> & params,
486  const Eigen::VectorXd & q,
487  const Eigen::VectorXd & alpha,
488  const Eigen::VectorXd & alphaD,
489  const Eigen::VectorXd & tau,
490  const std::vector<sva::ForceVecd> & forces,
491  const sva::PTransformd & posW)
492  : parameters(params), q(q), alpha(alpha), alphaD(alphaD), tau(tau), forces(forces), posW(posW)
493  {
494  }
495 
496  std::vector<std::string> parameters;
497  Eigen::VectorXd q;
498  Eigen::VectorXd alpha;
499  Eigen::VectorXd alphaD;
500  Eigen::VectorXd tau;
501  std::vector<sva::ForceVecd> forces;
502  sva::PTransformd posW;
503 };
504 
505 } // namespace gui
506 
507 template<>
509 {
511  {
513  polyhedron.fromConfig(config);
514  return polyhedron;
515  }
516 
518  {
519  return polyhedron.saveConfig();
520  }
521 };
522 
523 } // namespace mc_rtc
#define MC_RTC_GUI_DLLAPI
Definition: api.h:50
LineStyle
Definition: types.h:131
void error(Args &&... args)
Definition: logging.h:63
Definition: Contact.h:88
static mc_rtc::gui::ArrowConfig load(const mc_rtc::Configuration &config)
Definition: types.h:275
static mc_rtc::Configuration save(const mc_rtc::gui::ArrowConfig &arrow)
Definition: types.h:282
static mc_rtc::gui::Color load(const mc_rtc::Configuration &config)
Definition: types.h:118
static mc_rtc::Configuration save(const mc_rtc::gui::Color &color)
Definition: types.h:125
static mc_rtc::gui::ForceConfig load(const mc_rtc::Configuration &config)
Definition: types.h:330
static mc_rtc::Configuration save(const mc_rtc::gui::ForceConfig &force)
Definition: types.h:337
static mc_rtc::Configuration save(const mc_rtc::gui::LineConfig &line)
Definition: types.h:201
static mc_rtc::gui::LineConfig load(const mc_rtc::Configuration &config)
Definition: types.h:194
static mc_rtc::gui::PointConfig load(const mc_rtc::Configuration &config)
Definition: types.h:386
static mc_rtc::Configuration save(const mc_rtc::gui::PointConfig &point)
Definition: types.h:393
static mc_rtc::gui::PolyhedronConfig load(const mc_rtc::Configuration &config)
Definition: types.h:510
static mc_rtc::Configuration save(const mc_rtc::gui::PolyhedronConfig &polyhedron)
Definition: types.h:517
Definition: Configuration.h:56
Exception thrown by this class when something bad occurs.
Definition: Configuration.h:237
void silence() const noexcept
Simplify access to values hold within a JSON file.
Definition: Configuration.h:166
bool has(const std::string &key) const
Check if the key is part of the conf.
void add(const std::string &key, bool value)
Add a bool element to the Configuration.
static Configuration fromMessagePack(const char *data, size_t size)
Static constructor to load from MessagePack data.
T at(size_t i, const T &v) const
Retrieve a given value from a JSON array.
Definition: Configuration.h:933
static Configuration rootArray()
Returns a Configuration with an array as root entry.
std::string dump(bool pretty=false, bool yaml=false) const
Dump the configuration into a string.
Definition: MessagePackBuilder.h:87
void start_array(size_t size)
Definition: types.h:207
void fromMessagePack(const mc_rtc::Configuration &config)
Definition: types.h:212
static constexpr size_t write_size()
Definition: types.h:247
void fromConfig(const mc_rtc::Configuration &config)
Definition: types.h:223
ArrowConfig(const Color &color)
Definition: types.h:209
Color color
Definition: types.h:268
void write(mc_rtc::MessagePackBuilder &out) const
Definition: types.h:249
ArrowConfig(const mc_rtc::Configuration &config)
Definition: types.h:210
mc_rtc::Configuration saveConfig() const
Definition: types.h:234
ArrowConfig()
Definition: types.h:208
Definition: types.h:20
double b
Definition: types.h:28
void fromMessagePack(const mc_rtc::Configuration &config)
Definition: types.h:35
static const Color Black
Definition: types.h:102
bool operator!=(const Color &rhs) const
Definition: types.h:33
static const Color Red
Definition: types.h:103
static const Color LightGray
Definition: types.h:110
static const Color Gray
Definition: types.h:109
mc_rtc::Configuration saveConfig() const
Definition: types.h:79
static const Color Blue
Definition: types.h:105
static const Color White
Definition: types.h:101
Color(double r, double g, double b, double a=1.0)
Definition: types.h:22
Color()
Definition: types.h:21
static const Color Magenta
Definition: types.h:107
Color(const Eigen::Vector4d &color)
Definition: types.h:24
static constexpr size_t write_size()
Definition: types.h:89
bool operator==(const Color &rhs) const
Definition: types.h:31
void write(mc_rtc::MessagePackBuilder &builder) const
Definition: types.h:91
static const std::map< std::string, Color > ColorMap
Definition: types.h:111
Color(const Eigen::Vector3d &color)
Definition: types.h:23
double a
Definition: types.h:29
static const Color Green
Definition: types.h:104
void fromConfig(const mc_rtc::Configuration &config)
Definition: types.h:44
double g
Definition: types.h:27
static const Color Yellow
Definition: types.h:108
Color(const mc_rtc::Configuration &config)
Definition: types.h:25
double r
Definition: types.h:26
static const Color Cyan
Definition: types.h:106
Definition: types.h:288
void write(mc_rtc::MessagePackBuilder &out) const
Definition: types.h:314
ForceConfig(const Color &color)
Definition: types.h:290
void fromMessagePack(const mc_rtc::Configuration &config)
Definition: types.h:293
static constexpr size_t write_size()
Definition: types.h:312
double force_scale
Definition: types.h:322
void fromConfig(const mc_rtc::Configuration &config)
Definition: types.h:299
ForceConfig(const mc_rtc::Configuration &config)
Definition: types.h:291
ForceConfig()
Definition: types.h:289
mc_rtc::Configuration saveConfig() const
Definition: types.h:305
Definition: types.h:137
LineConfig(const mc_rtc::Configuration &config)
Definition: types.h:147
LineConfig(const Color &color, double width=0.01, const LineStyle &style=LineStyle::Solid)
Definition: types.h:143
LineConfig()
Definition: types.h:142
void write(mc_rtc::MessagePackBuilder &out) const
Definition: types.h:180
static constexpr size_t write_size()
Definition: types.h:178
mc_rtc::Configuration saveConfig() const
Definition: types.h:168
void fromConfig(const mc_rtc::Configuration &config)
Definition: types.h:156
void fromMessagePack(const mc_rtc::Configuration &config)
Definition: types.h:149
Definition: types.h:343
void fromMessagePack(const mc_rtc::Configuration &config)
Definition: types.h:348
void fromConfig(const mc_rtc::Configuration &config)
Definition: types.h:354
PointConfig()
Definition: types.h:344
void write(mc_rtc::MessagePackBuilder &out) const
Definition: types.h:370
PointConfig(const Color &color, double scale=0.02)
Definition: types.h:345
Color color
Definition: types.h:378
PointConfig(const mc_rtc::Configuration &config)
Definition: types.h:346
mc_rtc::Configuration saveConfig() const
Definition: types.h:360
static constexpr size_t write_size()
Definition: types.h:368
double scale
Definition: types.h:379
Definition: types.h:400
void fromMessagePack(const mc_rtc::Configuration &config)
Definition: types.h:433
static constexpr size_t write_size()
Definition: types.h:446
LineConfig edge_config
Definition: types.h:470
void write(mc_rtc::MessagePackBuilder &out) const
Default triangle face color.
Definition: types.h:448
void fromConfig(const mc_rtc::Configuration &config)
Definition: types.h:405
PointConfig vertices_config
Definition: types.h:474
Color triangle_color
Definition: types.h:466
mc_rtc::Configuration saveConfig() const
Definition: types.h:418
PolyhedronConfig()
Definition: types.h:401
PolyhedronConfig(const mc_rtc::Configuration &config)
Definition: types.h:403
Definition: types.h:483
std::vector< std::string > parameters
Definition: types.h:496
Eigen::VectorXd alphaD
Definition: types.h:499
RobotMsgData(const std::vector< std::string > &params, const Eigen::VectorXd &q, const Eigen::VectorXd &alpha, const Eigen::VectorXd &alphaD, const Eigen::VectorXd &tau, const std::vector< sva::ForceVecd > &forces, const sva::PTransformd &posW)
Definition: types.h:485
std::vector< sva::ForceVecd > forces
Definition: types.h:501
Eigen::VectorXd tau
Definition: types.h:500
Eigen::VectorXd q
Definition: types.h:497
sva::PTransformd posW
Definition: types.h:502
Eigen::VectorXd alpha
Definition: types.h:498