Cheetah Software  1.0
ControlParameters.h
Go to the documentation of this file.
1 
18 #ifndef PROJECT_CONTROLPAREMETERS_H
19 #define PROJECT_CONTROLPAREMETERS_H
20 
21 #include <map>
22 #include <mutex>
23 #include <string>
24 #include "Utilities/utilities.h"
25 #include "cTypes.h"
26 
27 #define CONTROL_PARAMETER_MAXIMUM_NAME_LENGTH 64
28 
29 #define INIT_PARAMETER(name) param_##name(#name, name, collection)
30 #define DECLARE_PARAMETER(type, name) \
31  type name; \
32  ControlParameter param_##name;
33 
35  FLOAT = 0,
36  DOUBLE = 1,
37  S64 = 2,
38  VEC3_DOUBLE = 3,
39  VEC3_FLOAT = 4
40 };
41 
43 
45  ControlParameterValueKind valueKind);
46 
48  float* f;
49  double* d;
50  s64* i;
51  float* vec3f;
52  double* vec3d;
53 };
54 
56  float f;
57  double d;
58  s64 i;
59  float vec3f[3];
60  double vec3d[3];
61 };
62 
65 
66 class ControlParameter;
67 
72  public:
73  explicit ControlParameterCollection(const std::string& name) : _name(name) {}
74 
80  void addParameter(ControlParameter* param, const std::string& name) {
81  if (mapContains(_map, name)) {
82  printf(
83  "[ERROR] ControlParameterCollection %s: tried to add parameter %s "
84  "twice!\n",
85  _name.c_str(), name.c_str());
86  throw std::runtime_error("control parameter error");
87  }
88  _map[name] = param;
89  }
90 
95  ControlParameter& lookup(const std::string& name) {
96  if (mapContains(_map, name)) {
97  return *_map[name];
98  } else {
99  // for now:
100  throw std::runtime_error("parameter " + name +
101  " wasn't found in parameter collection " +
102  _name);
103  }
104  }
105 
106  std::string
107  printToIniString();
108  std::string printToYamlString();
109  bool checkIfAllSet();
111  void clearAllSet();
112  void deleteAll();
113 
114  std::map<std::string, ControlParameter*> _map;
115 
116  private:
117  std::string _name;
118 };
119 
121  public:
127  ControlParameter(const std::string& name, double& value,
128  ControlParameterCollection& collection,
129  const std::string& units = "") {
130  _name = name;
131  truncateName();
132  _units = units;
133  _value.d = &value;
135  collection.addParameter(this, name);
136  }
137 
138  ControlParameter(const std::string& name, float& value,
139  ControlParameterCollection& collection,
140  const std::string& units = "") {
141  _name = name;
142  truncateName();
143  _units = units;
144  _value.f = &value;
146  collection.addParameter(this, name);
147  }
148 
149  ControlParameter(const std::string& name, s64& value,
150  ControlParameterCollection& collection,
151  const std::string& units = "") {
152  _name = name;
153  truncateName();
154  _units = units;
155  _value.i = &value;
157  collection.addParameter(this, name);
158  }
159 
160  ControlParameter(const std::string& name, Vec3<float>& value,
161  ControlParameterCollection& collection,
162  const std::string& units = "") {
163  _name = name;
164  truncateName();
165  _units = units;
166  _value.vec3f = value.data();
168  collection.addParameter(this, name);
169  }
170 
171  ControlParameter(const std::string& name, Vec3<double>& value,
172  ControlParameterCollection& collection,
173  const std::string& units = "") {
174  _name = name;
175  truncateName();
176  _units = units;
177  _value.vec3d = value.data();
179  collection.addParameter(this, name);
180  }
181 
182  ControlParameter(const std::string& name, ControlParameterValueKind kind) {
183  _name = name;
184  truncateName();
185  _kind = kind;
186  _value.vec3d = (double*)&_staticValue;
187  }
188 
189  void truncateName() {
190  if (_name.length() > CONTROL_PARAMETER_MAXIMUM_NAME_LENGTH) {
191  printf("[Error] name %s is too long, shortening to ", _name.c_str());
192  _name.resize(CONTROL_PARAMETER_MAXIMUM_NAME_LENGTH - 1);
193  printf("%s\n", _name.c_str());
194  }
195  }
196 
201  void initializeDouble(double d) {
202  if (_kind != ControlParameterValueKind::DOUBLE) {
203  throw std::runtime_error("Tried to initialize control parameter " +
204  _name + " as a double!");
205  }
206  _set = true;
207  *_value.d = d;
208  }
209 
210 
211 
212  void initializeFloat(float f) {
213  if (_kind != ControlParameterValueKind::FLOAT) {
214  throw std::runtime_error("Tried to initialize control parameter " +
215  _name + " as a float!");
216  }
217  _set = true;
218  *_value.f = f;
219  }
220 
222  if (_kind != ControlParameterValueKind::S64) {
223  throw std::runtime_error("Tried to initialize control parameter " +
224  _name + " as an integer!");
225  }
226  _set = true;
227  *_value.i = i;
228  }
229 
230  void initializeVec3f(const Vec3<float>& v) {
232  throw std::runtime_error("Tried to initialize control parameter " +
233  _name + " as a vector3f");
234  }
235  _set = true;
236  _value.vec3f[0] = v[0];
237  _value.vec3f[1] = v[1];
238  _value.vec3f[2] = v[2];
239  }
240 
241  void initializeVec3d(const Vec3<double>& v) {
243  throw std::runtime_error("Tried to initialize control parameter " +
244  _name + " as a vector3d");
245  }
246  _set = true;
247  _value.vec3d[0] = v[0];
248  _value.vec3d[1] = v[1];
249  _value.vec3d[2] = v[2];
250  }
251 
253  if (kind != _kind) {
254  throw std::runtime_error("type mismatch in set");
255  }
256  switch (kind) {
258  *_value.f = value.f;
259  break;
261  *_value.d = value.d;
262  break;
264  *_value.i = value.i;
265  break;
267  _value.vec3f[0] = value.vec3f[0];
268  _value.vec3f[1] = value.vec3f[1];
269  _value.vec3f[2] = value.vec3f[2];
270  break;
272  _value.vec3d[0] = value.vec3d[0];
273  _value.vec3d[1] = value.vec3d[1];
274  _value.vec3d[2] = value.vec3d[2];
275  break;
276  default:
277  throw std::runtime_error("invalid kind");
278  }
279  _set = true;
280  }
281 
283  ControlParameterValue value;
284  if (kind != _kind) {
285  throw std::runtime_error("type mismatch in get");
286  }
287  switch (_kind) {
289  value.f = *_value.f;
290  break;
292  value.d = *_value.d;
293  break;
295  value.i = *_value.i;
296  break;
298  value.vec3f[0] = _value.vec3f[0];
299  value.vec3f[1] = _value.vec3f[1];
300  value.vec3f[2] = _value.vec3f[2];
301  break;
303  value.vec3d[0] = _value.vec3d[0];
304  value.vec3d[1] = _value.vec3d[1];
305  value.vec3d[2] = _value.vec3d[2];
306  break;
307  default:
308  throw std::runtime_error("invalid kind");
309  }
310  return value;
311  }
312 
316  std::string toString() {
317  std::string result;
318  switch (_kind) {
320  result += numberToString(*_value.d);
321  break;
323  result += numberToString(*_value.f);
324  break;
326  result += std::to_string(*_value.i);
327  break;
329  result += "[";
330  result += numberToString(_value.vec3f[0]) + ", ";
331  result += numberToString(_value.vec3f[1]) + ", ";
332  result += numberToString(_value.vec3f[2]) + "]";
333  break;
335  result += "[";
336  result += numberToString(_value.vec3d[0]) + ", ";
337  result += numberToString(_value.vec3d[1]) + ", ";
338  result += numberToString(_value.vec3d[2]) + "]";
339  break;
340  default:
341  result += "<unknown type " + std::to_string((u32)(_kind)) +
342  "> (add it yourself in ControlParameters.h!)";
343  break;
344  }
345  return result;
346  }
347 
348  bool setFromString(const std::string& value) {
349  switch (_kind) {
351  *_value.d = std::stod(value);
352  break;
354  *_value.f = std::stof(value);
355  break;
357  *_value.i = std::stoll(value);
358  break;
360  Vec3<float> v = stringToVec3<float>(value);
361  _value.vec3f[0] = v[0];
362  _value.vec3f[1] = v[1];
363  _value.vec3f[2] = v[2];
364  } break;
366  Vec3<double> v = stringToVec3<double>(value);
367  _value.vec3d[0] = v[0];
368  _value.vec3d[1] = v[1];
369  _value.vec3d[2] = v[2];
370  } break;
371  default:
372  return false;
373  }
374  return true;
375  }
376 
377  bool _set = false;
379  std::string _name;
380  std::string _units;
383 
384  private:
385 };
386 
392  public:
398  ControlParameters(const std::string& name) : collection(name), _name(name) {}
399 
403  bool isFullyInitialized() { return collection.checkIfAllSet(); }
404 
408  void initializeDouble(const std::string& name, double d) {
409  collection.lookup(name).initializeDouble(d);
410  }
411 
412  void initializeFloat(const std::string& name, float f) {
413  collection.lookup(name).initializeFloat(f);
414  }
415 
416  void initializeInteger(const std::string& name, s64 i) {
417  collection.lookup(name).initializeInteger(i);
418  }
419 
420  void initializeVec3f(const std::string& name, Vec3<float>& v) {
421  collection.lookup(name).initializeVec3f(v);
422  }
423 
424  void initializeVec3d(const std::string& name, Vec3<double>& v) {
425  collection.lookup(name).initializeVec3d(v);
426  }
427 
428  void lockMutex() { _mutex.lock(); }
429 
430  void unlockMutex() { _mutex.unlock(); }
431 
432  void writeToIniFile(const std::string& path);
433  void initializeFromIniFile(const std::string& path);
434  void initializeFromYamlFile(const std::string& path);
435  void defineAndInitializeFromYamlFile(const std::string& path);
436 
437  void writeToYamlFile(const std::string& path);
438 
439  std::string generateUnitializedList();
440 
442 
443  protected:
444  std::string _name;
445  std::mutex _mutex;
446 };
447 
448 #endif // PROJECT_CONTROLPAREMETERS_H
Common types that are only valid in C++.
ControlParameter(const std::string &name, s64 &value, ControlParameterCollection &collection, const std::string &units="")
ControlParameterCollection collection
ControlParameterValueKind _kind
ControlParameterValueKind
std::string controlParameterValueKindToString(ControlParameterValueKind valueKind)
std::map< std::string, ControlParameter * > _map
ControlParameterCollection(const std::string &name)
ControlParameterValue _staticValue
std::string numberToString(T number)
Definition: utilities.h:130
typename Eigen::Matrix< T, 3, 1 > Vec3
Definition: cppTypes.h:26
ControlParameterValuePtr _value
std::string toString()
ControlParameter(const std::string &name, float &value, ControlParameterCollection &collection, const std::string &units="")
int64_t s64
Definition: cTypes.h:24
void initializeDouble(const std::string &name, double d)
void initializeVec3d(const Vec3< double > &v)
void initializeInteger(s64 i)
void initializeDouble(double d)
uint32_t u32
Definition: cTypes.h:18
#define CONTROL_PARAMETER_MAXIMUM_NAME_LENGTH
void initializeVec3d(const std::string &name, Vec3< double > &v)
void initializeInteger(const std::string &name, s64 i)
ControlParameter(const std::string &name, ControlParameterValueKind kind)
ControlParameter & lookup(const std::string &name)
ControlParameter(const std::string &name, Vec3< float > &value, ControlParameterCollection &collection, const std::string &units="")
std::string controlParameterValueToString(ControlParameterValue v, ControlParameterValueKind kind)
void initializeFloat(const std::string &name, float f)
uint64_t u64
Definition: cTypes.h:17
void initializeVec3f(const Vec3< float > &v)
void initializeFloat(float f)
void initializeVec3f(const std::string &name, Vec3< float > &v)
ControlParameter(const std::string &name, double &value, ControlParameterCollection &collection, const std::string &units="")
ControlParameterValueKind getControlParameterValueKindFromString(const std::string &str)
bool setFromString(const std::string &value)
void addParameter(ControlParameter *param, const std::string &name)
ControlParameter(const std::string &name, Vec3< double > &value, ControlParameterCollection &collection, const std::string &units="")
bool mapContains(const std::map< T1, T2 > &set, T1 key)
Definition: utilities.h:120
ControlParameters(const std::string &name)
MX f(const MX &x, const MX &u)