ArrayInput.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 
7 #include <mc_rtc/gui/elements.h>
8 
9 #include <mc_rbdyn/rpy_utils.h>
10 
11 namespace mc_rtc::gui
12 {
13 
14 namespace details
15 {
16 
26 template<typename GetT, typename SetT>
27 struct ArrayInputImpl : public CommonInputImpl<GetT, SetT>
28 {
29  static constexpr auto type = Elements::ArrayInput;
30 
32  ArrayInputImpl(const std::string & name, GetT get_fn, SetT set_fn) : ArrayInputImpl(name, {}, get_fn, set_fn) {}
33 
35  ArrayInputImpl(const std::string & name, const std::vector<std::string> & labels, GetT get_fn, SetT set_fn)
36  : CommonInputImpl<GetT, SetT>::CommonInputImpl(name, get_fn, set_fn), labels_(labels)
37  {
38  }
39 
40  static constexpr size_t write_size() { return CommonInputImpl<GetT, SetT>::write_size() + 1; }
41 
43  {
45  writer.write(labels_);
46  }
47 
50 
51 private:
52  std::vector<std::string> labels_;
53 };
54 
55 } // namespace details
56 
58 template<typename GetT, typename SetT>
59 auto ArrayInput(const std::string & name, GetT get_fn, SetT set_fn)
60 {
61  return details::ArrayInputImpl(name, get_fn, set_fn);
62 }
63 
65 template<typename GetT, typename SetT>
66 auto ArrayInput(const std::string & name, const std::vector<std::string> & labels, GetT get_fn, SetT set_fn)
67 {
68  return details::ArrayInputImpl(name, labels, get_fn, set_fn);
69 }
70 
72 template<typename T>
73 auto ArrayInput(const std::string & name, const std::vector<std::string> & labels, T & value)
74 {
75  return details::ArrayInputImpl(name, labels, details::read(value), details::write(value));
76 }
77 
82 template<typename T>
83 auto ArrayInput(const std::string & name, T & value)
84 {
85  using Labels = details::Labels<std::decay_t<T>>;
86  auto read = details::read(value);
87  auto write = details::write(value);
88  if constexpr(Labels::has_labels) { return ArrayInput(name, Labels::labels, read, write); }
89  else { return ArrayInput(name, read, write); }
90 }
91 
96 template<bool Degrees = true, typename T>
97 auto RPYInput(const std::string & name, T & value)
98 {
99  return ArrayInput(name, details::RPYLabels<Degrees>::labels, details::read_rpy<Degrees>(value),
100  [&value](const Eigen::Vector3d & rpy)
101  {
102  if constexpr(Degrees) { value = mc_rbdyn::rpyToMat(rpy * mc_rtc::constants::PI / 180.); }
103  else { value = mc_rbdyn::rpyToMat(rpy); }
104  });
105 }
106 
107 } // namespace mc_rtc::gui
Eigen::Matrix3d rpyToMat(const double &r, const double &p, const double &y)
Definition: rpy_utils.h:25
constexpr double PI
Definition: constants.h:18
auto read(const T &&value)
Definition: traits.h:188
auto write(T &value)
Definition: traits.h:224
Definition: Observer.h:16
auto ArrayInput(const std::string &name, GetT get_fn, SetT set_fn)
Definition: ArrayInput.h:59
auto RPYInput(const std::string &name, T &value)
Definition: ArrayInput.h:97
Definition: MessagePackBuilder.h:87
Definition: elements.h:167
void write(mc_rtc::MessagePackBuilder &builder)
Definition: elements.h:108
static constexpr size_t write_size()
Definition: elements.h:106
const std::string & name() const
Definition: elements.h:61
Definition: ArrayInput.h:28
void write(mc_rtc::MessagePackBuilder &writer)
Definition: ArrayInput.h:42
static constexpr size_t write_size()
Definition: ArrayInput.h:40
static constexpr auto type
Definition: ArrayInput.h:29
ArrayInputImpl(const std::string &name, GetT get_fn, SetT set_fn)
Definition: ArrayInput.h:32
ArrayInputImpl(const std::string &name, const std::vector< std::string > &labels, GetT get_fn, SetT set_fn)
Definition: ArrayInput.h:35
ArrayInputImpl()
Definition: ArrayInput.h:49
Definition: traits.h:120
Definition: traits.h:176