From 4edb45e08013396c8403f3c14429369a6d0cc417 Mon Sep 17 00:00:00 2001 From: Kaushik Gunasekaren Date: Thu, 19 Nov 2026 10:47:56 -0700 Subject: [PATCH 1/2] Make plane containment independent of coefficient scale Compare perpendicular point-to-plane distance while preserving zero-normal behavior. --- spatialmath/geom3d.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/spatialmath/geom3d.py b/spatialmath/geom3d.py index c1efbbb2..a4533293 100644 --- a/spatialmath/geom3d.py +++ b/spatialmath/geom3d.py @@ -189,8 +189,18 @@ def contains(self, p: ArrayLike3, tol: float = 20) -> bool: :type tol: float :return: if the point is in the plane :rtype: bool + + The tolerance applies to perpendicular distance from the plane and is + independent of the scale of the plane coefficients. """ - return abs(np.dot(self.n, p) + self.d) < tol * _eps + normal_length = math.hypot(*self.n) + if normal_length == 0: + return abs(self.d) < tol * _eps + # Plane coefficients are homogeneous. Compare point-to-plane distance, + # not the residual whose size depends on their arbitrary scale. + return ( + abs(np.dot(self.n / normal_length, p) + self.d / normal_length) < tol * _eps + ) def plot( self, From 5b03b96c3726b31da409157665135057c178a3dc Mon Sep 17 00:00:00 2001 From: Kaushik Gunasekaren Date: Thu, 19 Nov 2026 10:48:08 -0700 Subject: [PATCH 2/2] Test homogeneous plane containment and distance tolerance Cover signed coefficient scales, off-plane points, oblique normals and legacy degenerate behavior. --- tests/test_plane_contains_scale.py | 32 ++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 tests/test_plane_contains_scale.py diff --git a/tests/test_plane_contains_scale.py b/tests/test_plane_contains_scale.py new file mode 100644 index 00000000..b9912642 --- /dev/null +++ b/tests/test_plane_contains_scale.py @@ -0,0 +1,32 @@ +import numpy as np +import pytest +from spatialmath import Plane3 + + +@pytest.mark.parametrize("scale", [1, -1, 1e-12, -1e-12, 1e12, -1e12]) +@pytest.mark.parametrize("offset, expected", [(0, True), (1e-6, False), (-1e-6, False)]) +def test_homogeneous_scaling_keeps_containment(scale, offset, expected): + plane = Plane3(scale * np.array([1.0, 0, 0, -2])) + assert bool(plane.contains([2 + offset, 3, -4])) is expected + + +@pytest.mark.parametrize("scale", [1, -1, 1e-12, -1e-12, 1e12, -1e12]) +@pytest.mark.parametrize("tol, expected", [(1, False), (20, True), (100, True)]) +def test_distance_tolerance_is_scale_independent(scale, tol, expected): + plane = Plane3(scale * np.array([0.0, 0, 1, 0])) + assert bool(plane.contains([3, -4, 10 * np.finfo(float).eps], tol=tol)) is expected + + +@pytest.mark.parametrize("scale", [1, -1, 1e-12, 1e12]) +def test_oblique_normal_uses_euclidean_distance(scale): + normal = np.array([3.0, 4, 0]) + plane = Plane3(scale * np.r_[normal, 0]) + eps = np.finfo(float).eps + assert plane.contains(normal / 5 * 10 * eps, tol=20) + assert not plane.contains(normal / 5 * 40 * eps, tol=20) + + +@pytest.mark.parametrize("d", [0, 1]) +def test_degenerate_coefficients_keep_existing_behavior(d): + plane = Plane3([0, 0, 0, d]) + assert bool(plane.contains([1, 2, 3])) is (d == 0)