Skip to content

Commit e1c081d

Browse files
authored
Merge pull request #98 from akaszynski/feat/wrap_pyvista
Wrap PyVista
2 parents 2af1460 + c546d7c commit e1c081d

File tree

2 files changed

+82
-14
lines changed

2 files changed

+82
-14
lines changed

docs/source/api_reference/pyvista.rst

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,25 @@ PyVista
33

44
https://docs.pyvista.org
55

6-
PyVista is a great library that exposes a high level API for vtk, it has way more features
7-
than ipygany. For this reason, ipygany supports PyVista objects:
6+
PyVista is a great library that exposes a high level API for ``vtk``
7+
and it has way more features than ``ipygany``. For this reason,
8+
``ipygany`` supports PyVista objects:
89

910

1011
.. jupyter-execute::
1112

1213
import pyvista as pv
1314
from pyvista import examples
1415

15-
pvmesh = examples.download_st_helens()
16-
ugrid = pvmesh.cast_to_unstructured_grid()
17-
1816
from ipygany import PolyMesh, Scene, IsoColor, Warp
1917

20-
# Turn the PyVista mesh into a PolyMesh
21-
mesh = PolyMesh.from_vtk(ugrid)
18+
# Download a pyvista example and convert it to a PolyMesh
19+
pvmesh = examples.download_st_helens()
20+
mesh = PolyMesh.from_pyvista(pvmesh)
21+
22+
# Plot it
2223
warped_mesh = Warp(mesh, input=(0, 0, ('Elevation', 'X1')), warp_factor=1.)
2324
colored_mesh = IsoColor(warped_mesh, input='Elevation', min=682, max=2543)
24-
2525
Scene([colored_mesh])
2626

2727

@@ -30,13 +30,12 @@ than ipygany. For this reason, ipygany supports PyVista objects:
3030
import pyvista as pv
3131
from pyvista import examples
3232

33-
nefertiti = examples.download_nefertiti()
34-
ugrid = nefertiti.cast_to_unstructured_grid()
35-
3633
from ipygany import PolyMesh, Scene, IsoColor, Warp
3734

38-
# Turn the PyVista mesh into a PolyMesh
39-
mesh = PolyMesh.from_vtk(ugrid)
40-
mesh.default_color = 'gray'
35+
# Download the pyvista nefertiti example and convert it to a PolyMesh
36+
nefertiti = examples.download_nefertiti()
37+
mesh = PolyMesh.from_pyvista(nefertiti)
4138

39+
# Plot it
40+
mesh.default_color = 'gray'
4241
Scene([mesh])

ipygany/ipygany.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,75 @@ def from_vtk(path, **kwargs):
242242
**kwargs
243243
)
244244

245+
def from_pyvista(obj, **kwargs):
246+
"""Import a mesh from ``pyvista`` or ``vtk``.
247+
248+
Parameters
249+
----------
250+
obj : pyvista compatible object
251+
Any object compatible with pyvista. Includes most ``vtk``
252+
objects.
253+
254+
Returns
255+
-------
256+
PolyMesh
257+
``ipygany.PolyMesh`` object
258+
259+
Examples
260+
--------
261+
262+
Convert a pyvista mesh to a PolyMesh
263+
264+
>>> import pyvista as pv
265+
>>> from ipygany import PolyMesh
266+
>>> pv_mesh = pv.Sphere()
267+
>>> mesh = PolyMesh.from_pyvista(pv_mesh)
268+
>>> mesh
269+
PolyMesh(data=[Data(components=[Component(array=array([-6.1232343e-17,
270+
6.1232343e-17, -1.0811902e-01, -2.1497…
271+
272+
"""
273+
try:
274+
import pyvista as pv
275+
except ImportError:
276+
raise ImportError('Please install ``pyvista`` to use this feature')
277+
278+
from .vtk_loader import get_ugrid_data
279+
280+
# attempt to wrap non-pyvista objects
281+
if not pv.is_pyvista_dataset(obj):
282+
mesh = pv.wrap(obj)
283+
if not pv.is_pyvista_dataset(mesh):
284+
raise TypeError(f'Object type ({type(mesh)}) cannot be converted to '
285+
'a pyvista dataset')
286+
else:
287+
mesh = obj
288+
289+
# PolyMesh requires vertices and triangles, so we need to
290+
# convert the mesh to an all triangle polydata
291+
if not isinstance(obj, pv.PolyData):
292+
# unlikely case that mesh does not have extract_surface
293+
if not hasattr(mesh, 'extract_surface'):
294+
mesh = mesh.cast_to_unstructured_grid()
295+
surf = mesh.extract_surface()
296+
else:
297+
surf = mesh
298+
299+
# convert to an all-triangular surface
300+
if surf.is_all_triangles():
301+
trimesh = surf
302+
else:
303+
trimesh = surf.triangulate()
304+
305+
# finally, pass the triangle vertices to PolyMesh
306+
triangle_indices = trimesh.faces.reshape(-1, 4)[:, 1:]
307+
308+
return PolyMesh(
309+
vertices=trimesh.points,
310+
triangle_indices=triangle_indices,
311+
data=_grid_data_to_data_widget(get_ugrid_data(trimesh))
312+
)
313+
245314
def reload(self, path, reload_vertices=False, reload_triangles=False, reload_data=True):
246315
"""Reload a vtk file, entirely or partially."""
247316
from .vtk_loader import (

0 commit comments

Comments
 (0)