Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix convert_from_homogeneous for points at infinity

edgarriba967fb740b05c
diff --git a/test/test_conversions.py b/test/test_conversions.py
index f2b4c53b2..231a003c8 100644
--- a/test/test_conversions.py
+++ b/test/test_conversions.py
@@ -173,27 +173,44 @@ def test_convert_points_to_homogeneous(batch_shape, device_type):
raise_exception=True)
[email protected]("batch_shape", [
- (2, 3), (1, 2, 3), (2, 3, 3), (5, 5, 3), ])
-def test_convert_points_from_homogeneous(batch_shape, device_type):
- # generate input data
- points_h = torch.rand(batch_shape)
- points_h = points_h.to(torch.device(device_type))
- points_h[..., -1] = 1.0
-
- # to euclidean
- points = tgm.convert_points_from_homogeneous(points_h)
+class TestConvertPointsFromHomogeneous:
+ @pytest.mark.parametrize("batch_shape", [
+ (2, 3), (1, 2, 3), (2, 3, 3), (5, 5, 3), ])
+ def test_convert_points(self, batch_shape, device_type):
+ # generate input data
+ points_h = torch.rand(batch_shape)
+ points_h = points_h.to(torch.device(device_type))
+
+ # to euclidean
+ points = tgm.convert_points_from_homogeneous(points_h)
+
+ error = utils.compute_mse(points_h[..., :2], points)
+ assert pytest.approx(error.item(), 0.0)
+
+ @pytest.mark.parametrize("batch_shape", [
+ (2, 3), (1, 2, 3), (2, 3, 3), (5, 5, 3), ])
+ def test_convert_points_infinity(self, batch_shape, device_type):
+ # generate input data
+ points_h = torch.ones(batch_shape)
+ points_h = points_h.to(torch.device(device_type))
+ points_h[..., 0, -1] = 0.0
+ points_h[..., 1] = 2.0
+
+ # to euclidean
+ points = tgm.convert_points_from_homogeneous(points_h)
+
+ assert utils.check_equal_torch(
+ points_h[..., 0:2], points[..., 0:2])
+
+ @pytest.mark.parametrize("batch_shape", [
+ (2, 3), (1, 2, 3), (2, 3, 3), (5, 5, 3), ])
+ def test_gradcheck(self, batch_shape):
+ points_h = torch.rand(batch_shape)
- error = utils.compute_mse(points_h[..., :2], points)
- assert pytest.approx(error.item(), 0.0)
-
- # functional
- assert torch.allclose(points, tgm.ConvertPointsFromHomogeneous()(points_h))
-
- # evaluate function gradient
- points = utils.tensor_to_gradcheck_var(points) # to var
- assert gradcheck(tgm.convert_points_from_homogeneous, (points,),
- raise_exception=True)
+ # evaluate function gradient
+ points_h = utils.tensor_to_gradcheck_var(points_h) # to var
+ assert gradcheck(tgm.convert_points_from_homogeneous, (points_h,),
+ raise_exception=True)
@pytest.mark.parametrize("batch_size", [1, 2, 5])
diff --git a/test/test_transformations.py b/test/test_transformations.py
index 6f640fc58..b77f03bfe 100644
--- a/test/test_transformations.py
+++ b/test/test_transformations.py
@@ -69,6 +69,26 @@ class TestTransformPoints:
error = utils.compute_mse(points_src, points_dst_to_src)
assert pytest.approx(error.item(), 0.0)
+ @pytest.mark.parametrize("device_type", TEST_DEVICES)
+ @pytest.mark.parametrize("batch_size", [1, 2, 5])
+ def test_nan_input(self, device_type, batch_size):
+ # generate input data
+ num_dims = 2
+ num_points = 1
+ eye_size = num_dims + 1
+ points_src = torch.zeros(
+ batch_size, num_points, num_dims, device=torch.device(device_type))
+
+ dst_homo_src = utils.create_eye_batch(batch_size, eye_size)
+ dst_homo_src = dst_homo_src.to(torch.device(device_type))
+ dst_homo_src[..., 0, 0] *= 9999
+ dst_homo_src[..., 1, 1] *= 9999
+ dst_homo_src[..., 2, 2] *= 9999
+
+ # transform the points from dst to ref
+ points_dst = tgm.transform_points(dst_homo_src, points_src)
+ pass
+
def test_gradcheck(self):
# generate input data
batch_size, num_points, num_dims = 2, 3, 2
diff --git a/torchgeometry/core/conversions.py b/torchgeometry/core/conversions.py
index 656127c1c..34e81793a 100644
--- a/torchgeometry/core/conversions.py
+++ b/torchgeometry/core/conversions.py
@@ -74,7 +74,7 @@ def deg2rad(tensor):
return tensor * pi.to(tensor.device).type(tensor.dtype) / 180.
-def convert_points_from_homogeneous(points):
+def convert_points_from_homogeneous(points: torch.Tensor) -> torch.Tensor:
r"""Function that converts points from homogeneous to Euclidean space.
See :class:`~torchgeometry.ConvertPointsFromHomogeneous` for details.
@@ -91,7 +91,12 @@ def convert_points_from_homogeneous(points):
raise ValueError("Input must be at least a 2D tensor. Got {}".format(
points.shape))
- return points[..., :-1] / points[..., -1:]
+ # we check for points at infinity
+ z_vec: torch.Tensor = points[..., -1:]
+ scale: torch.Tensor = torch.where(
+ z_vec != 0.0, 1. / z_vec, torch.ones_like(z_vec))
+
+ return scale * points[..., :-1]
def convert_points_to_homogeneous(points):