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
12 changes: 11 additions & 1 deletion spatialmath/geom3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,18 @@ def contains(self, p: ArrayLike3, tol: float = 20) -> bool:
:type tol: float
:return: if the point is in the plane
:rtype: bool

The tolerance applies to perpendicular distance from the plane and is
independent of the scale of the plane coefficients.
"""
return abs(np.dot(self.n, p) + self.d) < tol * _eps
normal_length = math.hypot(*self.n)
if normal_length == 0:
return abs(self.d) < tol * _eps
# Plane coefficients are homogeneous. Compare point-to-plane distance,
# not the residual whose size depends on their arbitrary scale.
return (
abs(np.dot(self.n / normal_length, p) + self.d / normal_length) < tol * _eps
)

def plot(
self,
Expand Down
32 changes: 32 additions & 0 deletions tests/test_plane_contains_scale.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import numpy as np
import pytest
from spatialmath import Plane3


@pytest.mark.parametrize("scale", [1, -1, 1e-12, -1e-12, 1e12, -1e12])
@pytest.mark.parametrize("offset, expected", [(0, True), (1e-6, False), (-1e-6, False)])
def test_homogeneous_scaling_keeps_containment(scale, offset, expected):
plane = Plane3(scale * np.array([1.0, 0, 0, -2]))
assert bool(plane.contains([2 + offset, 3, -4])) is expected


@pytest.mark.parametrize("scale", [1, -1, 1e-12, -1e-12, 1e12, -1e12])
@pytest.mark.parametrize("tol, expected", [(1, False), (20, True), (100, True)])
def test_distance_tolerance_is_scale_independent(scale, tol, expected):
plane = Plane3(scale * np.array([0.0, 0, 1, 0]))
assert bool(plane.contains([3, -4, 10 * np.finfo(float).eps], tol=tol)) is expected


@pytest.mark.parametrize("scale", [1, -1, 1e-12, 1e12])
def test_oblique_normal_uses_euclidean_distance(scale):
normal = np.array([3.0, 4, 0])
plane = Plane3(scale * np.r_[normal, 0])
eps = np.finfo(float).eps
assert plane.contains(normal / 5 * 10 * eps, tol=20)
assert not plane.contains(normal / 5 * 40 * eps, tol=20)


@pytest.mark.parametrize("d", [0, 1])
def test_degenerate_coefficients_keep_existing_behavior(d):
plane = Plane3([0, 0, 0, d])
assert bool(plane.contains([1, 2, 3])) is (d == 0)