iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
nrt::StateDef< Elements > Class Template Reference

#include <nrt/Probabilistic/Types/StateDef.H>

template<typename... Elements>
class nrt::StateDef< Elements >

A Simple class to represent possibly complex and nested state information with dual access.

The goal of this class is not to store data but rather to provide easy, intuitive, hierarchical named access to data stored in an Eigen vector. The StateDef class thus does not provide storage but only convenience access. This class assumes that you have defined some datatypes whose internal data members are reference to double, such as:

struct Pos3D
Pos3D(double storage[]) : x(storage[0]), y(storage[1]), z(storage[2]) { }
double & x;
double & y;
double & z;
static size_t const size = 3;
struct Vel3D
Vel3D(double storage[]) : vx(storage[0]), vy(storage[1]), vz(storage[2]) { }
double & vx;
double & vy;
double & vz;
static size_t const size = 3;

The constructor from double[] and the size static data member are required. Once you have this, you can use StateDef to build hierarchical structures of these datatypes and to access their leaf data elements naturally:

typedef StateDef<Pos3D, Vel3D> myStateDef;
myStateDef::datatype data; // allocate some Eigen vector to actually store the data
myStateDef state(data); // attach a state to it, which will allow easy named access to data elements
state.get<Pos3D>().x = 1.0;
state.get<Vel3D>().vz = 6.0;

And of course the data remains accessible as an Eigen vector for vector/matrix operations on it. You can compose State elements, for example:

typedef StateDef<Vel3D, Pos3D> MachineState;
typedef StateDef<MachineState, Pos3D, Vel3D> FactoryState;
typedef StateDef<Pos3D, FactoryState, Vel3D> ComplicatedState;
FactoryState::datatype factorydata;
FactoryState s(factorydata);
std::cout << "FactoryState size = " << FactoryState::size << std::endl;
ComplicatedState::datatype csdata;
ComplicatedState cs(csdata);
std::cout << "ComplicatedState size = " << ComplicatedState::size << std::endl;
cs.get<Vel3D>().vy = 123.0;
cs.get<FactoryState>().get<Pos3D>().x = 456.0;
cs.get<FactoryState>().get<MachineState>().get<Pos3D>().z = 789.0;
size_t idx = cs.indexof(cs.get<FactoryState>().get<MachineState>().get<Vel3D>().vy);
csdata[idx] = 42.0; // same effect as cs[idx] = 42.0;
std::cout << "ComplicatedState: " << cs << std::endl;

Definition at line 117 of file StateDef.H.

Inherits nrt::StateAccessor< Elements >.

Public Types

typedef Eigen::Matrix< double,
StateAccessor< Elements...>
::size, 1 > 
 The datatype this particular StateDef instance should be attached to.

Public Member Functions

 StateDef (datatype &data)
 Construct by attaching to an already allocated Eigen vector.
 StateDef (double data[])
 Construct by attaching to an already allocated raw C vector. More...
size_t const indexof (double &addr)
 Get the index of a particular leaf element. More...
double & operator[] (size_t const idx)
 Get an element by index, non-const version.
double const & operator[] (size_t const idx) const
 Get an element by index, const version.

Constructor & Destructor Documentation

template<typename... Elements>
nrt::StateDef< Elements >::StateDef ( double  data[])

Construct by attaching to an already allocated raw C vector.

Users should normally not use this constructor but it is used by nested StateDef elements.

Definition at line 91 of file StateDefImpl.H.

Member Function Documentation

template<typename... Elements>
size_t const nrt::StateDef< Elements >::indexof ( double &  addr)

Get the index of a particular leaf element.

Leaf elements are of type double &, and can be retrieved using the get() functions of StateAccessor.

Definition at line 96 of file StateDefImpl.H.

The documentation for this class was generated from the following files: