Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(geometry): return one point per plane from ParametrizedLine.intersect (#6589)

Dmytro Mishkin28bf2be5608e
diff --git a/changelog.d/6589.breaking.md b/changelog.d/6589.breaking.md
new file mode 100644
index 000000000..68c95803c
--- /dev/null
+++ b/changelog.d/6589.breaking.md
@@ -0,0 +1 @@
+`ParametrizedLine.intersect` with a `(M, 1)` plane batch now returns points of shape `(M, 1, 3)`, following the rule `point.shape == lambda.shape + (3,)`. It used to return `(M, 3)`; the values do not change.
diff --git a/changelog.d/6589.fixed.md b/changelog.d/6589.fixed.md
new file mode 100644
index 000000000..45bf5e91f
--- /dev/null
+++ b/changelog.d/6589.fixed.md
@@ -0,0 +1 @@
+`ParametrizedLine.intersect` now returns one point per plane for a plane batch with more than one axis. A `(M, K)` batch used to give wrong points of shape `(M, 3)` without an error, or raise when the shapes did not broadcast, and now gives `(M, K, 3)`.
diff --git a/kornia/geometry/line.py b/kornia/geometry/line.py
index edd619a96..9c8ed116c 100644
--- a/kornia/geometry/line.py
+++ b/kornia/geometry/line.py
@@ -283,9 +283,9 @@ class ParametrizedLine(nn.Module):
Return:
The pair ``(lambda, point)``. ``lambda`` is the line parameter of the intersection in units of the stored
- direction, ``()`` for one unbatched line and one unbatched plane, and ``(B,)`` for the broadcast batch,
- which for one unbatched line is the batch of the planes. ``point`` is the intersection
- ``point_at(lambda)``, ``(3,)`` or ``(B, 3)`` with the same batch.
+ direction, ``()`` for one unbatched line and one unbatched plane, and otherwise the broadcast batch
+ ``(...)``, which for one unbatched line is the batch of the planes, over any number of axes. ``point`` is
+ the intersection ``origin + lambda * direction``, one per plane, of shape ``(..., 3)`` with the same batch.
Note:
If the line is parallel to the plane (``|normal . direction| < eps``) there is no unique
@@ -305,7 +305,10 @@ class ParametrizedLine(nn.Module):
torch.zeros_like(dot_prod),
)
- res_point = self.point_at(res_lambda)
+ # Not point_at: it reads a multi-axis step element-wise against the direction, as it reads a (B, 1) step. A 0-d
+ # step stays 0-d, so that a float64 plane does not promote the point of a float32 line.
+ step = res_lambda if res_lambda.ndim == 0 else res_lambda[..., None]
+ res_point = self.origin + self.direction * step
return res_lambda, res_point
diff --git a/tests/geometry/test_line.py b/tests/geometry/test_line.py
index 86f708393..618429b49 100644
--- a/tests/geometry/test_line.py
+++ b/tests/geometry/test_line.py
@@ -239,6 +239,45 @@ class TestParametrizedLine(BaseTester):
self.assert_close(steps, -offset.data)
self.assert_close(points, expected_points)
+ @pytest.mark.parametrize("line_batch", [(), (1,)])
+ @pytest.mark.parametrize("plane_batch", [(2, 3), (3, 2), (2, 1), (1, 3), (2, 2, 2)])
+ def test_line_intersect_multi_axis_batch_of_planes_6497(self, line_batch, plane_batch, device, dtype):
+ """Each plane of a multi-axis batch gets its own point: ``point.shape == lambda.shape + (3,)``."""
+ origin = torch.tensor([1.0, 2.0, 0.5], device=device, dtype=dtype).expand(*line_batch, 3)
+ direction = torch.tensor([1.0, -1.0, 2.0], device=device, dtype=dtype).expand(*line_batch, 3)
+ line = ParametrizedLine(origin, direction)
+ normal = torch.tensor([0.0, 0.0, 1.0], device=device, dtype=dtype).expand(*plane_batch, 3).clone()
+ offset = -torch.arange(1.0, torch.Size(plane_batch).numel() + 1, device=device, dtype=dtype).view(plane_batch)
+
+ steps, points = line.intersect(Hyperplane(Vector3(normal), Scalar(offset)))
+
+ assert steps.shape == plane_batch
+ assert points.shape == (*plane_batch, 3)
+ expected = [
+ line.intersect(Hyperplane(Vector3(n), Scalar(d)))[1].squeeze(0)
+ for n, d in zip(normal.view(-1, 3), offset.view(-1))
+ ]
+ self.assert_close(points.view(-1, 3), torch.stack(expected))
+ # Each point lies on its own plane, n . p + d = 0, with this normal along z: z = -d.
+ self.assert_close(points[..., 2], -offset)
+
+ def test_unbatched_line_keeps_its_dtype_for_one_plane_6497(self, device):
+ if device.type == "mps":
+ pytest.skip("float64 is unavailable on MPS")
+ # A 0-d step does not promote the line's dtype; a (1,) one would.
+ line = ParametrizedLine(
+ torch.tensor([1.0, 2.0, 0.0], device=device, dtype=torch.float32),
+ torch.tensor([0.0, 0.0, 1.0], device=device, dtype=torch.float32),
+ )
+ plane = Hyperplane(
+ Vector3(torch.tensor([0.0, 0.0, 1.0], device=device, dtype=torch.float64)),
+ Scalar(torch.tensor(-3.0, device=device, dtype=torch.float64)),
+ )
+ steps, point = line.intersect(plane)
+ assert steps.dtype == torch.float64
+ assert point.dtype == torch.float32
+ self.assert_close(point, torch.tensor([1.0, 2.0, 3.0], device=device, dtype=torch.float32))
+
def test_intersect_plane_returns_tensors(self, device, dtype):
plane = Hyperplane.through(
torch.tensor([0.0, 0.0, 2.0], device=device, dtype=dtype),