forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrigid_body.cc
282 lines (235 loc) · 8.32 KB
/
rigid_body.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include "drake/multibody/rigid_body.h"
#include <stdexcept>
#include "drake/common/autodiff.h"
#include "drake/common/text_logging.h"
#include "drake/util/drakeGeometryUtil.h"
using Eigen::Isometry3d;
using Eigen::Matrix;
using Eigen::Vector3d;
using std::make_unique;
using std::move;
using std::ostream;
using std::runtime_error;
using std::string;
using std::stringstream;
using std::vector;
template <typename T>
RigidBody<T>::RigidBody() {
center_of_mass_ = Vector3d::Zero();
spatial_inertia_ << drake::SquareTwistMatrix<double>::Zero();
}
template <typename T>
std::unique_ptr<RigidBody<T>> RigidBody<T>::Clone() const {
auto body = make_unique<RigidBody<T>>();
body->set_name(get_name());
body->set_model_name(get_model_name());
body->set_model_instance_id(get_model_instance_id());
body->set_body_index(body_index_);
body->set_position_start_index(position_start_index_);
body->set_velocity_start_index(velocity_start_index_);
body->set_contact_points(contact_points_);
body->set_mass(mass_);
body->set_center_of_mass(center_of_mass_);
body->set_spatial_inertia(spatial_inertia_);
return move(body);
}
template <typename T>
const std::string& RigidBody<T>::get_name() const { return name_; }
template <typename T>
void RigidBody<T>::set_name(const std::string& name) { name_ = name; }
template <typename T>
const std::string& RigidBody<T>::get_model_name() const { return model_name_; }
template <typename T>
void RigidBody<T>::set_model_name(const std::string& name) {
model_name_ = name;
}
template <typename T>
int RigidBody<T>::get_model_instance_id() const { return model_instance_id_; }
template <typename T>
void RigidBody<T>::set_model_instance_id(int model_instance_id) {
model_instance_id_ = model_instance_id;
}
template <typename T>
void RigidBody<T>::setJoint(std::unique_ptr<DrakeJoint> joint) {
joint_ = move(joint);
}
template <typename T>
void RigidBody<T>::set_parent(RigidBody* parent) { parent_ = parent; }
// TODO(liang.fok): Remove this deprecated method prior to Release 1.0.
template <typename T>
bool RigidBody<T>::hasParent() const { return has_parent_body(); }
template <typename T>
void RigidBody<T>::set_body_index(int body_index) { body_index_ = body_index; }
template <typename T>
void RigidBody<T>::set_position_start_index(int position_start_index) {
position_start_index_ = position_start_index;
}
template <typename T>
void RigidBody<T>::set_velocity_start_index(int velocity_start_index) {
velocity_start_index_ = velocity_start_index;
}
template <typename T>
void RigidBody<T>::AddVisualElement(const DrakeShapes::VisualElement& element) {
visual_elements_.push_back(element);
}
template <typename T>
const DrakeShapes::VectorOfVisualElements& RigidBody<T>::get_visual_elements()
const {
return visual_elements_;
}
template <typename T>
void RigidBody<T>::AddCollisionElement(
const std::string& group_name,
drake::multibody::collision::Element* element) {
drake::multibody::collision::ElementId id = element->getId();
collision_element_ids_.push_back(id);
collision_element_groups_[group_name].push_back(id);
collision_elements_.push_back(element);
}
template <typename T>
std::vector<drake::multibody::collision::ElementId>&
RigidBody<T>::get_mutable_collision_element_ids() {
return collision_element_ids_;
}
template <typename T>
const std::map<std::string,
std::vector<drake::multibody::collision::ElementId>>&
RigidBody<T>::get_group_to_collision_ids_map() const {
return collision_element_groups_;
}
template <typename T>
std::map<std::string, std::vector<drake::multibody::collision::ElementId>>&
RigidBody<T>::get_mutable_group_to_collision_ids_map() {
return collision_element_groups_;
}
template <typename T>
bool RigidBody<T>::IsRigidlyFixedToWorld() const {
if (parent_ == nullptr) {
// We assume that the world frame is the root of the tree, and, as such, the
// first body in the vector of bodies. The body_index_ member is defined
// to be that position in the vector.
if (body_index_ != 0) {
throw std::runtime_error(
"Found a rigid body without a parent that is "
"not the world frame: " +
name_);
}
return true;
}
if (joint_ == nullptr) {
throw std::runtime_error("Found a rigid body without a parent joint: " +
name_);
}
if (joint_->is_fixed()) return parent_->IsRigidlyFixedToWorld();
return false;
}
template <typename T>
Isometry3d RigidBody<T>::ComputeWorldFixedPose() const {
if (parent_ == nullptr) {
return Isometry3d::Identity();
}
// RigidBodyTree::compile should enforce this property.
DRAKE_ASSERT(joint_ != nullptr);
if (!joint_->is_fixed()) {
throw std::runtime_error(
"Trying to compute world pose for a body with a "
"non-fixed parent joint: " +
name_);
}
return parent_->ComputeWorldFixedPose() *
joint_->get_transform_to_parent_body();
}
template <typename T>
bool RigidBody<T>::adjacentTo(const RigidBody& other) const {
return ((has_as_parent(other) && !(joint_ && joint_->is_floating())) ||
(other.has_as_parent(*this) &&
!(other.joint_ && other.joint_->is_floating())));
}
template <typename T>
bool RigidBody<T>::CanCollideWith(const RigidBody& other) const {
bool ignored = this == &other || adjacentTo(other);
return !ignored;
}
template <typename T>
bool RigidBody<T>::appendCollisionElementIdsFromThisBody(
const string& group_name,
// TODO(#2274) Fix NOLINTNEXTLINE(runtime/references).
vector<drake::multibody::collision::ElementId>& ids) const {
auto group_ids_iter = collision_element_groups_.find(group_name);
if (group_ids_iter != collision_element_groups_.end()) {
ids.reserve(ids.size() + distance(group_ids_iter->second.begin(),
group_ids_iter->second.end()));
ids.insert(ids.end(), group_ids_iter->second.begin(),
group_ids_iter->second.end());
return true;
} else {
return false;
}
}
template <typename T>
bool RigidBody<T>::appendCollisionElementIdsFromThisBody(
// TODO(#2274) Fix NOLINTNEXTLINE(runtime/references).
vector<drake::multibody::collision::ElementId>& ids) const {
ids.reserve(ids.size() + collision_element_ids_.size());
ids.insert(ids.end(), collision_element_ids_.begin(),
collision_element_ids_.end());
return true;
}
template <typename T>
void RigidBody<T>::ApplyTransformToJointFrame(
const Eigen::Isometry3d& transform_body_to_joint) {
spatial_inertia_ = transformSpatialInertia(transform_body_to_joint,
spatial_inertia_);
for (auto& v : visual_elements_) {
v.SetLocalTransform(transform_body_to_joint * v.getLocalTransform());
}
}
template <typename T>
const Eigen::Matrix3Xd& RigidBody<T>::get_contact_points() const {
return contact_points_;
}
template <typename T>
void RigidBody<T>::set_contact_points(const Eigen::Matrix3Xd& contact_points) {
contact_points_ = contact_points;
}
template <typename T>
void RigidBody<T>::set_mass(double mass) {
mass_ = mass;
}
template <typename T>
double RigidBody<T>::get_mass() const {
return mass_;
}
template <typename T>
void RigidBody<T>::set_center_of_mass(const Eigen::Vector3d& center_of_mass) {
center_of_mass_ = center_of_mass;
}
template <typename T>
const Eigen::Vector3d& RigidBody<T>::get_center_of_mass() const {
return center_of_mass_;
}
template <typename T>
void RigidBody<T>::set_spatial_inertia(const drake::SquareTwistMatrix<double>&
spatial_inertia) {
spatial_inertia_ = spatial_inertia;
}
ostream& operator<<(ostream& out, const RigidBody<double>& b) {
std::string parent_joint_name =
b.has_parent_body() ? b.getJoint().get_name() : "no parent joint";
std::stringstream collision_element_str;
collision_element_str << "[";
for (size_t ii = 0; ii < b.get_collision_element_ids().size(); ii++) {
collision_element_str << b.get_collision_element_ids()[ii];
if (ii < b.get_collision_element_ids().size() - 1)
collision_element_str << ", ";
}
collision_element_str << "]";
out << "RigidBody\n"
<< " - link name: " << b.name_ << "\n"
<< " - parent joint: " << parent_joint_name << "\n"
<< " - Collision elements IDs: " << collision_element_str.str();
return out;
}
// Explicitly instantiates on the most common scalar types.
template class RigidBody<double>;
template class RigidBody<drake::AutoDiffXd>;