Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Remove deprecated code from `kornia.geometry.conversion` (#2437)

Priya Nagda3c175a968c6e
diff --git a/kornia/geometry/conversions.py b/kornia/geometry/conversions.py
index 7d923cd4f..7794f462f 100644
--- a/kornia/geometry/conversions.py
+++ b/kornia/geometry/conversions.py
@@ -1,8 +1,5 @@
from __future__ import annotations
-import enum
-import warnings
-
import torch
import torch.nn.functional as F
@@ -55,11 +52,6 @@ __all__ = [
]
-class QuaternionCoeffOrder(enum.Enum):
- XYZW = 'xyzw'
- WXYZ = 'wxyz'
-
-
def rad2deg(tensor: Tensor) -> Tensor:
r"""Function that converts angles from radians to degrees.
@@ -369,24 +361,18 @@ def rotation_matrix_to_angle_axis(rotation_matrix: Tensor) -> Tensor:
if not rotation_matrix.shape[-2:] == (3, 3):
raise ValueError(f"Input size must be a (*, 3, 3) tensor. Got {rotation_matrix.shape}")
- quaternion: Tensor = rotation_matrix_to_quaternion(rotation_matrix, order=QuaternionCoeffOrder.WXYZ)
- return quaternion_to_angle_axis(quaternion, order=QuaternionCoeffOrder.WXYZ)
+ quaternion: Tensor = rotation_matrix_to_quaternion(rotation_matrix)
+ return quaternion_to_angle_axis(quaternion)
-def rotation_matrix_to_quaternion(
- rotation_matrix: Tensor, eps: float = 1.0e-8, order: QuaternionCoeffOrder = QuaternionCoeffOrder.XYZW
-) -> Tensor:
+def rotation_matrix_to_quaternion(rotation_matrix: Tensor, eps: float = 1.0e-8) -> Tensor:
r"""Convert 3x3 rotation matrix to 4d quaternion vector.
- The quaternion vector has components in (w, x, y, z) or (x, y, z, w) format.
-
- .. note::
- The (x, y, z, w) order is going to be deprecated in favor of efficiency.
+ The quaternion vector has components in (w, x, y, z) format.
Args:
rotation_matrix: the rotation matrix to convert with shape :math:`(*, 3, 3)`.
eps: small value to avoid zero division.
- order: quaternion coefficient order. Note: 'xyzw' will be deprecated in favor of 'wxyz'.
Return:
the rotation in quaternion with shape :math:`(*, 4)`.
@@ -395,8 +381,7 @@ def rotation_matrix_to_quaternion(
>>> input = tensor([[1., 0., 0.],
... [0., 1., 0.],
... [0., 0., 1.]])
- >>> rotation_matrix_to_quaternion(input, eps=torch.finfo(input.dtype).eps,
- ... order=QuaternionCoeffOrder.WXYZ)
+ >>> rotation_matrix_to_quaternion(input, eps=torch.finfo(input.dtype).eps)
tensor([1., 0., 0., 0.])
"""
if not isinstance(rotation_matrix, Tensor):
@@ -405,16 +390,6 @@ def rotation_matrix_to_quaternion(
if not rotation_matrix.shape[-2:] == (3, 3):
raise ValueError(f"Input size must be a (*, 3, 3) tensor. Got {rotation_matrix.shape}")
- if order.name not in QuaternionCoeffOrder.__members__.keys():
- raise ValueError(f"order must be one of {QuaternionCoeffOrder.__members__.keys()}")
-
- if order == QuaternionCoeffOrder.XYZW:
- warnings.warn(
- "`XYZW` quaternion coefficient order is deprecated and"
- " will be removed after > 0.6. "
- "Please use `QuaternionCoeffOrder.WXYZ` instead."
- )
-
def safe_zero_division(numerator: Tensor, denominator: Tensor) -> Tensor:
eps: float = torch.finfo(numerator.dtype).tiny
return numerator / torch.clamp(denominator, min=eps)
@@ -431,8 +406,6 @@ def rotation_matrix_to_quaternion(
qx = safe_zero_division(m21 - m12, sq)
qy = safe_zero_division(m02 - m20, sq)
qz = safe_zero_division(m10 - m01, sq)
- if order == QuaternionCoeffOrder.XYZW:
- return concatenate((qx, qy, qz, qw), dim=-1)
return concatenate((qw, qx, qy, qz), dim=-1)
def cond_1():
@@ -441,8 +414,6 @@ def rotation_matrix_to_quaternion(
qx = 0.25 * sq
qy = safe_zero_division(m01 + m10, sq)
qz = safe_zero_division(m02 + m20, sq)
- if order == QuaternionCoeffOrder.XYZW:
- return concatenate((qx, qy, qz, qw), dim=-1)
return concatenate((qw, qx, qy, qz), dim=-1)
def cond_2():
@@ -451,8 +422,6 @@ def rotation_matrix_to_quaternion(
qx = safe_zero_division(m01 + m10, sq)
qy = 0.25 * sq
qz = safe_zero_division(m12 + m21, sq)
- if order == QuaternionCoeffOrder.XYZW:
- return concatenate((qx, qy, qz, qw), dim=-1)
return concatenate((qw, qx, qy, qz), dim=-1)
def cond_3():
@@ -461,8 +430,6 @@ def rotation_matrix_to_quaternion(
qx = safe_zero_division(m02 + m20, sq)
qy = safe_zero_division(m12 + m21, sq)
qz = 0.25 * sq
- if order == QuaternionCoeffOrder.XYZW:
- return concatenate((qx, qy, qz, qw), dim=-1)
return concatenate((qw, qx, qy, qz), dim=-1)
where_2 = where(m11 > m22, cond_2(), cond_3())
@@ -503,24 +470,21 @@ def normalize_quaternion(quaternion: Tensor, eps: float = 1.0e-12) -> Tensor:
# https://github.com/tensorflow/graphics/blob/master/tensorflow_graphics/geometry/transformation/rotation_matrix_3d.py#L247
-def quaternion_to_rotation_matrix(
- quaternion: Tensor, order: QuaternionCoeffOrder = QuaternionCoeffOrder.XYZW
-) -> Tensor:
+def quaternion_to_rotation_matrix(quaternion: Tensor) -> Tensor:
r"""Convert a quaternion to a rotation matrix.
- The quaternion should be in (x, y, z, w) or (w, x, y, z) format.
+ The quaternion should be in (w, x, y, z) format.
Args:
quaternion: a tensor containing a quaternion to be converted.
The tensor can be of shape :math:`(*, 4)`.
- order: quaternion coefficient order. Note: 'xyzw' will be deprecated in favor of 'wxyz'.
Return:
the rotation matrix of shape :math:`(*, 3, 3)`.
Example:
>>> quaternion = tensor((0., 0., 0., 1.))
- >>> quaternion_to_rotation_matrix(quaternion, order=QuaternionCoeffOrder.WXYZ)
+ >>> quaternion_to_rotation_matrix(quaternion)
tensor([[-1., 0., 0.],
[ 0., -1., 0.],
[ 0., 0., 1.]])
@@ -531,24 +495,14 @@ def quaternion_to_rotation_matrix(
if not quaternion.shape[-1] == 4:
raise ValueError(f"Input must be a tensor of shape (*, 4). Got {quaternion.shape}")
- if order.name not in QuaternionCoeffOrder.__members__.keys():
- raise ValueError(f"order must be one of {QuaternionCoeffOrder.__members__.keys()}")
-
- if order == QuaternionCoeffOrder.XYZW:
- warnings.warn(
- "`XYZW` quaternion coefficient order is deprecated and"
- " will be removed after > 0.6. "
- "Please use `QuaternionCoeffOrder.WXYZ` instead."
- )
-
# normalize the input quaternion
quaternion_norm: Tensor = normalize_quaternion(quaternion)
# unpack the normalized quaternion components
- if order == QuaternionCoeffOrder.XYZW:
- x, y, z, w = torch.chunk(quaternion_norm, chunks=4, dim=-1)
- else:
- w, x, y, z = torch.chunk(quaternion_norm, chunks=4, dim=-1)
+ w = quaternion_norm[..., 0]
+ x = quaternion_norm[..., 1]
+ y = quaternion_norm[..., 2]
+ z = quaternion_norm[..., 3]
# compute the actual conversion
tx: Tensor = 2.0 * x
@@ -587,16 +541,15 @@ def quaternion_to_rotation_matrix(
return matrix
-def quaternion_to_angle_axis(quaternion: Tensor, order: QuaternionCoeffOrder = QuaternionCoeffOrder.XYZW) -> Tensor:
+def quaternion_to_angle_axis(quaternion: Tensor) -> Tensor:
"""Convert quaternion vector to angle axis of rotation in radians.
- The quaternion should be in (x, y, z, w) or (w, x, y, z) format.
+ The quaternion should be in (w, x, y, z) format.
Adapted from ceres C++ library: ceres-solver/include/ceres/rotation.h
Args:
quaternion: tensor with quaternions.
- order: quaternion coefficient order. Note: 'xyzw' will be deprecated in favor of 'wxyz'.
Return:
tensor with angle axis of rotation.
@@ -608,7 +561,7 @@ def quaternion_to_angle_axis(quaternion: Tensor, order: QuaternionCoeffOrder = Q
Example:
>>> quaternion = tensor((1., 0., 0., 0.))
>>> quaternion_to_angle_axis(quaternion)
- tensor([3.1416, 0.0000, 0.0000])
+ tensor([0., 0., 0.])
"""
if not torch.is_tensor(quaternion):
raise TypeError(f"Input type is not a Tensor. Got {type(quaternion)}")
@@ -616,31 +569,16 @@ def quaternion_to_angle_axis(quaternion: Tensor, order: QuaternionCoeffOrder = Q
if not quaternion.shape[-1] == 4:
raise ValueError(f"Input must be a tensor of shape Nx4 or 4. Got {quaternion.shape}")
- if order.name not in QuaternionCoeffOrder.__members__.keys():
- raise ValueError(f"order must be one of {QuaternionCoeffOrder.__members__.keys()}")
-
- if order == QuaternionCoeffOrder.XYZW:
- warnings.warn(
- "`XYZW` quaternion coefficient order is deprecated and"
- " will be removed after > 0.6. "
- "Please use `QuaternionCoeffOrder.WXYZ` instead."
- )
# unpack input and compute conversion
q1: Tensor = tensor([])
q2: Tensor = tensor([])
q3: Tensor = tensor([])
cos_theta: Tensor = tensor([])
- if order == QuaternionCoeffOrder.XYZW:
- q1 = quaternion[..., 0]
- q2 = quaternion[..., 1]
- q3 = quaternion[..., 2]
- cos_theta = quaternion[..., 3]
- else:
- cos_theta = quaternion[..., 0]
- q1 = quaternion[..., 1]
- q2 = quaternion[..., 2]
- q3 = quaternion[..., 3]
+ cos_theta = quaternion[..., 0]
+ q1 = quaternion[..., 1]
+ q2 = quaternion[..., 2]
+ q3 = quaternion[..., 3]
sin_squared_theta: Tensor = q1 * q1 + q2 * q2 + q3 * q3
@@ -660,26 +598,22 @@ def quaternion_to_angle_axis(quaternion: Tensor, order: QuaternionCoeffOrder = Q
return angle_axis
-def quaternion_log_to_exp(
- quaternion: Tensor, eps: float = 1.0e-8, order: QuaternionCoeffOrder = QuaternionCoeffOrder.XYZW
-) -> Tensor:
+def quaternion_log_to_exp(quaternion: Tensor, eps: float = 1.0e-8) -> Tensor:
r"""Apply exponential map to log quaternion.
- The quaternion should be in (x, y, z, w) or (w, x, y, z) format.
+ The quaternion should be in (w, x, y, z) format.
Args:
quaternion: a tensor containing a quaternion to be converted.
The tensor can be of shape :math:`(*, 3)`.
eps: a small number for clamping.
- order: quaternion coefficient order. Note: 'xyzw' will be deprecated in favor of 'wxyz'.
Return:
the quaternion exponential map of shape :math:`(*, 4)`.
Example:
>>> quaternion = tensor((0., 0., 0.))
- >>> quaternion_log_to_exp(quaternion, eps=torch.finfo(quaternion.dtype).eps,
- ... order=QuaternionCoeffOrder.WXYZ)
+ >>> quaternion_log_to_exp(quaternion, eps=torch.finfo(quaternion.dtype).eps)
tensor([1., 0., 0., 0.])
"""
if not isinstance(quaternion, Tensor):
@@ -688,16 +622,6 @@ def quaternion_log_to_exp(
if not quaternion.shape[-1] == 3:
raise ValueError(f"Input must be a tensor of shape (*, 3). Got {quaternion.shape}")
- if order.name not in QuaternionCoeffOrder.__members__.keys():
- raise ValueError(f"order must be one of {QuaternionCoeffOrder.__members__.keys()}")
-
- if order == QuaternionCoeffOrder.XYZW:
- warnings.warn(
- "`XYZW` quaternion coefficient order is deprecated and"
- " will be removed after > 0.6. "
- "Please use `QuaternionCoeffOrder.WXYZ` instead."
- )
-
# compute quaternion norm
norm_q: Tensor = torch.norm(quaternion, p=2, dim=-1, keepdim=True).clamp(min=eps)
@@ -707,34 +631,27 @@ def quaternion_log_to_exp(
# compose quaternion and return
quaternion_exp: Tensor = tensor([])
- if order == QuaternionCoeffOrder.XYZW:
- quaternion_exp = concatenate((quaternion_vector, quaternion_scalar), dim=-1)
- else:
- quaternion_exp = concatenate((quaternion_scalar, quaternion_vector), dim=-1)
+ quaternion_exp = concatenate((quaternion_scalar, quaternion_vector), dim=-1)
return quaternion_exp
-def quaternion_exp_to_log(
- quaternion: Tensor, eps: float = 1.0e-8, order: QuaternionCoeffOrder = QuaternionCoeffOrder.XYZW
-) -> Tensor:
+def quaternion_exp_to_log(quaternion: Tensor, eps: float = 1.0e-8) -> Tensor:
r"""Apply the log map to a quaternion.
- The quaternion should be in (x, y, z, w) format.
+ The quaternion should be in (w, x, y, z) format.
Args:
quaternion: a tensor containing a quaternion to be converted.
The tensor can be of shape :math:`(*, 4)`.
eps: a small number for clamping.
- order: quaternion coefficient order. Note: 'xyzw' will be deprecated in favor of 'wxyz'.
Return:
the quaternion log map of shape :math:`(*, 3)`.
Example:
>>> quaternion = tensor((1., 0., 0., 0.))
- >>> quaternion_exp_to_log(quaternion, eps=torch.finfo(quaternion.dtype).eps,
- ... order=QuaternionCoeffOrder.WXYZ)
+ >>> quaternion_exp_to_log(quaternion, eps=torch.finfo(quaternion.dtype).eps)
tensor([0., 0., 0.])
"""
if not isinstance(quaternion, Tensor):
@@ -743,26 +660,12 @@ def quaternion_exp_to_log(
if not quaternion.shape[-1] == 4:
raise ValueError(f"Input must be a tensor of shape (*, 4). Got {quaternion.shape}")
- if order.name not in QuaternionCoeffOrder.__members__.keys():
- raise ValueError(f"order must be one of {QuaternionCoeffOrder.__members__.keys()}")
-
- if order == QuaternionCoeffOrder.XYZW:
- warnings.warn(
- "`XYZW` quaternion coefficient order is deprecated and"
- " will be removed after > 0.6. "
- "Please use `QuaternionCoeffOrder.WXYZ` instead."
- )
-
# unpack quaternion vector and scalar
quaternion_vector: Tensor = tensor([])
quaternion_scalar: Tensor = tensor([])
- if order == QuaternionCoeffOrder.XYZW:
- quaternion_vector = quaternion[..., 0:3]
- quaternion_scalar = quaternion[..., 3:4]
- else:
- quaternion_scalar = quaternion[..., 0:1]
- quaternion_vector = quaternion[..., 1:4]
+ quaternion_scalar = quaternion[..., 0:1]
+ quaternion_vector = quaternion[..., 1:4]
# compute quaternion norm
norm_q: Tensor = torch.norm(quaternion_vector, p=2, dim=-1, keepdim=True).clamp(min=eps)
@@ -777,16 +680,15 @@ def quaternion_exp_to_log(
# https://github.com/facebookresearch/QuaterNet/blob/master/common/quaternion.py#L138
-def angle_axis_to_quaternion(angle_axis: Tensor, order: QuaternionCoeffOrder = QuaternionCoeffOrder.XYZW) -> Tensor:
+def angle_axis_to_quaternion(angle_axis: Tensor) -> Tensor:
r"""Convert an angle axis to a quaternion.
- The quaternion vector has components in (x, y, z, w) or (w, x, y, z) format.
+ The quaternion vector has components in (w, x, y, z) format.
Adapted from ceres C++ library: ceres-solver/include/ceres/rotation.h
Args:
angle_axis: tensor with angle axis in radians.
- order: quaternion coefficient order. Note: 'xyzw' will be deprecated in favor of 'wxyz'.
Return:
tensor with quaternion.
@@ -797,7 +699,7 @@ def angle_axis_to_quaternion(angle_axis: Tensor, order: QuaternionCoeffOrder = Q
Example:
>>> angle_axis = tensor((0., 1., 0.))
- >>> angle_axis_to_quaternion(angle_axis, order=QuaternionCoeffOrder.WXYZ)
+ >>> angle_axis_to_quaternion(angle_axis)
tensor([0.8776, 0.0000, 0.4794, 0.0000])
"""
if not torch.is_tensor(angle_axis):
@@ -806,16 +708,6 @@ def angle_axis_to_quaternion(angle_axis: Tensor, order: QuaternionCoeffOrder = Q
if not angle_axis.shape[-1] == 3:
raise ValueError(f"Input must be a tensor of shape Nx3 or 3. Got {angle_axis.shape}")
- if order.name not in QuaternionCoeffOrder.__members__.keys():
- raise ValueError(f"order must be one of {QuaternionCoeffOrder.__members__.keys()}")
-
- if order == QuaternionCoeffOrder.XYZW:
- warnings.warn(
- "`XYZW` quaternion coefficient order is deprecated and"
- " will be removed after > 0.6. "
- "Please use `QuaternionCoeffOrder.WXYZ` instead."
- )
-
# unpack input and compute conversion
a0: Tensor = angle_axis[..., 0:1]
a1: Tensor = angle_axis[..., 1:2]
@@ -834,16 +726,10 @@ def angle_axis_to_quaternion(angle_axis: Tensor, order: QuaternionCoeffOrder = Q
w: Tensor = where(mask, torch.cos(half_theta), ones)
quaternion: Tensor = torch.zeros(size=(*angle_axis.shape[:-1], 4), dtype=angle_axis.dtype, device=angle_axis.device)
- if order == QuaternionCoeffOrder.XYZW:
- quaternion[..., 0:1] = a0 * k
- quaternion[..., 1:2] = a1 * k
- quaternion[..., 2:3] = a2 * k
- quaternion[..., 3:4] = w
- else:
- quaternion[..., 1:2] = a0 * k
- quaternion[..., 2:3] = a1 * k
- quaternion[..., 3:4] = a2 * k
- quaternion[..., 0:1] = w
+ quaternion[..., 1:2] = a0 * k
+ quaternion[..., 2:3] = a1 * k
+ quaternion[..., 3:4] = a2 * k
+ quaternion[..., 0:1] = w
return quaternion
@@ -1561,9 +1447,9 @@ def ARKitQTVecs_to_ColmapQTVecs(qvec: Tensor, tvec: Tensor) -> tuple[Tensor, Ten
"""
# ToDo: integrate QuaterniaonAPI
- Rcg = quaternion_to_rotation_matrix(qvec, order=QuaternionCoeffOrder.WXYZ)
+ Rcg = quaternion_to_rotation_matrix(qvec)
Rcv, Tcv = camtoworld_graphics_to_vision_Rt(Rcg, tvec)
R_colmap, t_colmap = camtoworld_to_worldtocam_Rt(Rcv, Tcv)
t_colmap = t_colmap.reshape(-1, 3, 1)
- q_colmap = rotation_matrix_to_quaternion(R_colmap.contiguous(), order=QuaternionCoeffOrder.WXYZ)
+ q_colmap = rotation_matrix_to_quaternion(R_colmap.contiguous())
return q_colmap, t_colmap
diff --git a/kornia/geometry/quaternion.py b/kornia/geometry/quaternion.py
index 44bbff995..ff7f16a0c 100644
--- a/kornia/geometry/quaternion.py
+++ b/kornia/geometry/quaternion.py
@@ -8,7 +8,6 @@ from typing import Optional, Tuple, Union
from kornia.core import Device, Dtype, Module, Parameter, Tensor, concatenate, rand, stack, tensor, where
from kornia.core.check import KORNIA_CHECK_TYPE
from kornia.geometry.conversions import (
- QuaternionCoeffOrder,
angle_axis_to_quaternion,
normalize_quaternion,
quaternion_to_rotation_matrix,
@@ -241,7 +240,7 @@ class Quaternion(Module):
[0., 1., 0.],
[0., 0., 1.]], grad_fn=<ReshapeAliasBackward0>)
"""
- return quaternion_to_rotation_matrix(self.data, order=QuaternionCoeffOrder.WXYZ)
+ return quaternion_to_rotation_matrix(self.data)
@classmethod
def from_matrix(cls, matrix: Tensor) -> 'Quaternion':
@@ -257,7 +256,7 @@ class Quaternion(Module):
Parameter containing:
tensor([[1., 0., 0., 0.]], requires_grad=True)
"""
- return cls(rotation_matrix_to_quaternion(matrix, order=QuaternionCoeffOrder.WXYZ))
+ return cls(rotation_matrix_to_quaternion(matrix))
@classmethod
def from_axis_angle(cls, axis_angle: Tensor) -> 'Quaternion':
@@ -273,7 +272,7 @@ class Quaternion(Module):
Parameter containing:
tensor([[0.8776, 0.4794, 0.0000, 0.0000]], requires_grad=True)
"""
- return cls(angle_axis_to_quaternion(axis_angle, order=QuaternionCoeffOrder.WXYZ))
+ return cls(angle_axis_to_quaternion(axis_angle))
@classmethod
def identity(
diff --git a/kornia/nerf/camera_utils.py b/kornia/nerf/camera_utils.py
index 72482d881..f8e166f18 100755
--- a/kornia/nerf/camera_utils.py
+++ b/kornia/nerf/camera_utils.py
@@ -5,7 +5,7 @@ import torch
from kornia.core import Device, Tensor
from kornia.geometry.camera import PinholeCamera
-from kornia.geometry.conversions import QuaternionCoeffOrder, quaternion_to_rotation_matrix
+from kornia.geometry.conversions import quaternion_to_rotation_matrix
def parse_colmap_output(
@@ -94,7 +94,7 @@ def parse_colmap_output(
# Extrinsic
q = torch.tensor([qw, qx, qy, qz], device=device)
- R = quaternion_to_rotation_matrix(q, order=QuaternionCoeffOrder.WXYZ)
+ R = quaternion_to_rotation_matrix(q)
t = torch.tensor([tx, ty, tz], device=device)
extrinsic = torch.eye(4, device=device, dtype=dtype)
extrinsic[:3, :3] = R
diff --git a/test/geometry/liegroup/test_se3.py b/test/geometry/liegroup/test_se3.py
index 8bba0f4f9..6c336ce6f 100644
--- a/test/geometry/liegroup/test_se3.py
+++ b/test/geometry/liegroup/test_se3.py
@@ -1,7 +1,7 @@
import pytest
import torch
-from kornia.geometry.conversions import QuaternionCoeffOrder, euler_from_quaternion, rotation_matrix_to_quaternion
+from kornia.geometry.conversions import euler_from_quaternion, rotation_matrix_to_quaternion
from kornia.geometry.liegroup import Se3, So3
from kornia.geometry.quaternion import Quaternion
from kornia.geometry.vector import Vector3
@@ -201,7 +201,7 @@ class TestSe3(BaseTester):
def test_rot_x(self, device, dtype, batch_size):
x = self._make_rand_data(device, dtype, batch_size, dims=1).squeeze(-1)
se3 = Se3.rot_x(x)
- quat = rotation_matrix_to_quaternion(se3.so3.matrix(), order=QuaternionCoeffOrder.WXYZ)
+ quat = rotation_matrix_to_quaternion(se3.so3.matrix())
quat = Quaternion(quat)
roll, _, _ = euler_from_quaternion(*quat.coeffs)
self.assert_close(x, roll)
@@ -211,7 +211,7 @@ class TestSe3(BaseTester):
def test_rot_y(self, device, dtype, batch_size):
y = self._make_rand_data(device, dtype, batch_size, dims=1).squeeze(-1)
se3 = Se3.rot_y(y)
- quat = rotation_matrix_to_quaternion(se3.so3.matrix(), order=QuaternionCoeffOrder.WXYZ)
+ quat = rotation_matrix_to_quaternion(se3.so3.matrix())
quat = Quaternion(quat)
_, pitch, _ = euler_from_quaternion(*quat.coeffs)
self.assert_close(y, pitch)
@@ -221,7 +221,7 @@ class TestSe3(BaseTester):
def test_rot_z(self, device, dtype, batch_size):
z = self._make_rand_data(device, dtype, batch_size, dims=1).squeeze(-1)
se3 = Se3.rot_z(z)
- quat = rotation_matrix_to_quaternion(se3.so3.matrix(), order=QuaternionCoeffOrder.WXYZ)
+ quat = rotation_matrix_to_quaternion(se3.so3.matrix())
quat = Quaternion(quat)
_, _, yaw = euler_from_quaternion(*quat.coeffs)
self.assert_close(z, yaw)
diff --git a/test/geometry/test_conversions.py b/test/geometry/test_conversions.py
index c80a471fe..373835872 100644
--- a/test/geometry/test_conversions.py
+++ b/test/geometry/test_conversions.py
@@ -8,7 +8,6 @@ from torch.autograd import gradcheck
import kornia
from kornia.geometry.conversions import (
ARKitQTVecs_to_ColmapQTVecs,
- QuaternionCoeffOrder,
Rt_to_matrix4x4,
camtoworld_graphics_to_vision_4x4,
camtoworld_graphics_to_vision_Rt,
@@ -45,171 +44,72 @@ def rtol(device, dtype):
class TestAngleAxisToQuaternion:
- def test_smoke_xyzw(self, device, dtype):
- angle_axis = torch.zeros(3, dtype=dtype, device=device)
- with pytest.warns(UserWarning):
- quaternion = kornia.geometry.conversions.angle_axis_to_quaternion(
- angle_axis, order=QuaternionCoeffOrder.XYZW
- )
- assert quaternion.shape == (4,)
-
def test_smoke(self, device, dtype):
angle_axis = torch.zeros(3, dtype=dtype, device=device)
- quaternion = kornia.geometry.conversions.angle_axis_to_quaternion(angle_axis, order=QuaternionCoeffOrder.WXYZ)
+ quaternion = kornia.geometry.conversions.angle_axis_to_quaternion(angle_axis)
assert quaternion.shape == (4,)
- @pytest.mark.parametrize("batch_size", (1, 3, 8))
- def test_smoke_batch_xyzw(self, batch_size, device, dtype):
- angle_axis = torch.zeros(batch_size, 3, device=device, dtype=dtype)
- with pytest.warns(UserWarning):
- quaternion = kornia.geometry.conversions.angle_axis_to_quaternion(
- angle_axis, order=QuaternionCoeffOrder.XYZW
- )
- assert quaternion.shape == (batch_size, 4)
-
@pytest.mark.parametrize("batch_size", (1, 3, 8))
def test_smoke_batch(self, batch_size, device, dtype):
angle_axis = torch.zeros(batch_size, 3, device=device, dtype=dtype)
- quaternion = kornia.geometry.conversions.angle_axis_to_quaternion(angle_axis, order=QuaternionCoeffOrder.WXYZ)
+ quaternion = kornia.geometry.conversions.angle_axis_to_quaternion(angle_axis)
assert quaternion.shape == (batch_size, 4)
- def test_zero_angle_xyzw(self, device, dtype, atol, rtol):
- angle_axis = torch.tensor((0.0, 0.0, 0.0), device=device, dtype=dtype)
- expected = torch.tensor((0.0, 0.0, 0.0, 1.0), device=device, dtype=dtype)
- with pytest.warns(UserWarning):
- quaternion = kornia.geometry.conversions.angle_axis_to_quaternion(
- angle_axis, order=QuaternionCoeffOrder.XYZW
- )
- assert_close(quaternion, expected, atol=atol, rtol=rtol)
-
def test_zero_angle(self, device, dtype, atol, rtol):
angle_axis = torch.tensor((0.0, 0.0, 0.0), device=device, dtype=dtype)
expected = torch.tensor((1.0, 0.0, 0.0, 0.0), device=device, dtype=dtype)
- quaternion = kornia.geometry.conversions.angle_axis_to_quaternion(angle_axis, order=QuaternionCoeffOrder.WXYZ)
- assert_close(quaternion, expected, atol=atol, rtol=rtol)
-
- def test_small_angle_x_xyzw(self, device, dtype, atol, rtol):
- theta = 1.0e-2
- angle_axis = torch.tensor((theta, 0.0, 0.0), device=device, dtype=dtype)
- expected = torch.tensor((np.sin(theta / 2.0), 0.0, 0.0, np.cos(theta / 2.0)), device=device, dtype=dtype)
- with pytest.warns(UserWarning):
- quaternion = kornia.geometry.conversions.angle_axis_to_quaternion(
- angle_axis, order=QuaternionCoeffOrder.XYZW
- )
+ quaternion = kornia.geometry.conversions.angle_axis_to_quaternion(angle_axis)
assert_close(quaternion, expected, atol=atol, rtol=rtol)
def test_small_angle_x(self, device, dtype, atol, rtol):
theta = 1.0e-2
angle_axis = torch.tensor((theta, 0.0, 0.0), device=device, dtype=dtype)
expected = torch.tensor((np.cos(theta / 2.0), np.sin(theta / 2.0), 0.0, 0.0), device=device, dtype=dtype)
- quaternion = kornia.geometry.conversions.angle_axis_to_quaternion(angle_axis, order=QuaternionCoeffOrder.WXYZ)
- assert_close(quaternion, expected, atol=atol, rtol=rtol)
-
- def test_small_angle_y_xyzw(self, device, dtype, atol, rtol):
- theta = 1.0e-2
- angle_axis = torch.tensor((0.0, theta, 0.0), device=device, dtype=dtype)
- expected = torch.tensor((0.0, np.sin(theta / 2.0), 0.0, np.cos(theta / 2.0)), device=device, dtype=dtype)
- with pytest.warns(UserWarning):
- quaternion = kornia.geometry.conversions.angle_axis_to_quaternion(
- angle_axis, order=QuaternionCoeffOrder.XYZW
- )
+ quaternion = kornia.geometry.conversions.angle_axis_to_quaternion(angle_axis)
assert_close(quaternion, expected, atol=atol, rtol=rtol)
def test_small_angle_y(self, device, dtype, atol, rtol):
theta = 1.0e-2
angle_axis = torch.tensor((0.0, theta, 0.0), device=device, dtype=dtype)
expected = torch.tensor((np.cos(theta / 2.0), 0.0, np.sin(theta / 2.0), 0.0), device=device, dtype=dtype)
- quaternion = kornia.geometry.conversions.angle_axis_to_quaternion(angle_axis, order=QuaternionCoeffOrder.WXYZ)
- assert_close(quaternion, expected, atol=atol, rtol=rtol)
-
- def test_small_angle_z_xyzw(self, device, dtype, atol, rtol):
- theta = 1.0e-2
- angle_axis = torch.tensor((0.0, 0.0, theta), device=device, dtype=dtype)
- expected = torch.tensor((0.0, 0.0, np.sin(theta / 2.0), np.cos(theta / 2.0)), device=device, dtype=dtype)
- with pytest.warns(UserWarning):
- quaternion = kornia.geometry.conversions.angle_axis_to_quaternion(
- angle_axis, order=QuaternionCoeffOrder.XYZW
- )
+ quaternion = kornia.geometry.conversions.angle_axis_to_quaternion(angle_axis)
assert_close(quaternion, expected, atol=atol, rtol=rtol)
def test_small_angle_z(self, device, dtype, atol, rtol):
theta = 1.0e-2
angle_axis = torch.tensor((0.0, 0.0, theta), device=device, dtype=dtype)
expected = torch.tensor((np.cos(theta / 2.0), 0.0, 0.0, np.sin(theta / 2.0)), device=device, dtype=dtype)
- quaternion = kornia.geometry.conversions.angle_axis_to_quaternion(angle_axis, order=QuaternionCoeffOrder.WXYZ)
- assert_close(quaternion, expected, atol=atol, rtol=rtol)
-
- def test_x_rotation_xyzw(self, device, dtype, atol, rtol):
- half_sqrt2 = 0.5 * np.sqrt(2.0)
- angle_axis = torch.tensor((kornia.pi / 2.0, 0.0, 0.0), device=device, dtype=dtype)
- expected = torch.tensor((half_sqrt2, 0.0, 0.0, half_sqrt2), device=device, dtype=dtype)
- with pytest.warns(UserWarning):
- quaternion = kornia.geometry.conversions.angle_axis_to_quaternion(
- angle_axis, order=QuaternionCoeffOrder.XYZW
- )
+ quaternion = kornia.geometry.conversions.angle_axis_to_quaternion(angle_axis)
assert_close(quaternion, expected, atol=atol, rtol=rtol)
def test_x_rotation(self, device, dtype, atol, rtol):
half_sqrt2 = 0.5 * np.sqrt(2.0)
angle_axis = torch.tensor((kornia.pi / 2.0, 0.0, 0.0), device=device, dtype=dtype)
expected = torch.tensor((half_sqrt2, half_sqrt2, 0.0, 0.0), device=device, dtype=dtype)
- quaternion = kornia.geometry.conversions.angle_axis_to_quaternion(angle_axis, order=QuaternionCoeffOrder.WXYZ)
- assert_close(quaternion, expected, atol=atol, rtol=rtol)
-
- def test_y_rotation_xyzw(self, device, dtype, atol, rtol):
- half_sqrt2 = 0.5 * np.sqrt(2.0)
- angle_axis = torch.tensor((0.0, kornia.pi / 2.0, 0.0), device=device, dtype=dtype)
- expected = torch.tensor((0.0, half_sqrt2, 0.0, half_sqrt2), device=device, dtype=dtype)
- with pytest.warns(UserWarning):
- quaternion = kornia.geometry.conversions.angle_axis_to_quaternion(
- angle_axis, order=QuaternionCoeffOrder.XYZW
- )
+ quaternion = kornia.geometry.conversions.angle_axis_to_quaternion(angle_axis)
assert_close(quaternion, expected, atol=atol, rtol=rtol)
def test_y_rotation(self, device, dtype, atol, rtol):
half_sqrt2 = 0.5 * np.sqrt(2.0)
angle_axis = torch.tensor((0.0, kornia.pi / 2.0, 0.0), device=device, dtype=dtype)
expected = torch.tensor((half_sqrt2, 0.0, half_sqrt2, 0.0), device=device, dtype=dtype)
- quaternion = kornia.geometry.conversions.angle_axis_to_quaternion(angle_axis, order=QuaternionCoeffOrder.WXYZ)
- assert_close(quaternion, expected, atol=atol, rtol=rtol)
-
- def test_z_rotation_xyzw(self, device, dtype, atol, rtol):
- half_sqrt2 = 0.5 * np.sqrt(2.0)
- angle_axis = torch.tensor((0.0, 0.0, kornia.pi / 2.0), device=device, dtype=dtype)
- expected = torch.tensor((0.0, 0.0, half_sqrt2, half_sqrt2), device=device, dtype=dtype)
- with pytest.warns(UserWarning):
- quaternion = kornia.geometry.conversions.angle_axis_to_quaternion(
- angle_axis, order=QuaternionCoeffOrder.XYZW
- )
+ quaternion = kornia.geometry.conversions.angle_axis_to_quaternion(angle_axis)
assert_close(quaternion, expected, atol=atol, rtol=rtol)
def test_z_rotation(self, device, dtype, atol, rtol):
half_sqrt2 = 0.5 * np.sqrt(2.0)
angle_axis = torch.tensor((0.0, 0.0, kornia.pi / 2.0), device=device, dtype=dtype)
expected = torch.tensor((half_sqrt2, 0.0, 0.0, half_sqrt2), device=device, dtype=dtype)
- quaternion = kornia.geometry.conversions.angle_axis_to_quaternion(angle_axis, order=QuaternionCoeffOrder.WXYZ)
+ quaternion = kornia.geometry.conversions.angle_axis_to_quaternion(angle_axis)
assert_close(quaternion, expected, atol=atol, rtol=rtol)
- def test_gradcheck_xyzw(self, device, dtype):
- eps = torch.finfo(dtype).eps
- angle_axis = torch.tensor((0.0, 0.0, 0.0), device=device, dtype=dtype) + eps
- angle_axis = tensor_to_gradcheck_var(angle_axis)
- # evaluate function gradient
- with pytest.warns(UserWarning):
- assert gradcheck(
- partial(kornia.geometry.conversions.angle_axis_to_quaternion, order=QuaternionCoeffOrder.XYZW),
- (angle_axis,),
- raise_exception=True,
- fast_mode=True,
- )
-
def test_gradcheck(self, device, dtype):
eps = torch.finfo(dtype).eps
angle_axis = torch.tensor((0.0, 0.0, 0.0), device=device, dtype=dtype) + eps
angle_axis = tensor_to_gradcheck_var(angle_axis)
# evaluate function gradient
assert gradcheck(
- partial(kornia.geometry.conversions.angle_axis_to_quaternion, order=QuaternionCoeffOrder.WXYZ),
+ partial(kornia.geometry.conversions.angle_axis_to_quaternion),
(angle_axis,),
raise_exception=True,
fast_mode=True,
@@ -217,165 +117,69 @@ class TestAngleAxisToQuaternion:
class TestQuaternionToAngleAxis:
- def test_smoke_xyzw(self, device, dtype):
- quaternion = torch.zeros(4, device=device, dtype=dtype)
- with pytest.warns(UserWarning):
- angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(
- quaternion, order=QuaternionCoeffOrder.XYZW
- )
- assert angle_axis.shape == (3,)
-
def test_smoke(self, device, dtype):
quaternion = torch.zeros(4, device=device, dtype=dtype)
- angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(quaternion, order=QuaternionCoeffOrder.WXYZ)
+ angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(quaternion)
assert angle_axis.shape == (3,)
- @pytest.mark.parametrize("batch_size", (1, 3, 8))
- def test_smoke_batch_xyzw(self, batch_size, device, dtype):
- quaternion = torch.zeros(batch_size, 4, device=device, dtype=dtype)
- with pytest.warns(UserWarning):
- angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(
- quaternion, order=QuaternionCoeffOrder.XYZW
- )
- assert angle_axis.shape == (batch_size, 3)
-
@pytest.mark.parametrize("batch_size", (1, 3, 8))
def test_smoke_batch(self, batch_size, device, dtype):
quaternion = torch.zeros(batch_size, 4, device=device, dtype=dtype)
- angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(quaternion, order=QuaternionCoeffOrder.WXYZ)
+ angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(quaternion)
assert angle_axis.shape == (batch_size, 3)
- def test_unit_quaternion_xyzw(self, device, dtype, atol, rtol):
- quaternion = torch.tensor((0.0, 0.0, 0.0, 1.0), device=device, dtype=dtype)
- expected = torch.tensor((0.0, 0.0, 0.0), device=device, dtype=dtype)
- with pytest.warns(UserWarning):
- angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(
- quaternion, order=QuaternionCoeffOrder.XYZW
- )
- assert_close(angle_axis, expected, atol=atol, rtol=rtol)
-
def test_unit_quaternion(self, device, dtype, atol, rtol):
quaternion = torch.tensor((1.0, 0.0, 0.0, 0.0), device=device, dtype=dtype)
expected = torch.tensor((0.0, 0.0, 0.0), device=device, dtype=dtype)
- angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(quaternion, order=QuaternionCoeffOrder.WXYZ)
- assert_close(angle_axis, expected, atol=atol, rtol=rtol)
-
- def test_x_rotation_xyzw(self, device, dtype, atol, rtol):
- quaternion = torch.tensor((1.0, 0.0, 0.0, 0.0), device=device, dtype=dtype)
- expected = torch.tensor((kornia.pi, 0.0, 0.0), device=device, dtype=dtype)
- with pytest.warns(UserWarning):
- angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(
- quaternion, order=QuaternionCoeffOrder.XYZW
- )
+ angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(quaternion)
assert_close(angle_axis, expected, atol=atol, rtol=rtol)
def test_x_rotation(self, device, dtype, atol, rtol):
quaternion = torch.tensor((0.0, 1.0, 0.0, 0.0), device=device, dtype=dtype)
expected = torch.tensor((kornia.pi, 0.0, 0.0), device=device, dtype=dtype)
- angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(quaternion, order=QuaternionCoeffOrder.WXYZ)
- assert_close(angle_axis, expected, atol=atol, rtol=rtol)
-
- def test_y_rotation_xyzw(self, device, dtype, atol, rtol):
- quaternion = torch.tensor((0.0, 1.0, 0.0, 0.0), device=device, dtype=dtype)
- expected = torch.tensor((0.0, kornia.pi, 0.0), device=device, dtype=dtype)
- with pytest.warns(UserWarning):
- angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(
- quaternion, order=QuaternionCoeffOrder.XYZW
- )
+ angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(quaternion)
assert_close(angle_axis, expected, atol=atol, rtol=rtol)
def test_y_rotation(self, device, dtype, atol, rtol):
quaternion = torch.tensor((0.0, 0.0, 1.0, 0.0), device=device, dtype=dtype)
expected = torch.tensor((0.0, kornia.pi, 0.0), device=device, dtype=dtype)
- angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(quaternion, order=QuaternionCoeffOrder.WXYZ)
- assert_close(angle_axis, expected, atol=atol, rtol=rtol)
-
- def test_z_rotation_xyzw(self, device, dtype, atol, rtol):
- quaternion = torch.tensor((0.0, 0.0, 0.5, np.sqrt(3.0) / 2.0), device=device, dtype=dtype)
- expected = torch.tensor((0.0, 0.0, kornia.pi / 3.0), device=device, dtype=dtype)
- with pytest.warns(UserWarning):
- angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(
- quaternion, order=QuaternionCoeffOrder.XYZW
- )
+ angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(quaternion)
assert_close(angle_axis, expected, atol=atol, rtol=rtol)
def test_z_rotation(self, device, dtype, atol, rtol):
quaternion = torch.tensor((np.sqrt(3.0) / 2.0, 0.0, 0.0, 0.5), device=device, dtype=dtype)
expected = torch.tensor((0.0, 0.0, kornia.pi / 3.0), device=device, dtype=dtype)
- angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(quaternion, order=QuaternionCoeffOrder.WXYZ)
- assert_close(angle_axis, expected, atol=atol, rtol=rtol)
-
- def test_small_angle_x_xyzw(self, device, dtype, atol, rtol):
- theta = 1.0e-2
- quaternion = torch.tensor((np.sin(theta / 2.0), 0.0, 0.0, np.cos(theta / 2.0)), device=device, dtype=dtype)
- expected = torch.tensor((theta, 0.0, 0.0), device=device, dtype=dtype)
- with pytest.warns(UserWarning):
- angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(
- quaternion, order=QuaternionCoeffOrder.XYZW
- )
+ angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(quaternion)
assert_close(angle_axis, expected, atol=atol, rtol=rtol)
def test_small_angle_x(self, device, dtype, atol, rtol):
theta = 1.0e-2
quaternion = torch.tensor((np.cos(theta / 2.0), np.sin(theta / 2.0), 0.0, 0.0), device=device, dtype=dtype)
expected = torch.tensor((theta, 0.0, 0.0), device=device, dtype=dtype)
- angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(quaternion, order=QuaternionCoeffOrder.WXYZ)
- assert_close(angle_axis, expected, atol=atol, rtol=rtol)
-
- def test_small_angle_y_xyzw(self, device, dtype, atol, rtol):
- theta = 1.0e-2
- quaternion = torch.tensor((0.0, np.sin(theta / 2), 0.0, np.cos(theta / 2)), device=device, dtype=dtype)
- expected = torch.tensor((0.0, theta, 0.0), device=device, dtype=dtype)
- with pytest.warns(UserWarning):
- angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(
- quaternion, order=QuaternionCoeffOrder.XYZW
- )
+ angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(quaternion)
assert_close(angle_axis, expected, atol=atol, rtol=rtol)
def test_small_angle_y(self, device, dtype, atol, rtol):
theta = 1.0e-2
quaternion = torch.tensor((np.cos(theta / 2), 0.0, np.sin(theta / 2), 0.0), device=device, dtype=dtype)
expected = torch.tensor((0.0, theta, 0.0), device=device, dtype=dtype)
- angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(quaternion, order=QuaternionCoeffOrder.WXYZ)
- assert_close(angle_axis, expected, atol=atol, rtol=rtol)
-
- def test_small_angle_z_xyzw(self, device, dtype, atol, rtol):
- theta = 1.0e-2
- quaternion = torch.tensor((0.0, 0.0, np.sin(theta / 2), np.cos(theta / 2)), device=device, dtype=dtype)
- expected = torch.tensor((0.0, 0.0, theta), device=device, dtype=dtype)
- with pytest.warns(UserWarning):
- angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(
- quaternion, order=QuaternionCoeffOrder.XYZW
- )
+ angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(quaternion)
assert_close(angle_axis, expected, atol=atol, rtol=rtol)
def test_small_angle_z(self, device, dtype, atol, rtol):
theta = 1.0e-2
quaternion = torch.tensor((np.cos(theta / 2), 0.0, 0.0, np.sin(theta / 2)), device=device, dtype=dtype)
expected = torch.tensor((0.0, 0.0, theta), device=device, dtype=dtype)
- angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(quaternion, order=QuaternionCoeffOrder.WXYZ)
+ angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(quaternion)
assert_close(angle_axis, expected, atol=atol, rtol=rtol)
- def test_gradcheck_xyzw(self, device, dtype):
- eps = torch.finfo(dtype).eps
- quaternion = torch.tensor((0.0, 0.0, 0.0, 1.0), device=device, dtype=dtype) + eps
- quaternion = tensor_to_gradcheck_var(quaternion)
- # evaluate function gradient
- with pytest.warns(UserWarning):
- assert gradcheck(
- partial(kornia.geometry.conversions.quaternion_to_angle_axis, order=QuaternionCoeffOrder.XYZW),
- (quaternion,),
- raise_exception=True,
- fast_mode=True,
- )
-
def test_gradcheck(self, device, dtype):
eps = torch.finfo(dtype).eps
quaternion = torch.tensor((1.0, 0.0, 0.0, 0.0), device=device, dtype=dtype) + eps
quaternion = tensor_to_gradcheck_var(quaternion)
# evaluate function gradient
assert gradcheck(
- partial(kornia.geometry.conversions.quaternion_to_angle_axis, order=QuaternionCoeffOrder.WXYZ),
+ partial(kornia.geometry.conversions.quaternion_to_angle_axis),
(quaternion,),
raise_exception=True,
fast_mode=True,
@@ -383,97 +187,32 @@ class TestQuaternionToAngleAxis:
class TestRotationMatrixToQuaternion:
- @pytest.mark.parametrize("batch_size", (1, 3, 8))
- def test_smoke_batch_xyzw(self, batch_size, device, dtype):
- matrix = torch.zeros(batch_size, 3, 3, device=device, dtype=dtype)
- with pytest.warns(UserWarning):
- quaternion = kornia.geometry.conversions.rotation_matrix_to_quaternion(
- matrix, order=QuaternionCoeffOrder.XYZW
- )
- assert quaternion.shape == (batch_size, 4)
-
@pytest.mark.parametrize("batch_size", (1, 3, 8))
def test_smoke_batch(self, batch_size, device, dtype):
matrix = torch.zeros(batch_size, 3, 3, device=device, dtype=dtype)
- quaternion = kornia.geometry.conversions.rotation_matrix_to_quaternion(matrix, order=QuaternionCoeffOrder.WXYZ)
+ quaternion = kornia.geometry.conversions.rotation_matrix_to_quaternion(matrix)
assert quaternion.shape == (batch_size, 4)
- def test_identity_xyzw(self, device, dtype, atol, rtol):
- matrix = torch.tensor(((1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 1.0)), device=device, dtype=dtype)
- expected = torch.tensor((0.0, 0.0, 0.0, 1.0), device=device, dtype=dtype)
- with pytest.warns(UserWarning):
- quaternion = kornia.geometry.conversions.rotation_matrix_to_quaternion(
- matrix, order=QuaternionCoeffOrder.XYZW
- )
- assert_close(quaternion, expected, atol=atol, rtol=rtol)
-
def test_identity(self, device, dtype, atol, rtol):
matrix = torch.tensor(((1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 1.0)), device=device, dtype=dtype)
expected = torch.tensor((1.0, 0.0, 0.0, 0.0), device=device, dtype=dtype)
- quaternion = kornia.geometry.conversions.rotation_matrix_to_quaternion(matrix, order=QuaternionCoeffOrder.WXYZ)
- assert_close(quaternion, expected, atol=atol, rtol=rtol)
-
- def test_rot_x_45_xyzw(self, device, dtype, atol, rtol):
- matrix = torch.tensor(((1.0, 0.0, 0.0), (0.0, 0.0, -1.0), (0.0, 1.0, 0.0)), device=device, dtype=dtype)
- pi_half2 = torch.cos(kornia.pi / 4.0).to(device=device, dtype=dtype)
- expected = torch.tensor((pi_half2, 0.0, 0.0, pi_half2), device=device, dtype=dtype)
- with pytest.warns(UserWarning):
- quaternion = kornia.geometry.conversions.rotation_matrix_to_quaternion(
- matrix, order=QuaternionCoeffOrder.XYZW
- )
+ quaternion = kornia.geometry.conversions.rotation_matrix_to_quaternion(matrix)
assert_close(quaternion, expected, atol=atol, rtol=rtol)
def test_rot_x_45(self, device, dtype, atol, rtol):
matrix = torch.tensor(((1.0, 0.0, 0.0), (0.0, 0.0, -1.0), (0.0, 1.0, 0.0)), device=device, dtype=dtype)
pi_half2 = torch.cos(kornia.pi / 4.0).to(device=device, dtype=dtype)
expected = torch.tensor((pi_half2, pi_half2, 0.0, 0.0), device=device, dtype=dtype)
- quaternion = kornia.geometry.conversions.rotation_matrix_to_quaternion(matrix, order=QuaternionCoeffOrder.WXYZ)
+ quaternion = kornia.geometry.conversions.rotation_matrix_to_quaternion(matrix)
assert_close(quaternion, expected, atol=atol, rtol=rtol)
- def test_back_and_forth_xyzw(self, device, dtype, atol, rtol):
- eps = torch.finfo(dtype).eps
- matrix = torch.tensor(((1.0, 0.0, 0.0), (0.0, 0.0, -1.0), (0.0, 1.0, 0.0)), device=device, dtype=dtype)
- with pytest.warns(UserWarning):
- quaternion = kornia.geometry.conversions.rotation_matrix_to_quaternion(
- matrix, eps=eps, order=QuaternionCoeffOrder.XYZW
- )
- matrix_hat = kornia.geometry.conversions.quaternion_to_rotation_matrix(
- quaternion, order=QuaternionCoeffOrder.XYZW
- )
- assert_close(matrix, matrix_hat, atol=atol, rtol=rtol)
-
def test_back_and_forth(self, device, dtype, atol, rtol):
eps = torch.finfo(dtype).eps
matrix = torch.tensor(((1.0, 0.0, 0.0), (0.0, 0.0, -1.0), (0.0, 1.0, 0.0)), device=device, dtype=dtype)
- quaternion = kornia.geometry.conversions.rotation_matrix_to_quaternion(
- matrix, eps=eps, order=QuaternionCoeffOrder.WXYZ
- )
- matrix_hat = kornia.geometry.conversions.quaternion_to_rotation_matrix(
- quaternion, order=QuaternionCoeffOrder.WXYZ
- )
+ quaternion = kornia.geometry.conversions.rotation_matrix_to_quaternion(matrix, eps=eps)
+ matrix_hat = kornia.geometry.conversions.quaternion_to_rotation_matrix(quaternion)
assert_close(matrix, matrix_hat, atol=atol, rtol=rtol)
- def test_corner_case_xyzw(self, device, dtype, atol, rtol):
- eps = torch.finfo(dtype).eps
- matrix = torch.tensor(
- (
- (-0.7799533010, -0.5432914495, 0.3106555045),
- (0.0492402576, -0.5481169224, -0.8349509239),
- (0.6238971353, -0.6359263659, 0.4542570710),
- ),
- device=device,
- dtype=dtype,
- )
- quaternion_true = torch.tensor(
- (0.280136495828629, -0.440902262926102, 0.834015488624573, 0.177614107728004), device=device, dtype=dtype
- )
- with pytest.warns(UserWarning):
- quaternion = kornia.geometry.conversions.rotation_matrix_to_quaternion(
- matrix, eps=eps, order=QuaternionCoeffOrder.XYZW
- )
- torch.set_printoptions(precision=10)
- assert_close(quaternion_true, quaternion, atol=atol, rtol=rtol)
-
def test_corner_case(self, device, dtype, atol, rtol):
eps = torch.finfo(dtype).eps
matrix = torch.tensor(
@@ -488,211 +227,98 @@ class TestRotationMatrixToQuaternion:
quaternion_true = torch.tensor(
(0.177614107728004, 0.280136495828629, -0.440902262926102, 0.834015488624573), device=device, dtype=dtype
)
- quaternion = kornia.geometry.conversions.rotation_matrix_to_quaternion(
- matrix, eps=eps, order=QuaternionCoeffOrder.WXYZ
- )
+ quaternion = kornia.geometry.conversions.rotation_matrix_to_quaternion(matrix, eps=eps)
torch.set_printoptions(precision=10)
assert_close(quaternion_true, quaternion, atol=atol, rtol=rtol)
- def test_gradcheck_xyzw(self, device, dtype):
- eps = torch.finfo(dtype).eps
- matrix = torch.eye(3, device=device, dtype=dtype)
- matrix = tensor_to_gradcheck_var(matrix)
- # evaluate function gradient
- with pytest.warns(UserWarning):
- assert gradcheck(
- partial(
- kornia.geometry.conversions.rotation_matrix_to_quaternion, eps=eps, order=QuaternionCoeffOrder.XYZW
- ),
- (matrix,),
- raise_exception=True,
- fast_mode=True,
- )
-
def test_gradcheck(self, device, dtype):
eps = torch.finfo(dtype).eps
matrix = torch.eye(3, device=device, dtype=dtype)
matrix = tensor_to_gradcheck_var(matrix)
# evaluate function gradient
assert gradcheck(
- partial(
- kornia.geometry.conversions.rotation_matrix_to_quaternion, eps=eps, order=QuaternionCoeffOrder.WXYZ
- ),
+ partial(kornia.geometry.conversions.rotation_matrix_to_quaternion, eps=eps),
(matrix,),
raise_exception=True,
fast_mode=True,
)
- @pytest.mark.parametrize('order', ['wxyz', 'xyzw'])
- def test_dynamo(self, device, dtype, torch_optimizer, order):
+ def test_dynamo(self, device, dtype, torch_optimizer):
quaternion = torch.tensor((0.0, 0.0, 1.0), device=device, dtype=dtype)
op = kornia.geometry.conversions.quaternion_log_to_exp
op_optimized = torch_optimizer(op)
- actual = op_optimized(quaternion, order=QuaternionCoeffOrder(order))
- expected = op(quaternion, order=QuaternionCoeffOrder(order))
+ actual = op_optimized(quaternion)
+ expected = op(quaternion)
assert_close(actual, expected)
class TestQuaternionToRotationMatrix:
- @pytest.mark.parametrize("batch_dims", ((), (1,), (3,), (8,), (1, 1), (5, 6)))
- def test_smoke_batch_xyzw(self, batch_dims, device, dtype):
- quaternion = torch.zeros(*batch_dims, 4, device=device, dtype=dtype)
- with pytest.warns(UserWarning):
- matrix = kornia.geometry.conversions.quaternion_to_rotation_matrix(
- quaternion, order=QuaternionCoeffOrder.XYZW
- )
- assert matrix.shape == (*batch_dims, 3, 3)
-
@pytest.mark.parametrize("batch_dims", ((), (1,), (3,), (8,), (1, 1), (5, 6)))
def test_smoke_batch(self, batch_dims, device, dtype):
quaternion = torch.zeros(*batch_dims, 4, device=device, dtype=dtype)
- matrix = kornia.geometry.conversions.quaternion_to_rotation_matrix(quaternion, order=QuaternionCoeffOrder.WXYZ)
+ matrix = kornia.geometry.conversions.quaternion_to_rotation_matrix(quaternion)
assert matrix.shape == (*batch_dims, 3, 3)
- def test_unit_quaternion_xyzw(self, device, dtype, atol, rtol):
- quaternion = torch.tensor((0.0, 0.0, 0.0, 1.0), device=device, dtype=dtype)
- expected = torch.tensor(((1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 1.0)), device=device, dtype=dtype)
- with pytest.warns(UserWarning):
- matrix = kornia.geometry.conversions.quaternion_to_rotation_matrix(
- quaternion, order=QuaternionCoeffOrder.XYZW
- )
- assert_close(matrix, expected, atol=atol, rtol=rtol)
-
def test_unit_quaternion(self, device, dtype, atol, rtol):
quaternion = torch.tensor((1.0, 0.0, 0.0, 0.0), device=device, dtype=dtype)
expected = torch.tensor(((1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 1.0)), device=device, dtype=dtype)
- matrix = kornia.geometry.conversions.quaternion_to_rotation_matrix(quaternion, order=QuaternionCoeffOrder.WXYZ)
- assert_close(matrix, expected, atol=atol, rtol=rtol)
-
- def test_x_rotation_xyzw(self, device, dtype, atol, rtol):
- quaternion = torch.tensor((1.0, 0.0, 0.0, 0.0), device=device, dtype=dtype)
- expected = torch.tensor(((1.0, 0.0, 0.0), (0.0, -1.0, 0.0), (0.0, 0.0, -1.0)), device=device, dtype=dtype)
- with pytest.warns(UserWarning):
- matrix = kornia.geometry.conversions.quaternion_to_rotation_matrix(
- quaternion, order=QuaternionCoeffOrder.XYZW
- )
+ matrix = kornia.geometry.conversions.quaternion_to_rotation_matrix(quaternion)
assert_close(matrix, expected, atol=atol, rtol=rtol)
def test_x_rotation(self, device, dtype, atol, rtol):
quaternion = torch.tensor((0.0, 1.0, 0.0, 0.0), device=device, dtype=dtype)
expected = torch.tensor(((1.0, 0.0, 0.0), (0.0, -1.0, 0.0), (0.0, 0.0, -1.0)), device=device, dtype=dtype)
- matrix = kornia.geometry.conversions.quaternion_to_rotation_matrix(quaternion, order=QuaternionCoeffOrder.WXYZ)
- assert_close(matrix, expected, atol=atol, rtol=rtol)
-
- def test_y_rotation_xyzw(self, device, dtype, atol, rtol):
- quaternion = torch.tensor((0.0, 1.0, 0.0, 0.0), device=device, dtype=dtype)
- expected = torch.tensor(((-1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, -1.0)), device=device, dtype=dtype)
-
- with pytest.warns(UserWarning):
- matrix = kornia.geometry.conversions.quaternion_to_rotation_matrix(
- quaternion, order=QuaternionCoeffOrder.XYZW
- )
+ matrix = kornia.geometry.conversions.quaternion_to_rotation_matrix(quaternion)
assert_close(matrix, expected, atol=atol, rtol=rtol)
def test_y_rotation(self, device, dtype, atol, rtol):
quaternion = torch.tensor((0.0, 0.0, 1.0, 0.0), device=device, dtype=dtype)
expected = torch.tensor(((-1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, -1.0)), device=device, dtype=dtype)
- matrix = kornia.geometry.conversions.quaternion_to_rotation_matrix(quaternion, order=QuaternionCoeffOrder.WXYZ)
- assert_close(matrix, expected, atol=atol, rtol=rtol)
-
- def test_z_rotation_xyzw(self, device, dtype, atol, rtol):
- quaternion = torch.tensor((0.0, 0.0, 1.0, 0.0), device=device, dtype=dtype)
- expected = torch.tensor(((-1.0, 0.0, 0.0), (0.0, -1.0, 0.0), (0.0, 0.0, 1.0)), device=device, dtype=dtype)
- with pytest.warns(UserWarning):
- matrix = kornia.geometry.conversions.quaternion_to_rotation_matrix(
- quaternion, order=QuaternionCoeffOrder.XYZW
- )
+ matrix = kornia.geometry.conversions.quaternion_to_rotation_matrix(quaternion)
assert_close(matrix, expected, atol=atol, rtol=rtol)
def test_z_rotation(self, device, dtype, atol, rtol):
quaternion = torch.tensor((0.0, 0.0, 0.0, 1.0), device=device, dtype=dtype)
expected = torch.tensor(((-1.0, 0.0, 0.0), (0.0, -1.0, 0.0), (0.0, 0.0, 1.0)), device=device, dtype=dtype)
- matrix = kornia.geometry.conversions.quaternion_to_rotation_matrix(quaternion, order=QuaternionCoeffOrder.WXYZ)
+ matrix = kornia.geometry.conversions.quaternion_to_rotation_matrix(quaternion)
assert_close(matrix, expected, atol=atol, rtol=rtol)
- def test_gradcheck_xyzw(self, device, dtype):
- quaternion = torch.tensor((0.0, 0.0, 0.0, 1.0), device=device, dtype=dtype)
- quaternion = tensor_to_gradcheck_var(quaternion)
- # evaluate function gradient
- with pytest.warns(UserWarning):
- assert gradcheck(
- partial(kornia.geometry.conversions.quaternion_to_rotation_matrix, order=QuaternionCoeffOrder.XYZW),
- (quaternion,),
- raise_exception=True,
- fast_mode=True,
- )
-
def test_gradcheck(self, device, dtype):
quaternion = torch.tensor((0.0, 0.0, 0.0, 1.0), device=device, dtype=dtype)
quaternion = tensor_to_gradcheck_var(quaternion)
# evaluate function gradient
assert gradcheck(
- partial(kornia.geometry.conversions.quaternion_to_rotation_matrix, order=QuaternionCoeffOrder.WXYZ),
+ partial(kornia.geometry.conversions.quaternion_to_rotation_matrix),
(quaternion,),
raise_exception=True,
fast_mode=True,
)
- @pytest.mark.parametrize('order', ['wxyz', 'xyzw'])
- def test_dynamo(self, device, dtype, torch_optimizer, order):
+ def test_dynamo(self, device, dtype, torch_optimizer):
quaternion = torch.tensor((0.0, 0.0, 1.0, 0.0), device=device, dtype=dtype)
op = kornia.geometry.conversions.quaternion_to_rotation_matrix
op_optimized = torch_optimizer(op)
- actual = op_optimized(quaternion, order=QuaternionCoeffOrder(order))
- expected = op(quaternion, order=QuaternionCoeffOrder(order))
+ actual = op_optimized(quaternion)
+ expected = op(quaternion)
assert_close(actual, expected)
class TestQuaternionLogToExp:
- @pytest.mark.parametrize("batch_size", (1, 3, 8))
- def test_smoke_batch_xyzw(self, batch_size, device, dtype):
- quaternion_log = torch.zeros(batch_size, 3, device=device, dtype=dtype)
- with pytest.warns(UserWarning):
- quaternion_exp = kornia.geometry.conversions.quaternion_log_to_exp(
- quaternion_log, order=QuaternionCoeffOrder.XYZW
- )
- assert quaternion_exp.shape == (batch_size, 4)
-
@pytest.mark.parametrize("batch_size", (1, 3, 8))
def test_smoke_batch(self, batch_size, device, dtype):
quaternion_log = torch.zeros(batch_size, 3, device=device, dtype=dtype)
- quaternion_exp = kornia.geometry.conversions.quaternion_log_to_exp(
- quaternion_log, order=QuaternionCoeffOrder.WXYZ
- )
+ quaternion_exp = kornia.geometry.conversions.quaternion_log_to_exp(quaternion_log)
assert quaternion_exp.shape == (batch_size, 4)
- def test_unit_quaternion_xyzw(self, device, dtype, atol, rtol):
- eps = torch.finfo(dtype).eps
- quaternion_log = torch.tensor((0.0, 0.0, 0.0), device=device, dtype=dtype)
- expected = torch.tensor((0.0, 0.0, 0.0, 1.0), device=device, dtype=dtype)
- with pytest.warns(UserWarning):
- quaternion_exp = kornia.geometry.conversions.quaternion_log_to_exp(
- quaternion_log, eps=eps, order=QuaternionCoeffOrder.XYZW
- )
- assert_close(quaternion_exp, expected, atol=atol, rtol=rtol)
-
def test_unit_quaternion(self, device, dtype, atol, rtol):
eps = torch.finfo(dtype).eps
quaternion_log = torch.tensor((0.0, 0.0, 0.0), device=device, dtype=dtype)
expected = torch.tensor((1.0, 0.0, 0.0, 0.0), device=device, dtype=dtype)
- quaternion_exp = kornia.geometry.conversions.quaternion_log_to_exp(
- quaternion_log, eps=eps, order=QuaternionCoeffOrder.WXYZ
- )
- assert_close(quaternion_exp, expected, atol=atol, rtol=rtol)
-
- def test_pi_quaternion_x_xyzw(self, device, dtype, atol, rtol):
- eps = torch.finfo(dtype).eps
- one = torch.tensor(1.0, device=device, dtype=dtype)
- quaternion_log = torch.tensor((1.0, 0.0, 0.0), device=device, dtype=dtype)
- expected = torch.tensor((torch.sin(one), 0.0, 0.0, torch.cos(one)), device=device, dtype=dtype)
- with pytest.warns(UserWarning):
- quaternion_exp = kornia.geometry.conversions.quaternion_log_to_exp(
- quaternion_log, eps=eps, order=QuaternionCoeffOrder.XYZW
- )
+ quaternion_exp = kornia.geometry.conversions.quaternion_log_to_exp(quaternion_log, eps=eps)
assert_close(quaternion_exp, expected, atol=atol, rtol=rtol)
def test_pi_quaternion_x(self, device, dtype, atol, rtol):
@@ -700,20 +326,7 @@ class TestQuaternionLogToExp:
one = torch.tensor(1.0, device=device, dtype=dtype)
quaternion_log = torch.tensor((1.0, 0.0, 0.0), device=device, dtype=dtype)
expected = torch.tensor((torch.cos(one), torch.sin(one), 0.0, 0.0), device=device, dtype=dtype)
- quaternion_exp = kornia.geometry.conversions.quaternion_log_to_exp(
- quaternion_log, eps=eps, order=QuaternionCoeffOrder.WXYZ
- )
- assert_close(quaternion_exp, expected, atol=atol, rtol=rtol)
-
- def test_pi_quaternion_y_xyzw(self, device, dtype, atol, rtol):
- eps = torch.finfo(dtype).eps
- one = torch.tensor(1.0, device=device, dtype=dtype)
- quaternion_log = torch.tensor((0.0, 1.0, 0.0), device=device, dtype=dtype)
- expected = torch.tensor((0.0, torch.sin(one), 0.0, torch.cos(one)), device=device, dtype=dtype)
- with pytest.warns(UserWarning):
- quaternion_exp = kornia.geometry.conversions.quaternion_log_to_exp(
- quaternion_log, eps=eps, order=QuaternionCoeffOrder.XYZW
- )
+ quaternion_exp = kornia.geometry.conversions.quaternion_log_to_exp(quaternion_log, eps=eps)
assert_close(quaternion_exp, expected, atol=atol, rtol=rtol)
def test_pi_quaternion_y(self, device, dtype, atol, rtol):
@@ -721,21 +334,7 @@ class TestQuaternionLogToExp:
one = torch.tensor(1.0, device=device, dtype=dtype)
quaternion_log = torch.tensor((0.0, 1.0, 0.0), device=device, dtype=dtype)
expected = torch.tensor((torch.cos(one), 0.0, torch.sin(one), 0.0), device=device, dtype=dtype)
- quaternion_exp = kornia.geometry.conversions.quaternion_log_to_exp(
- quaternion_log, eps=eps, order=QuaternionCoeffOrder.WXYZ
- )
- assert_close(quaternion_exp, expected, atol=atol, rtol=rtol)
-
- def test_pi_quaternion_z_xyzw(self, device, dtype, atol, rtol):
- eps = torch.finfo(dtype).eps
- one = torch.tensor(1.0, device=device, dtype=dtype)
- quaternion_log = torch.tensor((0.0, 0.0, 1.0), device=device, dtype=dtype)
- expected = torch.tensor((0.0, 0.0, torch.sin(one), torch.cos(one)), device=device, dtype=dtype)
-
- with pytest.warns(UserWarning):
- quaternion_exp = kornia.geometry.conversions.quaternion_log_to_exp(
- quaternion_log, eps=eps, order=QuaternionCoeffOrder.XYZW
- )
+ quaternion_exp = kornia.geometry.conversions.quaternion_log_to_exp(quaternion_log, eps=eps)
assert_close(quaternion_exp, expected, atol=atol, rtol=rtol)
def test_pi_quaternion_z(self, device, dtype, atol, rtol):
@@ -743,228 +342,102 @@ class TestQuaternionLogToExp:
one = torch.tensor(1.0, device=device, dtype=dtype)
quaternion_log = torch.tensor((0.0, 0.0, 1.0), device=device, dtype=dtype)
expected = torch.tensor((torch.cos(one), 0.0, 0.0, torch.sin(one)), device=device, dtype=dtype)
- quaternion_exp = kornia.geometry.conversions.quaternion_log_to_exp(
- quaternion_log, eps=eps, order=QuaternionCoeffOrder.WXYZ
- )
+ quaternion_exp = kornia.geometry.conversions.quaternion_log_to_exp(quaternion_log, eps=eps)
assert_close(quaternion_exp, expected, atol=atol, rtol=rtol)
- def test_back_and_forth_xyzw(self, device, dtype, atol, rtol):
- eps = torch.finfo(dtype).eps
- quaternion_log = torch.tensor((1.0, 0.0, 0.0), device=device, dtype=dtype)
-
- with pytest.warns(UserWarning):
- quaternion_exp = kornia.geometry.conversions.quaternion_log_to_exp(
- quaternion_log, eps=eps, order=QuaternionCoeffOrder.XYZW
- )
- with pytest.warns(UserWarning):
- quaternion_log_hat = kornia.geometry.conversions.quaternion_exp_to_log(
- quaternion_exp, eps=eps, order=QuaternionCoeffOrder.XYZW
- )
- assert_close(quaternion_log, quaternion_log_hat, atol=atol, rtol=rtol)
-
def test_back_and_forth(self, device, dtype, atol, rtol):
eps = torch.finfo(dtype).eps
quaternion_log = torch.tensor((1.0, 0.0, 0.0), device=device, dtype=dtype)
- quaternion_exp = kornia.geometry.conversions.quaternion_log_to_exp(
- quaternion_log, eps=eps, order=QuaternionCoeffOrder.WXYZ
- )
- quaternion_log_hat = kornia.geometry.conversions.quaternion_exp_to_log(
- quaternion_exp, eps=eps, order=QuaternionCoeffOrder.WXYZ
- )
+ quaternion_exp = kornia.geometry.conversions.quaternion_log_to_exp(quaternion_log, eps=eps)
+ quaternion_log_hat = kornia.geometry.conversions.quaternion_exp_to_log(quaternion_exp, eps=eps)
assert_close(quaternion_log, quaternion_log_hat, atol=atol, rtol=rtol)
- def test_gradcheck_xyzw(self, device, dtype):
- eps = torch.finfo(dtype).eps
- quaternion = torch.tensor((0.0, 0.0, 1.0), device=device, dtype=dtype)
- quaternion = tensor_to_gradcheck_var(quaternion)
- # evaluate function gradient
- with pytest.warns(UserWarning):
- assert gradcheck(
- partial(kornia.geometry.conversions.quaternion_log_to_exp, eps=eps, order=QuaternionCoeffOrder.XYZW),
- (quaternion,),
- raise_exception=True,
- fast_mode=True,
- )
-
def test_gradcheck(self, device, dtype):
eps = torch.finfo(dtype).eps
quaternion = torch.tensor((0.0, 0.0, 1.0), device=device, dtype=dtype)
quaternion = tensor_to_gradcheck_var(quaternion)
# evaluate function gradient
assert gradcheck(
- partial(kornia.geometry.conversions.quaternion_log_to_exp, eps=eps, order=QuaternionCoeffOrder.WXYZ),
+ partial(kornia.geometry.conversions.quaternion_log_to_exp, eps=eps),
(quaternion,),
raise_exception=True,
fast_mode=True,
)
- @pytest.mark.parametrize('order', ['wxyz', 'xyzw'])
- def test_dynamo(self, device, dtype, torch_optimizer, order):
+ def test_dynamo(self, device, dtype, torch_optimizer):
quaternion = torch.tensor((0.0, 0.0, 1.0), device=device, dtype=dtype)
op = kornia.geometry.conversions.quaternion_log_to_exp
op_optimized = torch_optimizer(op)
- actual = op_optimized(quaternion, order=QuaternionCoeffOrder(order))
- expected = op(quaternion, order=QuaternionCoeffOrder(order))
+ actual = op_optimized(quaternion)
+ expected = op(quaternion)
assert_close(actual, expected)
class TestQuaternionExpToLog:
- @pytest.mark.parametrize("batch_size", (1, 3, 8))
- def test_smoke_batch_xyzw(self, batch_size, device, dtype):
- eps = torch.finfo(dtype).eps
- quaternion_exp = torch.zeros(batch_size, 4, device=device, dtype=dtype)
- with pytest.warns(UserWarning):
- quaternion_log = kornia.geometry.conversions.quaternion_exp_to_log(
- quaternion_exp, eps=eps, order=QuaternionCoeffOrder.XYZW
- )
- assert quaternion_log.shape == (batch_size, 3)
-
@pytest.mark.parametrize("batch_size", (1, 3, 8))
def test_smoke_batch(self, batch_size, device, dtype):
eps = torch.finfo(dtype).eps
quaternion_exp = torch.zeros(batch_size, 4, device=device, dtype=dtype)
- quaternion_log = kornia.geometry.conversions.quaternion_exp_to_log(
- quaternion_exp, eps=eps, order=QuaternionCoeffOrder.WXYZ
- )
+ quaternion_log = kornia.geometry.conversions.quaternion_exp_to_log(quaternion_exp, eps=eps)
assert quaternion_log.shape == (batch_size, 3)
- def test_unit_quaternion_xyzw(self, device, dtype, atol, rtol):
- eps = torch.finfo(dtype).eps
- quaternion_exp = torch.tensor((0.0, 0.0, 0.0, 1.0), device=device, dtype=dtype)
- expected = torch.tensor((0.0, 0.0, 0.0), device=device, dtype=dtype)
- with pytest.warns(UserWarning):
- quaternion_log = kornia.geometry.conversions.quaternion_exp_to_log(
- quaternion_exp, eps=eps, order=QuaternionCoeffOrder.XYZW
- )
- assert_close(quaternion_log, expected, atol=atol, rtol=rtol)
-
def test_unit_quaternion(self, device, dtype, atol, rtol):
eps = torch.finfo(dtype).eps
quaternion_exp = torch.tensor((1.0, 0.0, 0.0, 0.0), device=device, dtype=dtype)
expected = torch.tensor((0.0, 0.0, 0.0), device=device, dtype=dtype)
- quaternion_log = kornia.geometry.conversions.quaternion_exp_to_log(
- quaternion_exp, eps=eps, order=QuaternionCoeffOrder.WXYZ
- )
- assert_close(quaternion_log, expected, atol=atol, rtol=rtol)
-
- def test_pi_quaternion_x_xyzw(self, device, dtype, atol, rtol):
- eps = torch.finfo(dtype).eps
- quaternion_exp = torch.tensor((1.0, 0.0, 0.0, 0.0), device=device, dtype=dtype)
- expected = torch.tensor((kornia.pi / 2.0, 0.0, 0.0), device=device, dtype=dtype)
- with pytest.warns(UserWarning):
- quaternion_log = kornia.geometry.conversions.quaternion_exp_to_log(
- quaternion_exp, eps=eps, order=QuaternionCoeffOrder.XYZW
- )
+ quaternion_log = kornia.geometry.conversions.quaternion_exp_to_log(quaternion_exp, eps=eps)
assert_close(quaternion_log, expected, atol=atol, rtol=rtol)
def test_pi_quaternion_x(self, device, dtype, atol, rtol):
eps = torch.finfo(dtype).eps
quaternion_exp = torch.tensor((0.0, 1.0, 0.0, 0.0), device=device, dtype=dtype)
expected = torch.tensor((kornia.pi / 2.0, 0.0, 0.0), device=device, dtype=dtype)
- quaternion_log = kornia.geometry.conversions.quaternion_exp_to_log(
- quaternion_exp, eps=eps, order=QuaternionCoeffOrder.WXYZ
- )
- assert_close(quaternion_log, expected, atol=atol, rtol=rtol)
-
- def test_pi_quaternion_y_xyzw(self, device, dtype, atol, rtol):
- eps = torch.finfo(dtype).eps
- quaternion_exp = torch.tensor((0.0, 1.0, 0.0, 0.0), device=device, dtype=dtype)
- expected = torch.tensor((0.0, kornia.pi / 2.0, 0.0), device=device, dtype=dtype)
- with pytest.warns(UserWarning):
- quaternion_log = kornia.geometry.conversions.quaternion_exp_to_log(
- quaternion_exp, eps=eps, order=QuaternionCoeffOrder.XYZW
- )
+ quaternion_log = kornia.geometry.conversions.quaternion_exp_to_log(quaternion_exp, eps=eps)
assert_close(quaternion_log, expected, atol=atol, rtol=rtol)
def test_pi_quaternion_y(self, device, dtype, atol, rtol):
eps = torch.finfo(dtype).eps
quaternion_exp = torch.tensor((0.0, 0.0, 1.0, 0.0), device=device, dtype=dtype)
expected = torch.tensor((0.0, kornia.pi / 2.0, 0.0), device=device, dtype=dtype)
- quaternion_log = kornia.geometry.conversions.quaternion_exp_to_log(
- quaternion_exp, eps=eps, order=QuaternionCoeffOrder.WXYZ
- )
- assert_close(quaternion_log, expected, atol=atol, rtol=rtol)
-
- def test_pi_quaternion_z_xyzw(self, device, dtype, atol, rtol):
- eps = torch.finfo(dtype).eps
- quaternion_exp = torch.tensor((0.0, 0.0, 1.0, 0.0), device=device, dtype=dtype)
- expected = torch.tensor((0.0, 0.0, kornia.pi / 2.0), device=device, dtype=dtype)
- with pytest.warns(UserWarning):
- quaternion_log = kornia.geometry.conversions.quaternion_exp_to_log(
- quaternion_exp, eps=eps, order=QuaternionCoeffOrder.XYZW
- )
+ quaternion_log = kornia.geometry.conversions.quaternion_exp_to_log(quaternion_exp, eps=eps)
assert_close(quaternion_log, expected, atol=atol, rtol=rtol)
def test_pi_quaternion_z(self, device, dtype, atol, rtol):
eps = torch.finfo(dtype).eps
quaternion_exp = torch.tensor((0.0, 0.0, 0.0, 1.0), device=device, dtype=dtype)
expected = torch.tensor((0.0, 0.0, kornia.pi / 2.0), device=device, dtype=dtype)
- quaternion_log = kornia.geometry.conversions.quaternion_exp_to_log(
- quaternion_exp, eps=eps, order=QuaternionCoeffOrder.WXYZ
- )
+ quaternion_log = kornia.geometry.conversions.quaternion_exp_to_log(quaternion_exp, eps=eps)
assert_close(quaternion_log, expected, atol=atol, rtol=rtol)
- def test_back_and_forth_xyzw(self, device, dtype, atol, rtol):
- eps = torch.finfo(dtype).eps
- quaternion_exp = torch.tensor((1.0, 0.0, 0.0, 0.0), device=device, dtype=dtype)
-
- with pytest.warns(UserWarning):
- quaternion_log = kornia.geometry.conversions.quaternion_exp_to_log(
- quaternion_exp, eps=eps, order=QuaternionCoeffOrder.XYZW
- )
- with pytest.warns(UserWarning):
- quaternion_exp_hat = kornia.geometry.conversions.quaternion_log_to_exp(
- quaternion_log, eps=eps, order=QuaternionCoeffOrder.XYZW
- )
- assert_close(quaternion_exp, quaternion_exp_hat, atol=atol, rtol=rtol)
-
def test_back_and_forth(self, device, dtype, atol, rtol):
eps = torch.finfo(dtype).eps
quaternion_exp = torch.tensor((0.0, 1.0, 0.0, 0.0), device=device, dtype=dtype)
- quaternion_log = kornia.geometry.conversions.quaternion_exp_to_log(
- quaternion_exp, eps=eps, order=QuaternionCoeffOrder.WXYZ
- )
- quaternion_exp_hat = kornia.geometry.conversions.quaternion_log_to_exp(
- quaternion_log, eps=eps, order=QuaternionCoeffOrder.WXYZ
- )
+ quaternion_log = kornia.geometry.conversions.quaternion_exp_to_log(quaternion_exp, eps=eps)
+ quaternion_exp_hat = kornia.geometry.conversions.quaternion_log_to_exp(quaternion_log, eps=eps)
assert_close(quaternion_exp, quaternion_exp_hat, atol=atol, rtol=rtol)
- def test_gradcheck_xyzw(self, device, dtype):
- eps = torch.finfo(dtype).eps
- quaternion = torch.tensor((1.0, 0.0, 0.0, 0.0), device=device, dtype=dtype)
- quaternion = tensor_to_gradcheck_var(quaternion)
- # evaluate function gradient
- with pytest.warns(UserWarning):
- assert gradcheck(
- partial(kornia.geometry.conversions.quaternion_exp_to_log, eps=eps, order=QuaternionCoeffOrder.XYZW),
- (quaternion,),
- raise_exception=True,
- fast_mode=True,
- )
-
def test_gradcheck(self, device, dtype):
eps = torch.finfo(dtype).eps
quaternion = torch.tensor((0.0, 1.0, 0.0, 0.0), device=device, dtype=dtype)
quaternion = tensor_to_gradcheck_var(quaternion)
# evaluate function gradient
assert gradcheck(
- partial(kornia.geometry.conversions.quaternion_exp_to_log, eps=eps, order=QuaternionCoeffOrder.WXYZ),
+ partial(kornia.geometry.conversions.quaternion_exp_to_log, eps=eps),
(quaternion,),
raise_exception=True,
fast_mode=True,
)
- @pytest.mark.parametrize('order', ['wxyz', 'xyzw'])
- def test_dynamo(self, device, dtype, torch_optimizer, order):
+ def test_dynamo(self, device, dtype, torch_optimizer):
quaternion = torch.tensor((0.0, 0.0, 1.0, 0.0), device=device, dtype=dtype)
op = kornia.geometry.conversions.quaternion_exp_to_log
op_optimized = torch_optimizer(op)
- actual = op_optimized(quaternion, order=QuaternionCoeffOrder(order))
- expected = op(quaternion, order=QuaternionCoeffOrder(order))
+ actual = op_optimized(quaternion)
+ expected = op(quaternion)
assert_close(actual, expected)
@@ -1025,9 +498,7 @@ class TestRotationMatrixToAngleAxis:
# generate input data
quaternion = torch.rand(batch_size, 4, device=device, dtype=dtype)
quaternion = kornia.geometry.conversions.normalize_quaternion(quaternion + 1e-6)
- rotation_matrix = kornia.geometry.conversions.quaternion_to_rotation_matrix(
- quaternion=quaternion, order=QuaternionCoeffOrder.WXYZ
- )
+ rotation_matrix = kornia.geometry.conversions.quaternion_to_rotation_matrix(quaternion=quaternion)
eye_batch = create_eye_batch(batch_size, 3, device=device, dtype=dtype)
rotation_matrix_eye = torch.matmul(rotation_matrix, rotation_matrix.transpose(-2, -1))
@@ -1038,9 +509,7 @@ class TestRotationMatrixToAngleAxis:
def test_gradcheck(self, batch_size, device, dtype):
quaternion = torch.rand(batch_size, 4, device=device, dtype=dtype)
quaternion = kornia.geometry.conversions.normalize_quaternion(quaternion + 1e-6)
- rotation_matrix = kornia.geometry.conversions.quaternion_to_rotation_matrix(
- quaternion=quaternion, order=QuaternionCoeffOrder.WXYZ
- )
+ rotation_matrix = kornia.geometry.conversions.quaternion_to_rotation_matrix(quaternion=quaternion)
# evaluate function gradient
rotation_matrix = tensor_to_gradcheck_var(rotation_matrix) # to var
assert gradcheck(
@@ -1715,11 +1184,11 @@ class TestCARKitToColmap:
t = torch.tensor([1, 0, 0], device=device, dtype=dtype).view(1, 3, 1)
ang_deg = torch.tensor([45, 60.0, 0.0], device=device, dtype=dtype)[None]
ang_rad = kornia.geometry.conversions.deg2rad(ang_deg)
- qvec = kornia.geometry.angle_axis_to_quaternion(ang_rad, order=QuaternionCoeffOrder.WXYZ)
+ qvec = kornia.geometry.angle_axis_to_quaternion(ang_rad)
q_colmap, t_colmap = ARKitQTVecs_to_ColmapQTVecs(qvec, t)
- angles_colmap = kornia.geometry.conversions.quaternion_to_angle_axis(q_colmap, order=QuaternionCoeffOrder.WXYZ)
+ angles_colmap = kornia.geometry.conversions.quaternion_to_angle_axis(q_colmap)
angles_colmap = kornia.geometry.conversions.rad2deg(angles_colmap)
expected_angles = torch.tensor([[116.8870620728, 0.0, -71.7524719238]], device=device, dtype=dtype)
expected_t = torch.tensor([[[-0.5256], [0.3558], [0.7727]]], device=device, dtype=dtype)
diff --git a/test/integration/test_conversions.py b/test/integration/test_conversions.py
index bd3feb2bd..51a1ffcb6 100644
--- a/test/integration/test_conversions.py
+++ b/test/integration/test_conversions.py
@@ -3,7 +3,6 @@ import pytest
import torch
import kornia
-from kornia.geometry.conversions import QuaternionCoeffOrder
from kornia.testing import assert_close
@@ -26,10 +25,8 @@ def rtol(device, dtype):
class TestAngleAxisToQuaternionToAngleAxis:
def test_zero_angle(self, device, dtype, atol, rtol):
angle_axis = torch.tensor((0.0, 0.0, 0.0), device=device, dtype=dtype)
- quaternion = kornia.geometry.conversions.angle_axis_to_quaternion(angle_axis, order=QuaternionCoeffOrder.WXYZ)
- angle_axis_hat = kornia.geometry.conversions.quaternion_to_angle_axis(
- quaternion, order=QuaternionCoeffOrder.WXYZ
- )
+ quaternion = kornia.geometry.conversions.angle_axis_to_quaternion(angle_axis)
+ angle_axis_hat = kornia.geometry.conversions.quaternion_to_angle_axis(quaternion)
assert_close(angle_axis_hat, angle_axis, atol=atol, rtol=rtol)
@pytest.mark.parametrize("axis", (0, 1, 2))
@@ -38,10 +35,8 @@ class TestAngleAxisToQuaternionToAngleAxis:
array = [0.0, 0.0, 0.0]
array[axis] = theta
angle_axis = torch.tensor(array, device=device, dtype=dtype)
- quaternion = kornia.geometry.conversions.angle_axis_to_quaternion(angle_axis, order=QuaternionCoeffOrder.WXYZ)
- angle_axis_hat = kornia.geometry.conversions.quaternion_to_angle_axis(
- quaternion, order=QuaternionCoeffOrder.WXYZ
- )
+ quaternion = kornia.geometry.conversions.angle_axis_to_quaternion(angle_axis)
+ angle_axis_hat = kornia.geometry.conversions.quaternion_to_angle_axis(quaternion)
assert_close(angle_axis_hat, angle_axis, atol=atol, rtol=rtol)
@pytest.mark.parametrize("axis", (0, 1, 2))
@@ -50,47 +45,16 @@ class TestAngleAxisToQuaternionToAngleAxis:
array = [0.0, 0.0, 0.0]
array[axis] = kornia.pi / 2.0
angle_axis = torch.tensor(array, device=device, dtype=dtype)
- quaternion = kornia.geometry.conversions.angle_axis_to_quaternion(angle_axis, order=QuaternionCoeffOrder.WXYZ)
- angle_axis_hat = kornia.geometry.conversions.quaternion_to_angle_axis(
- quaternion, order=QuaternionCoeffOrder.WXYZ
- )
+ quaternion = kornia.geometry.conversions.angle_axis_to_quaternion(angle_axis)
+ angle_axis_hat = kornia.geometry.conversions.quaternion_to_angle_axis(quaternion)
assert_close(angle_axis_hat, angle_axis, atol=atol, rtol=rtol)
class TestQuaternionToAngleAxisToQuaternion:
- def test_unit_quaternion_xyzw(self, device, dtype, atol, rtol):
- quaternion = torch.tensor((0.0, 0.0, 0.0, 1.0), device=device, dtype=dtype)
- with pytest.warns(UserWarning):
- angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(
- quaternion, order=QuaternionCoeffOrder.XYZW
- )
- with pytest.warns(UserWarning):
- quaternion_hat = kornia.geometry.conversions.angle_axis_to_quaternion(
- angle_axis, order=QuaternionCoeffOrder.XYZW
- )
- assert_close(quaternion_hat, quaternion, atol=atol, rtol=rtol)
-
def test_unit_quaternion(self, device, dtype, atol, rtol):
quaternion = torch.tensor((1.0, 0.0, 0.0, 0.0), device=device, dtype=dtype)
- angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(quaternion, order=QuaternionCoeffOrder.WXYZ)
- quaternion_hat = kornia.geometry.conversions.angle_axis_to_quaternion(
- angle_axis, order=QuaternionCoeffOrder.WXYZ
- )
- assert_close(quaternion_hat, quaternion, atol=atol, rtol=rtol)
-
- @pytest.mark.parametrize("axis", (0, 1, 2))
- def test_rotation_xyzw(self, axis, device, dtype, atol, rtol):
- array = [0.0, 0.0, 0.0, 0.0]
- array[axis] = 1.0
- quaternion = torch.tensor(array, device=device, dtype=dtype)
- with pytest.warns(UserWarning):
- angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(
- quaternion, order=QuaternionCoeffOrder.XYZW
- )
- with pytest.warns(UserWarning):
- quaternion_hat = kornia.geometry.conversions.angle_axis_to_quaternion(
- angle_axis, order=QuaternionCoeffOrder.XYZW
- )
+ angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(quaternion)
+ quaternion_hat = kornia.geometry.conversions.angle_axis_to_quaternion(angle_axis)
assert_close(quaternion_hat, quaternion, atol=atol, rtol=rtol)
@pytest.mark.parametrize("axis", (0, 1, 2))
@@ -98,33 +62,8 @@ class TestQuaternionToAngleAxisToQuaternion:
array = [0.0, 0.0, 0.0, 0.0]
array[1 + axis] = 1.0
quaternion = torch.tensor(array, device=device, dtype=dtype)
- angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(quaternion, order=QuaternionCoeffOrder.WXYZ)
- quaternion_hat = kornia.geometry.conversions.angle_axis_to_quaternion(
- angle_axis, order=QuaternionCoeffOrder.WXYZ
- )
- assert_close(quaternion_hat, quaternion, atol=atol, rtol=rtol)
-
- # just to be sure, check that mixing orders fails
- with pytest.warns(UserWarning):
- quaternion_hat_wrong = kornia.geometry.conversions.angle_axis_to_quaternion(
- angle_axis, order=QuaternionCoeffOrder.XYZW
- )
- assert not torch.allclose(quaternion_hat_wrong, quaternion, atol=atol, rtol=rtol)
-
- @pytest.mark.parametrize("axis", (0, 1, 2))
- def test_small_angle_xyzw(self, axis, device, dtype, atol, rtol):
- theta = 1.0e-2
- array = [0.0, 0.0, 0.0, np.cos(theta / 2)]
- array[axis] = np.sin(theta / 2.0)
- quaternion = torch.tensor(array, device=device, dtype=dtype)
- with pytest.warns(UserWarning):
- angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(
- quaternion, order=QuaternionCoeffOrder.XYZW
- )
- with pytest.warns(UserWarning):
- quaternion_hat = kornia.geometry.conversions.angle_axis_to_quaternion(
- angle_axis, order=QuaternionCoeffOrder.XYZW
- )
+ angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(quaternion)
+ quaternion_hat = kornia.geometry.conversions.angle_axis_to_quaternion(angle_axis)
assert_close(quaternion_hat, quaternion, atol=atol, rtol=rtol)
@pytest.mark.parametrize("axis", (0, 1, 2))
@@ -133,46 +72,12 @@ class TestQuaternionToAngleAxisToQuaternion:
array = [np.cos(theta / 2), 0.0, 0.0, 0.0]
array[1 + axis] = np.sin(theta / 2.0)
quaternion = torch.tensor(array, device=device, dtype=dtype)
- angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(quaternion, order=QuaternionCoeffOrder.WXYZ)
- quaternion_hat = kornia.geometry.conversions.angle_axis_to_quaternion(
- angle_axis, order=QuaternionCoeffOrder.WXYZ
- )
+ angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(quaternion)
+ quaternion_hat = kornia.geometry.conversions.angle_axis_to_quaternion(angle_axis)
assert_close(quaternion_hat, quaternion, atol=atol, rtol=rtol)
- # just to be sure, check that mixing orders fails
- with pytest.warns(UserWarning):
- quaternion_hat_wrong = kornia.geometry.conversions.angle_axis_to_quaternion(
- angle_axis, order=QuaternionCoeffOrder.XYZW
- )
- assert not torch.allclose(quaternion_hat_wrong, quaternion, atol=atol, rtol=rtol)
-
class TestQuaternionToRotationMatrixToAngleAxis:
- @pytest.mark.parametrize("axis", (0, 1, 2))
- def test_triplet_qma_xyzw(self, axis, device, dtype, atol, rtol):
- array = [[0.0, 0.0, 0.0, 0.0]]
- array[0][axis] = 1.0 # `0 + axis` should fail when WXYZ
- quaternion = torch.tensor(array, device=device, dtype=dtype)
- assert quaternion.shape[-1] == 4
-
- with pytest.warns(UserWarning):
- mm = kornia.geometry.conversions.quaternion_to_rotation_matrix(quaternion, order=QuaternionCoeffOrder.XYZW)
- assert mm.shape[-1] == 3
- assert mm.shape[-2] == 3
-
- angle_axis = kornia.geometry.conversions.rotation_matrix_to_angle_axis(mm)
- assert angle_axis.shape[-1] == 3
- angle_axis_expected = [[0.0, 0.0, 0.0]]
- angle_axis_expected[0][axis] = kornia.pi
- angle_axis_expected = torch.tensor(angle_axis_expected, device=device, dtype=dtype)
- assert_close(angle_axis, angle_axis_expected, atol=atol, rtol=rtol)
-
- with pytest.warns(UserWarning):
- quaternion_hat = kornia.geometry.conversions.angle_axis_to_quaternion(
- angle_axis, order=QuaternionCoeffOrder.XYZW
- )
- assert_close(quaternion_hat, quaternion, atol=atol, rtol=rtol)
-
@pytest.mark.parametrize("axis", (0, 1, 2))
def test_triplet_qma(self, axis, device, dtype, atol, rtol):
array = [[0.0, 0.0, 0.0, 0.0]]
@@ -180,7 +85,7 @@ class TestQuaternionToRotationMatrixToAngleAxis:
quaternion = torch.tensor(array, device=device, dtype=dtype)
assert quaternion.shape[-1] == 4
- mm = kornia.geometry.conversions.quaternion_to_rotation_matrix(quaternion, order=QuaternionCoeffOrder.WXYZ)
+ mm = kornia.geometry.conversions.quaternion_to_rotation_matrix(quaternion)
assert mm.shape[-1] == 3
assert mm.shape[-2] == 3
@@ -191,32 +96,7 @@ class TestQuaternionToRotationMatrixToAngleAxis:
angle_axis_expected = torch.tensor(angle_axis_expected, device=device, dtype=dtype)
assert_close(angle_axis, angle_axis_expected, atol=atol, rtol=rtol)
- quaternion_hat = kornia.geometry.conversions.angle_axis_to_quaternion(
- angle_axis, order=QuaternionCoeffOrder.WXYZ
- )
- assert_close(quaternion_hat, quaternion, atol=atol, rtol=rtol)
-
- @pytest.mark.parametrize("axis", (0, 1, 2))
- def test_triplet_qam_xyzw(self, axis, device, dtype, atol, rtol):
- array = [[0.0, 0.0, 0.0, 0.0]]
- array[0][axis] = 1.0
- quaternion = torch.tensor(array, device=device, dtype=dtype)
- assert quaternion.shape[-1] == 4
-
- with pytest.warns(UserWarning):
- angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(
- quaternion, order=QuaternionCoeffOrder.XYZW
- )
- assert angle_axis.shape[-1] == 3
-
- rot_m = kornia.geometry.conversions.angle_axis_to_rotation_matrix(angle_axis)
- assert rot_m.shape[-1] == 3
- assert rot_m.shape[-2] == 3
-
- with pytest.warns(UserWarning):
- quaternion_hat = kornia.geometry.conversions.rotation_matrix_to_quaternion(
- rot_m, order=QuaternionCoeffOrder.XYZW
- )
+ quaternion_hat = kornia.geometry.conversions.angle_axis_to_quaternion(angle_axis)
assert_close(quaternion_hat, quaternion, atol=atol, rtol=rtol)
@pytest.mark.parametrize("axis", (0, 1, 2))
@@ -226,41 +106,16 @@ class TestQuaternionToRotationMatrixToAngleAxis:
quaternion = torch.tensor(array, device=device, dtype=dtype)
assert quaternion.shape[-1] == 4
- angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(quaternion, order=QuaternionCoeffOrder.WXYZ)
+ angle_axis = kornia.geometry.conversions.quaternion_to_angle_axis(quaternion)
assert angle_axis.shape[-1] == 3
rot_m = kornia.geometry.conversions.angle_axis_to_rotation_matrix(angle_axis)
assert rot_m.shape[-1] == 3
assert rot_m.shape[-2] == 3
- quaternion_hat = kornia.geometry.conversions.rotation_matrix_to_quaternion(
- rot_m, order=QuaternionCoeffOrder.WXYZ
- )
+ quaternion_hat = kornia.geometry.conversions.rotation_matrix_to_quaternion(rot_m)
assert_close(quaternion_hat, quaternion, atol=atol, rtol=rtol)
- @pytest.mark.parametrize("axis", (0, 1, 2))
- def test_triplet_amq_xyzw(self, axis, device, dtype, atol, rtol):
- array = [[0.0, 0.0, 0.0]]
- array[0][axis] = kornia.pi / 2.0
- angle_axis = torch.tensor(array, device=device, dtype=dtype)
- assert angle_axis.shape[-1] == 3
-
- rot_m = kornia.geometry.conversions.angle_axis_to_rotation_matrix(angle_axis)
- assert rot_m.shape[-1] == 3
- assert rot_m.shape[-2] == 3
-
- with pytest.warns(UserWarning):
- quaternion = kornia.geometry.conversions.rotation_matrix_to_quaternion(
- rot_m, order=QuaternionCoeffOrder.XYZW
- )
- assert quaternion.shape[-1] == 4
-
- with pytest.warns(UserWarning):
- angle_axis_hat = kornia.geometry.conversions.quaternion_to_angle_axis(
- quaternion, order=QuaternionCoeffOrder.XYZW
- )
- assert_close(angle_axis_hat, angle_axis, atol=atol, rtol=rtol)
-
@pytest.mark.parametrize("axis", (0, 1, 2))
def test_triplet_amq(self, axis, device, dtype, atol, rtol):
array = [[0.0, 0.0, 0.0]]
@@ -272,35 +127,10 @@ class TestQuaternionToRotationMatrixToAngleAxis:
assert rot_m.shape[-1] == 3
assert rot_m.shape[-2] == 3
- quaternion = kornia.geometry.conversions.rotation_matrix_to_quaternion(rot_m, order=QuaternionCoeffOrder.WXYZ)
+ quaternion = kornia.geometry.conversions.rotation_matrix_to_quaternion(rot_m)
assert quaternion.shape[-1] == 4
- angle_axis_hat = kornia.geometry.conversions.quaternion_to_angle_axis(
- quaternion, order=QuaternionCoeffOrder.WXYZ
- )
- assert_close(angle_axis_hat, angle_axis, atol=atol, rtol=rtol)
-
- @pytest.mark.parametrize("axis", (0, 1, 2))
- def test_triplet_aqm_xyzw(self, axis, device, dtype, atol, rtol):
- array = [[0.0, 0.0, 0.0]]
- array[0][axis] = kornia.pi / 2.0
- angle_axis = torch.tensor(array, device=device, dtype=dtype)
- assert angle_axis.shape[-1] == 3
-
- with pytest.warns(UserWarning):
- quaternion = kornia.geometry.conversions.angle_axis_to_quaternion(
- angle_axis, order=QuaternionCoeffOrder.XYZW
- )
- assert quaternion.shape[-1] == 4
-
- with pytest.warns(UserWarning):
- rot_m = kornia.geometry.conversions.quaternion_to_rotation_matrix(
- quaternion, order=QuaternionCoeffOrder.XYZW
- )
- assert rot_m.shape[-1] == 3
- assert rot_m.shape[-2] == 3
-
- angle_axis_hat = kornia.geometry.conversions.rotation_matrix_to_angle_axis(rot_m)
+ angle_axis_hat = kornia.geometry.conversions.quaternion_to_angle_axis(quaternion)
assert_close(angle_axis_hat, angle_axis, atol=atol, rtol=rtol)
@pytest.mark.parametrize("axis", (0, 1, 2))
@@ -310,10 +140,10 @@ class TestQuaternionToRotationMatrixToAngleAxis:
angle_axis = torch.tensor(array, device=device, dtype=dtype)
assert angle_axis.shape[-1] == 3
- quaternion = kornia.geometry.conversions.angle_axis_to_quaternion(angle_axis, order=QuaternionCoeffOrder.WXYZ)
+ quaternion = kornia.geometry.conversions.angle_axis_to_quaternion(angle_axis)
assert quaternion.shape[-1] == 4
- rot_m = kornia.geometry.conversions.quaternion_to_rotation_matrix(quaternion, order=QuaternionCoeffOrder.WXYZ)
+ rot_m = kornia.geometry.conversions.quaternion_to_rotation_matrix(quaternion)
assert rot_m.shape[-1] == 3
assert rot_m.shape[-2] == 3
@@ -402,37 +232,6 @@ class TestAngleOfRotations:
matrix_angle_abs = TestAngleOfRotations.matrix_angle_abs(rot_m)
assert_close(torch.abs(angle), matrix_angle_abs)
- @pytest.mark.parametrize('axis_name', ('x', 'y', 'z'))
- @pytest.mark.parametrize("angle_deg", (-179.9, -90.0, -45.0, 0.0, 45, 90, 179.9))
- def test_quaternion_xyzw(self, axis_name, angle_deg, device, dtype, atol, rtol):
- eps = torch.finfo(dtype).eps
- angle = torch.tensor((angle_deg * kornia.pi / 180.0,), device=device, dtype=dtype).repeat(2, 1)
- pi = torch.ones_like(angle) * kornia.pi
- assert 2 <= len(angle.shape)
- rot_m, axis = TestAngleOfRotations.axis_and_angle_to_rotation_matrix(
- axis_name=axis_name, angle=angle, device=device, dtype=dtype
- )
- with pytest.warns(UserWarning):
- quaternion = kornia.geometry.conversions.rotation_matrix_to_quaternion(
- rot_m, eps=eps, order=QuaternionCoeffOrder.XYZW
- )
- # compute quaternion rotation angle
- # See Section 2.4.4 Equation (105a) in https://arxiv.org/pdf/1711.02508.pdf
- angle_hat = 2.0 * torch.atan2(quaternion[..., :3].norm(p=2, dim=-1, keepdim=True), quaternion[..., 3:4])
- # make sure it lands between [-pi..pi)
- mask = pi < angle_hat
- while torch.any(mask):
- angle_hat = torch.where(mask, angle_hat - 2.0 * kornia.pi, angle_hat)
- mask = pi < angle_hat
- # invert angle, if quaternion axis points in the opposite direction of the original axis
- dots = (quaternion[..., :3] * axis).sum(dim=-1, keepdim=True)
- angle_hat = torch.where(dots < 0.0, angle_hat * -1.0, angle_hat)
- # quaternion angle should match input angle
- assert_close(angle_hat, angle, atol=atol, rtol=rtol)
- # magnitude of angle should match matrix rotation angle
- matrix_angle_abs = TestAngleOfRotations.matrix_angle_abs(rot_m)
- assert_close(torch.abs(angle_hat), matrix_angle_abs, atol=atol, rtol=rtol)
-
@pytest.mark.parametrize('axis_name', ('x', 'y', 'z'))
@pytest.mark.parametrize("angle_deg", (-179.9, -90.0, -45.0, 0.0, 45, 90, 179.9))
def test_quaternion(self, axis_name, angle_deg, device, dtype, atol, rtol):
@@ -443,9 +242,7 @@ class TestAngleOfRotations:
rot_m, axis = TestAngleOfRotations.axis_and_angle_to_rotation_matrix(
axis_name=axis_name, angle=angle, device=device, dtype=dtype
)
- quaternion = kornia.geometry.conversions.rotation_matrix_to_quaternion(
- rot_m, eps=eps, order=QuaternionCoeffOrder.WXYZ
- )
+ quaternion = kornia.geometry.conversions.rotation_matrix_to_quaternion(rot_m, eps=eps)
# compute quaternion rotation angle
# See Section 2.4.4 Equation (105a) in https://arxiv.org/pdf/1711.02508.pdf
angle_hat = 2.0 * torch.atan2(quaternion[..., 1:4].norm(p=2, dim=-1, keepdim=True), quaternion[..., 0:1])
@@ -482,39 +279,6 @@ class TestAngleOfRotations:
matrix_angle_abs = TestAngleOfRotations.matrix_angle_abs(rot_m)
assert_close(torch.abs(angle_hat), matrix_angle_abs, atol=atol, rtol=rtol)
- @pytest.mark.parametrize('axis_name', ('x', 'y', 'z'))
- @pytest.mark.parametrize("angle_deg", (-179.9, -90.0, -45.0, 0, 45, 90, 179.9))
- def test_log_quaternion_xyzw(self, axis_name, angle_deg, device, dtype, atol, rtol):
- eps = torch.finfo(dtype).eps
- angle = (angle_deg * kornia.pi / 180.0).to(dtype).to(device).repeat(2, 1)
- pi = torch.ones_like(angle) * kornia.pi
- rot_m, axis = TestAngleOfRotations.axis_and_angle_to_rotation_matrix(
- axis_name=axis_name, angle=angle, device=device, dtype=dtype
- )
- with pytest.warns(UserWarning):
- quaternion = kornia.geometry.conversions.rotation_matrix_to_quaternion(
- rot_m, eps=eps, order=QuaternionCoeffOrder.XYZW
- )
- with pytest.warns(UserWarning):
- log_q = kornia.geometry.conversions.quaternion_exp_to_log(
- quaternion, eps=eps, order=QuaternionCoeffOrder.XYZW
- )
- # compute angle_axis rotation angle
- angle_hat = 2.0 * log_q.norm(p=2, dim=-1, keepdim=True)
- # make sure it lands between [-pi..pi)
- mask = pi < angle_hat
- while torch.any(mask):
- angle_hat = torch.where(mask, angle_hat - 2.0 * kornia.pi, angle_hat)
- mask = pi < angle_hat
- # invert angle, if angle_axis axis points in the opposite direction of the original axis
- dots = (log_q * axis).sum(dim=-1, keepdim=True)
- angle_hat = torch.where(dots < 0.0, angle_hat * -1.0, angle_hat)
- # angle_axis angle should match input angle
- assert_close(angle_hat, angle, atol=atol, rtol=rtol)
- # magnitude of angle should match matrix rotation angle
- matrix_angle_abs = TestAngleOfRotations.matrix_angle_abs(rot_m)
- assert_close(torch.abs(angle_hat), matrix_angle_abs, atol=atol, rtol=rtol)
-
@pytest.mark.parametrize('axis_name', ('x', 'y', 'z'))
@pytest.mark.parametrize("angle_deg", (-179.9, -90.0, -45.0, 0, 45, 90, 179.9))
def test_log_quaternion(self, axis_name, angle_deg, device, dtype, atol, rtol):
@@ -524,10 +288,8 @@ class TestAngleOfRotations:
rot_m, axis = TestAngleOfRotations.axis_and_angle_to_rotation_matrix(
axis_name=axis_name, angle=angle, device=device, dtype=dtype
)
- quaternion = kornia.geometry.conversions.rotation_matrix_to_quaternion(
- rot_m, eps=eps, order=QuaternionCoeffOrder.WXYZ
- )
- log_q = kornia.geometry.conversions.quaternion_exp_to_log(quaternion, eps=eps, order=QuaternionCoeffOrder.WXYZ)
+ quaternion = kornia.geometry.conversions.rotation_matrix_to_quaternion(rot_m, eps=eps)
+ log_q = kornia.geometry.conversions.quaternion_exp_to_log(quaternion, eps=eps)
# compute angle_axis rotation angle
angle_hat = 2.0 * log_q.norm(p=2, dim=-1, keepdim=True)
# make sure it lands between [-pi..pi)
diff --git a/test/nerf/test_camera_utils.py b/test/nerf/test_camera_utils.py
index 51d4811ed..cb4533eac 100755
--- a/test/nerf/test_camera_utils.py
+++ b/test/nerf/test_camera_utils.py
@@ -4,7 +4,7 @@ import urllib.request
import pytest
import torch
-from kornia.geometry.conversions import QuaternionCoeffOrder, quaternion_to_rotation_matrix
+from kornia.geometry.conversions import quaternion_to_rotation_matrix
from kornia.nerf.camera_utils import create_spiral_path, parse_colmap_output
from kornia.testing import assert_close
@@ -66,7 +66,7 @@ def test_parse_colmap_output(device, dtype, colmap_cameras_path, colmap_images_p
tz = -1.0631749488011808
q = torch.tensor([qw, qx, qy, qz], device=device, dtype=dtype)
- R = quaternion_to_rotation_matrix(q, order=QuaternionCoeffOrder.WXYZ)
+ R = quaternion_to_rotation_matrix(q)
t = torch.tensor([tx, ty, tz], device=device, dtype=dtype)
assert_close(R, cameras.rotation_matrix[2])