Skip to content

Commit b8bb65a

Browse files
committed
Normalize finite homogeneous endpoints in line-segment intersection
1 parent 824a459 commit b8bb65a

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

‎spatialmath/geom2d.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,17 @@ def intersect_segment(
178178
Tests whether the line intersects the line segment defined by endpoints
179179
``p1`` and ``p2`` which are given in Euclidean or homogeneous form.
180180
"""
181+
# Use unit weight for finite endpoints in the sign and tolerance tests.
181182
p1 = smb.getvector(p1)
182183
if len(p1) == 2:
183184
p1 = np.r_[p1, 1]
185+
elif p1[2] != 0:
186+
p1 = p1 / p1[2]
184187
p2 = smb.getvector(p2)
185188
if len(p2) == 2:
186189
p2 = np.r_[p2, 1]
190+
elif p2[2] != 0:
191+
p2 = p2 / p2[2]
187192

188193
z1 = np.dot(self.line, p1)
189194
z2 = np.dot(self.line, p2)

‎tests/test_geom2d.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,18 @@ def test_intersect_segment(self):
140140
self.assertFalse(l1.intersect_segment((2, 1), (2, 3)))
141141
self.assertTrue(l1.intersect_segment((2, 1), (2, -1)))
142142

143+
def test_intersect_segment_homogeneous_endpoints(self):
144+
line = Line2([0, 1, 0]) # y = 0
145+
above = np.array([1, 2, 1])
146+
below = np.array([1, -2, 1])
147+
for p1, p2, expected in (
148+
((0, 1), -above, False),
149+
(-below, (0, 1), True),
150+
(1e-16 * above, (0, 1), False),
151+
):
152+
with self.subTest(p1=p1, p2=p2):
153+
self.assertEqual(line.intersect_segment(p1, p2), expected)
154+
143155

144156
class EllipseTest(unittest.TestCase):
145157
def test_constructor(self):

0 commit comments

Comments
 (0)