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;
32  return !std::is_pointer<ReturnT>::value && !std::is_same<ReturnT, void>::value;
33 }
34 
35 template<typename GetT>
36 constexpr bool is_getter_impl(...)
37 {
38  return false;
39 }
40 
46 template<typename GetT>
47 constexpr bool is_getter()
48 {
49  return is_getter_impl<GetT>(nullptr);
50 }
51 
53 struct NotAGetter
54 {
55 };
56 
58 template<typename GetT, bool is_getter>
60 {
61  using type = typename std::decay<decltype(std::declval<GetT>()())>::type;
62 };
63 
64 template<typename GetT>
65 struct ReturnTypeImpl<GetT, false>
66 {
67  using type = NotAGetter;
68 };
69 
71 template<typename GetT>
72 struct ReturnType
73 {
75 };
76 
78 template<typename GetT>
80 
85 template<typename GetT, typename... Args>
87 {
88  static constexpr bool value = false;
89 };
90 
91 template<typename GetT, typename T>
92 struct CheckReturnType<GetT, T>
93 {
94  static constexpr bool value = std::is_convertible<ReturnTypeT<GetT>, typename std::decay<T>::type>::value;
95 };
96 
97 template<typename GetT, typename T, typename... Args>
98 struct CheckReturnType<GetT, T, Args...>
99 {
100  static constexpr bool value = CheckReturnType<GetT, T>::value || CheckReturnType<GetT, Args...>::value;
101 };
102 
103 template<typename CallbackT, typename FunT>
104 inline constexpr bool has_compatible_signature_v = std::is_convertible_v<CallbackT, std::function<FunT>>;
105 
118 template<typename T>
119 struct Labels
120 {
121  static constexpr bool has_labels = false;
122  inline static const std::vector<std::string> labels = {};
123 };
124 
125 template<>
126 struct Labels<Eigen::Vector2d>
127 {
128  static constexpr bool has_labels = true;
129  inline static const std::vector<std::string> labels = {"x", "y"};
130 };
131 
132 template<>
133 struct Labels<Eigen::Vector3d>
134 {
135  static constexpr bool has_labels = true;
136  inline static const std::vector<std::string> labels = {"x", "y", "z"};
137 };
138 
139 template<>
140 struct Labels<Eigen::Vector4d>
141 {
142  static constexpr bool has_labels = true;
143  inline static const std::vector<std::string> labels = {"x", "y", "z", "w"};
144 };
145 
146 template<>
147 struct Labels<Eigen::Quaterniond>
148 {
149  static constexpr bool has_labels = true;
150  inline static const std::vector<std::string> labels = {"w", "x", "y", "z"};
151 };
152 
153 template<>
154 struct Labels<sva::MotionVecd>
155 {
156  static constexpr bool has_labels = true;
157  inline static const std::vector<std::string> labels = {"wx", "wy", "wz", "vx", "vy", "vz"};
158 };
159 
160 template<>
161 struct Labels<sva::ForceVecd>
162 {
163  static constexpr bool has_labels = true;
164  inline static const std::vector<std::string> labels = {"cx", "cy", "cz", "fx", "fy", "fz"};
165 };
166 
167 template<>
168 struct Labels<sva::ImpedanceVecd>
169 {
170  static constexpr bool has_labels = true;
171  inline static const std::vector<std::string> labels = {"cx", "cy", "cz", "fx", "fy", "fz"};
172 };
173 
174 template<bool Degrees>
175 struct RPYLabels
176 {
177  inline static const std::vector<std::string> labels = {"r [deg]", "p [deg]", "y [deg]"};
178 };
179 
180 template<>
181 struct RPYLabels<false>
182 {
183  inline static const std::vector<std::string> labels = {"r [rad]", "p [rad]", "y [rad]"};
184 };
185 
187 template<typename T>
188 auto read(const T && value)
189 {
190  return [value]() -> const T & { return value; };
191 }
192 
194 template<bool Degrees, typename T>
195 auto read_rpy(const T && value)
196 {
197  return [value]() -> Eigen::Vector3d
198  {
199  if constexpr(Degrees) { return mc_rbdyn::rpyFromRotation(value) * 180. / mc_rtc::constants::PI; }
200  else { return mc_rbdyn::rpyFromRotation(value); }
201  };
202 }
203 
205 template<typename T>
206 auto read(const T & value)
207 {
208  return [&value]() -> const T & { return value; };
209 }
210 
212 template<bool Degrees, typename T>
213 auto read_rpy(const T & value)
214 {
215  return [&value]() -> Eigen::Vector3d
216  {
217  if constexpr(Degrees) { return mc_rbdyn::rpyFromRotation(value) * 180. / mc_rtc::constants::PI; }
218  else { return mc_rbdyn::rpyFromRotation(value); }
219  };
220 }
221 
223 template<typename T>
224 auto write(T & value)
225 {
226  return [&value](const T & v) { value = v; };
227 }
228 
230 template<typename T>
231 auto GetValueOrCallbackValue(const T & value_or_cb)
232 {
233  if constexpr(std::is_invocable_v<T>) { return value_or_cb(); }
234  else { return value_or_cb; }
235 }
236 
238 template<typename T>
239 struct is_variant : public std::false_type
240 {
241 };
242 
243 template<typename... Args>
244 struct is_variant<std::variant<Args...>> : public std::true_type
245 {
246 };
247 
248 template<typename T>
249 inline constexpr bool is_variant_v = is_variant<T>::value;
250 
251 } // namespace mc_rtc::gui::details
mc_rtc::gui::details::read_rpy
auto read_rpy(const T &&value)
Definition: traits.h:195
mc_rtc::gui::details::ReturnTypeImpl
Definition: traits.h:59
mc_rtc::gui::details::CheckReturnType::value
static constexpr bool value
Definition: traits.h:88
mc_rtc::gui::details::Labels::has_labels
static constexpr bool has_labels
Definition: traits.h:121
rpy_utils.h
mc_rtc::gui::details
Definition: ArrayInput.h:14
mc_rtc::gui::details::ReturnType::type
typename ReturnTypeImpl< GetT, is_getter< GetT >()>::type type
Definition: traits.h:74
mc_rtc::gui::details::is_variant
Definition: traits.h:239
mc_rtc::gui::details::is_variant_v
constexpr bool is_variant_v
Definition: traits.h:249
mc_rtc::gui::details::GetValueOrCallbackValue
auto GetValueOrCallbackValue(const T &value_or_cb)
Definition: traits.h:231
mc_rtc::gui::details::void_t
void void_t
Definition: traits.h:25
mc_rtc::gui::details::read
auto read(const T &&value)
Definition: traits.h:188
mc_rtc::gui::details::ReturnTypeT
typename ReturnType< GetT >::type ReturnTypeT
Definition: traits.h:79
mc_rtc::gui::details::write
auto write(T &value)
Definition: traits.h:224
mc_rtc::gui::details::Labels::labels
static const std::vector< std::string > labels
Definition: traits.h:122
mc_rbdyn::rpyFromRotation
Eigen::Vector3d rpyFromRotation(const T &value)
Definition: rpy_utils.h:110
mc_rtc::gui::details::is_getter
constexpr bool is_getter()
Definition: traits.h:47
mc_rtc::gui::details::has_compatible_signature_v
constexpr bool has_compatible_signature_v
Definition: traits.h:104
mc_rtc::gui::details::RPYLabels
Definition: traits.h:175
mc_rtc::gui::details::ReturnType
Definition: traits.h:72
mc_rtc::gui::details::RPYLabels::labels
static const std::vector< std::string > labels
Definition: traits.h:177
mc_rtc::constants::PI
constexpr double PI
Definition: constants.h:18
mc_rtc::gui::details::is_getter_impl
constexpr bool is_getter_impl(void_t< decltype(std::declval< GetT >()())> *)
Definition: traits.h:29
constants.h
std
Definition: Contact.h:66
mc_rtc::gui::details::NotAGetter
Definition: traits.h:53
mc_rtc::gui::details::ReturnTypeImpl::type
typename std::decay< decltype(std::declval< GetT >()())>::type type
Definition: traits.h:61
mc_rtc::gui::details::CheckReturnType
Definition: traits.h:86
mc_rtc::gui::details::Labels
Definition: traits.h:119