Skip to content

yaw_quat function optimization and test #2247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions source/isaaclab/isaaclab/utils/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,13 +529,13 @@ def yaw_quat(quat: torch.Tensor) -> torch.Tensor:
A quaternion with only yaw component.
"""
shape = quat.shape
quat_yaw = quat.clone().view(-1, 4)
quat_yaw = quat.view(-1, 4)
qw = quat_yaw[:, 0]
qx = quat_yaw[:, 1]
qy = quat_yaw[:, 2]
qz = quat_yaw[:, 3]
yaw = torch.atan2(2 * (qw * qz + qx * qy), 1 - 2 * (qy * qy + qz * qz))
quat_yaw[:] = 0.0
quat_yaw = torch.zeros_like(quat_yaw)
quat_yaw[:, 3] = torch.sin(yaw / 2)
quat_yaw[:, 0] = torch.cos(yaw / 2)
quat_yaw = normalize(quat_yaw)
Expand Down
20 changes: 20 additions & 0 deletions source/isaaclab/test/utils/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,26 @@ def test_wrap_to_pi(self):
# Check that the wrapped angle is close to the expected value
torch.testing.assert_close(wrapped_angle, expected_angle)

def test_yaw_quat(self):
"""
Test for yaw_quat methods.
"""
# 90-degree (n/2 radians) rotations about the Y-axis
quat_input = torch.Tensor([0.7071, 0, 0.7071, 0])
cloned_quat_input = quat_input.clone()

# Calculated output that the function should return
expected_output = torch.Tensor([1, 0, 0, 0])

# Compute the result using the existing implementation
result = math_utils.yaw_quat(quat_input)

# Verify original quat is not being modified
torch.testing.assert_close(quat_input, cloned_quat_input)

# check that the output is equivalent to the expected output
torch.testing.assert_close(result, expected_output)

def test_quat_rotate_and_quat_rotate_inverse(self):
"""Test for quat_rotate and quat_rotate_inverse methods.

Expand Down