Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

cleanup numpy codes

edgarriba1c6736067c09
diff --git a/test/test_conversions.py b/test/test_conversions.py
index 56f18b48a..6200689e4 100644
--- a/test/test_conversions.py
+++ b/test/test_conversions.py
@@ -11,7 +11,7 @@ from utils import check_equal_torch, check_equal_numpy
class Tester(unittest.TestCase):
- def test_angle_axis_to_rotation_matrix_torch(self):
+ def test_angle_axis_to_rotation_matrix(self):
# generate input data
batch_size = 2
angle_axis = torch.rand(batch_size, 3)
@@ -24,16 +24,6 @@ class Tester(unittest.TestCase):
rotation_matrix, rotation_matrix.transpose(1, 2))
self.assertTrue(check_equal_torch(rotation_matrix_eye, eye_batch))
- def test_angle_axis_to_rotation_matrix_numpy(self):
- # generate input data
- angle_axis = np.random.rand(3)
-
- # apply transform
- rotation_matrix = tgm.angle_axis_to_rotation_matrix(angle_axis)
-
- rotation_matrix_eye = rotation_matrix.dot(rotation_matrix.T)
- self.assertTrue(check_equal_numpy(rotation_matrix_eye, np.eye(4)))
-
def test_angle_axis_to_rotation_matrix_gradcheck(self):
# generate input data
batch_size = 2
@@ -59,7 +49,7 @@ class Tester(unittest.TestCase):
self.assertTrue(res)
- def test_rotation_matrix_to_angle_axis_torch(self):
+ def test_rotation_matrix_to_angle_axis(self):
rmat_1 = torch.tensor([[-0.30382753, -0.95095137, -0.05814062, 0.],
[-0.71581715, 0.26812278, -0.64476041, 0.],
[ 0.62872461, -0.15427791, -0.76217038, 0.],
@@ -87,17 +77,6 @@ class Tester(unittest.TestCase):
self.assertTrue(res)
- def test_rotation_matrix_to_angle_axis_numpy(self):
- rmat = np.asarray([[-0.30382753, -0.95095137, -0.05814062, 0.],
- [-0.71581715, 0.26812278, -0.64476041, 0.],
- [0.62872461, -0.15427791, -0.76217038, 0.],
- [0., 0., 0., 1.]])
- rvec = np.array([1.50485376, -2.10737739, 0.7214174])
- self.assertTrue(
- check_equal_numpy(
- tgm.rotation_matrix_to_angle_axis(rmat),
- rvec))
-
@unittest.skip('')
def test_rotation_matrix_to_angle_axis_gradcheck(self):
print('test_rotation_matrix_to_angle_axis_gradcheck to be implemented :)') # noqa
diff --git a/torchgeometry/conversions.py b/torchgeometry/conversions.py
index dbeed3949..165620865 100644
--- a/torchgeometry/conversions.py
+++ b/torchgeometry/conversions.py
@@ -147,57 +147,7 @@ def transform_points(dst_pose_src, points_src):
return points_dst
-def angle_axis_to_rotation_matrix_numpy(angle_axis):
- """
- Convert 3d vector of axis-angle rotation to 4x4 rotation matrix
-
- Args:
- angle_axis (numpy.ndarray): array of 3d vector of axis-angle rotations
- of size (1, 3).
-
- Returns:
- numpy.ndarray: array of 4x4 rotation matrices of size (4, 4).
-
- """
- # stolen from ceres/rotation.h
- k_one = 1.0
- theta2 = angle_axis.dot(angle_axis)
- rotation_matrix = np.eye(4, dtype=angle_axis.dtype)
- if theta2 > np.finfo(np.float32).eps:
- # We want to be careful to only evaluate the square root if the
- # norm of the angle_axis vector is greater than zero. Otherwise
- # we get a division by zero.
- theta = np.sqrt(theta2)
- wx = angle_axis[0] / theta
- wy = angle_axis[1] / theta
- wz = angle_axis[2] / theta
- cos_theta = np.cos(theta)
- sin_theta = np.sin(theta)
-
- rotation_matrix[0, 0] = cos_theta + wx * wx * (k_one - cos_theta)
- rotation_matrix[1, 0] = wz * sin_theta + wx * wy * (k_one - cos_theta)
- rotation_matrix[2, 0] = -wy * sin_theta + wx * wz * (k_one - cos_theta)
- rotation_matrix[0, 1] = wx * wy * (k_one - cos_theta) - wz * sin_theta
- rotation_matrix[1, 1] = cos_theta + wy * wy * (k_one - cos_theta)
- rotation_matrix[2, 1] = wx * sin_theta + wy * wz * (k_one - cos_theta)
- rotation_matrix[0, 2] = wy * sin_theta + wx * wz * (k_one - cos_theta)
- rotation_matrix[1, 2] = -wx * sin_theta + wy * wz * (k_one - cos_theta)
- rotation_matrix[2, 2] = cos_theta + wz * wz * (k_one - cos_theta)
- else:
- # Near zero, we switch to using the first order Taylor expansion.
- rotation_matrix[0, 0] = k_one
- rotation_matrix[1, 0] = angle_axis[2]
- rotation_matrix[2, 0] = -angle_axis[1]
- rotation_matrix[0, 1] = -angle_axis[2]
- rotation_matrix[1, 1] = k_one
- rotation_matrix[2, 1] = angle_axis[0]
- rotation_matrix[0, 2] = angle_axis[1]
- rotation_matrix[1, 2] = -angle_axis[0]
- rotation_matrix[2, 2] = k_one
- return rotation_matrix
-
-
-def angle_axis_to_rotation_matrix_torch(angle_axis, eps=1e-6):
+def angle_axis_to_rotation_matrix(angle_axis):
"""
Convert 3d vector of axis-angle rotation to 4x4 rotation matrix
@@ -210,7 +160,7 @@ def angle_axis_to_rotation_matrix_torch(angle_axis, eps=1e-6):
"""
- def _compute_rotation_matrix(angle_axis, theta2):
+ def _compute_rotation_matrix(angle_axis, theta2, eps=1e-6):
# We want to be careful to only evaluate the square root if the
# norm of the angle_axis vector is greater than zero. Otherwise
# we get a division by zero.
@@ -252,6 +202,7 @@ def angle_axis_to_rotation_matrix_torch(angle_axis, eps=1e-6):
rotation_matrix_taylor = _compute_rotation_matrix_taylor(angle_axis)
# create mask to handle both cases
+ eps = 1e-6
mask = (theta2 > eps).view(-1, 1, 1).to(theta2.device)
mask_pos = (mask).type_as(theta2)
mask_neg = (mask == False).type_as(theta2) # noqa
@@ -266,18 +217,6 @@ def angle_axis_to_rotation_matrix_torch(angle_axis, eps=1e-6):
return rotation_matrix # Nx4x4
-def angle_axis_to_rotation_matrix(angle_axis):
- if isinstance(angle_axis, np.ndarray):
- return angle_axis_to_rotation_matrix_numpy(angle_axis)
- elif isinstance(angle_axis, torch.Tensor):
- if not (len(angle_axis.shape) == 2 and angle_axis.shape[1] == 3):
- raise ValueError("Input must be a two dimensional torch.Tensor.")
- return angle_axis_to_rotation_matrix_torch(angle_axis)
- else:
- raise NotImplementedError('Not suported type {}'.format(
- type(angle_axis)))
-
-
def rtvec_to_pose(rtvec):
"""
Convert axis-angle rotation and translation vector to 4x4 pose matrix
@@ -295,33 +234,6 @@ def rtvec_to_pose(rtvec):
return pose
-def rotation_matrix_to_quaternion(rotation_matrix):
- if isinstance(rotation_matrix, np.ndarray):
- return rotation_matrix_to_quaternion_numpy(rotation_matrix)
- elif isinstance(rotation_matrix, torch.Tensor):
- if not (len(rotation_matrix.shape) == 3 and # noqa
- rotation_matrix.shape[1] == 4):
- raise ValueError("Input must be a three dimensional torch.Tensor.")
- return rotation_matrix_to_quaternion_torch(rotation_matrix)
- else:
- raise NotImplementedError(
- 'Not suported type {}'.format(
- type(rotation_matrix)))
-
-
-def quaternion_to_angle_axis(quaternion):
- if isinstance(quaternion, np.ndarray):
- return quaternion_to_angle_axis_numpy(quaternion)
- elif isinstance(quaternion, torch.Tensor):
- if not (len(quaternion.shape) == 2 and quaternion.shape[1] == 4):
- raise ValueError("Input must be a two dimensional torch.Tensor.")
- return quaternion_to_angle_axis_torch(quaternion)
- else:
- raise NotImplementedError(
- 'Not suported type {}'.format(
- type(rotation_matrix)))
-
-
def rotation_matrix_to_angle_axis(rotation_matrix):
'''
Convert 4x4 rotation matrix to 4d quaternion vector
@@ -338,96 +250,15 @@ def rotation_matrix_to_angle_axis(rotation_matrix):
return quaternion_to_angle_axis(quaternion)
-# def rotation_matrix_to_angle_axis_torch(rotation_matrix):
-# '''
-# Convert 4x4 rotation matrix to 4d quaternion vector
-# '''
-# quaternion = rotation_matrix_to_quaternion_torch(rotation_matrix)
-# return quaternion_to_angle_axis_torch(quaternion)
-
-
-def rotation_matrix_to_quaternion_numpy(rotation_matrix):
- '''
- Convert 4x4 rotation matrix to 4d quaternion vector
-
- Args:
- rotation_matrix (Tensor): rotation matrix of size (4, 4).
-
- Returns:
- Tensor: quaternion representation of rotation of size (1, 4).
-
- '''
- quaternion = np.zeros(4, np.float32)
- trace = rotation_matrix[0, 0] + \
- rotation_matrix[1, 1] + rotation_matrix[2, 2]
- if trace >= 0.0:
- t = np.sqrt(trace + 1.0)
- quaternion[0] = 0.5 * t
- t = 0.5 / t
- quaternion[1] = (rotation_matrix[2, 1] - rotation_matrix[1, 2]) * t
- quaternion[2] = (rotation_matrix[0, 2] - rotation_matrix[2, 0]) * t
- quaternion[3] = (rotation_matrix[1, 0] - rotation_matrix[0, 1]) * t
- else:
- i = 0
- if rotation_matrix[1, 1] > rotation_matrix[0, 0]:
- i = 1
-
- if rotation_matrix[2, 2] > rotation_matrix[i, i]:
- i = 2
-
- j = (i + 1) % 3
- k = (j + 1) % 3
- t = np.sqrt(rotation_matrix[i, i] - rotation_matrix[j, j] - # noqa
- rotation_matrix[k, k] + 1.0)
- quaternion[i + 1] = 0.5 * t
- t = 0.5 / t
- quaternion[0] = (rotation_matrix[k, j] - rotation_matrix[j, k]) * t
- quaternion[j + 1] = (rotation_matrix[j, i] + rotation_matrix[i, j]) * t
- quaternion[k + 1] = (rotation_matrix[k, i] + rotation_matrix[i, k]) * t
- quaternion /= np.linalg.norm(quaternion)
- return quaternion
-
-
-def quaternion_to_angle_axis_numpy(quaternion):
- '''
- Convert quaternion vector to angle axis of rotation
- Adapted from ceres C++ library: ceres-solver/include/ceres/rotation.h
-
- Args:
- quaternion (Tensor): quaternion rotation vector of length 4
-
- Returns:
- Tensor: axis-angle rotation of size (1, 3).
-
- '''
- assert quaternion.shape[-1] == 4, 'Input must be a vector of length 4'
- normalizer = 1 / np.linalg.norm(quaternion)
- q1 = quaternion[1] * normalizer
- q2 = quaternion[2] * normalizer
- q3 = quaternion[3] * normalizer
-
- sin_squared = q1 * q1 + q2 * q2 + q3 * q3
- angle_axis = np.zeros(3)
-
- if sin_squared > 0:
- sin_theta = np.sqrt(sin_squared)
- cos_theta = quaternion[0] * normalizer
- theta = np.arctan2(-sin_theta, -cos_theta) if cos_theta < 0.0\
- else np.arctan2(sin_theta, cos_theta)
- two_theta = 2 * theta
- k = two_theta / sin_theta
- angle_axis[0] = q1 * k
- angle_axis[1] = q2 * k
- angle_axis[2] = q3 * k
- else:
- k = 2.0
- angle_axis[0] = q1 * k
- angle_axis[1] = q2 * k
- angle_axis[2] = q3 * k
- return angle_axis
+def rotation_matrix_to_angle_axis(rotation_matrix):
+ '''
+ Convert 4x4 rotation matrix to 4d quaternion vector
+ '''
+ quaternion = rotation_matrix_to_quaternion(rotation_matrix)
+ return quaternion_to_angle_axis(quaternion)
-def rotation_matrix_to_quaternion_torch(rotation_matrix, eps=1e-6):
+def rotation_matrix_to_quaternion(rotation_matrix, eps=1e-6):
'''
Convert 4x4 rotation matrix to 4d quaternion vector
This algorithm is based on algorithm described in
@@ -480,7 +311,7 @@ def rotation_matrix_to_quaternion_torch(rotation_matrix, eps=1e-6):
return q
-def quaternion_to_angle_axis_torch(quaternion, eps=1e-6):
+def quaternion_to_angle_axis(quaternion, eps=1e-6):
'''
Convert quaternion vector to angle axis of rotation
Adapted from ceres C++ library: ceres-solver/include/ceres/rotation.h