Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(geometry): read an (N,) step and a batch of planes on an unbatched ParametrizedLine (#6491)

Parman Mohammadalizadeh26c4d881936b
diff --git a/changelog.d/6491.fixed.md b/changelog.d/6491.fixed.md
new file mode 100644
index 000000000..4004be891
--- /dev/null
+++ b/changelog.d/6491.fixed.md
@@ -0,0 +1 @@
+`ParametrizedLine.point_at` on an unbatched `(D,)` line reads an `(N,)` `t` as `N` steps and returns `(N, D)` points, as it already did for `(N, 1)`. For `D > 1` it used to multiply the steps with the direction element-wise, which returned one wrong point when `N` equalled `D` and raised for any other `N` but 1; for `D = 1` it returned the `N` points flattened to `(N,)`, now `(N, 1)`. `intersect` on an unbatched line with a batch of `M` planes therefore returns `(M, 3)` points; it used to raise, or return one wrong point for `M = 3`. A one-element `(1,)` `t`, and a batch of one plane, now give the same point with shape `(1, D)` instead of `(D,)`, matching the shape of `t` or of the returned `lambda`. `line[i]` now returns the origin for `0` and `-2`, the direction for `1` and `-1`, and raises `IndexError` for any other index; it used to return the direction for every index but `0`.
diff --git a/kornia/geometry/line.py b/kornia/geometry/line.py
index 1697eb8eb..6536e7b9e 100644
--- a/kornia/geometry/line.py
+++ b/kornia/geometry/line.py
@@ -80,7 +80,7 @@ class ParametrizedLine(nn.Module):
return str(self)
def __getitem__(self, idx: int) -> torch.Tensor:
- return self.origin if idx == 0 else self.direction
+ return (self.origin, self.direction)[idx]
def __iter__(self) -> Iterator[torch.Tensor]:
yield from (self.origin, self.direction)
@@ -130,8 +130,9 @@ class ParametrizedLine(nn.Module):
"""Get the point at :math:`t` along this line.
Args:
- t: step along the line: a number or a 0-d tensor for every row, or one step per row of shape
- ``(B,)`` or ``(B, 1)`` for a line of shape ``(B, D)``.
+ t: step along the line: a number or a 0-d tensor for every row, one step per row of shape
+ ``(B,)`` or ``(B, 1)`` for a line of shape ``(B, D)``, or ``N`` steps of shape ``(N,)`` or
+ ``(N, 1)`` for a line of shape ``(D,)``, giving ``(N, D)`` points.
Return:
tensor with the point.
@@ -145,7 +146,7 @@ class ParametrizedLine(nn.Module):
"""
if isinstance(t, Scalar):
t = t.data
- if isinstance(t, torch.Tensor) and t.ndim > 0 and t.ndim == self.direction.ndim - 1:
+ if isinstance(t, torch.Tensor) and t.ndim > 0 and t.ndim == max(self.direction.ndim - 1, 1):
t = t[..., None]
return self.origin + self.direction * t
diff --git a/tests/geometry/test_line.py b/tests/geometry/test_line.py
index 5a769c439..06892eff9 100644
--- a/tests/geometry/test_line.py
+++ b/tests/geometry/test_line.py
@@ -97,6 +97,18 @@ class TestParametrizedLine(BaseTester):
assert type(from_scalar) is torch.Tensor
self.assert_close(from_scalar, expected)
+ def test_unbatched_point_at_n_steps_6483(self, device, dtype):
+ line = ParametrizedLine(
+ torch.tensor([1.0, 2.0], device=device, dtype=dtype),
+ torch.tensor([1.0, 0.0], device=device, dtype=dtype),
+ )
+ for n in (1, 2, 3):
+ steps = torch.arange(1.0, n + 1, device=device, dtype=dtype)
+ expected = torch.stack([line.point_at(s) for s in steps])
+ assert expected.shape == (n, 2)
+ self.assert_close(line.point_at(steps), expected)
+ self.assert_close(line.point_at(steps[:, None]), expected)
+
def test_scalar_point_at_preserves_line_dtype(self, device, dtype):
line = ParametrizedLine(
torch.tensor([1.0, 2.0], device=device, dtype=dtype),
@@ -214,6 +226,19 @@ class TestParametrizedLine(BaseTester):
self.assert_close(steps, torch.stack(expected_steps))
self.assert_close(points, torch.stack(expected_points))
+ @pytest.mark.parametrize("num_planes", [1, 3, 4])
+ def test_unbatched_line_intersect_batch_of_planes_6483(self, device, dtype, num_planes):
+ line = ParametrizedLine(
+ torch.tensor([1.0, 2.0, 0.0], device=device, dtype=dtype),
+ torch.tensor([0.0, 0.0, 1.0], device=device, dtype=dtype),
+ )
+ normal = Vector3(torch.tensor([[0.0, 0.0, 1.0]] * num_planes, device=device, dtype=dtype))
+ offset = Scalar(-torch.arange(1.0, num_planes + 1, device=device, dtype=dtype))
+ steps, points = line.intersect(Hyperplane(normal, offset))
+ expected_points = torch.tensor([[1.0, 2.0, z] for z in range(1, num_planes + 1)], device=device, dtype=dtype)
+ self.assert_close(steps, -offset.data)
+ self.assert_close(points, expected_points)
+
def test_intersect_plane_returns_tensors(self, device, dtype):
plane = Hyperplane.through(
torch.tensor([0.0, 0.0, 2.0], device=device, dtype=dtype),
@@ -377,6 +402,18 @@ class TestParametrizedLine(BaseTester):
self.assert_close(plain.origin.grad, torch.ones_like(origin))
self.assert_close(plain.direction.grad, torch.full_like(direction, 2.0))
+ def test_getitem_6483(self, device, dtype):
+ origin = torch.tensor([1.0, 2.0], device=device, dtype=dtype)
+ direction = torch.tensor([1.0, 0.0], device=device, dtype=dtype)
+ line = ParametrizedLine(origin, direction)
+ self.assert_close(line[0], origin)
+ self.assert_close(line[1], direction)
+ self.assert_close(line[-2], origin)
+ self.assert_close(line[-1], direction)
+ for idx in (2, 5, -3):
+ with pytest.raises(IndexError):
+ line[idx]
+
def test_integer_input_raises(self, device):
# An integer origin or direction cannot be optimizable state: the constructor names the dtype instead of
# raising torch's autograd error (#5201). Cast it to a floating-point dtype first.