From 32d492ff01126262bad0dec46d4249af8641c521 Mon Sep 17 00:00:00 2001 From: Kaushik Gunasekaren Date: Fri, 20 Nov 2026 10:35:44 -0700 Subject: [PATCH 1/2] Make 2D line intersection independent of coefficient scale --- spatialmath/geom2d.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/spatialmath/geom2d.py b/spatialmath/geom2d.py index ee42c08f..4157fe32 100755 --- a/spatialmath/geom2d.py +++ b/spatialmath/geom2d.py @@ -123,24 +123,28 @@ def plot(self, **kwargs) -> None: """ smb.plot_homline(self.line, **kwargs) - def intersect(self, other: Line2, tol: float = 20) -> R3: + def intersect(self, other: Line2, tol: float = 20) -> bool: """ - Intersection with line + Test whether two lines have a finite intersection :param other: another 2D line :type other: Line2 - :param tol: tolerance in units of eps, defaults to 20 + :param tol: angular tolerance in units of eps, defaults to 20 :type tol: float - :return: intersection point in homogeneous form - :rtype: ndarray(3) + :return: True if the lines are not parallel + :rtype: bool - If the lines are parallel then the third element of the returned - homogeneous point will be zero (an ideal point). + The sine of the angle between the line normals must exceed + ``tol * eps``, independently of a nonzero scale on either line's + homogeneous coefficients. A line with zero normal returns False. """ - # return intersection of 2 lines - # return mindist and points if no intersect - c = np.cross(self.line, other.line) - return abs(c[2]) > tol * _eps + n1 = np.hypot(*self.line[:2]) + n2 = np.hypot(*other.line[:2]) + if n1 == 0 or n2 == 0: + return False + a1, b1 = self.line[:2] / n1 + a2, b2 = other.line[:2] / n2 + return abs(a1 * b2 - b1 * a2) > tol * _eps def contains(self, p: ArrayLike2, tol: float = 20) -> bool: """ From 6159ed91c5578ff0a28cb3a180b8a5e86b705057 Mon Sep 17 00:00:00 2001 From: Kaushik Gunasekaren Date: Fri, 20 Nov 2026 10:36:15 -0700 Subject: [PATCH 2/2] Test homogeneous scaling and angle tolerance for line intersections --- tests/test_line2_intersection_scale.py | 49 ++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 tests/test_line2_intersection_scale.py diff --git a/tests/test_line2_intersection_scale.py b/tests/test_line2_intersection_scale.py new file mode 100644 index 00000000..b695f099 --- /dev/null +++ b/tests/test_line2_intersection_scale.py @@ -0,0 +1,49 @@ +import numpy as np +import pytest +from spatialmath import Line2 + + +@pytest.mark.parametrize( + "scales", + [ + (1, 1), + (1e-12, 1), + (1, 1e-12), + (1e-12, 1e-12), + (-1e-12, 1e12), + (1e12, -1e-12), + (1e200, 1e200), + (1e-200, -1e-200), + ], +) +@pytest.mark.parametrize( + "coefficients, expected", + [ + ([0, 1, -3], True), + ([1, 1, -3], True), + ([1, 0, -3], False), + ([1, 0, -2], False), + ], +) +def test_intersection_is_invariant_to_homogeneous_scale(scales, coefficients, expected): + a = Line2(scales[0] * np.array([1.0, 0, -2])) + b = Line2(scales[1] * np.array(coefficients, dtype=float)) + with np.errstate(over="raise", invalid="raise"): + assert bool(a.intersect(b)) is expected + assert bool(b.intersect(a)) is expected + + +@pytest.mark.parametrize("scale", [1e-12, 1, -1e12]) +@pytest.mark.parametrize("tol, expected", [(1, True), (20, False), (100, False)]) +def test_normalized_angle_tolerance(scale, tol, expected): + a = Line2([1, 0, -2]) + b = Line2(scale * np.array([1.0, 10 * np.finfo(float).eps, -3])) + assert bool(a.intersect(b, tol=tol)) is expected + + +@pytest.mark.parametrize("coefficients", [[0, 0, 0], [0, 0, 1]]) +def test_degenerate_line_keeps_false_result(coefficients): + a = Line2(coefficients) + b = Line2([1, 0, -2]) + assert not a.intersect(b) + assert not b.intersect(a)