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
10namespace 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 {
73 using Type = Eigen::Matrix4f;
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 {
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 {
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 {
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 {
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 {
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 {
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
#define float
Definition 16_Level.h:22
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:196
T getSingleValue(ValueVariant const &value, const std::string &name="")
Eigen::Vector3f fromIce(Vector3f v)
Definition Storage.cpp:100
bool getSingleValue< bool >(ValueVariant const &value, const std::string &name)
Definition Storage.h:89
bool operator==(const ValueVariant &left, const ValueVariant &right)
Definition Storage.cpp:41
float getSingleValue< float >(ValueVariant const &value, const std::string &name)
Definition Storage.h:117
std::nullptr_t getSingleValue< std::nullptr_t >(RemoteGui::ValueVariant const &value, const std::string &name)
Definition Storage.h:81
T getAndReturnValue(ValueMap const &values, std::string const &name)
Definition Storage.h:190
Vector3f toIceF(Eigen::Vector3f v)
Definition Storage.cpp:80
Eigen::Matrix4f getSingleValue< Eigen::Matrix4f >(ValueVariant const &value, const std::string &name)
Definition Storage.h:165
int getSingleValue< int >(ValueVariant const &value, const std::string &name)
Definition Storage.h:103
ValueVariant makeValue(bool value)
Definition Storage.cpp:144
std::map< std::string, ValueVariant > ValueMap
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
bool buttonClicked(RemoteGui::ValueMap const &newValues, RemoteGui::ValueMap const &oldValues, std::string const &name)
Definition Storage.cpp:7
Eigen::Vector3f getSingleValue< Eigen::Vector3f >(ValueVariant const &value, const std::string &name)
Definition Storage.h:145
bool operator!=(const ValueVariant &left, const ValueVariant &right)
Definition Storage.cpp:74
std::string getSingleValue< std::string >(ValueVariant const &value, const std::string &name)
Definition Storage.h:131
const char * getVariantTypeName(ValueVariantType type)
Definition Storage.cpp:120
Vector3i toIceI(Eigen::Vector3i v)
Definition Storage.cpp:90
std::string GetHandledExceptionString()
std::string GetTypeString(const std::type_info &tinf, bool withoutNamespaceSpecifier=false)
std::nullptr_t Type
Definition Storage.h:37
#define ARMARX_TRACE
Definition trace.h:77