Skip to content

Commit 9c6d9b8

Browse files
committed
Enable Transform and Quaternion for bullet
1 parent d35a32b commit 9c6d9b8

File tree

2 files changed

+110
-38
lines changed

2 files changed

+110
-38
lines changed

tf2_bullet/include/tf2_bullet/tf2_bullet.h

Lines changed: 94 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <tf2/transform_datatypes.h>
3838

3939
#include <geometry_msgs/msg/point_stamped.hpp>
40+
#include <geometry_msgs/msg/vector3.hpp>
4041

4142
#if (BT_BULLET_VERSION <= 282)
4243
// Suppress compilation warning on older versions of Bullet.
@@ -49,41 +50,6 @@ inline int bullet_btInfinityMask()
4950

5051
namespace tf2
5152
{
52-
/** \brief Convert a timestamped transform to the equivalent Bullet data type.
53-
* \param t The transform to convert, as a geometry_msgs TransformedStamped message.
54-
* \return The transform message converted to a Bullet btTransform.
55-
*/
56-
inline
57-
btTransform transformToBullet(const geometry_msgs::msg::TransformStamped & t)
58-
{
59-
return btTransform(
60-
btQuaternion(
61-
static_cast < float > (t.transform.rotation.x),
62-
static_cast < float > (t.transform.rotation.y),
63-
static_cast < float > (t.transform.rotation.z),
64-
static_cast < float > (t.transform.rotation.w)),
65-
btVector3(
66-
static_cast < float > (t.transform.translation.x),
67-
static_cast < float > (t.transform.translation.y),
68-
static_cast < float > (t.transform.translation.z)));
69-
}
70-
71-
72-
/** \brief Apply a geometry_msgs TransformStamped to a Bullet-specific Vector3 type.
73-
* This function is a specialization of the doTransform template defined in tf2/convert.h
74-
* \param t_in The vector to transform, as a timestamped Bullet btVector3 data type.
75-
* \param t_out The transformed vector, as a timestamped Bullet btVector3 data type.
76-
* \param transform The timestamped transform to apply, as a TransformStamped message.
77-
*/
78-
template <>
79-
inline void doTransform(
80-
const tf2::Stamped<btVector3> & t_in, tf2::Stamped<btVector3> & t_out,
81-
const geometry_msgs::msg::TransformStamped & transform)
82-
{
83-
t_out = tf2::Stamped<btVector3>(
84-
transformToBullet(transform) * t_in, transform.header.stamp, transform.header.frame_id);
85-
}
86-
8753
namespace impl
8854
{
8955
template <>
@@ -93,14 +59,26 @@ struct defaultMessage<btVector3>
9359
};
9460

9561
template <>
96-
struct ImplDetails<btVector3, geometry_msgs::msg::Point>
62+
struct defaultMessage<btQuaternion>
63+
{
64+
using type = geometry_msgs::msg::Quaternion;
65+
};
66+
67+
template <>
68+
struct defaultMessage<btTransform>
69+
{
70+
using type = geometry_msgs::msg::Transform;
71+
};
72+
73+
template <class Message>
74+
struct BulletVectorImpl
9775
{
9876
/** \brief Convert a stamped Bullet Vector3 type to a PointStamped message.
9977
* This function is a specialization of the toMsg template defined in tf2/convert.h
10078
* \param in The timestamped Bullet btVector3 to convert.
10179
* \return The vector converted to a PointStamped message.
10280
*/
103-
static void toMsg(const btVector3 & in, geometry_msgs::msg::Point & msg)
81+
static void toMsg(const btVector3 & in, Message & msg)
10482
{
10583
msg.x = in[0];
10684
msg.y = in[1];
@@ -112,15 +90,93 @@ struct ImplDetails<btVector3, geometry_msgs::msg::Point>
11290
* \param msg The PointStamped message to convert.
11391
* \param out The point converted to a timestamped Bullet Vector3.
11492
*/
115-
static void fromMsg(const geometry_msgs::msg::Point & msg, btVector3 & out)
93+
static void fromMsg(const Message & msg, btVector3 & out)
94+
{
95+
out[0] = msg.x;
96+
out[1] = msg.y;
97+
out[2] = msg.z;
98+
}
99+
};
100+
101+
template <>
102+
struct ImplDetails<btVector3, geometry_msgs::msg::Point>
103+
: BulletVectorImpl<geometry_msgs::msg::Point>
104+
{
105+
};
106+
107+
template <>
108+
struct ImplDetails<btVector3, geometry_msgs::msg::Vector3>
109+
: BulletVectorImpl<geometry_msgs::msg::Vector3>
110+
{
111+
};
112+
113+
template <>
114+
struct ImplDetails<btQuaternion, geometry_msgs::msg::Quaternion>
115+
{
116+
static void toMsg(const btQuaternion & in, geometry_msgs::msg::Quaternion & msg)
117+
{
118+
msg.x = in[0];
119+
msg.y = in[1];
120+
msg.z = in[2];
121+
msg.w = in[3];
122+
}
123+
124+
static void fromMsg(const geometry_msgs::msg::Quaternion & msg, btQuaternion & out)
116125
{
117126
out[0] = msg.x;
118127
out[1] = msg.y;
119128
out[2] = msg.z;
129+
out[3] = msg.w;
130+
if (msg.w < 0) out *= -1;
131+
}
132+
};
133+
134+
template <>
135+
struct ImplDetails<btTransform, geometry_msgs::msg::Transform>
136+
{
137+
static void fromMsg(geometry_msgs::msg::Transform const & in, btTransform & out)
138+
{
139+
btVector3 trans;
140+
btQuaternion rot;
141+
tf2::fromMsg<>(in.rotation, rot);
142+
tf2::fromMsg<>(in.translation, trans);
143+
out = btTransform(rot, trans);
144+
}
145+
146+
static void toMsg(btTransform const & in, geometry_msgs::msg::Transform & out)
147+
{
148+
tf2::toMsg<>(in.getRotation(), out.rotation);
149+
tf2::toMsg<>(in.getOrigin(), out.translation);
120150
}
121151
};
122152
} // namespace impl
123153

154+
/** \brief Convert a timestamped transform to the equivalent Bullet data type.
155+
* \param t The transform to convert, as a geometry_msgs TransformedStamped message.
156+
* \return The transform message converted to a Bullet btTransform.
157+
*/
158+
inline btTransform transformToBullet(const geometry_msgs::msg::TransformStamped & t)
159+
{
160+
btTransform ans;
161+
fromMsg<>(t.transform, ans);
162+
return ans;
163+
}
164+
165+
/** \brief Apply a geometry_msgs TransformStamped to a Bullet-specific Vector3 type.
166+
* This function is a specialization of the doTransform template defined in tf2/convert.h
167+
* \param t_in The vector to transform, as a timestamped Bullet btVector3 data type.
168+
* \param t_out The transformed vector, as a timestamped Bullet btVector3 data type.
169+
* \param transform The timestamped transform to apply, as a TransformStamped message.
170+
*/
171+
template <>
172+
inline void doTransform(
173+
const tf2::Stamped<btVector3> & t_in, tf2::Stamped<btVector3> & t_out,
174+
const geometry_msgs::msg::TransformStamped & transform)
175+
{
176+
t_out = tf2::Stamped<btVector3>(
177+
transformToBullet(transform) * t_in, transform.header.stamp, transform.header.frame_id);
178+
}
179+
124180
/** \brief Apply a geometry_msgs TransformStamped to a Bullet-specific Transform data type.
125181
* This function is a specialization of the doTransform template defined in tf2/convert.h
126182
* \param t_in The frame to transform, as a timestamped Bullet btTransform.

tf2_bullet/test/test_tf2_bullet.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,22 @@ TEST(TfBullet, ConvertVector)
5151
EXPECT_EQ(v1, v2);
5252
}
5353

54+
TEST(TfBullet, ConvertTransform)
55+
{
56+
geometry_msgs::msg::Transform transform;
57+
transform.rotation.x = 1.0;
58+
transform.rotation.w = 0.0;
59+
transform.translation.z = 2.0;
60+
61+
btTransform transform_bt;
62+
tf2::convert(transform, transform_bt);
63+
EXPECT_EQ(transform_bt.getRotation(), btQuaternion(1.0f, 0.0f, 0.0f, 0.0f));
64+
EXPECT_EQ(transform_bt.getOrigin(), btVector3(0.0f, 0.0f, 2.0f));
65+
66+
geometry_msgs::msg::Transform transform2;
67+
tf2::convert(transform_bt, transform2);
68+
EXPECT_EQ(transform2, transform);
69+
}
5470

5571
int main(int argc, char ** argv)
5672
{

0 commit comments

Comments
 (0)