From e3d3a67a89907476bd5b63289f9669bd427ae550 Mon Sep 17 00:00:00 2001 From: Stone Tao Date: Thu, 10 Jul 2025 06:08:19 -0700 Subject: [PATCH] Clamp matrices in matrix_to_euler_angles function (#1989) Summary: Closes https://github.com/facebookresearch/pytorch3d/issues/1988 Credit goes to tylerlum for raising this issue and suggesting this fix in https://github.com/haosulab/ManiSkill/pull/1090 Pull Request resolved: https://github.com/facebookresearch/pytorch3d/pull/1989 Reviewed By: MichaelRamamonjisoa Differential Revision: D78021983 Pulled By: bottler fbshipit-source-id: d723f1924a399f4d7fd072e96ea740ae73cf280f --- pytorch3d/transforms/rotation_conversions.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pytorch3d/transforms/rotation_conversions.py b/pytorch3d/transforms/rotation_conversions.py index 19fb890d..95f0ff85 100644 --- a/pytorch3d/transforms/rotation_conversions.py +++ b/pytorch3d/transforms/rotation_conversions.py @@ -294,10 +294,11 @@ def matrix_to_euler_angles(matrix: torch.Tensor, convention: str) -> torch.Tenso tait_bryan = i0 != i2 if tait_bryan: central_angle = torch.asin( - matrix[..., i0, i2] * (-1.0 if i0 - i2 in [-1, 2] else 1.0) + torch.clamp(matrix[..., i0, i2], -1.0, 1.0) + * (-1.0 if i0 - i2 in [-1, 2] else 1.0) ) else: - central_angle = torch.acos(matrix[..., i0, i0]) + central_angle = torch.acos(torch.clamp(matrix[..., i0, i0], -1.0, 1.0)) o = ( _angle_from_tan(