ocra-recipes
Doxygen documentation for the ocra-recipes repository
Variable.h
Go to the documentation of this file.
1 
16 #ifndef _OCRABASE_VARIABLE_H_
17 #define _OCRABASE_VARIABLE_H_
18 
20 #include "ocra/optim/Composite.h"
21 
22 #include <Eigen/Eigen>
23 namespace ocra{using namespace Eigen;}
24 
25 #include <boost/noncopyable.hpp>
26 
27 #include <string>
28 #include <vector>
29 
30 namespace ocra
31 {
32  class CompositeVariable;
33  // ------------------------------------------------------------
34  // --- HELPERS ------------------------------------------------
35  // ------------------------------------------------------------
36 
38 
45  {
47  friend class Variable;
48  friend class BaseVariable;
49  friend class CompositeVariable;
50  };
51 
53 
57  {
59 
61  VariableParenthood() throw();
62  };
63 
64 
65  // ------------------------------------------------------------
66  // --- VARIABLE -----------------------------------------------
67  // ------------------------------------------------------------
68 
70 
105  class Variable
106  // --- Base classes ---
108  , public ObserverSubject
109  // --- Constraints ---
110  , boost::noncopyable
112  {
113 
114  // --- Type ---------------------------------------------------
115  public:
117  // --- Construction/Destruction, accessors --------------------
118  public:
119  Variable(const std::string& name = "");
120 
122  virtual ~Variable() = 0;
123 
124  virtual const std::string& getName() const;
125 
127  virtual bool isBaseVariable() const;
128 
129  // --- Variables as functions in R^n --------------------------
130  public:
132 
136  double operator[](size_t i) const;
137  double at(size_t i) const;
139 
140  int getSize() const;
141 
143  operator const VectorXd& () const;
144 
145  const VectorXd& getValue() const;
146  void setValue(const VectorXd& value);
147 
149 
154  virtual Variable& getTimeDerivative();
155  virtual Variable& getTimePrimitive();
157 
159 
163  void createTimeDerivative(const std::string& name);
164  void createTimePrimitive(const std::string& name);
166 
167  bool hasTimeDerivative() const;
168  bool hasTimePrimitive() const;
169 
170  // --- Variables as trees (composites) ------------------------
171  public:
173 
185  void getRelativeMappingOf(const Variable& subVariable, std::vector<int>& mapping) const;
186 
188  size_t getNumberOfChildren() const;
189 
191  void printNode(int depth, std::ostream& os) const;
192 
193  protected:
195 
199  void onAttachedParent(const parenthood_t& parent);
200  void onDetachedParent(const parenthood_t& parent);
202 
203  // --- To overload in children classes ------------------------
204  protected:
206  virtual void do_setValue(const VectorXd& value) = 0;
207 
209 
214  virtual Variable* do_createTimeDerivative(const std::string& name) = 0;
215  virtual Variable* do_createTimePrimitive(const std::string& name) = 0;
217 
219 
222  virtual size_t do_getNumberOfChildren() const = 0;
223 
224  // --- Additional interface for derived classes ---------------
225  protected:
227 
234  static void callInsertInMemoryMap
235  (Variable& obj, const parenthood_t& child, size_t whereInChild,
236  std::vector<const double*>::const_iterator start, std::vector<const double*>::const_iterator end);
237 
238  static void callRemoveFromMemoryMap(Variable& obj, const parenthood_t& child, size_t whereInChild, size_t numElements);
239 
240  static std::vector<const double*>& getMemoryMap(Variable& obj);
242 
243  private:
250  struct VariablePtr
251  {
252  Variable* var_;
253  bool owns_;
254  VariablePtr(Variable* var, bool owns): var_(var), owns_(owns) {}
255  operator bool() const { return (var_ != 0x0); }
256  };
257 
259  void insertInMemoryMap(const parenthood_t& child, size_t whereInChild, std::vector<const double*>::const_iterator start, std::vector<const double*>::const_iterator end);
261  void removeFromMemoryMap(const parenthood_t& child, size_t whereInChild, size_t numElements);
263 
264  void updateValue() const;
265 
267 
271  void onChildValueChanged(int timestamp);
272 
273  private:
274  std::vector<const double*> memoryMap_;
275  std::string name_;
276  VariablePtr timeDerivative_;
277  VariablePtr timePrimitive_;
278  mutable VectorXd value_;
279  mutable bool updateValue_;
280  };
281 
282 
283  // ------------------------------------------------------------
284  // --- BASE VARIABLE ------------------------------------------
285  // ------------------------------------------------------------
286 
288  /*
289  This class is part of the Variable hierarchy and correspond to the leaves of a tree
290  of CompositeVariable instances. They embed the memory pointed by the composite variables
291  which they are part of.
292 
293  All the methods here are documented in ocra::Variable.
294  \sa ocra::Variable
295 
296  \warning When a BaseVariable is part of several trees (it has more than one parent),
297  then modifying one parent variable will result in modifying the other one(s), since
298  all parents share the same memory (the one embedded in BaseVariable).
299 
300  \internal Most of the behavior is implemented in ocra::Variable. This class implements
301  the abstract methods defined in ocra::Variable and overloads the methods that handle
302  the internal memory of the variable.
303  */
305  : public Leaf<BaseVariable>
306  , public Variable
307  {
308  public:
310 
313  BaseVariable(const std::string& name, size_t size);
314 
316  bool isBaseVariable() const;
317 
318  void resize(size_t newSize);
319 
320  BaseVariable& getTimeDerivative();
321  BaseVariable& getTimePrimitive();
322 
323  int isAncestorOf(const Variable& var) const;
324  void printSubTree(int depth, std::ostream& os) const;
325 
326  protected:
327  void do_setValue(const VectorXd& value);
328 
330  Variable* do_createTimeDerivative(const std::string& name);
332  Variable* do_createTimePrimitive(const std::string& name);
334 
336  size_t do_getNumberOfChildren() const;
337 
338  private:
339  std::vector<double> memory_;
340  };
341 
342 
343  // ------------------------------------------------------------
344  // --- COMPOSITE VARIABLE -------------------------------------
345  // ------------------------------------------------------------
346 
348  /*
349  This class is part of the Variable hierarchy and correspond to the nodes of a tree
350  of CompositeVariable instances.
351 
352  Most methods declared here are documented in ocra::Variable.
353  \sa ocra::Variable
354 
355  \internal Most of the behavior is implemented in ocra::Variable.
356  */
357  class CompositeVariable
358  : public Composite<Variable, CompositeVariable, VariableParenthood>
359  , public Variable
360  {
361  public:
362  CompositeVariable(const std::string& name);
363  CompositeVariable(const std::string& name, Variable& var);
364  CompositeVariable(const std::string& name, Variable& var1, Variable& var2);
365  CompositeVariable(const std::string& name, const std::vector<Variable*>& vars);
366 
368  void clear();
369 
370  Variable& operator()(size_t i);
371 
372  CompositeVariable& addByMerge(Variable& v);
373 
374  CompositeVariable& getTimeDerivative();
375  CompositeVariable& getTimePrimitive();
376 
378  CompositeVariable& add(Variable& child);
380  CompositeVariable& remove(Variable& child);
382  const Variable& operator()(size_t i) const;
383 
384  int isAncestorOf(const Variable& var) const;
385  void printSubTree(int depth, std::ostream& os) const;
386 
387  protected:
388  void do_setValue(const VectorXd& value);
389 
390  protected:
392 
396  void onAttachedChild(const parenthood_t& child);
397  void onDetachedChild(const parenthood_t& child);
399 
400  protected:
402  Variable* do_createTimeDerivative(const std::string& name);
404  Variable* do_createTimePrimitive(const std::string& name);
406 
408  size_t do_getNumberOfChildren() const;
409  };
410 }
411 
412 #endif //_OCRABASE_VARIABLE_H_
413 
414 // cmake:sourcegroup=Variable
This class stores information about a composite-component relationship.
Definition: Variable.h:56
Utility classes and functions to implement the Composite design pattern.
Component< Variable, CompositeVariable, VariableParenthood >::parenthood_t parenthood_t
Definition: Variable.h:116
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
Base class for the Composite class of the Composite pattern.
Definition: Composite.h:38
size_t startIndexInParentMap
Definition: Variable.h:58
This class represents a variable in a mathematical sense.
Definition: Variable.h:105
Base class for the Component class of the Composite pattern.
Definition: Composite.h:37
Base class for the Leaf class of the Composite pattern.
Definition: Composite.h:774
A concatenation of base variables and other composite variables.
Definition: Variable.h:357
Declaration file of the Observer, Subject and ObserverSubject classes.
Implements a basic variable.
Definition: Variable.h:304