Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

rotation_matrix_to_angle is added

Mona Fathollahi79e768d6be4c
diff --git a/test/test_transforms.py b/test/test_transforms.py
index 6025d4ca9..814d60c7d 100644
--- a/test/test_transforms.py
+++ b/test/test_transforms.py
@@ -13,7 +13,7 @@ class Tester(unittest.TestCase):
def test_angle_axis_to_rotation_matrix_torch(self):
# generate input data
- batch_size = 2
+ batch_size = 2
angle_axis = torch.rand(batch_size, 3)
eye_batch = utils.create_eye_batch(batch_size, 4)
@@ -23,7 +23,7 @@ class Tester(unittest.TestCase):
rotation_matrix_eye = torch.matmul(
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)
@@ -58,6 +58,17 @@ class Tester(unittest.TestCase):
res = gradcheck(tf.rtvec_to_pose, (rtvec,), raise_exception=True)
self.assertTrue(res)
+ def test_rotation_matrix_to_angle_axis_torch(self):
+ rmat = torch.tensor([[-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 = torch.tensor([1.50485376, -2.10737739, 0.7214174 ])
+ self.assertTrue(check_equal_torch(tf.rotation_matrix_to_angle_axis(rmat), rvec))
+
+ def test_rotation_matrix_to_angle_axis_gradcheck(self):
+ print('test_rotation_matrix_to_angle_axis_gradcheck to be implemented :)')
+ self.assertTrue(True)
if __name__ == '__main__':
unittest.main()
diff --git a/torchgeometry/__init__.py b/torchgeometry/__init__.py
index 0f4141ff9..56e860f17 100644
--- a/torchgeometry/__init__.py
+++ b/torchgeometry/__init__.py
@@ -1,6 +1,6 @@
from .functional import *
from .homography_warper import HomographyWarper
-from .depth_warper import DepthWarper
+#from .depth_warper import DepthWarper
from torchgeometry import utils
from torchgeometry import transforms
diff --git a/torchgeometry/transforms/__init__.py b/torchgeometry/transforms/__init__.py
index ee77f54df..597808231 100644
--- a/torchgeometry/transforms/__init__.py
+++ b/torchgeometry/transforms/__init__.py
@@ -1,6 +1,7 @@
-from .functional import angle_axis_to_rotation_matrix, rtvec_to_pose
+from .functional import angle_axis_to_rotation_matrix, rtvec_to_pose, rotation_matrix_to_angle_axis
__all__ = [
'angle_axis_to_rotation_matrix',
'rtvec_to_pose',
+ 'rotation_matrix_to_angle_axis'
]
diff --git a/torchgeometry/transforms/functional.py b/torchgeometry/transforms/functional.py
index 005abf46d..395d8daa2 100644
--- a/torchgeometry/transforms/functional.py
+++ b/torchgeometry/transforms/functional.py
@@ -71,7 +71,7 @@ def angle_axis_to_rotation_matrix_torch(angle_axis, eps=1e-6):
rotation_matrix = torch.cat([
r00, r01, r02, r10, r11, r12, r20, r21, r22], dim=1)
return rotation_matrix.view(-1, 3, 3)
-
+
def _compute_rotation_matrix_taylor(angle_axis):
rx, ry, rz = torch.chunk(angle_axis, 3, dim=1)
k_one = torch.ones_like(rx)
@@ -125,6 +125,82 @@ def rtvec_to_pose(rtvec):
return pose
+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(rotation_matrix):
+ '''
+ Convert 4x4 rotation matrix to 4d quaternion vector
+ '''
+ quaternion = torch.zeros(4)
+ trace = rotation_matrix[0,0] + rotation_matrix[1,1] + rotation_matrix[2,2]
+ if trace >= 0.0:
+ t = torch.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 = torch.sqrt(rotation_matrix[i, i] - rotation_matrix[j, j] -
+ 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 /= torch.norm(quaternion)
+ return quaternion
+
+
+def quaternion_to_angle_axis(quaternion):
+ '''
+ Convert quaternion vector to angle axis of rotation
+ Adapted from ceres C++ library: ceres-solver/include/ceres/rotation.h
+ :param quaternion: Tensor vector of length 4
+ :return: angle axis of rotation (vector of length 3)
+ '''
+ assert quaternion.size(0) == 4, 'Input must be a vector of length 4'
+ normalizer = 1 / torch.norm(quaternion)
+ q1 = quaternion[1] * normalizer
+ q2 = quaternion[2] * normalizer
+ q3 = quaternion[3] * normalizer
+
+ sin_squared = q1 * q1 + q2 * q2 + q3 * q3
+ angle_axis = torch.zeros(3)
+
+ if sin_squared > 0:
+ sin_theta = torch.sqrt(sin_squared)
+ cos_theta = quaternion[0] * normalizer
+ theta = torch.atan2(-sin_theta, -cos_theta) if cos_theta < 0.0\
+ else torch.atan2(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
+
+
# TODO: add below funtionalities
# - rotation_matrix_to_angle_axis
# - pose_to_rtvec