Drake
state_vector.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstdint>
4 #include <stdexcept>
5 
6 #include <Eigen/Dense>
7 
10 
11 namespace drake {
12 namespace systems {
13 
19 template <typename T>
20 class StateVector {
21  public:
22  virtual ~StateVector() {}
23 
28  virtual int size() const = 0;
29 
35  virtual const T GetAtIndex(int index) const = 0;
36 
42  virtual void SetAtIndex(int index, const T& value) = 0;
43 
49  virtual void SetFromVector(const Eigen::Ref<const VectorX<T>>& value) = 0;
50 
55  virtual VectorX<T> CopyToVector() const = 0;
56 
64  virtual void ScaleAndAddToVector(const T& scale,
65  Eigen::Ref<VectorX<T>> vec) const {
66  if (vec.rows() != size()) {
67  throw std::out_of_range("Addends must be the same length.");
68  }
69  for (int i = 0; i < size(); ++i)
70  vec[i] += scale * GetAtIndex(i);
71  }
72 
80  virtual StateVector& PlusEqScaled(const T& scale, const StateVector<T>& rhs) {
81  if (rhs.size() != size()) {
82  throw std::out_of_range("Addends must be the same length.");
83  }
84  for (int i = 0; i < size(); ++i)
85  SetAtIndex(i, GetAtIndex(i) + scale * rhs.GetAtIndex(i));
86  return *this;
87  }
88 
91  return PlusEqScaled(T(1), rhs);
92  }
93 
96  return PlusEqScaled(T(-1), rhs);
97  }
98 
99  protected:
101 
102  private:
103  // StateVector objects are neither copyable nor moveable.
104  StateVector(const StateVector& other) = delete;
105  StateVector& operator=(const StateVector& other) = delete;
106  StateVector(StateVector&& other) = delete;
107  StateVector& operator=(StateVector&& other) = delete;
108 };
109 
110 } // namespace systems
111 } // namespace drake
virtual int size() const =0
Returns the number of elements in the vector.
Definition: constants.h:3
StateVector is an abstract base class template for vector quantities within the state of a System...
Definition: state_vector.h:20
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > VectorX
A column vector of any size, templated on scalar type.
Definition: eigen_types.h:36
StateVector()
Definition: state_vector.h:100
virtual void SetAtIndex(int index, const T &value)=0
Replaces the state at the given index with the value.
Provides Drake&#39;s assertion implementation.
StateVector & operator-=(const StateVector< T > &rhs)
Subtract in state vector rhs to this state vector.
Definition: state_vector.h:95
virtual const T GetAtIndex(int index) const =0
Returns the element at the given index in the vector.
virtual StateVector & PlusEqScaled(const T &scale, const StateVector< T > &rhs)
Add in scaled state vector rhs to this state vector.
Definition: state_vector.h:80
virtual void ScaleAndAddToVector(const T &scale, Eigen::Ref< VectorX< T >> vec) const
Adds a scaled version of this state vector to Eigen vector vec, which must be the same size...
Definition: state_vector.h:64
virtual VectorX< T > CopyToVector() const =0
Copies the entire state to a vector with no semantics.
virtual void SetFromVector(const Eigen::Ref< const VectorX< T >> &value)=0
Replaces the entire state with the contents of value.
StateVector & operator+=(const StateVector< T > &rhs)
Add in state vector rhs to this state vector.
Definition: state_vector.h:90
virtual ~StateVector()
Definition: state_vector.h:22