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
mc_rtc::gui::DataElement::write_size
static constexpr size_t write_size()
Definition: elements.h:106
mc_rtc::MessagePackBuilder
Definition: MessagePackBuilder.h:86
mc_rtc::gui::Elements::ArrayInput
@ ArrayInput
mc_rtc::gui::details::ArrayInputImpl::write_size
static constexpr size_t write_size()
Definition: ArrayInput.h:40
mc_rtc::gui::details::ArrayInputImpl::write
void write(mc_rtc::MessagePackBuilder &writer)
Definition: ArrayInput.h:42
rpy_utils.h
mc_rtc::gui::details::ArrayInputImpl::ArrayInputImpl
ArrayInputImpl(const std::string &name, const std::vector< std::string > &labels, GetT get_fn, SetT set_fn)
Definition: ArrayInput.h:35
mc_rtc::gui::ArrayInput
auto ArrayInput(const std::string &name, GetT get_fn, SetT set_fn)
Definition: ArrayInput.h:59
mc_rtc::gui::details::ArrayInputImpl
Definition: ArrayInput.h:27
mc_rtc::gui::details::read
auto read(const T &&value)
Definition: traits.h:188
elements.h
mc_rtc::gui::details::write
auto write(T &value)
Definition: traits.h:224
mc_rtc::gui::details::ArrayInputImpl::ArrayInputImpl
ArrayInputImpl(const std::string &name, GetT get_fn, SetT set_fn)
Definition: ArrayInput.h:32
mc_rtc::MessagePackBuilder::write
void write()
mc_rtc::gui::details::RPYLabels
Definition: traits.h:175
mc_rtc::constants::PI
constexpr double PI
Definition: constants.h:18
mc_rtc::gui::details::ArrayInputImpl::type
static constexpr auto type
Definition: ArrayInput.h:29
mc_rtc::gui::details::ArrayInputImpl::ArrayInputImpl
ArrayInputImpl()
Definition: ArrayInput.h:49
mc_rtc::gui::RPYInput
auto RPYInput(const std::string &name, T &value)
Definition: ArrayInput.h:97
mc_rtc::gui::DataElement::write
void write(mc_rtc::MessagePackBuilder &builder)
Definition: elements.h:108
mc_rbdyn::rpyToMat
Eigen::Matrix3d rpyToMat(const double &r, const double &p, const double &y)
Definition: rpy_utils.h:25
mc_rtc::gui::CommonInputImpl
Definition: elements.h:166
mc_rtc::gui
Definition: Observer.h:15
mc_rtc::gui::details::Labels
Definition: traits.h:119
mc_rtc::gui::Element::name
const std::string & name() const
Definition: elements.h:61