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
7 changes: 6 additions & 1 deletion spatialmath/geom2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ def contains(self, p: ArrayLike2, tol: float = 20) -> bool:
"""
Test if point is in line

For a Euclidean point (or a homogeneous point with unit weight),
``tol`` bounds the perpendicular distance in units of machine epsilon,
independently of a nonzero scale on the line coefficients.

:param p1: point to test
:type p1: array_like(2) or array_like(3)
:param tol: tolerance in units of eps, defaults to 20
Expand All @@ -156,7 +160,8 @@ def contains(self, p: ArrayLike2, tol: float = 20) -> bool:
p = smb.getvector(p)
if len(p) == 2:
p = np.r_[p, 1]
return abs(np.dot(self.line, p)) < tol * _eps
normal = np.hypot(self.line[0], self.line[1])
return abs(np.dot(self.line / normal, p)) < tol * _eps

# variant that gives lambda

Expand Down
35 changes: 35 additions & 0 deletions tests/test_line2_contains_scale.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import numpy as np
import pytest

from spatialmath.geom2d import Line2, Polygon2


@pytest.mark.parametrize("scale", [1, -1, 1e-20, -1e-20, 1e-8, -1e8, 1e20])
@pytest.mark.parametrize(
"coefficients,on,off",
[([1, 0, -1], [1, 2], [2, 2]), ([3, 4, -10], [2, 1], [2, 2])],
)
def test_contains_coefficient_scale(scale, coefficients, on, off):
line = Line2(scale * np.array(coefficients))
assert line.contains(on)
assert not line.contains(off)
assert line.contains(np.r_[on, 1])
assert not line.contains(np.r_[off, 1])


@pytest.mark.parametrize("scale", [1, -1, 1e-8, -1e8])
def test_contains_geometric_tolerance(scale):
eps = np.finfo(float).eps
line = Line2(scale * np.array([0, 1, 0]))
assert line.contains([0, 10 * eps])
assert not line.contains([0, 30 * eps])
assert line.contains([0, 30 * eps], tol=40)


@pytest.mark.parametrize("scale", [1, 1e-20, -1e-20, 1e8])
def test_polygon_line_nonintersection_is_scale_invariant(scale):
polygon = Polygon2([(-1, -1), (1, -1), (1, 1), (-1, 1)])
outside = Line2(scale * np.array([0, 1, -2]))
crossing = Line2(scale * np.array([0, 1, 0]))
assert not polygon.intersects(outside)
assert polygon.intersects(crossing)