-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
multibody_position_to_geometry_pose.cc
91 lines (79 loc) · 3.38 KB
/
multibody_position_to_geometry_pose.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
#include "drake/systems/rendering/multibody_position_to_geometry_pose.h"
#include <utility>
#include <vector>
#include "drake/common/drake_assert.h"
#include "drake/common/pointer_cast.h"
namespace drake {
namespace systems {
namespace rendering {
template <typename T>
MultibodyPositionToGeometryPose<T>::MultibodyPositionToGeometryPose(
const multibody::MultibodyPlant<T>& plant, bool input_multibody_state)
: plant_(plant) {
Configure(input_multibody_state);
}
// Note: This constructor is not *obviously* correct. Compare it with this code:
// unique_ptr<Foo> f;
// bar(*f, move(f));
// The invocation to bar may not be valid because the compiler can chose to
// perform the move of f before the dereference (which would make the first
// parameter a reference to null). However, this constructor works because:
// "... non-static data members are initialized in order of declaration in the
// class definition."
// https://en.cppreference.com/w/cpp/language/initializer_list#Initialization_order
template <typename T>
MultibodyPositionToGeometryPose<T>::MultibodyPositionToGeometryPose(
std::unique_ptr<multibody::MultibodyPlant<T>> owned_plant,
bool input_multibody_state)
: plant_(*owned_plant), owned_plant_(std::move(owned_plant)) {
DRAKE_DEMAND(owned_plant_ != nullptr);
Configure(input_multibody_state);
}
template <typename T>
void MultibodyPositionToGeometryPose<T>::Configure(
bool input_multibody_state) {
// Either we don't own the plant, or we own the plant we're storing the
// reference for.
DRAKE_DEMAND(owned_plant_ == nullptr || owned_plant_.get() == &plant_);
if (!plant_.is_finalized()) {
throw std::logic_error(
"MultibodyPositionToGeometryPose requires a MultibodyPlant that has "
"been finalized");
}
if (!plant_.geometry_source_is_registered()) {
throw std::logic_error(
"MultibodyPositionToGeometryPose requires a MultibodyPlant that has "
"been registered with a SceneGraph");
}
plant_context_ = plant_.CreateDefaultContext();
this->DeclareInputPort("position", kVectorValued,
input_multibody_state ? plant_.num_multibody_states()
: plant_.num_positions());
this->DeclareAbstractOutputPort(
"geometry_pose",
[this]() {
return this->plant_.get_geometry_poses_output_port().Allocate();
},
[this](const Context<T>& context, AbstractValue* output) {
return this->CalcGeometryPose(context, output);
});
// Fix all input ports in the Context to avoid leaving them unassigned.
// They should not impact the output values.
plant_.AllocateFixedInputs(plant_context_.get());
}
template <typename T>
void MultibodyPositionToGeometryPose<T>::CalcGeometryPose(
const Context<T>& context, AbstractValue* output) const {
// Set the positions in the owned (mutable) context so that we can ask the
// MultibodyPlant to compute the outputs.
// TODO(eric.cousineau): Place `plant_context_` in the cache of `context`,
// and remove mutable member.
plant_.GetMutablePositions(plant_context_.get()) =
get_input_port().Eval(context).head(plant_.num_positions());
// Evaluate the plant's output port.
plant_.get_geometry_poses_output_port().Calc(*plant_context_, output);
}
template class MultibodyPositionToGeometryPose<double>;
} // namespace rendering
} // namespace systems
} // namespace drake