From b8bb65aeb44ac17d375a2f66bf622d995c1cc4c6 Mon Sep 17 00:00:00 2001 From: Dori <111175613+Doribelove@users.noreply.github.com> Date: Tue, 3 Nov 2026 15:20:12 +0800 Subject: [PATCH] Normalize finite homogeneous endpoints in line-segment intersection --- spatialmath/geom2d.py | 5 +++++ tests/test_geom2d.py | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/spatialmath/geom2d.py b/spatialmath/geom2d.py index ee42c08f..7462e1f4 100755 --- a/spatialmath/geom2d.py +++ b/spatialmath/geom2d.py @@ -178,12 +178,17 @@ def intersect_segment( Tests whether the line intersects the line segment defined by endpoints ``p1`` and ``p2`` which are given in Euclidean or homogeneous form. """ + # Use unit weight for finite endpoints in the sign and tolerance tests. p1 = smb.getvector(p1) if len(p1) == 2: p1 = np.r_[p1, 1] + elif p1[2] != 0: + p1 = p1 / p1[2] p2 = smb.getvector(p2) if len(p2) == 2: p2 = np.r_[p2, 1] + elif p2[2] != 0: + p2 = p2 / p2[2] z1 = np.dot(self.line, p1) z2 = np.dot(self.line, p2) diff --git a/tests/test_geom2d.py b/tests/test_geom2d.py index 49aa1d8b..faeb5335 100755 --- a/tests/test_geom2d.py +++ b/tests/test_geom2d.py @@ -140,6 +140,18 @@ def test_intersect_segment(self): self.assertFalse(l1.intersect_segment((2, 1), (2, 3))) self.assertTrue(l1.intersect_segment((2, 1), (2, -1))) + def test_intersect_segment_homogeneous_endpoints(self): + line = Line2([0, 1, 0]) # y = 0 + above = np.array([1, 2, 1]) + below = np.array([1, -2, 1]) + for p1, p2, expected in ( + ((0, 1), -above, False), + (-below, (0, 1), True), + (1e-16 * above, (0, 1), False), + ): + with self.subTest(p1=p1, p2=p2): + self.assertEqual(line.intersect_segment(p1, p2), expected) + class EllipseTest(unittest.TestCase): def test_constructor(self):