jupyter-widgets-contrib / ipygany

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

Supporting cell data #105

Open adofarsi opened 3 years ago

adofarsi commented 3 years ago

Similarly to point data, could you also support cell data? It would be great if when loading a mesh from memory, I could manually pass cell data, i.e. data associated with the tetrahedra for a tetrahedral mesh. For instance if I wanted to colour my mesh with the element id number, this is something that I cannot do with point data. Similarly, when importing a vtk file that includes both point and cell data, being able to import both and visualise cell data would be amazing.

Thanks a lot for your time!

Best,

Ado

martinRenou commented 3 years ago

That would be nice indeed :)

dschwoerer commented 3 years ago

With this small script you can plot cell data:

import meshio
import ipygany
import numpy as np

def readcelldata(fn):
    msh = meshio.read(fn)
    indices = msh.cells[0]
    shp = indices.data.shape
    indices = indices.data.flatten()
    assert len(shp) == 2
    length = shp[0] * shp[1]
    indnew = np.arange(length, dtype=int)
    vert = np.empty((length, 3))
    datas = np.empty((length, len(msh.cell_data)))
    datold = np.array(list(msh.cell_data.values()))[:, 0, :]
    for i in range(length):
        vert[i] = msh.points[indices[i]]
        datas[i] = datold[:, i // shp[1]]
    datnew = {
        v: [ipygany.Component(name="value", array=datas[:, i])]
        for i, v in enumerate(msh.cell_data.keys())
    }
    return ipygany.PolyMesh(vert, indnew.reshape(shp), datnew)

However, proper support would be nice, because it isn't very efficient.