How can I make a colormap of triangular_mesh in mayavi, based on the value of triangles rather than value of vertices?

332 Views Asked by At

I am using mayavi with Python. Suppose that I have dataset of a 3D surface mesh: the positions of vertices X, Y, Z, and the triangles T (dimensions: E * 3) which consists of node numbers. The mesh has N vertices and E triangles. I can plot the mesh like below enter image description here

The commands are like

from mayavi import mlab as ML
ML.triangular_mesh(X, Y, Z, T, scalars=Z, opacity=0.9)
### ... other commands
ML.show()

You can see from both the command and the above plot that the colormap is based on the z values of vertices. This is a result of scalars=Z.

Now, I do not want to make a colormap based on vertices. Suppose that I have a array that stores E*1 values. Each value corresponds to one kind of attribute of each triangle. I want to generate the colormap based on the 1D array related to triangles' attribute. How can I achieve that?

I do know matlab can do that by using patch function. Details are in here. But I prefer using mayavi.

1

There are 1 best solutions below

0
Tingchang Yin On

Well, I found a method to set face color (each triangle is a face). The result is below enter image description here

The script is like:

# These are the scalar values for each triangle 
f = np.mean(t[np.array(triangles)], axis=1) 

# Plot it 
mesh = mlab.triangular_mesh(x, y, z, triangles, representation='wireframe', opacity=0) 
mesh.mlab_source.dataset.cell_data.scalars = f 
mesh.mlab_source.dataset.cell_data.scalars.name = 'Cell data' 
mesh.mlab_source.update() 
mesh.parent.update()

mesh2 = mlab.pipeline.set_active_attribute(mesh, cell_scalars='Cell data') 
s2 = mlab.pipeline.surface(mesh2) 


mlab.show()

Details are presented in here