-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
leaf_context.cc
113 lines (94 loc) · 3.64 KB
/
leaf_context.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "drake/systems/framework/leaf_context.h"
#include <utility>
#include "drake/systems/framework/basic_vector.h"
#include "drake/systems/framework/state.h"
namespace drake {
namespace systems {
template <typename T>
LeafContext<T>::LeafContext()
: state_(std::make_unique<State<T>>()) {}
template <typename T>
LeafContext<T>::~LeafContext() {}
template <typename T>
LeafContext<T>::LeafContext(const LeafContext& source) : Context<T>(source) {
// Make a deep copy of the state.
state_ = source.CloneState();
// Everything else was handled by the Context<T> copy constructor.
}
template <typename T>
std::unique_ptr<ContextBase> LeafContext<T>::DoCloneWithoutPointers() const {
return std::unique_ptr<ContextBase>(new LeafContext<T>(*this));
}
template <typename T>
std::unique_ptr<State<T>> LeafContext<T>::DoCloneState() const {
auto clone = std::make_unique<State<T>>();
// Make a deep copy of the continuous state using BasicVector::Clone().
const ContinuousState<T>& xc = this->get_continuous_state();
const int num_q = xc.get_generalized_position().size();
const int num_v = xc.get_generalized_velocity().size();
const int num_z = xc.get_misc_continuous_state().size();
const BasicVector<T>& xc_vector =
dynamic_cast<const BasicVector<T>&>(xc.get_vector());
clone->set_continuous_state(std::make_unique<ContinuousState<T>>(
xc_vector.Clone(), num_q, num_v, num_z));
// Make deep copies of the discrete and abstract states.
clone->set_discrete_state(state_->get_discrete_state().Clone());
clone->set_abstract_state(state_->get_abstract_state().Clone());
return clone;
}
template <typename T>
std::string LeafContext<T>::do_to_string() const {
std::ostringstream os;
os << this->GetSystemPathname() << " Context\n";
os << std::string(this->GetSystemPathname().size() + 9, '-') << "\n";
os << "Time: " << this->get_time() << "\n";
if (this->num_continuous_states() ||
this->num_discrete_state_groups() ||
this->num_abstract_states()) {
os << "States:\n";
if (this->num_continuous_states()) {
os << " " << this->num_continuous_states()
<< " continuous states\n";
os << " " << this->get_continuous_state_vector() << "\n";
}
if (this->num_discrete_state_groups()) {
os << " " << this->num_discrete_state_groups()
<< " discrete state groups with\n";
for (int i = 0; i < this->num_discrete_state_groups(); i++) {
os << " " << this->get_discrete_state(i).size() << " states\n";
os << " " << this->get_discrete_state(i) << "\n";
}
}
if (this->num_abstract_states()) {
os << " " << this->num_abstract_states() << " abstract states\n";
}
os << "\n";
}
if (this->num_numeric_parameter_groups() ||
this->num_abstract_parameters()) {
os << "Parameters:\n";
if (this->num_numeric_parameter_groups()) {
os << " " << this->num_numeric_parameter_groups()
<< " numeric parameter groups";
os << " with\n";
for (int i = 0; i < this->num_numeric_parameter_groups(); i++) {
os << " " << this->get_numeric_parameter(i).size()
<< " parameters\n";
os << " " << this->get_numeric_parameter(i) << "\n";
}
}
if (this->num_abstract_parameters()) {
os << " " << this->num_abstract_parameters()
<< " abstract parameters\n";
}
}
return os.str();
}
template <typename T>
void LeafContext<T>::notify_set_system_id(internal::SystemId id) {
state_->set_system_id(id);
}
} // namespace systems
} // namespace drake
DRAKE_DEFINE_CLASS_TEMPLATE_INSTANTIATIONS_ON_DEFAULT_SCALARS(
class ::drake::systems::LeafContext)