Skip to content

Commit e9eea91

Browse files
AlejandroFernandezLucespyansys-ci-botRobPasMue
authored
feat: Add StructuredGrid support (#180)
Co-authored-by: pyansys-ci-bot <[email protected]> Co-authored-by: Roberto Pastor Muela <[email protected]>
1 parent 4d22a05 commit e9eea91

File tree

5 files changed

+67
-7
lines changed

5 files changed

+67
-7
lines changed

doc/changelog.d/180.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
feat: Add StructuredGrid support

examples/00-basic-pyvista-examples/picker.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,36 @@ def name(self):
9797
pl.plot(mesh_object_cube)
9898
pl.plot(mesh_object_sphere)
9999
pl.show()
100+
101+
102+
############################
103+
# Using StructuredGrid mesh
104+
# ==========================
105+
106+
import numpy as np
107+
108+
109+
class CustomStructuredObject:
110+
def __init__(self):
111+
self.name = "CustomObject"
112+
xrng = np.arange(-10, 10, 2, dtype=np.float32)
113+
yrng = np.arange(-10, 10, 5, dtype=np.float32)
114+
zrng = np.arange(-10, 10, 1, dtype=np.float32)
115+
x, y, z = np.meshgrid(xrng, yrng, zrng, indexing='ij')
116+
grid = pv.StructuredGrid(x, y, z)
117+
self.mesh = grid
118+
119+
def get_mesh(self):
120+
return self.mesh
121+
122+
def name(self):
123+
return self.name
124+
125+
126+
pv_backend = PyVistaBackend()
127+
pl = Plotter(backend=pv_backend)
128+
129+
structured_object = CustomStructuredObject()
130+
mo_plot = MeshObjectPlot(structured_object, structured_object.get_mesh())
131+
pl.plot(mo_plot)
132+
pl.show()

src/ansys/tools/visualization_interface/backends/pyvista/pyvista_interface.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def view_zy(self) -> None:
136136
self.scene.view_zy()
137137

138138
def clip(
139-
self, mesh: Union[pv.PolyData, pv.MultiBlock], plane: ClipPlane
139+
self, mesh: Union[pv.PolyData, pv.MultiBlock, pv.UnstructuredGrid], plane: ClipPlane
140140
) -> Union[pv.PolyData, pv.MultiBlock]:
141141
"""Clip a given mesh with a plane.
142142
@@ -218,7 +218,7 @@ def plot_edges(self, custom_object: MeshObjectPlot, **plotting_options) -> None:
218218

219219
def plot(
220220
self,
221-
plottable_object: Union[pv.PolyData, pv.MultiBlock, MeshObjectPlot],
221+
plottable_object: Union[pv.PolyData, pv.MultiBlock, MeshObjectPlot, pv.UnstructuredGrid],
222222
name_filter: str = None,
223223
**plotting_options,
224224
) -> None:
@@ -229,7 +229,7 @@ def plot(
229229
230230
Parameters
231231
----------
232-
plottable_object : Union[pv.PolyData, pv.MultiBlock, MeshObjectPlot]
232+
plottable_object : Union[pv.PolyData, pv.MultiBlock, MeshObjectPlot, pv.UnstructuredGrid, pv.StructuredGrid]
233233
Object to plot.
234234
name_filter : str, default: None
235235
Regular expression with the desired name or names to include in the plotter.
@@ -246,7 +246,7 @@ def plot(
246246
self._show_edges = plotting_options["show_edges"]
247247

248248
# Check what kind of object we are dealing with
249-
if isinstance(plottable_object, (pv.PolyData, pv.UnstructuredGrid)):
249+
if isinstance(plottable_object, (pv.PolyData, pv.UnstructuredGrid, pv.StructuredGrid)):
250250
if "clipping_plane" in plotting_options:
251251
mesh = self.clip(plottable_object, plotting_options["clipping_plane"])
252252
plotting_options.pop("clipping_plane", None)
@@ -273,9 +273,6 @@ def plot_iter(
273273
) -> None:
274274
"""Plot elements of an iterable of any type of objects to the scene.
275275
276-
Supported object types are ``Body``, ``Component``, ``List[pv.PolyData]``,
277-
``pv.MultiBlock``, and ``Sketch``.
278-
279276
Parameters
280277
----------
281278
plotting_list : List[Any]

src/ansys/tools/visualization_interface/backends/pyvista/widgets/mesh_slider.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ def callback(self, state: bool) -> None:
8282
mesh_id = "UnstructuredGrid(" + mesh.memory_address + ")"
8383
elif isinstance(mesh, pv.MultiBlock):
8484
mesh_id = "MultiBlock(" + mesh.memory_address + ")"
85+
elif isinstance(mesh, pv.StructuredGrid):
86+
mesh_id = "StructuredGrid(" + mesh.memory_address + ")"
8587
self._mesh_actor_list.append(self.plotter_helper._pl.scene.actors[mesh_id])
8688
self.plotter_helper._pl.scene.remove_actor(mesh_id)
8789

tests/test_generic_plotter.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"""Test module for the generic plotter."""
2323
from pathlib import Path
2424

25+
import numpy as np
2526
import pyvista as pv
2627

2728
from ansys.tools.visualization_interface import ClipPlane, MeshObjectPlot, Plotter
@@ -52,6 +53,7 @@ def test_plotter_add_mb():
5253
pl.plot(mb)
5354
pl.show()
5455

56+
5557
def test_plotter_add_unstructured_grid():
5658
"""Adds unstructured grid to the plotter."""
5759
pl = Plotter()
@@ -60,6 +62,18 @@ def test_plotter_add_unstructured_grid():
6062
pl.plot(ug)
6163
pl.show()
6264

65+
66+
def test_plotter_add_structured_grid():
67+
"""Adds structured grid to the plotter."""
68+
pl = Plotter()
69+
xrng = np.arange(-10, 10, 2, dtype=np.float32)
70+
yrng = np.arange(-10, 10, 5, dtype=np.float32)
71+
zrng = np.arange(-10, 10, 1, dtype=np.float32)
72+
x, y, z = np.meshgrid(xrng, yrng, zrng, indexing='ij')
73+
grid = pv.StructuredGrid(x, y, z)
74+
pl.plot(grid)
75+
pl.show()
76+
6377
def test_plotter_add_custom():
6478
"""Adds a MeshObjectPlot object to the plotter."""
6579
sphere = pv.Sphere()
@@ -90,6 +104,19 @@ def test_clipping_plane():
90104
pl.show()
91105

92106

107+
def test_clipping_plane_structured_grid():
108+
"""Test clipping plane usage with structured grid."""
109+
pl = Plotter()
110+
xrng = np.arange(-10, 10, 2, dtype=np.float32)
111+
yrng = np.arange(-10, 10, 5, dtype=np.float32)
112+
zrng = np.arange(-10, 10, 1, dtype=np.float32)
113+
x, y, z = np.meshgrid(xrng, yrng, zrng, indexing='ij')
114+
grid = pv.StructuredGrid(x, y, z)
115+
clipping_plane = ClipPlane()
116+
pl.plot(grid, clipping_plane=clipping_plane)
117+
pl.show()
118+
119+
93120
def test_plotter_add_list():
94121
"""Adds a list to the plotter."""
95122
pl = Plotter()

0 commit comments

Comments
 (0)