π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(geometry): return tensors from line-plane intersection (#5044)
NiteeshGowda72a9492b6cceb5
diff --git a/changelog.d/5044.breaking.md b/changelog.d/5044.breaking.mdnew file mode 100644index 000000000..2979f8ec4--- /dev/null+++ b/changelog.d/5044.breaking.md@@ -0,0 +1,4 @@+`ParametrizedLine.intersect` now returns two `torch.Tensor`s, as annotated. Previously the lambda and the point came+back as `kornia.geometry.vector.Scalar` wrappers, because the plane's offset is a `Scalar`, and+`Hyperplane.signed_distance` rejected the returned point. Code that unwrapped them with `.data` still gets the same+values, but `.data` on a tensor is detached from autograd: drop the `.data` to keep gradients.diff --git a/kornia/geometry/line.py b/kornia/geometry/line.pyindex 2c41b1311..bcc1f57ad 100644--- a/kornia/geometry/line.py+++ b/kornia/geometry/line.py@@ -179,7 +179,7 @@ class ParametrizedLine(nn.Module):# TODO: add check for dot productres_lambda = torch.where(dot_prod_mask,- -(plane.offset + batched_dot_product(plane.normal.data, self.origin)) / dot_prod,+ -(plane.offset.data + batched_dot_product(plane.normal.data, self.origin)) / dot_prod,torch.zeros_like(dot_prod),)diff --git a/tests/geometry/test_line.py b/tests/geometry/test_line.pyindex 74162b8e4..8c1001a60 100644--- a/tests/geometry/test_line.py+++ b/tests/geometry/test_line.py@@ -131,6 +131,35 @@ class TestParametrizedLine(BaseTester):self.assert_close(lmbda, expected_lambda)self.assert_close(point, expected_point)+ def test_intersect_plane_returns_tensors(self, device, dtype):+ plane = Hyperplane.through(+ torch.tensor([0.0, 0.0, 2.0], device=device, dtype=dtype),+ torch.tensor([1.0, 0.0, 2.0], device=device, dtype=dtype),+ torch.tensor([0.0, 1.0, 2.0], device=device, dtype=dtype),+ )+ line = ParametrizedLine(+ torch.tensor([1.0, 1.0, 0.0], device=device, dtype=dtype),+ torch.tensor([0.0, 0.6, 0.8], device=device, dtype=dtype),+ )++ lmbda, point = line.intersect(plane)++ assert type(lmbda) is torch.Tensor+ assert type(point) is torch.Tensor++ self.assert_close(lmbda, torch.tensor(2.5, device=device, dtype=dtype))+ self.assert_close(+ point,+ torch.tensor([1.0, 2.5, 2.0], device=device, dtype=dtype),+ )+ self.assert_close(plane.signed_distance(point).data, torch.zeros_like(lmbda))++ # An origin off z = 0, so that the sign of n . origin in lambda is pinned as well.+ line = ParametrizedLine(torch.tensor([1.0, 1.0, 1.0], device=device, dtype=dtype), line.direction)+ lmbda, point = line.intersect(plane)+ self.assert_close(lmbda, torch.tensor(1.25, device=device, dtype=dtype))+ self.assert_close(point, torch.tensor([1.0, 1.75, 2.0], device=device, dtype=dtype))+def test_intersect_plane_parallel(self, device, dtype):# the degenerate branch must return deterministic values, not uninitialized memoryp0 = torch.tensor([0.0, 4.0, 0.0], device=device, dtype=dtype)