Storage.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <ArmarXGui/interface/RemoteGuiInterface.h>
4 
7 
8 #include <Eigen/Core>
9 
10 namespace armarx::RemoteGui
11 {
12  Vector3f toIceF(Eigen::Vector3f v);
13 
14  Vector3i toIceI(Eigen::Vector3i v);
15 
16  Eigen::Vector3f fromIce(Vector3f v);
17 
18  Eigen::Vector3i fromIce(Vector3i v);
19 
20  const char* getVariantTypeName(ValueVariantType type);
21 
22  ValueVariant makeValue(bool value);
23 
24  ValueVariant makeValue(int value);
25 
26  ValueVariant makeValue(float value);
27 
28  ValueVariant makeValue(std::string value);
29 
30  ValueVariant makeValue(Eigen::Vector3f const& value);
31 
32  ValueVariant makeValue(Eigen::Matrix4f const& value);
33 
34  template <ValueVariantType Type_>
35  struct Storage
36  {
37  using Type = std::nullptr_t;
38  };
39 
40  template <>
41  struct Storage<VALUE_VARIANT_BOOL>
42  {
43  using Type = bool;
44  };
45 
46  template <>
47  struct Storage<VALUE_VARIANT_INT>
48  {
49  using Type = int;
50  };
51 
52  template <>
53  struct Storage<VALUE_VARIANT_FLOAT>
54  {
55  using Type = float;
56  };
57 
58  template <>
59  struct Storage<VALUE_VARIANT_STRING>
60  {
61  using Type = std::string;
62  };
63 
64  template <>
65  struct Storage<VALUE_VARIANT_VECTOR3>
66  {
67  using Type = Eigen::Vector3f;
68  };
69 
70  template <>
71  struct Storage<VALUE_VARIANT_MATRIX4>
72  {
74  };
75 
76 
77 
78  template <typename T>
79  T getSingleValue(ValueVariant const& value, const std::string& name = "");
80 
81  template <>
82  inline std::nullptr_t getSingleValue<std::nullptr_t>(RemoteGui::ValueVariant const& value, const std::string& name)
83  {
85  return nullptr;
86  }
87 
88  template <>
89  inline bool getSingleValue<bool>(ValueVariant const& value, const std::string& name)
90  {
92  if (value.type != VALUE_VARIANT_BOOL)
93  {
94  throw LocalException()
95  << "Expected type 'bool' but got type '"
96  << getVariantTypeName(value.type) << "', name = '" << name << "'";
97  }
98  return value.i != 0;
99  }
100 
101  template <>
102  inline int getSingleValue<int>(ValueVariant const& value, const std::string& name)
103  {
104  ARMARX_TRACE;
105  if (value.type != VALUE_VARIANT_INT)
106  {
107  throw LocalException()
108  << "Expected type 'int' but got type '"
109  << getVariantTypeName(value.type) << "', name = '" << name << "'";
110  }
111  return value.i;
112  }
113 
114  template <>
115  inline float getSingleValue<float>(ValueVariant const& value, const std::string& name)
116  {
117  ARMARX_TRACE;
118  if (value.type != VALUE_VARIANT_FLOAT)
119  {
120  throw LocalException()
121  << "Expected type 'float' but got type '"
122  << getVariantTypeName(value.type) << "', name = '" << name << "'";
123  }
124  return value.f;
125  }
126 
127  template <>
128  inline std::string getSingleValue<std::string>(ValueVariant const& value, const std::string& name)
129  {
130  ARMARX_TRACE;
131  if (value.type != VALUE_VARIANT_STRING)
132  {
133  throw LocalException()
134  << "Expected type 'string' but got type '"
135  << getVariantTypeName(value.type) << "', name = '" << name << "'";
136  }
137  return value.s;
138  }
139 
140  template <>
141  inline Eigen::Vector3f getSingleValue<Eigen::Vector3f>(ValueVariant const& value, const std::string& name)
142  {
143  ARMARX_TRACE;
144  if (value.type != VALUE_VARIANT_VECTOR3)
145  {
146  throw LocalException()
147  << "Expected type 'vector3' but got type '"
148  << getVariantTypeName(value.type) << "', name = '" << name << "'";
149  }
150  if (value.v.size() != 3)
151  {
152  throw LocalException()
153  << "Expected type 'vector3' (size = 3) but got sequence with size '"
154  << value.v.size() << "', name = '" << name << "'";
155  }
156  return Eigen::Vector3f(value.v[0], value.v[1], value.v[2]);
157  }
158 
159  template <>
160  inline Eigen::Matrix4f getSingleValue<Eigen::Matrix4f>(ValueVariant const& value, const std::string& name)
161  {
162  ARMARX_TRACE;
163  if (value.type != VALUE_VARIANT_MATRIX4)
164  {
165  throw LocalException()
166  << "Expected type 'matrix4' but got type '"
167  << getVariantTypeName(value.type) << "', name = '" << name << "'";
168  }
169  if (value.v.size() != 16)
170  {
171  throw LocalException()
172  << "Expected type 'matrix4' (size = 16) but got sequence with size '"
173  << value.v.size() << "', name = '" << name << "'";
174  }
175  Eigen::Matrix4f result;
176  result.row(0) = Eigen::Vector4f(value.v[0], value.v[1], value.v[2], value.v[3]);
177  result.row(1) = Eigen::Vector4f(value.v[4], value.v[5], value.v[6], value.v[7]);
178  result.row(2) = Eigen::Vector4f(value.v[8], value.v[9], value.v[10], value.v[11]);
179  result.row(3) = Eigen::Vector4f(value.v[12], value.v[13], value.v[14], value.v[15]);
180  return result;
181  }
182 
183  template <typename T>
184  T getAndReturnValue(ValueMap const& values, std::string const& name)
185  {
186  ARMARX_TRACE;
187  try
188  {
189  ValueVariant const& variantValue = values.at(name);
190  return getSingleValue<T>(variantValue, name);
191  }
192  catch (...)
193  {
195  ARMARX_ERROR << "Type: " << GetTypeString<T>();
196  ARMARX_ERROR << "RemoteGui::getValue: name.size()" << name.size();
197  ARMARX_ERROR << "RemoteGui::getValue: name" << name;
198  throw;
199  }
200  }
201 
202  // This function can be overloaded to support more types
203  // Example: CartesianWaypointControllerConfig
204  // (in /RobotAPI/source/RobotAPI/libraries/ControllerUIUtility/CartesianWaypointControllerConfig/RemoteGui.h)
205  void getValueFromMap(bool& val, ValueMap const& values, std::string const& name);
206  void getValueFromMap(int& val, ValueMap const& values, std::string const& name);
207  void getValueFromMap(float& val, ValueMap const& values, std::string const& name);
208  void getValueFromMap(std::string& val, ValueMap const& values, std::string const& name);
209  void getValueFromMap(Eigen::Vector3f& val, ValueMap const& values, std::string const& name);
210  void getValueFromMap(Eigen::Matrix4f& val, ValueMap const& values, std::string const& name);
211 
212  bool buttonClicked(RemoteGui::ValueMap const& newValues, ValueMap const& oldValues, std::string const& name);
213 
214  bool operator == (ValueVariant const& left, ValueVariant const& right);
215 
216  bool operator != (ValueVariant const& left, ValueVariant const& right);
217 }
armarx::RemoteGui::toIceF
Vector3f toIceF(Eigen::Vector3f v)
Definition: Storage.cpp:73
armarx::RemoteGui::Storage
Definition: Storage.h:35
LocalException.h
armarx::RemoteGui
Definition: LightweightRemoteGuiComponentPlugin.h:30
armarx::RemoteGui::operator!=
bool operator!=(const ValueVariant &left, const ValueVariant &right)
Definition: Storage.cpp:68
armarx::RemoteGui::getSingleValue< float >
float getSingleValue< float >(ValueVariant const &value, const std::string &name)
Definition: Storage.h:115
ProsthesisInterface.values
values
Definition: ProsthesisInterface.py:190
armarx::RemoteGui::getSingleValue< int >
int getSingleValue< int >(ValueVariant const &value, const std::string &name)
Definition: Storage.h:102
armarx::RemoteGui::ValueMap
std::map< std::string, ValueVariant > ValueMap
Definition: RemoteGuiVisitors.h:41
armarx::RemoteGui::Storage< VALUE_VARIANT_BOOL >::Type
bool Type
Definition: Storage.h:43
armarx::RemoteGui::getAndReturnValue
T getAndReturnValue(ValueMap const &values, std::string const &name)
Definition: Storage.h:184
armarx::GetHandledExceptionString
std::string GetHandledExceptionString()
Definition: Exception.cpp:147
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:69
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::RemoteGui::toIceI
Vector3i toIceI(Eigen::Vector3i v)
Definition: Storage.cpp:82
armarx::RemoteGui::getSingleValue
T getSingleValue(ValueVariant const &value, const std::string &name="")
armarx::RemoteGui::operator==
bool operator==(const ValueVariant &left, const ValueVariant &right)
Definition: Storage.cpp:36
armarx::RemoteGui::Storage< VALUE_VARIANT_STRING >::Type
std::string Type
Definition: Storage.h:61
armarx::RemoteGui::getSingleValue< bool >
bool getSingleValue< bool >(ValueVariant const &value, const std::string &name)
Definition: Storage.h:89
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
armarx::RemoteGui::makeValue
ValueVariant makeValue(bool value)
Definition: Storage.cpp:133
armarx::RemoteGui::fromIce
Eigen::Vector3f fromIce(Vector3f v)
Definition: Storage.cpp:91
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
float
#define float
Definition: 16_Level.h:22
armarx::RemoteGui::Storage< VALUE_VARIANT_INT >::Type
int Type
Definition: Storage.h:49
armarx::RemoteGui::Storage< VALUE_VARIANT_VECTOR3 >::Type
Eigen::Vector3f Type
Definition: Storage.h:67
Logging.h
armarx::RemoteGui::getVariantTypeName
const char * getVariantTypeName(ValueVariantType type)
Definition: Storage.cpp:109
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
armarx::RemoteGui::Storage< VALUE_VARIANT_MATRIX4 >::Type
Eigen::Matrix4f Type
Definition: Storage.h:73
armarx::RemoteGui::buttonClicked
bool buttonClicked(RemoteGui::ValueMap const &newValues, RemoteGui::ValueMap const &oldValues, std::string const &name)
Definition: Storage.cpp:6
armarx::RemoteGui::Storage::Type
std::nullptr_t Type
Definition: Storage.h:37
armarx::RemoteGui::Storage< VALUE_VARIANT_FLOAT >::Type
float Type
Definition: Storage.h:55
armarx::RemoteGui::getValueFromMap
std::enable_if_t< meta::cfg::gui_definition_enabled_v< T >, void > getValueFromMap(T &cfg, RemoteGui::ValueMap const &values, std::string const &name)
Definition: GetValue.h:13