mc_rtc  2.16.0
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 { return details::ArrayInputImpl(name, get_fn, set_fn); }
61 
63 template<typename GetT, typename SetT>
64 auto ArrayInput(const std::string & name, const std::vector<std::string> & labels, GetT get_fn, SetT set_fn)
65 { return details::ArrayInputImpl(name, labels, get_fn, set_fn); }
66 
68 template<typename T>
69 auto ArrayInput(const std::string & name, const std::vector<std::string> & labels, T & value)
71 
76 template<typename T>
77 auto ArrayInput(const std::string & name, T & value)
78 {
79  using Labels = details::Labels<std::decay_t<T>>;
80  auto read = details::read(value);
81  auto write = details::write(value);
82  if constexpr(Labels::has_labels) { return ArrayInput(name, Labels::labels, read, write); }
83  else
84  {
85  return ArrayInput(name, read, write);
86  }
87 }
88 
93 template<bool Degrees = true, typename T>
94 auto RPYInput(const std::string & name, T & value)
95 {
96  return ArrayInput(name, details::RPYLabels<Degrees>::labels, details::read_rpy<Degrees>(value),
97  [&value](const Eigen::Vector3d & rpy)
98  {
99  if constexpr(Degrees) { value = mc_rbdyn::rpyToMat(rpy * mc_rtc::constants::PI / 180.); }
100  else
101  {
102  value = mc_rbdyn::rpyToMat(rpy);
103  }
104  });
105 }
106 
107 } // namespace mc_rtc::gui
std::optional< Eigen::VectorXd > value
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:184
auto write(T &value)
Definition: traits.h:226
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:94
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:116
Definition: traits.h:172