Drake
state_subvector.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <Eigen/Dense>
4 
5 #include <cstdint>
6 #include <stdexcept>
7 
10 
11 namespace drake {
12 namespace systems {
13 
18 template <typename T>
19 class StateSubvector : public StateVector<T> {
20  public:
25  StateSubvector(StateVector<T>* vector, int first_element,
26  int num_elements)
27  : vector_(vector),
28  first_element_(first_element),
29  num_elements_(num_elements) {
30  if (vector_ == nullptr) {
31  throw std::logic_error(
32  "Cannot create StateSubvector of a nullptr vector.");
33  }
34  if (first_element_ + num_elements_ > vector_->size()) {
35  throw std::out_of_range("StateSubvector out of bounds.");
36  }
37  }
38 
42  explicit StateSubvector(StateVector<T>* vector)
43  : StateSubvector(vector, 0, 0) {}
44 
45  int size() const override { return num_elements_; }
46 
47  const T GetAtIndex(int index) const override {
48  if (index >= size()) {
49  throw std::out_of_range("Index " + std::to_string(index) +
50  " out of bounds for state subvector of size " +
51  std::to_string(size()));
52  }
53  return vector_->GetAtIndex(first_element_ + index);
54  }
55 
56  void SetAtIndex(int index, const T& value) override {
57  if (index >= size()) {
58  throw std::out_of_range("Index " + std::to_string(index) +
59  " out of bounds for state subvector of size " +
60  std::to_string(size()));
61  }
62  vector_->SetAtIndex(first_element_ + index, value);
63  }
64 
65  void SetFromVector(const Eigen::Ref<const VectorX<T>>& value) override {
66  for (int i = 0; i < value.rows(); ++i) {
67  SetAtIndex(i, value[i]);
68  }
69  }
70 
71  VectorX<T> CopyToVector() const override {
72  VectorX<T> vec(size());
73  for (int i = 0; i < size(); ++i) {
74  vec[i] = GetAtIndex(i);
75  }
76  return vec;
77  }
78 
79  private:
80  // StateSubvector objects are neither copyable nor moveable.
81  StateSubvector(const StateSubvector& other) = delete;
82  StateSubvector& operator=(const StateSubvector& other) = delete;
83  StateSubvector(StateSubvector&& other) = delete;
84  StateSubvector& operator=(StateSubvector&& other) = delete;
85 
86  StateVector<T>* vector_{nullptr};
87  int first_element_{0};
88  int num_elements_{0};
89 };
90 
91 } // namespace systems
92 } // namespace drake
void SetAtIndex(int index, const T &value) override
Replaces the state at the given index with the value.
Definition: state_subvector.h:56
Definition: constants.h:3
int size() const override
Returns the number of elements in the vector.
Definition: state_subvector.h:45
StateVector is an abstract base class template for vector quantities within the state of a System...
Definition: state_vector.h:20
const T GetAtIndex(int index) const override
Returns the element at the given index in the vector.
Definition: state_subvector.h:47
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > VectorX
A column vector of any size, templated on scalar type.
Definition: eigen_types.h:36
StateSubvector(StateVector< T > *vector, int first_element, int num_elements)
Constructs a subvector of vector that consists of num_elements starting at first_element.
Definition: state_subvector.h:25
StateSubvector(StateVector< T > *vector)
Constructs an empty subvector.
Definition: state_subvector.h:42
StateSubvector is a concrete class template that implements StateVector by providing a sliced view of...
Definition: state_subvector.h:19
void SetFromVector(const Eigen::Ref< const VectorX< T >> &value) override
Replaces the entire state with the contents of value.
Definition: state_subvector.h:65
std::string to_string(const Eigen::MatrixBase< Derived > &a)
Definition: testUtil.h:29
VectorX< T > CopyToVector() const override
Copies the entire state to a vector with no semantics.
Definition: state_subvector.h:71