Skip to content

Commit 5d7a06a

Browse files
author
Chien-Liang Fok
committed
Adds ability to clone RigidBodyTree.
1 parent bfd5221 commit 5d7a06a

22 files changed

+382
-11
lines changed

drake/multibody/BUILD

+10
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,16 @@ cc_googletest(
176176
],
177177
)
178178

179+
cc_googletest(
180+
name = "rigid_body_tree_clone_test",
181+
srcs = ["test/rigid_body_tree/rigid_body_tree_clone_test.cc"],
182+
data = glob(["test/rigid_body_tree/*.urdf"]),
183+
deps = [
184+
":rigid_body_tree",
185+
"//drake/multibody/parsers",
186+
],
187+
)
188+
179189
cc_googletest(
180190
name = "rigid_body_tree_creation_test",
181191
srcs = ["test/rigid_body_tree/rigid_body_tree_creation_test.cc"],

drake/multibody/joints/drake_joint.h

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <memory>
34
#include <random>
45
#include <string>
56

@@ -101,6 +102,11 @@ class DrakeJoint {
101102
*/
102103
virtual ~DrakeJoint();
103104

105+
/**
106+
* Returns a clone of this DrakeJoint.
107+
*/
108+
virtual std::unique_ptr<DrakeJoint> Clone() const = 0;
109+
104110
/**
105111
* Returns the transform `X_PF` giving the pose of the joint's "fixed" frame
106112
* `F` in its parent body frame `P`. Frame `F` is the joint frame that is

drake/multibody/joints/fixed_joint.cc

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
#include "drake/multibody/joints/fixed_joint.h"
22

3+
#include <memory>
4+
#include <utility>
5+
6+
std::unique_ptr<DrakeJoint> FixedJoint::Clone() const {
7+
auto joint = std::make_unique<FixedJoint>(get_name(),
8+
get_transform_to_parent_body());
9+
return std::move(joint);
10+
}
11+
312
std::string FixedJoint::get_position_name(int index) const {
413
throw std::runtime_error("bad index");
514
}

drake/multibody/joints/fixed_joint.h

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <memory>
34
#include <string>
45

56
#include "drake/common/eigen_types.h"
@@ -15,6 +16,8 @@ class FixedJoint : public DrakeJointImpl<FixedJoint> {
1516

1617
virtual ~FixedJoint() {}
1718

19+
std::unique_ptr<DrakeJoint> Clone() const override;
20+
1821
template <typename DerivedQ>
1922
Eigen::Transform<typename DerivedQ::Scalar, 3, Eigen::Isometry>
2023
jointTransform(const Eigen::MatrixBase<DerivedQ>& q) const {

drake/multibody/joints/helical_joint.cc

+10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
#include "drake/multibody/joints/helical_joint.h"
22

3+
#include <memory>
4+
#include <utility>
5+
36
using Eigen::Vector3d;
47

8+
std::unique_ptr<DrakeJoint> HelicalJoint::Clone() const {
9+
auto joint = std::make_unique<HelicalJoint>(get_name(),
10+
get_transform_to_parent_body(),
11+
axis_, pitch_);
12+
return std::move(joint);
13+
}
14+
515
drake::TwistVector<double> HelicalJoint::spatialJointAxis(const Vector3d& axis,
616
double pitch) {
717
drake::TwistVector<double> ret;

drake/multibody/joints/helical_joint.h

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <memory>
34
#include <string>
45

56
#include "drake/common/eigen_types.h"
@@ -28,6 +29,8 @@ class HelicalJoint : public FixedAxisOneDoFJoint<HelicalJoint> {
2829

2930
virtual ~HelicalJoint() {}
3031

32+
std::unique_ptr<DrakeJoint> Clone() const override;
33+
3134
template <typename DerivedQ>
3235
Eigen::Transform<typename DerivedQ::Scalar, 3, Eigen::Isometry>
3336
jointTransform(const Eigen::MatrixBase<DerivedQ>& q) const {

drake/multibody/joints/prismatic_joint.cc

+10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
#include "drake/multibody/joints/prismatic_joint.h"
22

3+
#include <memory>
4+
#include <utility>
5+
36
using Eigen::Vector3d;
47

8+
std::unique_ptr<DrakeJoint> PrismaticJoint::Clone() const {
9+
auto joint = std::make_unique<PrismaticJoint>(get_name(),
10+
get_transform_to_parent_body(),
11+
translation_axis_);
12+
return std::move(joint);
13+
}
14+
515
drake::TwistVector<double> PrismaticJoint::spatialJointAxis(
616
const Vector3d& translation_axis) {
717
drake::TwistVector<double> ret;

drake/multibody/joints/prismatic_joint.h

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <memory>
34
#include <string>
45

56
#include "drake/common/drake_assert.h"
@@ -51,6 +52,8 @@ class PrismaticJoint : public FixedAxisOneDoFJoint<PrismaticJoint> {
5152

5253
virtual ~PrismaticJoint() {}
5354

55+
std::unique_ptr<DrakeJoint> Clone() const override;
56+
5457
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
5558

5659
private:

drake/multibody/joints/quaternion_floating_joint.cc

+8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
#include "drake/multibody/joints/quaternion_floating_joint.h"
22

3+
#include <memory>
34
#include <random>
5+
#include <utility>
46

57
#include "drake/math/random_rotation.h"
68

79
using Eigen::Vector4d;
810
using Eigen::VectorXd;
911
using std::normal_distribution;
1012

13+
std::unique_ptr<DrakeJoint> QuaternionFloatingJoint::Clone() const {
14+
auto joint = std::make_unique<QuaternionFloatingJoint>(get_name(),
15+
get_transform_to_parent_body());
16+
return std::move(joint);
17+
}
18+
1119
std::string QuaternionFloatingJoint::get_position_name(int index) const {
1220
switch (index) {
1321
case 0:

drake/multibody/joints/quaternion_floating_joint.h

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <memory>
34
#include <string>
45

56
#include "drake/common/constants.h"
@@ -80,6 +81,8 @@ class QuaternionFloatingJoint : public DrakeJointImpl<QuaternionFloatingJoint> {
8081

8182
virtual ~QuaternionFloatingJoint() {}
8283

84+
std::unique_ptr<DrakeJoint> Clone() const override;
85+
8386
/** Returns the transform `X_PB(q)` where P is the parent body and B the
8487
* child body connected by this joint.
8588
*/

drake/multibody/joints/revolute_joint.cc

+10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
#include "drake/multibody/joints/revolute_joint.h"
22

3+
#include <memory>
4+
#include <utility>
5+
36
using Eigen::Vector3d;
47

8+
std::unique_ptr<DrakeJoint> RevoluteJoint::Clone() const {
9+
auto joint = std::make_unique<RevoluteJoint>(get_name(),
10+
get_transform_to_parent_body(),
11+
rotation_axis);
12+
return std::move(joint);
13+
}
14+
515
drake::TwistVector<double> RevoluteJoint::spatialJointAxis(
616
const Vector3d& rotation_axis) {
717
drake::TwistVector<double> ret;

drake/multibody/joints/revolute_joint.h

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <memory>
34
#include <string>
45

56
#include <Eigen/Geometry>
@@ -11,13 +12,6 @@
1112
#pragma GCC diagnostic push
1213
#pragma GCC diagnostic ignored "-Woverloaded-virtual"
1314
class RevoluteJoint : public FixedAxisOneDoFJoint<RevoluteJoint> {
14-
// disable copy construction and assignment
15-
// RevoluteJoint(const RevoluteJoint&) = delete;
16-
// RevoluteJoint& operator=(const RevoluteJoint&) = delete;
17-
18-
private:
19-
Eigen::Vector3d rotation_axis;
20-
2115
public:
2216
RevoluteJoint(const std::string& name,
2317
const Eigen::Isometry3d& transform_to_parent_body,
@@ -31,6 +25,8 @@ class RevoluteJoint : public FixedAxisOneDoFJoint<RevoluteJoint> {
3125

3226
virtual ~RevoluteJoint() {}
3327

28+
std::unique_ptr<DrakeJoint> Clone() const override;
29+
3430
template <typename DerivedQ>
3531
Eigen::Transform<typename DerivedQ::Scalar, 3, Eigen::Isometry>
3632
jointTransform(const Eigen::MatrixBase<DerivedQ>& q) const {
@@ -47,5 +43,8 @@ class RevoluteJoint : public FixedAxisOneDoFJoint<RevoluteJoint> {
4743

4844
public:
4945
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
46+
47+
private:
48+
Eigen::Vector3d rotation_axis;
5049
};
5150
#pragma GCC diagnostic pop // pop -Wno-overloaded-virtual

drake/multibody/joints/roll_pitch_yaw_floating_joint.cc

+9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "drake/multibody/joints/roll_pitch_yaw_floating_joint.h"
22

3+
#include <memory>
34
#include <random>
5+
#include <utility>
46

57
#include <Eigen/Dense>
68

@@ -10,6 +12,13 @@ using Eigen::Map;
1012
using Eigen::Vector3d;
1113
using Eigen::VectorXd;
1214

15+
std::unique_ptr<DrakeJoint> RollPitchYawFloatingJoint::Clone() const {
16+
auto joint = std::make_unique<RollPitchYawFloatingJoint>(
17+
get_name(),
18+
get_transform_to_parent_body());
19+
return std::move(joint);
20+
}
21+
1322
std::string RollPitchYawFloatingJoint::get_position_name(int index) const {
1423
switch (index) {
1524
case 0:

drake/multibody/joints/roll_pitch_yaw_floating_joint.h

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <memory>
34
#include <string>
45

56
#include "drake/common/constants.h"
@@ -26,6 +27,8 @@ class RollPitchYawFloatingJoint
2627

2728
virtual ~RollPitchYawFloatingJoint() {}
2829

30+
std::unique_ptr<DrakeJoint> Clone() const override;
31+
2932
template <typename DerivedQ>
3033
Eigen::Transform<typename DerivedQ::Scalar, 3, Eigen::Isometry>
3134
jointTransform(const Eigen::MatrixBase<DerivedQ>& q) const {

drake/multibody/rigid_body.cc

+31
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ using Eigen::Isometry3d;
99
using Eigen::Matrix;
1010
using Eigen::Vector3d;
1111

12+
using std::make_unique;
13+
using std::move;
1214
using std::ostream;
1315
using std::runtime_error;
1416
using std::string;
@@ -21,6 +23,35 @@ RigidBody<T>::RigidBody() {
2123
spatial_inertia_ << drake::SquareTwistMatrix<double>::Zero();
2224
}
2325

26+
// TODO(liang.fok) For an explanation of why these SWIG preprocessor commands
27+
// are needed, see the comment immediately above the declaration of
28+
// RigidBody::Clone() in rigid_body.h.
29+
#ifndef SWIG
30+
template <typename T>
31+
std::unique_ptr<RigidBody<T>> RigidBody<T>::Clone() const {
32+
auto body = make_unique<RigidBody<T>>();
33+
body->set_name(get_name());
34+
body->set_model_name(get_model_name());
35+
body->set_model_instance_id(get_model_instance_id());
36+
body->set_body_index(body_index_);
37+
body->set_position_start_index(position_start_index_);
38+
body->set_velocity_start_index(velocity_start_index_);
39+
40+
// TODO(liang.fok) Add cloning of visual elements.
41+
// body::visual_elements_ = ;
42+
43+
// TODO(liang.fok Add cloning of collision elements.)
44+
// body::collision_element_ids_ = ;
45+
// body::collision_element_groups_ = ;
46+
47+
body->set_contact_points(contact_points_);
48+
body->set_mass(mass_);
49+
body->set_center_of_mass(center_of_mass_);
50+
body->set_spatial_inertia(spatial_inertia_);
51+
return move(body);
52+
}
53+
#endif
54+
2455
template <typename T>
2556
const std::string& RigidBody<T>::get_name() const { return name_; }
2657

drake/multibody/rigid_body.h

+13
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ class RigidBody {
2121
public:
2222
RigidBody();
2323

24+
// TODO(liang.fok) The following preprocessor condition is necessary because
25+
// wrapping method Clone() in SWIG caused a build error to the effect of "call
26+
// to implicitly-deleted copy constructor". Unfortunately, adding
27+
// "%ignore RigidBodyTree<double>::Clone()" to
28+
// drake-distro/drake/bindings/swig/rbtree.i does not work for some unknown
29+
// reason.
30+
#ifndef SWIG
31+
/**
32+
* Returns a clone of this RigidBody.
33+
*/
34+
virtual std::unique_ptr<RigidBody<T>> Clone() const;
35+
#endif
36+
2437
/**
2538
* Returns the name of this rigid body.
2639
*/

drake/multibody/rigid_body_frame.cc

+16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "drake/multibody/rigid_body_frame.h"
22

3+
#include <memory>
4+
35
#include "drake/common/eigen_autodiff_types.h"
46
#include "drake/math/roll_pitch_yaw.h"
57

@@ -16,6 +18,20 @@ RigidBodyFrame<T>::RigidBodyFrame(const std::string& name, RigidBody<T>* body,
1618
transform_to_body_.matrix() << drake::math::rpy2rotmat(rpy), xyz, 0, 0, 0, 1;
1719
}
1820

21+
// TODO(liang.fok) For an explanation of why these SWIG preprocessor commands
22+
// are needed, see the comment immediately above the declaration of
23+
// RigidBodyFrame::Clone() in rigid_body_frame.h.
24+
#ifndef SWIG
25+
template <typename T>
26+
std::shared_ptr<RigidBodyFrame<T>> RigidBodyFrame<T>::Clone() const {
27+
auto frame = std::make_shared<RigidBodyFrame<T>>();
28+
frame->set_name(name_);
29+
frame->set_transform_to_body(transform_to_body_);
30+
frame->set_frame_index(frame_index_);
31+
return move(frame);
32+
}
33+
#endif
34+
1935
template <typename T>
2036
int RigidBodyFrame<T>::get_model_instance_id() const {
2137
return body_->get_model_instance_id();

drake/multibody/rigid_body_frame.h

+15
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,21 @@ class RigidBodyFrame {
5757
RigidBodyFrame()
5858
: RigidBodyFrame("", nullptr, Eigen::Isometry3d::Identity()) {}
5959

60+
// TODO(liang.fok) The following preprocessor condition is necessary because
61+
// wrapping method Clone() in SWIG caused a build error to the effect of "call
62+
// to implicitly-deleted copy constructor". Unfortunately, adding
63+
// "%ignore RigidBodyTree<double>::Clone()" to
64+
// drake-distro/drake/bindings/swig/rbtree.i does not work for some unknown
65+
// reason.
66+
#ifndef SWIG
67+
// TODO(liang.fok) Update this to return a unique_ptr. This is related to
68+
// #3093.
69+
/**
70+
* Returns a clone of this RigidBodyFrame.
71+
*/
72+
virtual std::shared_ptr<RigidBodyFrame<T>> Clone() const;
73+
#endif
74+
6075
/**
6176
* Returns the ID of the model instance to which this rigid body frame
6277
* belongs.

0 commit comments

Comments
 (0)