13
13
#include " drake/common/constants.h"
14
14
#include " drake/common/eigen_autodiff_types.h"
15
15
#include " drake/common/eigen_types.h"
16
+ #include " drake/common/text_logging.h"
16
17
#include " drake/math/autodiff.h"
17
18
#include " drake/math/autodiff_gradient.h"
18
19
#include " drake/math/gradient.h"
@@ -124,6 +125,91 @@ RigidBodyTree<T>::RigidBodyTree()
124
125
template <typename T>
125
126
RigidBodyTree<T>::~RigidBodyTree () {}
126
127
128
+ // For an explanation of why these SWIG preprocessor commands are needed, see
129
+ // the comment immediately above the declaration of RigidBodyTree::Clone() in
130
+ // rigid_body_tree.h.
131
+ #ifndef SWIG
132
+ template <>
133
+ unique_ptr<RigidBodyTree<double >> RigidBodyTree<double >::Clone() const {
134
+ auto clone = make_unique<RigidBodyTree<double >>();
135
+ // The following is necessary to remove the world link from the clone. The
136
+ // world link will be re-added when the bodies are cloned below.
137
+ clone->bodies .clear ();
138
+
139
+ clone->joint_limit_min = this ->joint_limit_min ;
140
+ clone->joint_limit_max = this ->joint_limit_max ;
141
+ clone->a_grav = this ->a_grav ;
142
+ clone->B = this ->B ;
143
+ clone->num_positions_ = this ->num_positions_ ;
144
+ clone->num_velocities_ = this ->num_velocities_ ;
145
+ clone->num_model_instances_ = this ->num_model_instances_ ;
146
+ clone->initialized_ = this ->initialized_ ;
147
+
148
+ // Clones the rigid bodies.
149
+ for (const auto & body : bodies) {
150
+ clone->bodies .push_back (body->Clone ());
151
+ }
152
+
153
+ // Clones the joints and adds them to the cloned RigidBody objects.
154
+ for (const auto & original_body : bodies) {
155
+ const int body_index = original_body->get_body_index ();
156
+ if (body_index == RigidBodyTreeConstants::kWorldBodyIndex ) {
157
+ continue ;
158
+ }
159
+
160
+ RigidBody<double >* cloned_body = clone->get_mutable_body (body_index);
161
+ DRAKE_DEMAND (cloned_body != nullptr );
162
+ DRAKE_DEMAND (cloned_body->get_body_index () == body_index);
163
+
164
+ const RigidBody<double >* original_body_parent = original_body->get_parent ();
165
+ DRAKE_DEMAND (original_body_parent != nullptr );
166
+
167
+ const int parent_body_index = original_body_parent->get_body_index ();
168
+
169
+ RigidBody<double >* cloned_body_parent =
170
+ clone->get_mutable_body (parent_body_index);
171
+ DRAKE_DEMAND (cloned_body_parent != nullptr );
172
+
173
+ cloned_body->add_joint (cloned_body_parent,
174
+ original_body->getJoint ().Clone ());
175
+ }
176
+
177
+ for (const auto & original_frame : frames) {
178
+ const RigidBody<double >& original_frame_body =
179
+ original_frame->get_rigid_body ();
180
+ const int cloned_frame_body_index =
181
+ clone->FindBodyIndex (original_frame_body.get_name (),
182
+ original_frame_body.get_model_instance_id ());
183
+ RigidBody<double >* cloned_frame_body =
184
+ clone->get_mutable_body (cloned_frame_body_index);
185
+ DRAKE_DEMAND (cloned_frame_body != nullptr );
186
+ std::shared_ptr<RigidBodyFrame<double >> cloned_frame =
187
+ original_frame->Clone (cloned_frame_body);
188
+ clone->frames .push_back (cloned_frame);
189
+ }
190
+
191
+ for (const auto & actuator : actuators) {
192
+ const RigidBody<double >& cloned_body =
193
+ clone->get_body (actuator.body_ ->get_body_index ());
194
+ clone->actuators .emplace_back (
195
+ actuator.name_ , &cloned_body, actuator.reduction_ ,
196
+ actuator.effort_limit_min_ , actuator.effort_limit_max_ );
197
+ }
198
+
199
+ for (const auto & loop : loops) {
200
+ std::shared_ptr<RigidBodyFrame<double >> frame_a =
201
+ clone->findFrame (loop.frameA_ ->get_name (),
202
+ loop.frameA_ ->get_model_instance_id ());
203
+ std::shared_ptr<RigidBodyFrame<double >> frame_b =
204
+ clone->findFrame (loop.frameB_ ->get_name (),
205
+ loop.frameB_ ->get_model_instance_id ());
206
+ clone->loops .emplace_back (frame_a, frame_b, loop.axis_ );
207
+ }
208
+
209
+ return clone;
210
+ }
211
+ #endif
212
+
127
213
template <typename T>
128
214
bool RigidBodyTree<T>::transformCollisionFrame(
129
215
RigidBody<T>* body, const Eigen::Isometry3d& displace_transform) {
@@ -2760,11 +2846,16 @@ int RigidBodyTree<T>::FindIndexOfChildBodyOfJoint(const std::string& joint_name,
2760
2846
2761
2847
template <typename T>
2762
2848
const RigidBody<T>& RigidBodyTree<T>::get_body(int body_index) const {
2763
- DRAKE_DEMAND (body_index >= 0 &&
2764
- body_index < get_num_bodies ());
2849
+ DRAKE_DEMAND (body_index >= 0 && body_index < get_num_bodies ());
2765
2850
return *bodies[body_index].get ();
2766
2851
}
2767
2852
2853
+ template <typename T>
2854
+ RigidBody<T>* RigidBodyTree<T>::get_mutable_body(int body_index) {
2855
+ DRAKE_DEMAND (body_index >= 0 && body_index < get_num_bodies ());
2856
+ return bodies[body_index].get ();
2857
+ }
2858
+
2768
2859
template <typename T>
2769
2860
int RigidBodyTree<T>::get_num_bodies() const {
2770
2861
return static_cast <int >(bodies.size ());
@@ -2776,6 +2867,11 @@ int RigidBodyTree<T>::get_number_of_bodies() const {
2776
2867
return get_num_bodies ();
2777
2868
}
2778
2869
2870
+ template <typename T>
2871
+ int RigidBodyTree<T>::get_num_frames() const {
2872
+ return static_cast <int >(frames.size ());
2873
+ }
2874
+
2779
2875
// TODO(liang.fok) Remove this method prior to Release 1.0.
2780
2876
template <typename T>
2781
2877
RigidBody<T>* RigidBodyTree<T>::findJoint(const std::string& joint_name,
0 commit comments