Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

trans, trans_x, trans_y + minor (#2103)

Christie Jacob7a5e7367af69
diff --git a/kornia/geometry/liegroup/se2.py b/kornia/geometry/liegroup/se2.py
index 08e94c966..4ddc1e12c 100644
--- a/kornia/geometry/liegroup/se2.py
+++ b/kornia/geometry/liegroup/se2.py
@@ -2,10 +2,10 @@
# https://github.com/strasdat/Sophus/blob/master/sympy/sophus/se2.py
from typing import Optional, Tuple, overload
-from kornia.core import Module, Parameter, Tensor, concatenate, pad, rand, stack, tensor, where
+from kornia.core import Module, Parameter, Tensor, concatenate, pad, rand, stack, tensor, where, zeros_like
from kornia.geometry.liegroup._utils import check_se2_omega_shape, check_se2_r_t_shape, check_v_shape
from kornia.geometry.liegroup.so2 import So2
-from kornia.testing import KORNIA_CHECK, KORNIA_CHECK_TYPE
+from kornia.testing import KORNIA_CHECK, KORNIA_CHECK_SAME_DEVICES, KORNIA_CHECK_TYPE
class Se2(Module):
@@ -32,8 +32,8 @@ class Se2(Module):
Internally represented by a complex number `z` and a translation 2-vector.
Args:
- r: So2 group encompassing a rotation.
- t: translation vector with the shape of :math:`(B, 2)`.
+ rotation: So2 group encompassing a rotation.
+ translation: translation vector with the shape of :math:`(B, 2)`.
Example:
>>> so2 = So2.identity(1)
@@ -281,6 +281,40 @@ class Se2(Module):
shape = (batch_size, 2)
return cls(r, rand(shape, device=device, dtype=dtype))
+ @classmethod
+ def trans(cls, x: Tensor, y: Tensor) -> "Se2":
+ """Construct a translation only Se2 instance.
+
+ Args:
+ x: the x-axis translation.
+ y: the y-axis translation.
+ """
+ KORNIA_CHECK(x.shape == y.shape)
+ KORNIA_CHECK_SAME_DEVICES([x, y])
+ batch_size = x.shape[0] if len(x.shape) > 0 else None
+ rotation = So2.identity(batch_size, x.device, x.dtype)
+ return cls(rotation, stack((x, y), -1))
+
+ @classmethod
+ def trans_x(cls, x: Tensor) -> "Se2":
+ """Construct a x-axis translation.
+
+ Args:
+ x: the x-axis translation.
+ """
+ zs = zeros_like(x)
+ return cls.trans(x, zs)
+
+ @classmethod
+ def trans_y(cls, y: Tensor) -> "Se2":
+ """Construct a y-axis translation.
+
+ Args:
+ y: the y-axis translation.
+ """
+ zs = zeros_like(y)
+ return cls.trans(zs, y)
+
def adjoint(self) -> Tensor:
"""Returns the adjoint matrix of shape :math:`(B, 3, 3)`.
diff --git a/kornia/geometry/liegroup/se3.py b/kornia/geometry/liegroup/se3.py
index d8eb7bccc..92c63adc6 100644
--- a/kornia/geometry/liegroup/se3.py
+++ b/kornia/geometry/liegroup/se3.py
@@ -27,14 +27,14 @@ class Se3(Module):
tensor([1., 1., 1.], requires_grad=True)
"""
- def __init__(self, r: So3, t: Tensor) -> None:
+ def __init__(self, rotation: So3, translation: Tensor) -> None:
"""Constructor for the base class.
Internally represented by a unit quaternion `q` and a translation 3-vector.
Args:
- r: So3 group encompassing a rotation.
- t: translation vector with the shape of :math:`(B, 3)`.
+ rotation: So3 group encompassing a rotation.
+ translation: translation vector with the shape of :math:`(B, 3)`.
Example:
>>> from kornia.geometry.quaternion import Quaternion
@@ -48,16 +48,16 @@ class Se3(Module):
tensor([[1., 1., 1.]], requires_grad=True)
"""
super().__init__()
- KORNIA_CHECK_TYPE(r, So3)
+ KORNIA_CHECK_TYPE(rotation, So3)
# KORNIA_CHECK_SHAPE(t, ["B", "3"]) # FIXME: resolve shape bugs. @edgarriba
- self._r = r
- self._t = Parameter(t)
+ self._rotation = rotation
+ self._translation = Parameter(translation)
def __repr__(self) -> str:
return f"rotation: {self.r}\ntranslation: {self.t}"
def __getitem__(self, idx) -> 'Se3':
- return Se3(self._r[idx], self._t[idx])
+ return Se3(self._rotation[idx], self._translation[idx])
def __mul__(self, right: "Se3") -> "Se3":
"""Compose two Se3 transformations.
@@ -68,33 +68,45 @@ class Se3(Module):
Return:
The resulting Se3 transformation.
"""
+ so3 = self.so3
+ t = self.t
if isinstance(right, Se3):
KORNIA_CHECK_TYPE(right, Se3)
# https://github.com/strasdat/Sophus/blob/master/sympy/sophus/se3.py#L97
- r = self.so3 * right.so3
- t = self.t + self.so3 * right.t
- return Se3(r, t)
+ _r = so3 * right.so3
+ _t = t + so3 * right.t
+ return Se3(_r, _t)
elif isinstance(right, Tensor):
KORNIA_CHECK_TYPE(right, Tensor)
# KORNIA_CHECK_SHAPE(right, ["B", "N"]) # FIXME: resolve shape bugs. @edgarriba
- return self.so3 * right + self.t
+ return so3 * right + t
else:
raise TypeError(f"Unsupported type: {type(right)}")
@property
def so3(self) -> So3:
"""Return the underlying rotation(So3)."""
- return self._r
+ return self._rotation
@property
def r(self) -> So3:
"""Return the underlying rotation(So3)."""
- return self._r
+ return self._rotation
@property
def t(self) -> Tensor:
"""Return the underlying translation vector of shape :math:`(B,3)`."""
- return self._t
+ return self._translation
+
+ @property
+ def rotation(self) -> So3:
+ """Return the underlying rotation(So3)."""
+ return self._rotation
+
+ @property
+ def translation(self) -> Tensor:
+ """Return the underlying translation vector of shape :math:`(B,3)`."""
+ return self._translation
@staticmethod
def exp(v) -> 'Se3':
diff --git a/test/geometry/liegroup/test_se2.py b/test/geometry/liegroup/test_se2.py
index 879e19168..ae2003c43 100644
--- a/test/geometry/liegroup/test_se2.py
+++ b/test/geometry/liegroup/test_se2.py
@@ -176,6 +176,30 @@ class TestSe2(BaseTester):
self.assert_close(se2_in_se2.so2.z.imag, i.so2.z.imag)
self.assert_close(se2_in_se2.t, i.t)
+ @pytest.mark.parametrize("batch_size", (None, 1, 2, 5))
+ def test_trans(self, device, dtype, batch_size):
+ trans = self._make_rand_data(device, dtype, (batch_size, 2))
+ x, y = trans[..., 0], trans[..., 1]
+ se2 = Se2.trans(x, y)
+ self.assert_close(se2.t, trans)
+ self.assert_close(se2.so2.matrix(), So2.identity(batch_size, device, dtype).matrix())
+
+ @pytest.mark.parametrize("batch_size", (None, 1, 2, 5))
+ def test_trans_x(self, device, dtype, batch_size):
+ x = self._make_rand_data(device, dtype, (batch_size, 1)).squeeze(-1)
+ zs = torch.zeros_like(x)
+ se2 = Se2.trans_x(x)
+ self.assert_close(se2.t, torch.stack((x, zs), -1))
+ self.assert_close(se2.so2.matrix(), So2.identity(batch_size, device, dtype).matrix())
+
+ @pytest.mark.parametrize("batch_size", (None, 1, 2, 5))
+ def test_trans_y(self, device, dtype, batch_size):
+ y = self._make_rand_data(device, dtype, (batch_size, 1)).squeeze(-1)
+ zs = torch.zeros_like(y)
+ se2 = Se2.trans_y(y)
+ self.assert_close(se2.t, torch.stack((zs, y), -1))
+ self.assert_close(se2.so2.matrix(), So2.identity(batch_size, device, dtype).matrix())
+
@pytest.mark.parametrize("batch_size", (None, 1, 2, 5))
def test_adjoint(self, device, dtype, batch_size):
x = Se2.random(batch_size)