Ordinate.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 
9 
10 namespace mc_rtc
11 {
12 
13 namespace gui
14 {
15 
16 namespace plot
17 {
18 
19 namespace impl
20 {
21 
24 template<typename GetT>
25 struct Ordinate
26 {
27  static constexpr Type type = Type::Ordinate;
28 
29  Ordinate(std::string_view name, GetT get_fn, Color color, Style style, Side side)
30  : name_(name), get_fn_(get_fn), color_(color), style_(style), side_(side)
31  {
33  "Ordinate callback should return a single floating-point value");
34  cache_.reserve(16);
35  }
36 
37  void write(mc_rtc::MessagePackBuilder & builder) const
38  {
39  builder.start_array(6);
40  builder.write(static_cast<uint64_t>(type));
41  builder.write(name_);
42  builder.write(cache_);
43  color_.write(builder);
44  builder.write(static_cast<uint64_t>(style_));
45  builder.write(static_cast<uint64_t>(side_));
46  builder.finish_array();
47  cache_.resize(0);
48  }
49 
50  void update() const { cache_.push_back(get_fn_()); }
51 
52  Ordinate & style(Style style)
53  {
54  style_ = style;
55  return *this;
56  }
57 
58  Ordinate & side(Side side)
59  {
60  side_ = side;
61  return *this;
62  }
63 
64 protected:
65  std::string name_;
66  GetT get_fn_;
67  mutable Color color_;
68  mutable std::vector<double> cache_;
69  Style style_;
70  Side side_;
71 };
72 
74 template<typename GetT, typename GetColor>
75 struct OrdinateWithColor : public Ordinate<GetT>
76 {
77  OrdinateWithColor(std::string_view name, GetT get_fn, GetColor color, Style style, Side side)
78  : Ordinate<GetT>(name, get_fn, color(), style, side), get_color_(color)
79  {
80  static_assert(details::CheckReturnType<GetColor, Color>::value, "Ordinate color callback should return a color");
81  }
82 
83  void write(mc_rtc::MessagePackBuilder & builder) const
84  {
85  this->color_ = get_color_();
86  Ordinate<GetT>::write(builder);
87  }
88 
89 private:
90  GetColor get_color_;
91 };
92 
93 } // namespace impl
94 
96 template<typename GetT>
97 impl::Ordinate<GetT> Y(std::string_view name,
98  GetT get_fn,
99  Color color,
100  Style style = Style::Solid,
101  Side side = Side::Left)
102 {
103  return impl::Ordinate<GetT>(name, get_fn, color, style, side);
104 }
105 
107 template<typename GetT, typename GetColor>
109  GetT get_fn,
110  GetColor get_color,
111  Style style = Style::Solid,
112  Side side = Side::Left)
113 {
114  return impl::OrdinateWithColor<GetT, GetColor>(name, get_fn, get_color, style, side);
115 }
116 
117 } // namespace plot
118 
119 } // namespace gui
120 
121 } // namespace mc_rtc
impl::Ordinate< GetT > Y(std::string_view name, GetT get_fn, Color color, Style style=Style::Solid, Side side=Side::Left)
Definition: Ordinate.h:97
Type
Definition: types.h:31
Definition: Contact.h:88
Left
Definition: types.h:158
Solid
Definition: types.h:146
Definition: MessagePackBuilder.h:87
void start_array(size_t size)
Definition: types.h:20
void write(mc_rtc::MessagePackBuilder &builder) const
Definition: types.h:90
void write(mc_rtc::MessagePackBuilder &builder) const
Definition: Ordinate.h:83
OrdinateWithColor(std::string_view name, GetT get_fn, GetColor color, Style style, Side side)
Definition: Ordinate.h:77
Definition: Ordinate.h:26
Ordinate & side(Side side)
Definition: Ordinate.h:58
static constexpr Type type
Definition: Ordinate.h:27
std::vector< double > cache_
Definition: Ordinate.h:68
GetT get_fn_
Definition: Ordinate.h:66
Side side_
Definition: Ordinate.h:70
Style style_
Definition: Ordinate.h:69
void update() const
Definition: Ordinate.h:50
Ordinate & style(Style style)
Definition: Ordinate.h:52
Ordinate(std::string_view name, GetT get_fn, Color color, Style style, Side side)
Definition: Ordinate.h:29
Color color_
Definition: Ordinate.h:67
std::string name_
Definition: Ordinate.h:65
void write(mc_rtc::MessagePackBuilder &builder) const
Definition: Ordinate.h:37