Skip to content

Commit 4754092

Browse files
authored
Merge 259e9ef into 1cb2374
2 parents 1cb2374 + 259e9ef commit 4754092

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

core/include/gz/msgs/convert/Pose.hh

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,17 @@ inline void Set(gz::msgs::Pose *_msg, const gz::math::Pose3d &_data)
4040

4141
inline void Set(gz::math::Pose3d *_data, const gz::msgs::Pose &_msg)
4242
{
43-
auto pos = Convert(_msg.position());
44-
auto orientation = Convert(_msg.orientation());
43+
gz::math::Vector3d pos;
44+
gz::math::Quaterniond orientation;
45+
46+
if (_msg.has_position())
47+
pos = Convert(_msg.position());
48+
49+
// This bit is critical. If orientation hasn't been set in the message,
50+
// then we want the quaternion to default to identity.
51+
if (_msg.has_orientation())
52+
orientation = Convert(_msg.orientation());
53+
4554
_data->Set(pos, orientation);
4655
}
4756

test/integration/Utility_TEST.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,23 @@ TEST(UtilityTest, ConvertMsgPoseToMath)
122122
EXPECT_TRUE(math::equal(v.Rot().W(), 0.27059805007309851));
123123
}
124124

125+
/////////////////////////////////////////////////
126+
TEST(UtilityTest, ConvertMsgPoseToMathIdentity)
127+
{
128+
// Setting position but not orientation should still
129+
// result in a valid identity quaternion
130+
msgs::Pose msg;
131+
msg.mutable_position()->set_x(1.0);
132+
msg.mutable_position()->set_y(2.0);
133+
msg.mutable_position()->set_z(2.0);
134+
135+
math::Pose3d v = msgs::Convert(msg);
136+
EXPECT_TRUE(math::equal(v.Rot().W(), 1.0));
137+
EXPECT_TRUE(math::equal(v.Rot().X(), 0.0));
138+
EXPECT_TRUE(math::equal(v.Rot().Y(), 0.0));
139+
EXPECT_TRUE(math::equal(v.Rot().Z(), 0.0));
140+
}
141+
125142
/////////////////////////////////////////////////
126143
TEST(MsgsTest, ConvertMathColorToMsgs)
127144
{

0 commit comments

Comments
 (0)