Drake
basic_state_vector.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstdint>
4 #include <memory>
5 #include <stdexcept>
6 
10 #include "leaf_state_vector.h"
11 
12 namespace drake {
13 namespace systems {
14 
24 template <typename T>
25 class BasicStateVector : public LeafStateVector<T> {
26  public:
29  explicit BasicStateVector(int size)
31  std::unique_ptr<VectorInterface<T>>(new BasicVector<T>(size))) {}
32 
35  explicit BasicStateVector(std::unique_ptr<VectorInterface<T>> vector)
36  : vector_(std::move(vector)) {}
37 
38  int size() const override {
39  return static_cast<int>(vector_->get_value().rows());
40  }
41 
42  const T GetAtIndex(int index) const override {
43  if (index >= size()) {
44  throw std::out_of_range("Index " + std::to_string(index) +
45  " out of bounds for state vector of size " +
46  std::to_string(size()));
47  }
48  return vector_->get_value()[index];
49  }
50 
51  void SetAtIndex(int index, const T& value) override {
52  if (index >= size()) {
53  throw std::out_of_range("Index " + std::to_string(index) +
54  " out of bounds for state vector of size " +
55  std::to_string(size()));
56  }
57  vector_->get_mutable_value()[index] = value;
58  }
59 
60  void SetFromVector(const Eigen::Ref<const VectorX<T>>& value) override {
61  vector_->set_value(value);
62  }
63 
64  VectorX<T> CopyToVector() const override { return vector_->get_value(); }
65 
66  void ScaleAndAddToVector(const T& scale,
67  Eigen::Ref<VectorX<T>> vec) const override {
68  if (vec.rows() != size()) {
69  throw std::out_of_range("Addends must be the same length.");
70  }
71  vec += scale * vector_->get_value();
72  }
73 
74  BasicStateVector& PlusEqScaled(const T& scale,
75  const StateVector<T>& rhs) override {
76  rhs.ScaleAndAddToVector(scale, vector_->get_mutable_value());
77  return *this;
78  }
79 
80  protected:
82  : BasicStateVector(other.size()) {
83  SetFromVector(other.vector_->get_value());
84  }
85 
86  private:
87  BasicStateVector<T>* DoClone() const override {
88  return new BasicStateVector<T>(*this);
89  }
90 
91  // Assignment of BasicStateVectors could change size, so we forbid it.
92  BasicStateVector& operator=(const BasicStateVector& other) = delete;
93 
94  // BasicStateVector objects are not moveable.
95  BasicStateVector(BasicStateVector&& other) = delete;
96  BasicStateVector& operator=(BasicStateVector&& other) = delete;
97 
98  std::unique_ptr<VectorInterface<T>> vector_;
99 };
100 
101 } // namespace systems
102 } // namespace drake
VectorInterface is a pure abstract interface that real-valued signals between Systems must satisfy...
Definition: vector_interface.h:25
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
STL namespace.
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > VectorX
A column vector of any size, templated on scalar type.
Definition: eigen_types.h:36
VectorX< T > CopyToVector() const override
Copies the entire state to a vector with no semantics.
Definition: basic_state_vector.h:64
void ScaleAndAddToVector(const T &scale, Eigen::Ref< VectorX< T >> vec) const override
Adds a scaled version of this state vector to Eigen vector vec, which must be the same size...
Definition: basic_state_vector.h:66
BasicStateVector(std::unique_ptr< VectorInterface< T >> vector)
Constructs a BasicStateVector that owns an arbitrary vector, which must not be nullptr.
Definition: basic_state_vector.h:35
std::string to_string(const Eigen::MatrixBase< Derived > &a)
Definition: testUtil.h:29
BasicStateVector is a concrete class template that implements StateVector in a convenient manner for ...
Definition: basic_state_vector.h:25
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
BasicVector is a semantics-free wrapper around an Eigen vector that satisfies VectorInterface.
Definition: basic_vector.h:23
BasicStateVector & PlusEqScaled(const T &scale, const StateVector< T > &rhs) override
Add in scaled state vector rhs to this state vector.
Definition: basic_state_vector.h:74
LeafStateVector is an abstract class template that implements StateVector for leaf Systems...
Definition: leaf_state_vector.h:17
void SetAtIndex(int index, const T &value) override
Replaces the state at the given index with the value.
Definition: basic_state_vector.h:51
void SetFromVector(const Eigen::Ref< const VectorX< T >> &value) override
Replaces the entire state with the contents of value.
Definition: basic_state_vector.h:60
const T GetAtIndex(int index) const override
Returns the element at the given index in the vector.
Definition: basic_state_vector.h:42
int size() const override
Returns the number of elements in the vector.
Definition: basic_state_vector.h:38
BasicStateVector(int size)
Constructs a BasicStateVector that owns a generic BasicVector of the specified size.
Definition: basic_state_vector.h:29
BasicStateVector(const BasicStateVector &other)
Definition: basic_state_vector.h:81