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
27 changes: 18 additions & 9 deletions spatialmath/geom3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down
69 changes: 69 additions & 0 deletions tests/test_plane_plot_regression.py
Original file line number Diff line number Diff line change
@@ -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)