Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions spatialmath/geom2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_geom2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down