From e87a608d97b7fb93e372dc8ba25c9d70a6649c52 Mon Sep 17 00:00:00 2001 From: Stone Tao Date: Wed, 9 Jul 2025 10:11:24 -0700 Subject: [PATCH 1/3] Clamp matrices in matrix_to_euler_angles function --- pytorch3d/transforms/rotation_conversions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pytorch3d/transforms/rotation_conversions.py b/pytorch3d/transforms/rotation_conversions.py index 19fb890d..2bf43ee7 100644 --- a/pytorch3d/transforms/rotation_conversions.py +++ b/pytorch3d/transforms/rotation_conversions.py @@ -294,10 +294,10 @@ 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, 1) * (-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, 1)) o = ( _angle_from_tan( From 91871cf6c719a241cc5ed71b72eaf3fb88fb20d7 Mon Sep 17 00:00:00 2001 From: Stone Tao Date: Wed, 9 Jul 2025 10:13:53 -0700 Subject: [PATCH 2/3] add tolerance --- 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 2bf43ee7..1396034c 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( - torch.clamp(matrix[..., i0, i2], -1, 1) * (-1.0 if i0 - i2 in [-1, 2] else 1.0) + torch.clamp(matrix[..., i0, i2], -1.0 + 1e-8, 1.0 - 1e-8) + * (-1.0 if i0 - i2 in [-1, 2] else 1.0) ) else: - central_angle = torch.acos(torch.clamp(matrix[..., i0, i0], -1, 1)) + central_angle = torch.acos(torch.clamp(matrix[..., i0, i0], -1.0 + 1e-8, 1.0 - 1e-8)) o = ( _angle_from_tan( From 4a863186bcbec431f5190a55d133703179fe6e8e Mon Sep 17 00:00:00 2001 From: Stone Tao Date: Wed, 9 Jul 2025 17:20:46 +0000 Subject: [PATCH 3/3] remove eps --- pytorch3d/transforms/rotation_conversions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pytorch3d/transforms/rotation_conversions.py b/pytorch3d/transforms/rotation_conversions.py index 1396034c..95f0ff85 100644 --- a/pytorch3d/transforms/rotation_conversions.py +++ b/pytorch3d/transforms/rotation_conversions.py @@ -294,11 +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( - torch.clamp(matrix[..., i0, i2], -1.0 + 1e-8, 1.0 - 1e-8) + 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(torch.clamp(matrix[..., i0, i0], -1.0 + 1e-8, 1.0 - 1e-8)) + central_angle = torch.acos(torch.clamp(matrix[..., i0, i0], -1.0, 1.0)) o = ( _angle_from_tan(