Source code for pygimli.viewer.pv.utils

import numpy as np
import tempfile

import pygimli as pg

pv = pg.optImport('pyvista', requiredFor="properly visualize 3D data")

[docs]def pgMesh2pvMesh(mesh, data=None, label=None): """ pyGIMLi's mesh format is different from pyvista's needs, some preparation is necessary. Parameters ---------- mesh: pg.Mesh Structure generated by pyGIMLi to display. data: iterable Parameter to distribute to cells/nodes. """ _, tmp = tempfile.mkstemp(suffix=".vtk") # export given mesh temporarily is the easiest and fastest option ATM mesh.exportVTK(tmp) grid = # check for parameters inside the pg.Mesh for key, values in mesh.dataMap(): if len(values) == mesh.cellCount(): grid.cell_arrays[key] = np.asarray(values) elif len(values) == mesh.nodeCount(): grid.point_arrays[key] = np.asarray(values) # check the given data as well if data is not None: if len(data) == mesh.cellCount(): grid.cell_arrays[label] = np.asarray(data) elif len(data) == mesh.nodeCount(): grid.point_arrays[label] = np.asarray(data) else: pg.warn("Given data fits neither cell count nor node count:") pg.warn("{} vs. {} vs. {}".format(len(data), mesh.cellCount(), mesh.nodeCount())) if len(mesh.dataMap()) != 0 and label is None: grid.set_active_scalars(grid.array_names[-1]) elif len(mesh.dataMap()) != 0 and label is not None: grid.set_active_scalars(label) elif data is not None: grid.set_active_scalars(label) return grid