I have a PolyData mesh, and I am trying to color some cells that contain given vertices. However, I need an efficient way to to this.
For instance, this works, but it is way too slow for a large mesh:
import pyvista as pv
import numpy as np
mesh = pv.Sphere()
# Scalars are zero everywhere
data = [0] * mesh.n_cells
# I want to color the cells which contain these vertices
point_indexes = np.random.randint(0,mesh.n_points,100)
# Loop over all vertices
for point in point_indexes:
# This next part is inefficient in a large mesh:
# I extract the 3D coordinates of the point and look for
# a containing cell. I think I should look for a cell containing
# the vertex from its index...but I can't find a way to do it!
point_idx = mesh.points[point]
cell_idx = mesh.find_containing_cell(point_idx)
# Change the scalar
data [cell_idx] = 1
# Color the cells touching the vertices
mesh.cell_data['data'] = data
mesh.plot()
This is what I get, which is fine...I just need to do it much faster!