π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(geometry): drop Hyperplane.through's unreachable two-point form (#5034)
Dmytro Mishkin36e59e860696
diff --git a/changelog.d/5034.fixed.md b/changelog.d/5034.fixed.mdnew file mode 100644index 000000000..7548f1d3a--- /dev/null+++ b/changelog.d/5034.fixed.md@@ -0,0 +1 @@+`Hyperplane.through` no longer documents a two-point 2D form. That form never worked: `Hyperplane.through(p0, p1)` always raised a bare `Validation condition failed` (a 2D normal cannot be stored in a `Vector3`), and the unreachable code would have used the line direction as the normal. A two-point call now raises a `BaseError` saying that three points are required. The undocumented, unexported helper `kornia.geometry.plane.normalized`, used only by that code, is removed.diff --git a/docs/export_support/cases_geomA.py b/docs/export_support/cases_geomA.pyindex c8de22d1e..5ceb6c607 100644--- a/docs/export_support/cases_geomA.py+++ b/docs/export_support/cases_geomA.py@@ -1104,13 +1104,6 @@ CASES += [lambda a, b, c: (lambda pl: (pl.normal.data, pl.offset.data))(Hyperplane.through(a, b, c)),[p3, q3, torch.tensor([0.0, 1.0, 5.0])],),- case(- "plane.Hyperplane.through[2d]",- G,- lambda a, b: (lambda pl: (pl.normal.data, pl.offset.data))(Hyperplane.through(a, b)),- [torch.tensor([[1.0, 2.0]]), torch.tensor([[3.0, 5.0]])],- note="KORNIA BUG: 2-D branch wraps a 2-vector into Vector3 -> always fails validation (plane.py:185)",- ),case("plane.Hyperplane.from_vector",G,diff --git a/docs/source/_data/export_support.json b/docs/source/_data/export_support.jsonindex 528458efb..372f06b5e 100644--- a/docs/source/_data/export_support.json+++ b/docs/source/_data/export_support.json@@ -8330,23 +8330,6 @@"variant": "","where": ""},- {- "compile": "n/a",- "compile_detail": "does not run eagerly with the probed inputs: BaseError : Validation condition failed",- "export": "n/a",- "export_detail": "does not run eagerly with the probed inputs: BaseError : Validation condition failed",- "graph_breaks": 0,- "name": "plane.Hyperplane.through[2d]",- "note": "KORNIA BUG: 2-D branch wraps a 2-vector into Vector3 -> always fails validation (plane.py:185)",- "onnx": "n/a",- "onnx_detail": "does not run eagerly with the probed inputs: BaseError : Validation condition failed",- "operator": "Hyperplane.through",- "package": "kornia.geometry",- "ref": "",- "section": "plane",- "variant": "2d",- "where": ""- },{"compile": "ok","compile_detail": "",diff --git a/kornia/geometry/plane.py b/kornia/geometry/plane.pyindex defecad92..2cb07e5f8 100644--- a/kornia/geometry/plane.py+++ b/kornia/geometry/plane.py@@ -24,6 +24,7 @@ import torchfrom torch import nnfrom kornia.core.check import KORNIA_CHECK, KORNIA_CHECK_SHAPE, KORNIA_CHECK_TYPE+from kornia.core.exceptions import BaseErrorfrom kornia.core.tensor_wrapper import _unwrap, _wrapfrom kornia.core.utils import _torch_svd_castfrom kornia.geometry.linalg import batched_dot_product@@ -32,11 +33,6 @@ from kornia.geometry.vector import Scalar, Vector3__all__ = ["Hyperplane", "fit_plane"]-def normalized(v: torch.Tensor, eps: float = 1e-6) -> torch.Tensor:- norm_sq = (v * v).sum(dim=-1, keepdim=True) + eps- return v * norm_sq.rsqrt()--class Hyperplane(nn.Module):"""Represent a hyperplane in n-dimensional space.@@ -160,30 +156,31 @@ class Hyperplane(nn.Module):@classmethoddef through(cls, p0: torch.Tensor, p1: torch.Tensor, p2: Optional[torch.Tensor] = None) -> "Hyperplane":- """Construct a line-like 2D hyperplane or a 3D plane through points.+ """Construct the 3D plane through three points.++ Only the three-point form is supported: :class:`Hyperplane` stores its normal as a+ :class:`~kornia.geometry.vector.Vector3`, so it cannot represent a 2D line, and calling+ ``through`` with two points raises.Args:- p0: First point tensor, shaped ``(..., 2)`` for the 2D case or- ``(..., 3)`` for the 3D case.+ p0: First point tensor, shaped ``(..., 3)``.p1: Second point tensor with the same shape as ``p0``.- p2: Optional third point tensor. If omitted, the method builds the- 2D line representation from ``p0`` and ``p1``. If provided, it- builds the 3D plane passing through all three points.+ p2: Third point tensor with the same shape as ``p0``. It is required; the default of+ ``None`` only exists so that a two-point call fails with a clear error.Returns:- :class:`Hyperplane` with a normal and offset determined by the- provided point set.+ :class:`Hyperplane` passing through the three points.++ Raises:+ BaseError: if ``p2`` is omitted, or if the points are not ``(..., 3)`` tensors of the+ same shape."""- # 2d caseif p2 is None:- # TODO: improve tests- KORNIA_CHECK_SHAPE(p0, ["*", "2"])- KORNIA_CHECK(p0.shape == p1.shape)- # TODO: implement `.unitOrthonormal`- normal2d = normalized(p1 - p0)- offset2d = -batched_dot_product(p0, normal2d)- return Hyperplane(_wrap(normal2d, Vector3), _wrap(offset2d, Scalar))- # 3d case+ # Raised directly rather than through KORNIA_CHECK, so it still fires with checks disabled.+ raise BaseError(+ "Hyperplane.through requires three points p0, p1 and p2 of shape (..., 3); "+ "the two-point (2D line) form is not supported."+ )KORNIA_CHECK_SHAPE(p0, ["*", "3"])KORNIA_CHECK(p0.shape == p1.shape)KORNIA_CHECK(p1.shape == p2.shape)diff --git a/tests/geometry/test_plane.py b/tests/geometry/test_plane.pyindex 604c7c62c..c22ec18de 100644--- a/tests/geometry/test_plane.py+++ b/tests/geometry/test_plane.py@@ -18,6 +18,7 @@import pytestimport torch+from kornia.core.exceptions import BaseErrorfrom kornia.geometry.plane import Hyperplane, fit_planefrom kornia.geometry.vector import Vector3@@ -79,15 +80,14 @@ class TestHyperplane(BaseTester):loaded_plane = torch.load(file_path, weights_only=False)self.assert_close(plane.normal.unwrap(), loaded_plane.normal.unwrap())- # TODO: implement `Vector2`- # @pytest.mark.parametrize("batch_size", [1, 2])- # def test_through_two(self, device, dtype, batch_size):- # v0 = _VectorType.random((batch_size, 2), device, dtype)- # v1 = _VectorType.random((batch_size, 2), device, dtype)- # # TODO: improve api so that we can accept Vector too- # p0 = Hyperplane.through(v0.data, v1.data)- # assert p0.offset.shape == (batch_size,)- # assert p0.normal.shape == (batch_size, 2)+ @pytest.mark.parametrize("shape", ((2,), (1, 2), (3,), (1, 3)))+ def test_through_two_points_raises(self, device, dtype, shape):+ # Hyperplane stores a Vector3 normal and has no 2D form: two points must be rejected with a+ # message that names the three-point requirement, whatever the points' dimension.+ p0 = torch.rand(shape, device=device, dtype=dtype)+ p1 = torch.rand(shape, device=device, dtype=dtype)+ with pytest.raises(BaseError, match="requires three points"):+ Hyperplane.through(p0, p1)@pytest.mark.parametrize("shape", (None, (1,), (2, 1)))def test_through_three(self, device, dtype, shape):