Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

move transform_points to transformation submodule

edgarribaa15747002d7e
diff --git a/docs/source/conversions.rst b/docs/source/conversions.rst
index 42742d89e..1a9771a59 100644
--- a/docs/source/conversions.rst
+++ b/docs/source/conversions.rst
@@ -7,7 +7,6 @@ Conversions
.. autofunction:: deg2rad
.. autofunction:: convert_points_from_homogeneous
.. autofunction:: convert_points_to_homogeneous
-.. autofunction:: transform_points
.. autofunction:: angle_axis_to_rotation_matrix
.. autofunction:: rotation_matrix_to_angle_axis
.. autofunction:: rotation_matrix_to_quaternion
@@ -18,4 +17,3 @@ Conversions
.. autoclass:: DegToRad
.. autoclass:: ConvertPointsFromHomogeneous
.. autoclass:: ConvertPointsToHomogeneous
-.. autoclass:: TransformPoints
diff --git a/docs/source/transformations.rst b/docs/source/transformations.rst
index 15959c83f..e5c6a0893 100644
--- a/docs/source/transformations.rst
+++ b/docs/source/transformations.rst
@@ -6,3 +6,4 @@ Linear Transformations
.. autofunction:: relative_transformation
.. autofunction:: inverse_transformation
.. autofunction:: compose_transformations
+.. autofunction:: transform_points
diff --git a/torchgeometry/core/conversions.py b/torchgeometry/core/conversions.py
index 98fba1953..aa67143a5 100644
--- a/torchgeometry/core/conversions.py
+++ b/torchgeometry/core/conversions.py
@@ -8,7 +8,6 @@ __all__ = [
"deg2rad",
"convert_points_from_homogeneous",
"convert_points_to_homogeneous",
- "transform_points",
"angle_axis_to_rotation_matrix",
"rotation_matrix_to_angle_axis",
"rotation_matrix_to_quaternion",
@@ -19,7 +18,6 @@ __all__ = [
"DegToRad",
"ConvertPointsFromHomogeneous",
"ConvertPointsToHomogeneous",
- "TransformPoints",
]
@@ -113,47 +111,6 @@ def convert_points_to_homogeneous(points):
return nn.functional.pad(points, (0, 1), "constant", 1.0)
-def transform_points(dst_pose_src, points_src):
- r"""Function that applies transformations to a set of points.
-
- See :class:`~torchgeometry.TransformPoints` for details.
-
- Args:
- dst_pose_src (Tensor): tensor for transformations.
- points_src (Tensor): tensor of points.
-
- Returns:
- Tensor: tensor of N-dimensional points.
-
- Shape:
- - Input: :math:`(B, D+1, D+1)` and :math:`(B, D, N)`
- - Output: :math:`(B, N, D)`
-
- Examples::
-
- >>> input = torch.rand(2, 4, 3) # BxNx3
- >>> pose = torch.eye(4).view(1, 4, 4) # Bx4x4
- >>> output = tgm.transform_points(pose, input) # BxNx3
- """
- if not torch.is_tensor(dst_pose_src) or not torch.is_tensor(points_src):
- raise TypeError("Input type is not a torch.Tensor")
- if not dst_pose_src.device == points_src.device:
- raise TypeError("Tensor must be in the same device")
- if not dst_pose_src.shape[0] == points_src.shape[0]:
- raise ValueError("Input batch size must be the same for both tensors")
- if not dst_pose_src.shape[-1] == (points_src.shape[-1] + 1):
- raise ValueError("Last input dimensions must differe by one unit")
- # to homogeneous
- points_src_h = convert_points_to_homogeneous(points_src) # BxNxD+1
- # transform coordinates
- points_dst_h = torch.matmul(
- dst_pose_src.unsqueeze(1), points_src_h.unsqueeze(-1))
- points_dst_h = torch.squeeze(points_dst_h, dim=-1)
- # to euclidean
- points_dst = convert_points_from_homogeneous(points_dst_h) # BxNxD
- return points_dst
-
-
def angle_axis_to_rotation_matrix(angle_axis):
"""Convert 3d vector of axis-angle rotation to 4x4 rotation matrix
@@ -348,8 +305,8 @@ def rotation_matrix_to_quaternion(rotation_matrix, eps=1e-6):
mask_c3 = mask_c3.view(-1, 1).type_as(q3)
q = q0 * mask_c0 + q1 * mask_c1 + q2 * mask_c2 + q3 * mask_c3
- q /= torch.sqrt(t0_rep * mask_c0 + t1_rep * mask_c1 # noqa
- + t2_rep * mask_c2 + t3_rep * mask_c3) # noqa
+ q /= torch.sqrt(t0_rep * mask_c0 + t1_rep * mask_c1 + # noqa
+ t2_rep * mask_c2 + t3_rep * mask_c3) # noqa
q *= 0.5
return q
@@ -520,33 +477,3 @@ class ConvertPointsToHomogeneous(nn.Module):
def forward(self, input):
return convert_points_to_homogeneous(input)
-
-
-class TransformPoints(nn.Module):
- r"""Creates an object to transform a set of points.
-
- Args:
- dst_pose_src (Tensor): tensor for transformations of
- shape :math:`(B, D+1, D+1)`.
-
- Returns:
- Tensor: tensor of N-dimensional points.
-
- Shape:
- - Input: :math:`(B, D, N)`
- - Output: :math:`(B, N, D)`
-
- Examples::
-
- >>> input = torch.rand(2, 4, 3) # BxNx3
- >>> transform = torch.eye(4).view(1, 4, 4) # Bx4x4
- >>> transform_op = tgm.TransformPoints(transform)
- >>> output = transform_op(input) # BxNx3
- """
-
- def __init__(self, dst_homo_src):
- super(TransformPoints, self).__init__()
- self.dst_homo_src = dst_homo_src
-
- def forward(self, points_src):
- return transform_points(self.dst_homo_src, points_src)
diff --git a/torchgeometry/core/depth_warper.py b/torchgeometry/core/depth_warper.py
index 63c7e89f6..29abf9468 100644
--- a/torchgeometry/core/depth_warper.py
+++ b/torchgeometry/core/depth_warper.py
@@ -3,8 +3,8 @@ import torch
import torch.nn as nn
import torch.nn.functional as F
+from torchgeometry.core.transformations import transform_points
from torchgeometry.core.transformations import relative_transformation
-from torchgeometry.core.conversions import transform_points
from torchgeometry.core.conversions import convert_points_to_homogeneous
from torchgeometry.core.pinhole import PinholeCamera, PinholeCamerasList
from torchgeometry.core.pinhole import normalize_pixel_coordinates
diff --git a/torchgeometry/core/homography_warper.py b/torchgeometry/core/homography_warper.py
index 1f41fd0c4..1a886fb7e 100644
--- a/torchgeometry/core/homography_warper.py
+++ b/torchgeometry/core/homography_warper.py
@@ -5,7 +5,7 @@ import torch.nn as nn
import torch.nn.functional as F
from torchgeometry.utils import create_meshgrid
-from torchgeometry.core.conversions import transform_points
+from torchgeometry.core.transformations import transform_points
__all__ = [
diff --git a/torchgeometry/core/pinhole.py b/torchgeometry/core/pinhole.py
index fd81de3e8..7b6870bf0 100644
--- a/torchgeometry/core/pinhole.py
+++ b/torchgeometry/core/pinhole.py
@@ -4,8 +4,9 @@ import warnings
import torch
import torch.nn as nn
+from torchgeometry.core.conversions import rtvec_to_pose
+from torchgeometry.core.transformations import transform_points
from torchgeometry.core.transformations import inverse_transformation
-from torchgeometry.core.conversions import rtvec_to_pose, transform_points
__all__ = [
diff --git a/torchgeometry/core/transformations.py b/torchgeometry/core/transformations.py
index e129c80eb..4c43e4d13 100644
--- a/torchgeometry/core/transformations.py
+++ b/torchgeometry/core/transformations.py
@@ -3,11 +3,16 @@ from typing import Optional
import torch
import torch.nn as nn
+from torchgeometry.core.conversions import convert_points_to_homogeneous
+from torchgeometry.core.conversions import convert_points_from_homogeneous
+
__all__ = [
"compose_transformations",
"inverse_transformation",
"relative_transformation",
+ "transform_points",
+ "TransformPoints",
]
@@ -137,7 +142,7 @@ def relative_transformation(
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).
+ :math:`(N, 4, 4)` or :math:`(4, 4)`.
Shape:
- Output: :math:`(N, 4, 4)` or :math:`(4, 4)`.
@@ -170,9 +175,79 @@ def relative_transformation(
return trans_12
+def transform_points(dst_trans_src: torch.Tensor,
+ points_src: torch.Tensor) -> torch.Tensor:
+ r"""Function that applies transformations to a set of points.
+
+ Args:
+ dst_trans_src (torch.Tensor): tensor for transformations of shape
+ :math:`(B, D+1, D+1)`.
+ points_src (torch.Tensor): tensor of points of shape :math:`(B, N, D)`.
+ Returns:
+ torch.Tensor: tensor of N-dimensional points.
+
+ Shape:
+ - Output: :math:`(B, N, D)`
+
+ Examples:
+
+ >>> x_src = torch.rand(2, 4, 3) # BxNx3
+ >>> dst_trans_src = torch.eye(4).view(1, 4, 4) # Bx4x4
+ >>> x_dst = tgm.transform_points(dst_trans_src, x_src) # BxNx3
+ """
+ if not torch.is_tensor(dst_trans_src) or not torch.is_tensor(points_src):
+ raise TypeError("Input type is not a torch.Tensor")
+ if not dst_trans_src.device == points_src.device:
+ raise TypeError("Tensor must be in the same device")
+ if not dst_trans_src.shape[0] == points_src.shape[0]:
+ raise ValueError("Input batch size must be the same for both tensors")
+ if not dst_trans_src.shape[-1] == (points_src.shape[-1] + 1):
+ raise ValueError("Last input dimensions must differe by one unit")
+ # to homogeneous
+ points_src_h: torch.Tensor = convert_points_to_homogeneous(
+ points_src) # BxNxD+1
+ # transform coordinates
+ points_dst_h: torch.Tensor = torch.matmul(
+ dst_trans_src.unsqueeze(1), points_src_h.unsqueeze(-1))
+ points_dst_h = torch.squeeze(points_dst_h, dim=-1)
+ # to euclidean
+ points_dst: torch.Tensor = convert_points_from_homogeneous(
+ points_dst_h) # BxNxD
+ return points_dst
+
+
# layer api
+class TransformPoints(nn.Module):
+ r"""Creates an object to transform a set of points.
+
+ Args:
+ dst_pose_src (torhc.Tensor): tensor for transformations of
+ shape :math:`(B, D+1, D+1)`.
+
+ Returns:
+ torch.Tensor: tensor of N-dimensional points.
+
+ Shape:
+ - Input: :math:`(B, D, N)`
+ - Output: :math:`(B, N, D)`
+
+ Examples:
+ >>> input = torch.rand(2, 4, 3) # BxNx3
+ >>> transform = torch.eye(4).view(1, 4, 4) # Bx4x4
+ >>> transform_op = tgm.TransformPoints(transform)
+ >>> output = transform_op(input) # BxNx3
+ """
+
+ def __init__(self, dst_homo_src: torch.Tensor) -> None:
+ super(TransformPoints, self).__init__()
+ self.dst_homo_src: torch.Tensor = dst_homo_src
+
+ def forward(self, points_src: torch.Tensor) -> torch.Tensor:
+ return transform_points(self.dst_homo_src, points_src)
+
+
'''class InversePose(nn.Module):
r"""Creates a transformation that inverts a 4x4 pose.