iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
StateVector.H
Go to the documentation of this file.
1 /*! @file
2  @author Randolph Voorhies (voorhies at usc dot edu)
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 #ifndef INCLUDE_NRT_PROBABILISTIC_TYPES_STATEVECTOR_H
36 #define INCLUDE_NRT_PROBABILISTIC_TYPES_STATEVECTOR_H
37 
38 #include <nrt/Eigen/Eigen.H>
40 
41 namespace nrt
42 {
43  //! A StateVector is a wrapper around an Eigen::Vector which provides methods to access elements by a StateDefinition
44  /*!
45  \tparam StateDef The StateDefinition which defines the fields of this vector
46 
47  The only method provided by this class is access(), which is used to
48  access various fields or field groups of the internal Eigen::Vector
49 
50 @code
51 NRT_STATE_FIELD_GROUP(Position2D,(x)(y));
52 NRT_STATE_FIELD_GROUP(Velocity2D,(x)(y));
53 nrt::StateVector<nrt::StateDefinition<Position2D, Velocity2D>> myvector;
54 
55 // Single field access
56 myvector.access<Position2D::x>() = 1.1;
57 
58 // Field group access
59 myvector.access<Velocity>() = Eigen::Vector2d(3.3, 4.4);
60 
61 // Entire vector accss
62 myvector.access<>() = Eigen::Vector4d(1.1, 2.2, 3.3, 4.4);
63 
64 @endcode
65 
66  \ingroup probabalistic
67  \see probabalistic
68  */
69  template <class StateDef>
71  {
72  private:
73  typedef Eigen::Matrix<double, StateDef::num_fields, 1> Vector;
74  Vector itsVector;
75 
76  public:
77  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
78 
79  constexpr size_t size() { return StateDef::num_fields; }
80 
81  //! Default constructor
82  /*! The internal vector is set to 0 */
83  StateVector();
84 
85  //! Conversion constructor
86  /*! Copy the given Eigen::Matrix into our internal vector*/
87  StateVector(Vector const & vector);
88 
89  //! Access the value of a single field
90  /*! \tparam Field The single field to access, e.g. Position2D::x
91  \returns A reference to the scalar field
92 
93 @code
94 NRT_STATE_FIELD_GROUP(Position2D,(x)(y));
95 NRT_STATE_FIELD_GROUP(Velocity2D,(x)(y));
96 nrt::StateVector<nrt::StateDefinition<Position2D, Velocity2D>> myvector;
97 
98 myvector.access<Velocity2D::x>() = 1.0;
99 @endcode
100  */
101  template<class Field>
102  typename std::enable_if
103  <
104  is_state_field<Field>(),
105  double &
106  >::type
107  access();
108 
109  //! Const field access
110  template<class Field>
111  typename std::enable_if
112  <
113  is_state_field<Field>(),
114  double
115  >::type
116  access() const;
117 
118  //! Access the sub-vector of a field group
119  /*! \tparam FieldGroup The field group to access, e.g. Position2D
120  \returns An Eigen::Block, which acts like a reference to the vector of the field group.
121 
122 @code
123 NRT_STATE_FIELD_GROUP(Position2D,(x)(y));
124 NRT_STATE_FIELD_GROUP(Velocity2D,(x)(y));
125 nrt::StateVector<nrt::StateDefinition<Position2D, Velocity2D>> myvector;
126 
127 myvector.access<Velocity2D>() = Eigen::Vector2d(1.0, 2.0);
128 @endcode
129  */
130  template<class FieldGroup>
131  typename std::enable_if
132  <
133  is_state_field_group<FieldGroup>(),
134  Eigen::Block<Vector, FieldGroup::num_fields, 1>
135  >::type
136  access();
137 
138  //! Const field group access
139  template<class FieldGroup>
140  typename std::enable_if
141  <
142  is_state_field_group<FieldGroup>(),
143  Eigen::Block<Vector const, FieldGroup::num_fields, 1>
144  >::type
145  access() const;
146 
147  //! Access the entire vector
148  /*! \tparam Void Leave this template parameter empty to specify that you want the entire state vector
149  \returns A reference to the entire state vector
150 
151 @code
152 NRT_STATE_FIELD_GROUP(Position2D,(x)(y));
153 NRT_STATE_FIELD_GROUP(Velocity2D,(x)(y));
154 nrt::StateVector<nrt::StateDefinition<Position2D, Velocity2D>> myvector;
155 
156 myvector.access<>() = Eigen::Vector4d(1.0, 2.0, 3.0, 4.0);
157 @endcode
158  */
159  template<typename Void = void>
160  typename std::enable_if
161  <
162  std::is_void<Void>::value,
163  Vector &
164  >::type
165  access();
166 
167  //! Const access to the entire vector
168  template<typename Void = void>
169  typename std::enable_if
170  <
171  std::is_void<Void>::value,
172  Vector
173  >::type
174  access() const;
175  };
176 }
178 #endif // INCLUDE_NRT_PROBABILISTIC_TYPES_STATEVECTOR_H
179