Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

improve transformations module docs

edgarriba7c8cd56d7253
diff --git a/test/test_transformations.py b/test/test_transformations.py
index c1a2a8f43..1a9fec6ed 100644
--- a/test/test_transformations.py
+++ b/test/test_transformations.py
@@ -20,24 +20,24 @@ def euler_angles_to_rotation_matrix(x, y, z):
ones, zeros = torch.ones_like(x), torch.zeros_like(x)
# the rotation matrix for the x-axis
rx_tmp = [
- ones, zeros, zeros, zeros,
- zeros, torch.cos(x), -torch.sin(x), zeros,
- zeros, torch.sin(x), torch.cos(x), zeros,
- zeros, zeros, zeros, ones]
+ ones, zeros, zeros, zeros,
+ zeros, torch.cos(x), -torch.sin(x), zeros,
+ zeros, torch.sin(x), torch.cos(x), zeros,
+ zeros, zeros, zeros, ones]
rx = torch.stack(rx_tmp, dim=-1).view(-1, 4, 4)
# the rotation matrix for the y-axis
ry_tmp = [
- torch.cos(y), zeros, torch.sin(y), zeros,
- zeros, ones, zeros, zeros,
+ torch.cos(y), zeros, torch.sin(y), zeros,
+ zeros, ones, zeros, zeros,
-torch.sin(y), zeros, torch.cos(y), zeros,
- zeros, zeros, zeros, ones]
+ zeros, zeros, zeros, ones]
ry = torch.stack(ry_tmp, dim=-1).view(-1, 4, 4)
# the rotation matrix for the z-axis
rz_tmp = [
torch.cos(z), -torch.sin(z), zeros, zeros,
- torch.sin(z), torch.cos(z), zeros, zeros,
- zeros, zeros, ones, zeros,
- zeros, zeros, zeros, ones]
+ torch.sin(z), torch.cos(z), zeros, zeros,
+ zeros, zeros, ones, zeros,
+ zeros, zeros, zeros, ones]
rz = torch.stack(rz_tmp, dim=-1).view(-1, 4, 4)
return torch.matmul(rz, torch.matmul(ry, rx)) # Bx4x4
diff --git a/torchgeometry/core/pinhole.py b/torchgeometry/core/pinhole.py
index d999c5f9f..fd81de3e8 100644
--- a/torchgeometry/core/pinhole.py
+++ b/torchgeometry/core/pinhole.py
@@ -558,7 +558,8 @@ def homography_i_H_ref(pinhole_i, pinhole_ref):
assert pinhole_i.shape == pinhole_ref.shape, pinhole_ref.shape
i_pose_base = get_optical_pose_base(pinhole_i)
ref_pose_base = get_optical_pose_base(pinhole_ref)
- i_pose_ref = torch.matmul(i_pose_base, inverse_transformation(ref_pose_base))
+ i_pose_ref = torch.matmul(i_pose_base,
+ inverse_transformation(ref_pose_base))
return torch.matmul(
pinhole_matrix(pinhole_i),
torch.matmul(i_pose_ref, inverse_pinhole_matrix(pinhole_ref)))
diff --git a/torchgeometry/core/transformations.py b/torchgeometry/core/transformations.py
index f3ff3f71f..e129c80eb 100644
--- a/torchgeometry/core/transformations.py
+++ b/torchgeometry/core/transformations.py
@@ -15,6 +15,11 @@ def compose_transformations(
trans_01: torch.Tensor, trans_12: torch.Tensor) -> torch.Tensor:
r"""Functions that composes two homogeneous transformations.
+ .. math::
+
+ T_0^{2} = \begin{bmatrix} R_0^1 R_1^{2} & R_0^{1} t_1^{2} + t_0^{1} \\
+ \mathbf{0} & 1\end{bmatrix}
+
Args:
trans_01 (torch.Tensor): tensor with the homogenous transformation from
a reference frame 1 respect to a frame 0. The tensor has must have a
@@ -23,11 +28,17 @@ def compose_transformations(
a reference frame 2 respect to a frame 1. The tensor has must have a
shape of :math:`(B, 4, 4)` or :math:`(4, 4)`.
+ Shape:
+ - Output: :math:`(N, 4, 4)` or :math:`(4, 4)`
+
Returns:
- torch.Tensor: the transformation between the reference frame 1 respect
- to frame 2. The output shape will be :math:`(B, 4, 4)` or
- :math:`(4 ,4)`.
-
+ torch.Tensor: the transformation between the two frames.
+
+ Example::
+ >>> trans_01 = torch.eye(4) # 4x4
+ >>> trans_12 = torch.eye(4) # 4x4
+ >>> trans_02 = tgm.compose_transformations(trans_01, trans_12) # 4x4
+
"""
if not torch.is_tensor(trans_01):
raise TypeError("Input trans_01 type is not a torch.Tensor. Got {}"
@@ -64,24 +75,24 @@ def compose_transformations(
def inverse_transformation(trans_12):
r"""Function that inverts a 4x4 homogeneous transformation
- :math:`P = \begin{bmatrix} R & t \\ \mathbf{0} & 1 \end{bmatrix}`
+ :math:`T_1^{2} = \begin{bmatrix} R_1 & t_1 \\ \mathbf{0} & 1 \end{bmatrix}`
The inverse transformation is computed as follows:
.. math::
- P^{-1} = \begin{bmatrix} R^T & -R^T t \\ \mathbf{0} &
- 1\end{bmatrix}
+ T_2^{1} = (T_1^{2})^{-1} = \begin{bmatrix} R_1^T & -R_1^T t_1 \\
+ \mathbf{0} & 1\end{bmatrix}
Args:
- points (torch.Tensor): tensor with transformations.
+ trans_12 (torch.Tensor): transformation tensor of shape
+ :math:`(N, 4, 4)` or :math:`(4, 4)`.
Returns:
torch.Tensor: tensor with inverted transformations.
Shape:
- - Input: :math:`(N, 4, 4)`
- - Output: :math:`(N, 4, 4)`
+ - Output: :math:`(N, 4, 4)` or :math:`(4, 4)`
Example:
>>> trans_12 = torch.rand(1, 4, 4) # Nx4x4
@@ -111,35 +122,33 @@ def inverse_transformation(trans_12):
def relative_transformation(
trans_01: torch.Tensor, trans_02: torch.Tensor) -> torch.Tensor:
- r"""Function that computes the relative transformation from a reference
- pose :math:`P_1^{\{W\}} = \begin{bmatrix} R_1 & t_1 \\ \mathbf{0} & 1
- \end{bmatrix}` to destination :math:`P_2^{\{W\}} = \begin{bmatrix} R_2 &
- t_2 \\ \mathbf{0} & 1 \end{bmatrix}`.
+ r"""Function that computes the relative homogenous transformation from a
+ reference transformation :math:`T_1^{0} = \begin{bmatrix} R_1 & t_1 \\
+ \mathbf{0} & 1 \end{bmatrix}` to destination :math:`T_2^{0} =
+ \begin{bmatrix} R_2 & t_2 \\ \mathbf{0} & 1 \end{bmatrix}`.
The relative transformation is computed as follows:
.. math::
- P_1^{2} = \begin{bmatrix} R_2 R_1^T & R_1^T (t_2 - t_1) \\ \mathbf{0} &
- 1\end{bmatrix}
+ T_1^{2} = (T_0^{1})^{-1} \cdot T_0^{2}
Arguments:
- pose_1 (torch.Tensor): reference pose tensor of shape
- :math:`(N, 4, 4)`.
- pose_2 (torch.Tensor): destination pose tensor of shape
- :math:`(N, 4, 4)`.
+ trans_01 (torch.Tensor): reference transformation tensor of shape
+ :math:`(N, 4, 4)` or :math:`(4, 4)`.
+ trans_02 (torch.Tensor): destination transformation tensor of shape
+ :math:`(N, 4, 4)` or :math:`(4, 4).
Shape:
- - Output: :math:`(N, 4, 4)`
+ - Output: :math:`(N, 4, 4)` or :math:`(4, 4)`.
Returns:
- torch.Tensor: the relative transformation between the poses.
+ torch.Tensor: the relative transformation between the transformations.
Example::
-
- >>> pose_1 = torch.eye(4).unsqueeze(0) # 1x4x4
- >>> pose_2 = torch.eye(4).unsqueeze(0) # 1x4x4
- >>> pose_21 = tgm.relative_pose(pose_1, pose_2) # 1x4x4
+ >>> trans_01 = torch.eye(4) # 4x4
+ >>> trans_02 = torch.eye(4) # 4x4
+ >>> trans_12 = tgm.relative_transformation(trans_01, trans_02) # 4x4
"""
if not torch.is_tensor(trans_01):
raise TypeError("Input trans_01 type is not a torch.Tensor. Got {}"
@@ -147,10 +156,10 @@ def relative_transformation(
if not torch.is_tensor(trans_02):
raise TypeError("Input trans_02 type is not a torch.Tensor. Got {}"
.format(type(trans_02)))
- if not (len(trans_01.shape) in (2, 3) and trans_01.shape[-2:] == (4, 4)):
+ if not trans_01.dim() in (2, 3) and trans_01.shape[-2:] == (4, 4):
raise ValueError("Input must be a of the shape Nx4x4 or 4x4."
" Got {}".format(trans_01.shape))
- if not (len(trans_02.shape) in (2, 3) and trans_02.shape[-2:] == (4, 4)):
+ if not trans_02.dim() in (2, 3) and trans_02.shape[-2:] == (4, 4):
raise ValueError("Input must be a of the shape Nx4x4 or 4x4."
" Got {}".format(trans_02.shape))
if not trans_01.dim() == trans_02.dim():