Drake
basic_vector.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstdint>
4 #include <limits>
5 #include <stdexcept>
6 #include <vector>
7 
9 
10 #include <Eigen/Dense>
11 
12 namespace drake {
13 namespace systems {
14 
22 template <typename T>
23 class BasicVector : public VectorInterface<T> {
24  public:
25  explicit BasicVector(int size)
26  : values_(VectorX<T>::Constant(
27  size, std::numeric_limits<
28  typename Eigen::NumTraits<T>::Real>::quiet_NaN())) {}
29 
30  void set_value(const Eigen::Ref<const VectorX<T>>& value) override {
31  if (value.rows() != values_.rows()) {
32  throw std::out_of_range(
33  "Cannot set a BasicVector of size " + std::to_string(values_.rows()) +
34  " with a value of size " + std::to_string(value.rows()));
35  }
36  values_ = value;
37  }
38 
39  int size() const override { return static_cast<int>(values_.rows()); }
40 
41  Eigen::VectorBlock<const VectorX<T>> get_value() const override {
42  return values_.head(values_.rows());
43  }
44 
45  Eigen::VectorBlock<VectorX<T>> get_mutable_value() override {
46  return values_.head(values_.rows());
47  }
48 
53  std::unique_ptr<VectorInterface<T>> Clone() const final {
54  return std::unique_ptr<VectorInterface<T>>(DoClone());
55  }
56 
57  private:
58  // Returns a new BasicStateVector containing a copy of the entire state.
59  // Subclasses of BasicVector must override DoClone to return their covariant
60  // type.
61  virtual BasicVector<T>* DoClone() const {
62  BasicVector* clone = new BasicVector(size());
63  clone->values_ = values_;
64  return clone;
65  }
66 
67  // The column vector of T values.
68  VectorX<T> values_;
69 };
70 
71 } // namespace systems
72 } // namespace drake
VectorInterface is a pure abstract interface that real-valued signals between Systems must satisfy...
Definition: vector_interface.h:25
void set_value(const Eigen::Ref< const VectorX< T >> &value) override
Sets the vector to the given value.
Definition: basic_vector.h:30
Definition: constants.h:3
Eigen::VectorBlock< VectorX< T > > get_mutable_value() override
Returns a reference that allows mutation of the values in this vector, but does not allow resizing th...
Definition: basic_vector.h:45
STL namespace.
BasicVector(int size)
Definition: basic_vector.h:25
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > VectorX
A column vector of any size, templated on scalar type.
Definition: eigen_types.h:36
std::unique_ptr< VectorInterface< T > > Clone() const final
Copies the entire state to a new BasicVector.
Definition: basic_vector.h:53
std::string to_string(const Eigen::MatrixBase< Derived > &a)
Definition: testUtil.h:29
BasicVector is a semantics-free wrapper around an Eigen vector that satisfies VectorInterface.
Definition: basic_vector.h:23
Eigen::VectorBlock< const VectorX< T > > get_value() const override
Returns a column vector containing the entire value of the signal.
Definition: basic_vector.h:41
int size() const override
Returns the size of the vector, which must be equal to the number of rows in get_value().
Definition: basic_vector.h:39