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 MeshContainer.plot(), img = MeshContainer.screenshot() and ax = MeshContainer.imshow() #611

Merged
merged 3 commits into from
Feb 13, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ All notable changes to this project will be documented in this file. The format

## [Unreleased]

### Added
- Add `MeshContainer.plot()`, `img = MeshContainer.screenshot()` and `ax = MeshContainer.imshow()`. The default list of colors contains PyVista's default color as first item and then the list of matplotlib's named colors *C1*, *C2*, etc (excluding *C0*).

## [7.15.0] - 2024-02-11

### Added
Expand Down
Binary file added docs/felupe/images/mesh_container.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/felupe/images/mesh_sweep.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
87 changes: 87 additions & 0 deletions src/felupe/mesh/_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,93 @@ def copy(self):
"Return a deepcopy of the mesh container."
return deepcopy(self)

def plot(self, *args, colors=None, **kwargs):
"""Plot the meshes of the mesh container.

See Also
--------
felupe.Scene.plot: Plot method of a scene.
"""

if colors is None:
import pyvista
import matplotlib.colors as mcolors

colors = [
pyvista.global_theme.color,
*list(mcolors.TABLEAU_COLORS.values())[1:],
]

plotter = None
for mesh, color in zip(self.meshes, colors):
plotter = mesh.view().plot(
*args,
show_undeformed=False,
color=color,
plotter=plotter,
opacity=0.99,
**kwargs,
)

return plotter

def screenshot(
self,
*args,
filename="mesh.png",
transparent_background=None,
scale=None,
colors=None,
**kwargs,
):
"""Take a screenshot of the meshes of the mesh container.

See Also
--------
pyvista.Plotter.screenshot: Take a screenshot of a PyVista plotter.
"""

if colors is None:
import pyvista
import matplotlib.colors as mcolors

colors = [
pyvista.global_theme.color,
*list(mcolors.TABLEAU_COLORS.values())[1:],
]

plotter = None
for mesh, color in zip(self.meshes, colors):
plotter = mesh.plot(
*args,
off_screen=True,
color=color,
plotter=plotter,
opacity=0.99,
**kwargs,
)

return plotter.screenshot(
filename=filename,
transparent_background=transparent_background,
scale=scale,
)

def imshow(self, ax=None, *args, **kwargs):
"""Take a screenshot of the meshes of the mesh container, show the image data in
a figure and return the ax.
"""

if ax is None:
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax.imshow(self.screenshot(*args, filename=None, **kwargs))
ax.set_axis_off()

return ax

def __iadd__(self, mesh):
self.append(mesh)
return self
Expand Down
9 changes: 9 additions & 0 deletions src/felupe/mesh/_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,9 @@ def sweep(self, decimals=None):
Number of cells:
quad: 100
quad: 100

.. image:: images/mesh_container.png
:width: 400px

The meshes of the mesh container are :func:`stacked <felupe.mesh.stack>`.

Expand All @@ -573,10 +576,16 @@ def sweep(self, decimals=None):
the number of cells is unchanged.

>>> mesh = fem.mesh.sweep(stack)
>>> mesh
<felupe Mesh object>
Number of points: 220
Number of cells:
quad: 200

>>> ax = mesh.imshow(opacity=0.6)

.. image:: images/mesh_sweep.png
:width: 400px

.. note::
The :class:`~felupe.MeshContainer` may be directly created with
Expand Down
6 changes: 6 additions & 0 deletions src/felupe/mesh/_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,10 +373,16 @@ def sweep(points, cells, cell_type, decimals=None):
the number of cells is unchanged.

>>> mesh = fem.mesh.sweep(stack)
>>> mesh
<felupe Mesh object>
Number of points: 220
Number of cells:
quad: 200

>>> ax = mesh.imshow(opacity=0.6)

.. image:: images/mesh_sweep.png
:width: 400px

.. note::
The :class:`~felupe.MeshContainer` may be directly created with ``merge=True``.
Expand Down
6 changes: 6 additions & 0 deletions tests/test_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,12 @@ def test_view():
# img = mesh.screenshot(transparent_background=True)
# ax = mesh.imshow()

mesh2 = mesh.translate(move=0.8, axis=0)
container = fem.MeshContainer([mesh, mesh2])
plotter = container.plot(off_screen=True)
# img = container.screenshot(transparent_background=True)
# ax = container.imshow()


def test_mesh_update():
mesh = fem.Cube(n=11)
Expand Down
Loading