Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Add type checking in quaternion_to_angle_axis

Mona Fathollahi0ece76666552
diff --git a/torchgeometry/conversions.py b/torchgeometry/conversions.py
index 9ebfc0535..818e70b7f 100644
--- a/torchgeometry/conversions.py
+++ b/torchgeometry/conversions.py
@@ -270,7 +270,7 @@ def rtvec_to_pose(rtvec):
def rotation_matrix_to_angle_axis(rotation_matrix):
- """Convert 4x4 rotation matrix to Rodrigues vector
+ """Convert 3x4 rotation matrix to Rodrigues vector
Args:
rotation_matrix (Tensor): rotation matrix.
@@ -279,11 +279,11 @@ def rotation_matrix_to_angle_axis(rotation_matrix):
Tensor: Rodrigues vector transformation.
Shape:
- - Input: :math:`(N, 4, 4)`
+ - Input: :math:`(N, 3, 4)`
- Output: :math:`(N, 3)`
Example:
- >>> input = torch.rand(2, 4, 4) # Nx4x4
+ >>> input = torch.rand(2, 3, 4) # Nx4x4
>>> output = tgm.rotation_matrix_to_angle_axis(input) # Nx3
"""
# todo add check that matrix is a valid rotation matrix
@@ -292,7 +292,7 @@ def rotation_matrix_to_angle_axis(rotation_matrix):
def rotation_matrix_to_quaternion(rotation_matrix, eps=1e-6):
- """Convert 4x4 rotation matrix to 4d quaternion vector
+ """Convert 3x4 rotation matrix to 4d quaternion vector
This algorithm is based on algorithm described in
https://github.com/KieranWynn/pyquaternion/blob/master/pyquaternion/quaternion.py#L201
@@ -304,31 +304,30 @@ def rotation_matrix_to_quaternion(rotation_matrix, eps=1e-6):
Tensor: the rotation in quaternion
Shape:
- - Input: :math:`(N, 4, 4)`
+ - Input: :math:`(N, 3, 4)`
- Output: :math:`(N, 4)`
Example:
- >>> input = torch.rand(4, 4, 4) # Nx4x4
+ >>> input = torch.rand(4, 3, 4) # Nx3x4
>>> output = tgm.rotation_matrix_to_quaternion(input) # Nx4
"""
if not torch.is_tensor(rotation_matrix):
raise TypeError("Input type is not a torch.Tensor. Got {}".format(
type(rotation_matrix)))
- if not len(rotation_matrix.shape) > 3:
+ input_shape = rotation_matrix.shape
+ if len(input_shape) == 2:
+ rotation_matrix = rotation_matrix.unsqueeze(0)
+
+ if len(rotation_matrix.shape) > 3:
raise ValueError(
"Input size must be a three dimensional tensor. Got {}".format(
rotation_matrix.shape))
-
- if not rotation_matrix.shape[-2:] == (4, 4):
+ if not rotation_matrix.shape[-2:] == (3, 4):
raise ValueError(
- "Input size must be a N x 4 x 4 tensor. Got {}".format(
+ "Input size must be a N x 3 x 4 tensor. Got {}".format(
rotation_matrix.shape))
- input_shape = rotation_matrix.shape
- if len(input_shape) == 2:
- rotation_matrix = rotation_matrix.unsqueeze(0)
-
rmat_t = torch.transpose(rotation_matrix, 1, 2)
mask_d2 = rmat_t[:, 2, 2] < eps
@@ -373,7 +372,7 @@ def rotation_matrix_to_quaternion(rotation_matrix, eps=1e-6):
q /= torch.sqrt(t0_rep * mask_c0 + t1_rep * mask_c1 + # noqa
t2_rep * mask_c2 + t3_rep * mask_c3)
q *= 0.5
-
+
if len(input_shape) == 2:
q = q.squeeze(0)
return q
@@ -398,6 +397,14 @@ def quaternion_to_angle_axis(quaternion, eps=1e-6):
>>> input = torch.rand(2, 4) # Nx4
>>> output = tgm.quaternion_to_angle_axis(input) # Nx3
"""
+ if not torch.is_tensor(quaternion):
+ raise TypeError("Input type is not a torch.Tensor. Got {}".format(
+ type(quaternion)))
+
+ input_shape = quaternion.shape
+ if len(input_shape) == 1:
+ quaternion = torch.unsqueeze(quaternion, dim=0)
+
assert quaternion.size(1) == 4, 'Input must be a vector of length 4'
normalizer = 1 / torch.norm(quaternion, dim=1)
q1 = quaternion[:, 1] * normalizer
@@ -430,6 +437,10 @@ def quaternion_to_angle_axis(quaternion, eps=1e-6):
angle_axis[:, 0] = q1 * k
angle_axis[:, 1] = q2 * k
angle_axis[:, 2] = q3 * k
+
+ if len(input_shape) == 1:
+ angle_axis = angle_axis.squeeze(0)
+
return angle_axis
# TODO: add below funtionalities