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
26 changes: 15 additions & 11 deletions spatialmath/geom2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down
49 changes: 49 additions & 0 deletions tests/test_line2_intersection_scale.py
Original file line number Diff line number Diff line change
@@ -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)