From 66f0d6bf55a798676e9d25619b5badf9630950b3 Mon Sep 17 00:00:00 2001 From: Kaushik Gunasekaren Date: Tue, 17 Nov 2026 17:48:08 +0000 Subject: [PATCH 1/2] Plot vertical planes using a stable coordinate --- spatialmath/geom3d.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/spatialmath/geom3d.py b/spatialmath/geom3d.py index c1efbbb2..aacb722e 100644 --- a/spatialmath/geom3d.py +++ b/spatialmath/geom3d.py @@ -219,16 +219,25 @@ def plot( if bounds is None: bounds = np.r_[ax.get_xlim(), ax.get_ylim(), ax.get_zlim()] - # X, Y = np.meshgrid(bounds[0: 2], bounds[2: 4]) - # Z = -(X * self.plane[0] + Y * self.plane[1] + self.plane[3]) / self.plane[2] - - X, Y = np.meshgrid( - np.linspace(bounds[0], bounds[1], 50), np.linspace(bounds[2], bounds[3], 50) + # Solve for the coordinate with the largest normal component. A + # fixed z coordinate cannot represent vertical planes, and a nearly + # zero z component makes that parameterization ill-conditioned. + normal = self.n + dependent = np.argmax(np.abs(normal)) + independent = [i for i in range(3) if i != dependent] + coordinates = [None, None, None] + coordinates[independent[0]], coordinates[independent[1]] = np.meshgrid( + *[np.linspace(bounds[2 * i], bounds[2 * i + 1], 50) for i in independent] + ) + coordinates[dependent] = ( + -(sum(normal[i] * coordinates[i] for i in independent) + self.d) + / normal[dependent] ) - Z = -(X * self.plane[0] + Y * self.plane[1] + self.plane[3]) / self.plane[2] - Z[Z < bounds[4]] = np.nan - Z[Z > bounds[5]] = np.nan - ax.plot_surface(X, Y, Z, **kwargs) + values = coordinates[dependent] + values[ + (values < bounds[2 * dependent]) | (values > bounds[2 * dependent + 1]) + ] = np.nan + ax.plot_surface(*coordinates, **kwargs) def __str__(self) -> str: """ From 25c34d428debfc4ffc0c8778c1ae90a157dc6b5c Mon Sep 17 00:00:00 2001 From: Kaushik Gunasekaren Date: Tue, 17 Nov 2026 17:48:21 +0000 Subject: [PATCH 2/2] Test finite vertical-plane surfaces and clipping --- tests/test_plane_plot_regression.py | 69 +++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 tests/test_plane_plot_regression.py diff --git a/tests/test_plane_plot_regression.py b/tests/test_plane_plot_regression.py new file mode 100644 index 00000000..5ac2b34b --- /dev/null +++ b/tests/test_plane_plot_regression.py @@ -0,0 +1,69 @@ +import warnings + +import matplotlib + +matplotlib.use("Agg") +import matplotlib.pyplot as plt +import numpy as np +import numpy.testing as nt +import pytest + +from spatialmath import Plane3 + + +@pytest.mark.parametrize( + "normal,point", + [ + ([1, 0, 0], [1, 0, 0]), + ([0, 1, 0], [0, -1, 0]), + ([1, 2, 0], [1, 0, 0]), + ([0, 0, 1], [0, 0, 0.5]), + ([1, 2, 3], [0, 0, 0.5]), + ([1, 2, 1e-20], [0, 0, 0]), + ], +) +@pytest.mark.parametrize("scale", [1, -3, 1e-10]) +def test_plane_plot_finite_surface(normal, point, scale, monkeypatch): + bounds = np.array([-2, 3, -3, 2, -1, 4]) + plane = Plane3.PointNormal(point, np.array(normal) * scale) + fig = plt.figure() + ax = fig.add_subplot(projection="3d") + surfaces = [] + original = ax.plot_surface + + def record_surface(x, y, z, **kwargs): + surfaces.append(np.stack((x, y, z))) + return original(x, y, z, **kwargs) + + monkeypatch.setattr(ax, "plot_surface", record_surface) + try: + with warnings.catch_warnings(): + warnings.simplefilter("error", RuntimeWarning) + plane.plot(bounds=bounds, ax=ax, alpha=0.4) + assert len(surfaces) == 1 + xyz = surfaces[0].reshape(3, -1) + xyz = xyz[:, np.all(np.isfinite(xyz), axis=0)] + assert xyz.shape[1] > 0 + nt.assert_allclose( + np.array(normal) @ (xyz - np.array(point)[:, None]), 0, atol=1e-12 + ) + assert np.all(xyz >= bounds[::2, None]) + assert np.all(xyz <= bounds[1::2, None]) + fig.canvas.draw() + finally: + plt.close(fig) + + +@pytest.mark.parametrize("normal", [[1, 0, 0], [0, 1, 0], [0, 0, 1]]) +def test_plane_plot_uses_axes_bounds(normal): + fig = plt.figure() + ax = fig.add_subplot(projection="3d") + ax.set(xlim=(-2, 2), ylim=(-3, 3), zlim=(-4, 4)) + try: + with warnings.catch_warnings(): + warnings.simplefilter("error", RuntimeWarning) + Plane3.PointNormal([0, 0, 0], normal).plot(ax=ax) + assert len(ax.collections) == 1 + fig.canvas.draw() + finally: + plt.close(fig)