jupyter-widgets-contrib / ipygany

3-D Scientific Visualization in the Jupyter Notebook
BSD 3-Clause "New" or "Revised" License
487 stars 53 forks source link

Wrap PyVista #98

Closed akaszynski closed 3 years ago

akaszynski commented 3 years ago

Wrap PyVista Meshes Directly

First off, great work with this library/package!

I'd like to improve the performance when wrapping pyvista meshes, so I've proposed the following method:

PolyMesh.from_pyvista(pvmesh)

It can actually wrap any VTK/pyvista object and does it quickly as it utializes a bunch of pyvista's fast methods for getting data from VTK.

For example:

pvmesh = examples.download_nefertiti()

def old(pvmesh):
    ugrid = pvmesh.cast_to_unstructured_grid()
    return PolyMesh.from_vtk(ugrid)

def new(pvmesh):
    return PolyMesh.from_pyvista(pvmesh)

New approach is about 20x faster (and will be even faster with https://github.com/pyvista/pyvista/pull/1228):

>>> %timeit old(pvmesh)
2.28 s ± 20.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

>>> %timeit new(pvmesh)
117 ms ± 3.62 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

Also, makes examples a bit cleaner:

pyvista

martinRenou commented 3 years ago

Amazing! Thanks a ton :)