AbscissaOrdinate.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 UpdateCacheT>
26 {
27  static constexpr Type type = Type::AbscissaOrdinate;
28 
29  using CacheT = std::vector<std::array<double, 2>>;
30 
31  AbscissaOrdinate(std::string_view name, UpdateCacheT update_fn, Color color, Style style, Side side)
32  : name_(name), update_fn_(update_fn), color_(color), style_(style), side_(side)
33  {
34  static_assert(details::has_compatible_signature_v<UpdateCacheT, void(CacheT &)>,
35  "AbscissaOrdinate callback should update its cache");
36  cache_.reserve(16);
37  }
38 
39  void write(mc_rtc::MessagePackBuilder & builder) const
40  {
41  builder.start_array(6);
42  builder.write(static_cast<uint64_t>(type));
43  builder.write(name_);
44  builder.write(cache_);
45  color_.write(builder);
46  builder.write(static_cast<uint64_t>(style_));
47  builder.write(static_cast<uint64_t>(side_));
48  builder.finish_array();
49  cache_.resize(0);
50  }
51 
52  void update() const { update_fn_(cache_); }
53 
55  {
56  style_ = style;
57  return *this;
58  }
59 
61  {
62  side_ = side;
63  return *this;
64  }
65 
66 protected:
67  std::string name_;
68  UpdateCacheT update_fn_;
69  mutable Color color_;
70  mutable std::vector<std::array<double, 2>> cache_;
71  Style style_;
72  Side side_;
73 };
74 
76 template<typename UpdateCacheT, typename GetColor>
77 struct AbscissaOrdinateWithColor : public AbscissaOrdinate<UpdateCacheT>
78 {
79  AbscissaOrdinateWithColor(std::string_view name, UpdateCacheT update_cache, GetColor color, Style style, Side side)
80  : AbscissaOrdinate<UpdateCacheT>(name, update_cache, color(), style, side), get_color_(color)
81  {
83  "AbscissaOrdinate color callback should return a color");
84  }
85 
86  void write(mc_rtc::MessagePackBuilder & builder) const
87  {
88  this->color_ = get_color_();
90  }
91 
92 private:
93  GetColor get_color_;
94 };
95 } // namespace impl
96 
101 template<typename UpdateCacheT, typename MaybeGetColor>
102 auto XYChunk(std::string_view name,
103  UpdateCacheT update_fn,
104  MaybeGetColor color,
105  Style style = Style::Solid,
106  Side side = Side::Left)
107 {
108  if constexpr(std::is_same_v<std::decay_t<MaybeGetColor>, Color>)
109  {
110  return impl::AbscissaOrdinate(name, update_fn, color, style, side);
111  }
112  else { return impl::AbscissaOrdinateWithColor(name, update_fn, color, style, side); }
113 }
114 
116 template<typename GetXT, typename GetYT, typename MaybeGetColor>
117 auto XY(std::string_view name,
118  GetXT get_x_fn,
119  GetYT get_y_fn,
120  MaybeGetColor color,
121  Style style = Style::Solid,
122  Side side = Side::Left)
123 {
125  "XY x-callback should return a single floating-point value");
127  "XY y-callback should return a single floating-point value");
128  using XYCacheT = std::vector<std::array<double, 2>>;
129  if constexpr(std::is_same_v<std::decay_t<MaybeGetColor>, Color>)
130  {
131  return impl::AbscissaOrdinate(
132  name, [get_x_fn, get_y_fn](XYCacheT & cache) { cache.push_back({get_x_fn(), get_y_fn()}); }, color, style,
133  side);
134  }
135  else
136  {
137  static_assert(details::CheckReturnType<MaybeGetColor, Color>::value, "XY color callback should return a color");
139  name, [get_x_fn, get_y_fn](XYCacheT & cache) { cache.push_back({get_x_fn(), get_y_fn()}); }, color, style,
140  side);
141  }
142 }
143 
144 } // namespace plot
145 
146 } // namespace gui
147 
148 } // namespace mc_rtc
constexpr bool has_compatible_signature_v
Definition: traits.h:104
Type
Definition: types.h:31
auto XYChunk(std::string_view name, UpdateCacheT update_fn, MaybeGetColor color, Style style=Style::Solid, Side side=Side::Left)
Definition: AbscissaOrdinate.h:102
auto XY(std::string_view name, GetXT get_x_fn, GetYT get_y_fn, MaybeGetColor color, Style style=Style::Solid, Side side=Side::Left)
Definition: AbscissaOrdinate.h:117
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
AbscissaOrdinateWithColor(std::string_view name, UpdateCacheT update_cache, GetColor color, Style style, Side side)
Definition: AbscissaOrdinate.h:79
void write(mc_rtc::MessagePackBuilder &builder) const
Definition: AbscissaOrdinate.h:86
Definition: AbscissaOrdinate.h:26
Style style_
Definition: AbscissaOrdinate.h:71
void write(mc_rtc::MessagePackBuilder &builder) const
Definition: AbscissaOrdinate.h:39
AbscissaOrdinate(std::string_view name, UpdateCacheT update_fn, Color color, Style style, Side side)
Definition: AbscissaOrdinate.h:31
UpdateCacheT update_fn_
Definition: AbscissaOrdinate.h:68
std::vector< std::array< double, 2 > > cache_
Definition: AbscissaOrdinate.h:70
void update() const
Definition: AbscissaOrdinate.h:52
std::vector< std::array< double, 2 > > CacheT
Definition: AbscissaOrdinate.h:29
Color color_
Definition: AbscissaOrdinate.h:69
AbscissaOrdinate & side(Side side)
Definition: AbscissaOrdinate.h:60
AbscissaOrdinate & style(Style style)
Definition: AbscissaOrdinate.h:54
static constexpr Type type
Definition: AbscissaOrdinate.h:27
Side side_
Definition: AbscissaOrdinate.h:72
std::string name_
Definition: AbscissaOrdinate.h:67