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
mc_rtc::MessagePackBuilder
Definition: MessagePackBuilder.h:86
mc_rtc::gui::Color::write
void write(mc_rtc::MessagePackBuilder &builder) const
Definition: types.h:90
types.h
mc_rtc::gui::plot::impl::AbscissaOrdinate::name_
std::string name_
Definition: AbscissaOrdinate.h:67
mc_rtc::gui::plot::impl::AbscissaOrdinateWithColor::write
void write(mc_rtc::MessagePackBuilder &builder) const
Definition: AbscissaOrdinate.h:86
mc_rtc::gui::plot::impl::AbscissaOrdinate::side_
Side side_
Definition: AbscissaOrdinate.h:72
mc_rtc::MessagePackBuilder::finish_array
void finish_array()
mc_rtc::gui::plot::impl::AbscissaOrdinateWithColor
Definition: AbscissaOrdinate.h:77
mc_rtc::gui::plot::Type::AbscissaOrdinate
@ AbscissaOrdinate
mc_rtc::gui::plot::impl::AbscissaOrdinate::side
AbscissaOrdinate & side(Side side)
Definition: AbscissaOrdinate.h:60
mc_rtc::gui::plot::impl::AbscissaOrdinate::style_
Style style_
Definition: AbscissaOrdinate.h:71
mc_rtc::gui::plot::impl::AbscissaOrdinate::CacheT
std::vector< std::array< double, 2 > > CacheT
Definition: AbscissaOrdinate.h:29
mc_rtc::gui::plot::impl::AbscissaOrdinate::AbscissaOrdinate
AbscissaOrdinate(std::string_view name, UpdateCacheT update_fn, Color color, Style style, Side side)
Definition: AbscissaOrdinate.h:31
mc_rtc::gui::plot::impl::AbscissaOrdinate::style
AbscissaOrdinate & style(Style style)
Definition: AbscissaOrdinate.h:54
mc_rtc::gui::plot::impl::AbscissaOrdinate::cache_
std::vector< std::array< double, 2 > > cache_
Definition: AbscissaOrdinate.h:70
mc_rtc::gui::plot::impl::AbscissaOrdinate::update
void update() const
Definition: AbscissaOrdinate.h:52
mc_rtc::MessagePackBuilder::start_array
void start_array(size_t size)
mc_rtc::gui::plot::impl::AbscissaOrdinate::color_
Color color_
Definition: AbscissaOrdinate.h:69
mc_rtc::MessagePackBuilder::write
void write()
Solid
Solid
Definition: types.h:146
mc_rtc::gui::details::has_compatible_signature_v
constexpr bool has_compatible_signature_v
Definition: traits.h:104
mc_rtc::gui::Color
Definition: types.h:19
mc_rtc::gui::plot::impl::AbscissaOrdinate::type
static constexpr Type type
Definition: AbscissaOrdinate.h:27
mc_rtc::gui::plot::impl::AbscissaOrdinateWithColor::AbscissaOrdinateWithColor
AbscissaOrdinateWithColor(std::string_view name, UpdateCacheT update_cache, GetColor color, Style style, Side side)
Definition: AbscissaOrdinate.h:79
mc_rtc::gui::plot::XY
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
mc_rtc::gui::plot::Type
Type
Definition: types.h:30
mc_rtc::gui::plot::impl::AbscissaOrdinate::update_fn_
UpdateCacheT update_fn_
Definition: AbscissaOrdinate.h:68
mc_rtc::gui::plot::impl::AbscissaOrdinate::write
void write(mc_rtc::MessagePackBuilder &builder) const
Definition: AbscissaOrdinate.h:39
traits.h
mc_rtc::gui::details::CheckReturnType
Definition: traits.h:86
mc_rtc::gui::plot::XYChunk
auto XYChunk(std::string_view name, UpdateCacheT update_fn, MaybeGetColor color, Style style=Style::Solid, Side side=Side::Left)
Definition: AbscissaOrdinate.h:102
Left
Left
Definition: types.h:158
mc_rtc::gui::plot::impl::AbscissaOrdinate
Definition: AbscissaOrdinate.h:25
mc_rtc
Definition: Contact.h:87