Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add plot face normals #1686

Merged
merged 2 commits into from
Sep 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions pyvista/core/filters/data_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -4418,8 +4418,10 @@ def reflect(self, normal, point=None, inplace=False,

"""
t = transformations.reflection(normal, point=point)
return self.transform(t, transform_all_input_vectors=transform_all_input_vectors,
inplace=inplace, progress_bar=progress_bar)
return self.transform(
t, transform_all_input_vectors=transform_all_input_vectors,
inplace=inplace, progress_bar=progress_bar
)

def reconstruct_surface(self, nbr_sz=None, sample_spacing=None,
progress_bar=False):
Expand Down
29 changes: 23 additions & 6 deletions pyvista/core/filters/poly_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -1925,7 +1925,7 @@ def plot_boundaries(self, edge_color="red", line_width=None,
return plotter.show()

def plot_normals(self, show_mesh=True, mag=1.0, flip=False,
use_every=1, color=None, **kwargs):
use_every=1, faces=False, color=None, **kwargs):
"""Plot the point normals of a mesh.

Parameters
Expand All @@ -1945,6 +1945,9 @@ def plot_normals(self, show_mesh=True, mag=1.0, flip=False,
displayed. Display every 10th normal by setting this
parameter to 10.

faces : bool, optional
Plot face normals instead of the default point normals.

color : str, optional
Color of the arrows. Defaults to
:attr:`pyvista.themes.DefaultTheme.edge_color`.
Expand All @@ -1961,11 +1964,17 @@ def plot_normals(self, show_mesh=True, mag=1.0, flip=False,

Examples
--------
Plot the normals of a sphere.
Plot the point normals of a sphere.

>>> import pyvista as pv
>>> sphere = pv.Sphere()
>>> sphere.plot_normals(mag=0.1)
>>> sphere = pv.Sphere(phi_resolution=10, theta_resolution=10)
>>> sphere.plot_normals(mag=0.1, show_edges=True)

Plot the face normals of a sphere.

>>> import pyvista as pv
>>> sphere = pv.Sphere(phi_resolution=10, theta_resolution=10)
>>> sphere.plot_normals(mag=0.1, faces=True, show_edges=True)

"""
plotter = pyvista.Plotter(off_screen=kwargs.pop('off_screen', None),
Expand All @@ -1976,11 +1985,19 @@ def plot_normals(self, show_mesh=True, mag=1.0, flip=False,
if color is None:
color = pyvista.global_theme.edge_color

normals = self.point_normals
if faces:
centers = self.cell_centers().points[::use_every]
normals = self.cell_normals
else:
centers = self.points[::use_every]
normals = self.point_normals

if flip:
normals *= -1
plotter.add_arrows(self.points[::use_every], normals[::use_every],

plotter.add_arrows(centers, normals[::use_every],
mag=mag, color=color, show_scalar_bar=False)

return plotter.show()

def remove_points(self, remove, mode='any', keep_scalars=True, inplace=False):
Expand Down
18 changes: 12 additions & 6 deletions tests/test_polydata.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

radius = 0.5

skip_plotting = pytest.mark.skipif(
not system_supports_plotting(), reason="Requires system to support plotting"
)


@pytest.fixture
def sphere():
# this shadows the main sphere fixture from conftest!
Expand Down Expand Up @@ -245,7 +250,7 @@ def test_ray_trace(sphere):
assert np.any(ind)


@pytest.mark.skipif(not system_supports_plotting(), reason="Requires system to support plotting")
@skip_plotting
def test_ray_trace_plot(sphere):
points, ind = sphere.ray_trace([0, 0, 0], [1, 1, 1], plot=True, first_point=True,
off_screen=True)
Expand All @@ -265,7 +270,7 @@ def test_multi_ray_trace(sphere):
assert np.any(ind_t)


@pytest.mark.skipif(not system_supports_plotting(), reason="Requires system to support plotting")
@skip_plotting
def test_plot_curvature(sphere):
sphere.plot_curvature(off_screen=True)

Expand Down Expand Up @@ -563,16 +568,17 @@ def test_volume(sphere_dense):
assert np.isclose(sphere_dense.volume, ideal_volume, rtol=1E-3)


@pytest.mark.skipif(not system_supports_plotting(), reason="Requires system to support plotting")
@skip_plotting
def test_plot_boundaries():
# make sure to plot an object that has boundaries
pyvista.Cube().plot_boundaries(off_screen=True)


@pytest.mark.skipif(not system_supports_plotting(), reason="Requires system to support plotting")
@skip_plotting
@pytest.mark.parametrize('flip', [True, False])
def test_plot_normals(sphere, flip):
sphere.plot_normals(off_screen=True, flip=flip)
@pytest.mark.parametrize('faces', [True, False])
def test_plot_normals(sphere, flip, faces):
sphere.plot_normals(off_screen=True, flip=flip, faces=faces)


def test_remove_points_any(sphere):
Expand Down