Storage.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <Eigen/Core>
4 
7 
8 #include <ArmarXGui/interface/RemoteGuiInterface.h>
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  template <typename T>
77  T getSingleValue(ValueVariant const& value, const std::string& name = "");
78 
79  template <>
80  inline std::nullptr_t
81  getSingleValue<std::nullptr_t>(RemoteGui::ValueVariant const& value, const std::string& name)
82  {
84  return nullptr;
85  }
86 
87  template <>
88  inline bool
89  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 '" << getVariantTypeName(value.type)
96  << "', name = '" << name << "'";
97  }
98  return value.i != 0;
99  }
100 
101  template <>
102  inline int
103  getSingleValue<int>(ValueVariant const& value, const std::string& name)
104  {
105  ARMARX_TRACE;
106  if (value.type != VALUE_VARIANT_INT)
107  {
108  throw LocalException()
109  << "Expected type 'int' but got type '" << getVariantTypeName(value.type)
110  << "', name = '" << name << "'";
111  }
112  return value.i;
113  }
114 
115  template <>
116  inline float
117  getSingleValue<float>(ValueVariant const& value, const std::string& name)
118  {
119  ARMARX_TRACE;
120  if (value.type != VALUE_VARIANT_FLOAT)
121  {
122  throw LocalException()
123  << "Expected type 'float' but got type '" << getVariantTypeName(value.type)
124  << "', name = '" << name << "'";
125  }
126  return value.f;
127  }
128 
129  template <>
130  inline std::string
131  getSingleValue<std::string>(ValueVariant const& value, const std::string& name)
132  {
133  ARMARX_TRACE;
134  if (value.type != VALUE_VARIANT_STRING)
135  {
136  throw LocalException()
137  << "Expected type 'string' but got type '" << getVariantTypeName(value.type)
138  << "', name = '" << name << "'";
139  }
140  return value.s;
141  }
142 
143  template <>
144  inline Eigen::Vector3f
145  getSingleValue<Eigen::Vector3f>(ValueVariant const& value, const std::string& name)
146  {
147  ARMARX_TRACE;
148  if (value.type != VALUE_VARIANT_VECTOR3)
149  {
150  throw LocalException()
151  << "Expected type 'vector3' but got type '" << getVariantTypeName(value.type)
152  << "', name = '" << name << "'";
153  }
154  if (value.v.size() != 3)
155  {
156  throw LocalException()
157  << "Expected type 'vector3' (size = 3) but got sequence with size '"
158  << value.v.size() << "', name = '" << name << "'";
159  }
160  return Eigen::Vector3f(value.v[0], value.v[1], value.v[2]);
161  }
162 
163  template <>
164  inline Eigen::Matrix4f
165  getSingleValue<Eigen::Matrix4f>(ValueVariant const& value, const std::string& name)
166  {
167  ARMARX_TRACE;
168  if (value.type != VALUE_VARIANT_MATRIX4)
169  {
170  throw LocalException()
171  << "Expected type 'matrix4' but got type '" << getVariantTypeName(value.type)
172  << "', name = '" << name << "'";
173  }
174  if (value.v.size() != 16)
175  {
176  throw LocalException()
177  << "Expected type 'matrix4' (size = 16) but got sequence with size '"
178  << value.v.size() << "', name = '" << name << "'";
179  }
180  Eigen::Matrix4f result;
181  result.row(0) = Eigen::Vector4f(value.v[0], value.v[1], value.v[2], value.v[3]);
182  result.row(1) = Eigen::Vector4f(value.v[4], value.v[5], value.v[6], value.v[7]);
183  result.row(2) = Eigen::Vector4f(value.v[8], value.v[9], value.v[10], value.v[11]);
184  result.row(3) = Eigen::Vector4f(value.v[12], value.v[13], value.v[14], value.v[15]);
185  return result;
186  }
187 
188  template <typename T>
189  T
190  getAndReturnValue(ValueMap const& values, std::string const& name)
191  {
192  ARMARX_TRACE;
193  try
194  {
195  ValueVariant const& variantValue = values.at(name);
196  return getSingleValue<T>(variantValue, name);
197  }
198  catch (...)
199  {
201  ARMARX_ERROR << "Type: " << GetTypeString<T>();
202  ARMARX_ERROR << "RemoteGui::getValue: name.size()" << name.size();
203  ARMARX_ERROR << "RemoteGui::getValue: name" << name;
204  throw;
205  }
206  }
207 
208  // This function can be overloaded to support more types
209  // Example: CartesianWaypointControllerConfig
210  // (in /RobotAPI/source/RobotAPI/libraries/ControllerUIUtility/CartesianWaypointControllerConfig/RemoteGui.h)
211  void getValueFromMap(bool& val, ValueMap const& values, std::string const& name);
212  void getValueFromMap(int& val, ValueMap const& values, std::string const& name);
213  void getValueFromMap(float& val, ValueMap const& values, std::string const& name);
214  void getValueFromMap(std::string& val, ValueMap const& values, std::string const& name);
215  void getValueFromMap(Eigen::Vector3f& val, ValueMap const& values, std::string const& name);
216  void getValueFromMap(Eigen::Matrix4f& val, ValueMap const& values, std::string const& name);
217 
218  bool buttonClicked(RemoteGui::ValueMap const& newValues,
219  ValueMap const& oldValues,
220  std::string const& name);
221 
222  bool operator==(ValueVariant const& left, ValueVariant const& right);
223 
224  bool operator!=(ValueVariant const& left, ValueVariant const& right);
225 } // namespace armarx::RemoteGui
armarx::RemoteGui::toIceF
Vector3f toIceF(Eigen::Vector3f v)
Definition: Storage.cpp:80
armarx::RemoteGui::Storage
Definition: Storage.h:35
LocalException.h
armarx::RemoteGui
Definition: LightweightRemoteGuiComponentPlugin.h:30
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:650
armarx::RemoteGui::operator!=
bool operator!=(const ValueVariant &left, const ValueVariant &right)
Definition: Storage.cpp:74
armarx::RemoteGui::getSingleValue< float >
float getSingleValue< float >(ValueVariant const &value, const std::string &name)
Definition: Storage.h:117
ProsthesisInterface.values
values
Definition: ProsthesisInterface.py:190
armarx::RemoteGui::getSingleValue< int >
int getSingleValue< int >(ValueVariant const &value, const std::string &name)
Definition: Storage.h:103
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:190
armarx::GetHandledExceptionString
std::string GetHandledExceptionString()
Definition: Exception.cpp:165
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:77
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
armarx::RemoteGui::toIceI
Vector3i toIceI(Eigen::Vector3i v)
Definition: Storage.cpp:90
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:41
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:196
armarx::RemoteGui::makeValue
ValueVariant makeValue(bool value)
Definition: Storage.cpp:144
armarx::RemoteGui::fromIce
Eigen::Vector3f fromIce(Vector3f v)
Definition: Storage.cpp:100
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
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:120
T
float T
Definition: UnscentedKalmanFilterTest.cpp:38
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:7
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:12