mc_rtc  2.16.0
traits.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 #include <mc_rtc/constants.h>
10 
11 #include <mc_rbdyn/rpy_utils.h>
12 
13 #include <SpaceVecAlg/SpaceVecAlg>
14 
15 #include <array>
16 #include <type_traits>
17 #include <variant>
18 #include <vector>
19 
20 namespace mc_rtc::gui::details
21 {
22 
24 template<typename...>
25 using void_t = void;
26 
28 template<typename GetT>
29 constexpr bool is_getter_impl(void_t<decltype(std::declval<GetT>()())> *)
30 {
31  using ReturnT = typename std::decay<decltype(std::declval<GetT>()())>::type;
33 }
34 
35 template<typename GetT>
36 constexpr bool is_getter_impl(...)
37 { return false; }
38 
44 template<typename GetT>
45 constexpr bool is_getter()
46 { return is_getter_impl<GetT>(nullptr); }
47 
49 struct NotAGetter
50 {
51 };
52 
54 template<typename GetT, bool is_getter>
56 {
57  using type = typename std::decay<decltype(std::declval<GetT>()())>::type;
58 };
59 
60 template<typename GetT>
61 struct ReturnTypeImpl<GetT, false>
62 {
63  using type = NotAGetter;
64 };
65 
67 template<typename GetT>
68 struct ReturnType
69 {
71 };
72 
74 template<typename GetT>
76 
81 template<typename GetT, typename... Args>
83 {
84  static constexpr bool value = false;
85 };
86 
87 template<typename GetT, typename T>
88 struct CheckReturnType<GetT, T>
89 {
90  static constexpr bool value = std::is_convertible<ReturnTypeT<GetT>, typename std::decay<T>::type>::value;
91 };
92 
93 template<typename GetT, typename T, typename... Args>
94 struct CheckReturnType<GetT, T, Args...>
95 {
96  static constexpr bool value = CheckReturnType<GetT, T>::value || CheckReturnType<GetT, Args...>::value;
97 };
98 
99 template<typename CallbackT, typename FunT>
100 inline constexpr bool has_compatible_signature_v = std::is_convertible_v<CallbackT, std::function<FunT>>;
101 
114 template<typename T>
115 struct Labels
116 {
117  static constexpr bool has_labels = false;
118  inline static const std::vector<std::string> labels = {};
119 };
120 
121 template<>
122 struct Labels<Eigen::Vector2d>
123 {
124  static constexpr bool has_labels = true;
125  inline static const std::vector<std::string> labels = {"x", "y"};
126 };
127 
128 template<>
129 struct Labels<Eigen::Vector3d>
130 {
131  static constexpr bool has_labels = true;
132  inline static const std::vector<std::string> labels = {"x", "y", "z"};
133 };
134 
135 template<>
136 struct Labels<Eigen::Vector4d>
137 {
138  static constexpr bool has_labels = true;
139  inline static const std::vector<std::string> labels = {"x", "y", "z", "w"};
140 };
141 
142 template<>
143 struct Labels<Eigen::Quaterniond>
144 {
145  static constexpr bool has_labels = true;
146  inline static const std::vector<std::string> labels = {"w", "x", "y", "z"};
147 };
148 
149 template<>
151 {
152  static constexpr bool has_labels = true;
153  inline static const std::vector<std::string> labels = {"wx", "wy", "wz", "vx", "vy", "vz"};
154 };
155 
156 template<>
158 {
159  static constexpr bool has_labels = true;
160  inline static const std::vector<std::string> labels = {"cx", "cy", "cz", "fx", "fy", "fz"};
161 };
162 
163 template<>
165 {
166  static constexpr bool has_labels = true;
167  inline static const std::vector<std::string> labels = {"cx", "cy", "cz", "fx", "fy", "fz"};
168 };
169 
170 template<bool Degrees>
171 struct RPYLabels
172 {
173  inline static const std::vector<std::string> labels = {"r [deg]", "p [deg]", "y [deg]"};
174 };
175 
176 template<>
177 struct RPYLabels<false>
178 {
179  inline static const std::vector<std::string> labels = {"r [rad]", "p [rad]", "y [rad]"};
180 };
181 
183 template<typename T>
184 auto read(const T && value)
185 {
186  return [value]() -> const T & { return value; };
187 }
188 
190 template<bool Degrees, typename T>
191 auto read_rpy(const T && value)
192 {
193  return [value]() -> Eigen::Vector3d
194  {
195  if constexpr(Degrees) { return mc_rbdyn::rpyFromRotation(value) * 180. / mc_rtc::constants::PI; }
196  else
197  {
199  }
200  };
201 }
202 
204 template<typename T>
205 auto read(const T & value)
206 {
207  return [&value]() -> const T & { return value; };
208 }
209 
211 template<bool Degrees, typename T>
212 auto read_rpy(const T & value)
213 {
214  return [&value]() -> Eigen::Vector3d
215  {
216  if constexpr(Degrees) { return mc_rbdyn::rpyFromRotation(value) * 180. / mc_rtc::constants::PI; }
217  else
218  {
220  }
221  };
222 }
223 
225 template<typename T>
226 auto write(T & value)
227 {
228  return [&value](const T & v) { value = v; };
229 }
230 
232 template<typename T>
233 auto GetValueOrCallbackValue(const T & value_or_cb)
234 {
235  if constexpr(std::is_invocable_v<T>) { return value_or_cb(); }
236  else
237  {
238  return value_or_cb;
239  }
240 }
241 
243 template<typename T>
244 struct is_variant : public std::false_type
245 {
246 };
247 
248 template<typename... Args>
249 struct is_variant<std::variant<Args...>> : public std::true_type
250 {
251 };
252 
253 template<typename T>
254 inline constexpr bool is_variant_v = is_variant<T>::value;
255 
256 } // namespace mc_rtc::gui::details
std::optional< Eigen::VectorXd > value
Eigen::Vector3d rpyFromRotation(const T &value)
Definition: rpy_utils.h:102
constexpr double PI
Definition: constants.h:18
Definition: ArrayInput.h:15
auto GetValueOrCallbackValue(const T &value_or_cb)
Definition: traits.h:233
constexpr bool is_variant_v
Definition: traits.h:254
constexpr bool is_getter()
Definition: traits.h:45
void void_t
Definition: traits.h:25
auto read_rpy(const T &&value)
Definition: traits.h:191
typename ReturnType< GetT >::type ReturnTypeT
Definition: traits.h:75
constexpr bool has_compatible_signature_v
Definition: traits.h:100
constexpr bool is_getter_impl(void_t< decltype(std::declval< GetT >()())> *)
Definition: traits.h:29
auto read(const T &&value)
Definition: traits.h:184
auto write(T &value)
Definition: traits.h:226
ForceVec< double > ForceVecd
ImpedanceVec< double > ImpedanceVecd
MotionVec< double > MotionVecd
static constexpr bool value
Definition: traits.h:84
Definition: traits.h:116
static constexpr bool has_labels
Definition: traits.h:117
static const std::vector< std::string > labels
Definition: traits.h:118
Definition: traits.h:50
Definition: traits.h:172
static const std::vector< std::string > labels
Definition: traits.h:173
typename std::decay< decltype(std::declval< GetT >()())>::type type
Definition: traits.h:57
Definition: traits.h:69
typename ReturnTypeImpl< GetT, is_getter< GetT >()>::type type
Definition: traits.h:70
Definition: traits.h:245