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
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::OrdinateWithColor::write
void write(mc_rtc::MessagePackBuilder &builder) const
Definition: Ordinate.h:83
mc_rtc::gui::plot::impl::Ordinate::style_
Style style_
Definition: Ordinate.h:69
mc_rtc::gui::plot::Type::Ordinate
@ Ordinate
mc_rtc::gui::plot::impl::Ordinate::type
static constexpr Type type
Definition: Ordinate.h:27
mc_rtc::gui::plot::impl::Ordinate
Definition: Ordinate.h:25
mc_rtc::MessagePackBuilder::finish_array
void finish_array()
mc_rtc::gui::plot::impl::Ordinate::write
void write(mc_rtc::MessagePackBuilder &builder) const
Definition: Ordinate.h:37
mc_rtc::gui::plot::impl::Ordinate::side_
Side side_
Definition: Ordinate.h:70
mc_rtc::gui::plot::impl::Ordinate::color_
Color color_
Definition: Ordinate.h:67
mc_rtc::gui::plot::Y
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
mc_rtc::gui::plot::impl::Ordinate::cache_
std::vector< double > cache_
Definition: Ordinate.h:68
mc_rtc::gui::plot::impl::Ordinate::name_
std::string name_
Definition: Ordinate.h:65
mc_rtc::gui::plot::impl::Ordinate::side
Ordinate & side(Side side)
Definition: Ordinate.h:58
mc_rtc::MessagePackBuilder::start_array
void start_array(size_t size)
mc_rtc::gui::plot::impl::Ordinate::style
Ordinate & style(Style style)
Definition: Ordinate.h:52
mc_rtc::MessagePackBuilder::write
void write()
Solid
Solid
Definition: types.h:146
mc_rtc::gui::Color
Definition: types.h:19
mc_rtc::gui::plot::impl::Ordinate::update
void update() const
Definition: Ordinate.h:50
mc_rtc::gui::plot::impl::Ordinate::get_fn_
GetT get_fn_
Definition: Ordinate.h:66
mc_rtc::gui::plot::impl::Ordinate::Ordinate
Ordinate(std::string_view name, GetT get_fn, Color color, Style style, Side side)
Definition: Ordinate.h:29
mc_rtc::gui::plot::Type
Type
Definition: types.h:30
mc_rtc::gui::plot::impl::OrdinateWithColor::OrdinateWithColor
OrdinateWithColor(std::string_view name, GetT get_fn, GetColor color, Style style, Side side)
Definition: Ordinate.h:77
traits.h
mc_rtc::gui::details::CheckReturnType
Definition: traits.h:86
mc_rtc::gui::plot::impl::OrdinateWithColor
Definition: Ordinate.h:75
Left
Left
Definition: types.h:158
mc_rtc
Definition: Contact.h:87