Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

add support for quaternion in Se3 instantiation (#2433)

Christie Jacob3d5faa1b1d22
diff --git a/kornia/geometry/liegroup/se3.py b/kornia/geometry/liegroup/se3.py
index 933477706..2c4ba28b3 100644
--- a/kornia/geometry/liegroup/se3.py
+++ b/kornia/geometry/liegroup/se3.py
@@ -16,9 +16,10 @@ from kornia.core import (
where,
zeros_like,
)
-from kornia.core.check import KORNIA_CHECK, KORNIA_CHECK_SAME_DEVICES, KORNIA_CHECK_TYPE
+from kornia.core.check import KORNIA_CHECK, KORNIA_CHECK_SAME_DEVICES
from kornia.geometry.liegroup.so3 import So3
from kornia.geometry.linalg import batched_dot_product
+from kornia.geometry.quaternion import Quaternion
from kornia.geometry.vector import Vector3
@@ -30,9 +31,8 @@ class Se3(Module):
See more: https://ingmec.ual.es/~jlblanco/papers/jlblanco2010geometry3D_techrep.pdf
Example:
- >>> from kornia.geometry.quaternion import Quaternion
>>> q = Quaternion.identity()
- >>> s = Se3(So3(q), torch.ones(3))
+ >>> s = Se3(q, torch.ones(3))
>>> s.r
Parameter containing:
tensor([1., 0., 0., 0.], requires_grad=True)
@@ -41,7 +41,7 @@ class Se3(Module):
tensor([1., 1., 1.], requires_grad=True)
"""
- def __init__(self, rotation: So3, translation: Vector3 | Tensor) -> None:
+ def __init__(self, rotation: Quaternion | So3, translation: Vector3 | Tensor) -> None:
"""Constructor for the base class.
Internally represented by a unit quaternion `q` and a translation 3-vector.
@@ -53,7 +53,7 @@ class Se3(Module):
Example:
>>> from kornia.geometry.quaternion import Quaternion
>>> q = Quaternion.identity(batch_size=1)
- >>> s = Se3(So3(q), torch.ones((1, 3)))
+ >>> s = Se3(q, torch.ones((1, 3)))
>>> s.r
Parameter containing:
tensor([[1., 0., 0., 0.]], requires_grad=True)
@@ -62,17 +62,23 @@ class Se3(Module):
tensor([[1., 1., 1.]], requires_grad=True)
"""
super().__init__()
- KORNIA_CHECK_TYPE(rotation, So3)
+ # KORNIA_CHECK_TYPE(rotation, (Quaternion, So3))
+ if not isinstance(rotation, (Quaternion, So3)):
+ raise TypeError(f"rotation type is {type(rotation)}")
# KORNIA_CHECK_TYPE(translation, (Vector3, Tensor))
if not isinstance(translation, (Vector3, Tensor)):
raise TypeError(f"translation type is {type(translation)}")
# KORNIA_CHECK_SHAPE(t, ["B", "3"]) # FIXME: resolve shape bugs. @edgarriba
self._translation: Vector3 | Parameter
- self._rotation: So3 = rotation
+ self._rotation: So3
if isinstance(translation, Tensor):
self._translation = Parameter(translation)
else:
self._translation = translation
+ if isinstance(rotation, Quaternion):
+ self._rotation = So3(rotation)
+ else:
+ self._rotation = rotation
def __repr__(self) -> str:
return f"rotation: {self.r}\ntranslation: {self.t}"
@@ -107,6 +113,11 @@ class Se3(Module):
"""Return the underlying rotation(So3)."""
return self._rotation
+ @property
+ def quaternion(self) -> Quaternion:
+ """Return the underlying rotation(Quaternion)."""
+ return self._rotation.q
+
@property
def r(self) -> So3:
"""Return the underlying rotation(So3)."""
@@ -165,7 +176,7 @@ class Se3(Module):
Example:
>>> from kornia.geometry.quaternion import Quaternion
>>> q = Quaternion.identity()
- >>> Se3(So3(q), torch.zeros(3)).log()
+ >>> Se3(q, torch.zeros(3)).log()
tensor([0., 0., 0., 0., 0., 0.], grad_fn=<CatBackward0>)
"""
omega = self.r.log()
diff --git a/test/geometry/liegroup/test_se3.py b/test/geometry/liegroup/test_se3.py
index 6cf7b5c9e..095d9b846 100644
--- a/test/geometry/liegroup/test_se3.py
+++ b/test/geometry/liegroup/test_se3.py
@@ -12,7 +12,7 @@ class TestSe3(BaseTester):
def _make_rand_se3d(self, device, dtype, batch_size) -> Se3:
q = Quaternion.random(batch_size, device, dtype)
t = self._make_rand_data(device, dtype, batch_size, dims=3)
- return Se3(So3(q), t)
+ return Se3(q, t)
def _make_rand_se3d_vec(self, device, dtype, batch_size) -> Se3:
q = Quaternion.random(batch_size, device, dtype)
@@ -70,7 +70,7 @@ class TestSe3(BaseTester):
def test_getitem(self, device, dtype, batch_size):
q = Quaternion.random(batch_size, device, dtype)
t = torch.rand(batch_size, 3, device=device, dtype=dtype)
- s = Se3(So3(q), t)
+ s = Se3(q, t)
for i in range(batch_size):
s1 = s[i]
self.assert_close(s1.r.q.data, q.data[i])