Mayavi: interpolate face colors in triangular_mesh

I have pieced together the following code to generate a triangular mesh with colors specified by an optional scalar function:

#! /usr/bin/env python
import numpy as np
from mayavi import mlab

# Create cone
n = 8
t = np.linspace(-np.pi, np.pi, n)
z = np.exp(1j*t)
x = z.real.copy()
y = z.imag.copy()
z = np.zeros_like(x)
triangles = [(0, i, i+1) for i in range(n)]
x = np.r_[0, x]
y = np.r_[0, y]
z = np.r_[1, z]
t = np.r_[0, t]

# 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)
cell_data = mesh.mlab_source.dataset.cell_data
cell_data.scalars = f
cell_data.scalars.name = 'Cell data'
cell_data.update()

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

mlab.show()

      

This works well enough. However, instead of having each triangle with a uniform color and sharp transitions between the triangles, I would much rather have smooth interpolation across the entire surface.

Is there a way to do this?

+3


source to share


1 answer


I think you want to use point data instead of cell data. When using cell data, a single scalar value is not localized at any point. It is assigned to the entire person. It looks like you just want to assign data to the t

vertices. By default, rendering of point scalars will smoothly interpolate along each face.



point_data = mesh.mlab_source.dataset.point_data
point_data.scalars = t
point_data.scalars.name = 'Point data'
point_data.update()

mesh2 = mlab.pipeline.set_active_attribute(mesh,
        point_scalars='Point data')

      

+5


source







All Articles